summaryrefslogtreecommitdiffstats
path: root/clang/unittests/AST/StructuralEquivalenceTest.cpp
blob: a8ac79205dcfa129b367660960c8099cb58c705e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/AST/ASTStructuralEquivalence.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Tooling/Tooling.h"

#include "Language.h"
#include "DeclMatcher.h"

#include "gtest/gtest.h"

namespace clang {
namespace ast_matchers {

using std::get;

struct StructuralEquivalenceTest : ::testing::Test {
  std::unique_ptr<ASTUnit> AST0, AST1;
  std::string Code0, Code1; // Buffers for SourceManager

  // Get a pair of Decl pointers to the synthetised declarations from the given
  // code snipets. By default we search for the unique Decl with name 'foo' in
  // both snippets.
  std::tuple<NamedDecl *, NamedDecl *>
  makeNamedDecls(const std::string &SrcCode0, const std::string &SrcCode1,
                 Language Lang, const char *const Identifier = "foo") {

    this->Code0 = SrcCode0;
    this->Code1 = SrcCode1;
    ArgVector Args = getBasicRunOptionsForLanguage(Lang);

    const char *const InputFileName = "input.cc";

    AST0 = tooling::buildASTFromCodeWithArgs(Code0, Args, InputFileName);
    AST1 = tooling::buildASTFromCodeWithArgs(Code1, Args, InputFileName);

    ASTContext &Ctx0 = AST0->getASTContext(), &Ctx1 = AST1->getASTContext();

    auto getDecl = [](ASTContext &Ctx, const std::string &Name) -> NamedDecl * {
      IdentifierInfo *SearchedII = &Ctx.Idents.get(Name);
      assert(SearchedII && "Declaration with the identifier "
                           "should be specified in test!");
      DeclarationName SearchDeclName(SearchedII);
      SmallVector<NamedDecl *, 4> FoundDecls;
      Ctx.getTranslationUnitDecl()->localUncachedLookup(SearchDeclName,
                                                        FoundDecls);

      // We should find one Decl but one only.
      assert(FoundDecls.size() == 1);

      return FoundDecls[0];
    };

    NamedDecl *D0 = getDecl(Ctx0, Identifier);
    NamedDecl *D1 = getDecl(Ctx1, Identifier);
    assert(D0);
    assert(D1);
    return std::make_tuple(D0, D1);
  }

  bool testStructuralMatch(NamedDecl *D0, NamedDecl *D1) {
    llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls;
    StructuralEquivalenceContext Ctx(D0->getASTContext(), D1->getASTContext(),
                                     NonEquivalentDecls, false, false);
    return Ctx.IsStructurallyEquivalent(D0, D1);
  }

  bool testStructuralMatch(std::tuple<NamedDecl *, NamedDecl *> t) {
    return testStructuralMatch(get<0>(t), get<1>(t));
  }
};

TEST_F(StructuralEquivalenceTest, Int) {
  auto Decls = makeNamedDecls("int foo;", "int foo;", Lang_CXX);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedInt) {
  auto Decls = makeNamedDecls("int foo;", "signed int foo;", Lang_CXX);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, Char) {
  auto Decls = makeNamedDecls("char foo;", "char foo;", Lang_CXX);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

// This test is disabled for now.
// FIXME Whether this is equivalent is dependendant on the target.
TEST_F(StructuralEquivalenceTest, DISABLED_CharVsSignedChar) {
  auto Decls = makeNamedDecls("char foo;", "signed char foo;", Lang_CXX);
  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, ForwardRecordDecl) {
  auto Decls = makeNamedDecls("struct foo;", "struct foo;", Lang_CXX);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedIntInStruct) {
  auto Decls = makeNamedDecls("struct foo { int x; };",
                              "struct foo { signed int x; };", Lang_CXX);
  EXPECT_TRUE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharInStruct) {
  auto Decls = makeNamedDecls("struct foo { char x; };",
                              "struct foo { signed char x; };", Lang_CXX);
  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, IntVsSignedIntTemplateSpec) {
  auto Decls = makeNamedDecls(
      "template <class T> struct foo; template<> struct foo<int>{};",
      "template <class T> struct foo; template<> struct foo<signed int>{};",
      Lang_CXX);
  ClassTemplateSpecializationDecl *Spec0 =
      *cast<ClassTemplateDecl>(get<0>(Decls))->spec_begin();
  ClassTemplateSpecializationDecl *Spec1 =
      *cast<ClassTemplateDecl>(get<1>(Decls))->spec_begin();
  ASSERT_TRUE(Spec0 != nullptr);
  ASSERT_TRUE(Spec1 != nullptr);
  EXPECT_TRUE(testStructuralMatch(Spec0, Spec1));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpec) {
  auto Decls = makeNamedDecls(
      "template <class T> struct foo; template<> struct foo<char>{};",
      "template <class T> struct foo; template<> struct foo<signed char>{};",
      Lang_CXX);
  ClassTemplateSpecializationDecl *Spec0 =
      *cast<ClassTemplateDecl>(get<0>(Decls))->spec_begin();
  ClassTemplateSpecializationDecl *Spec1 =
      *cast<ClassTemplateDecl>(get<1>(Decls))->spec_begin();
  ASSERT_TRUE(Spec0 != nullptr);
  ASSERT_TRUE(Spec1 != nullptr);
  EXPECT_FALSE(testStructuralMatch(Spec0, Spec1));
}

TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpecWithInheritance) {
  auto Decls = makeNamedDecls(
      R"(
      struct true_type{};
      template <class T> struct foo;
      template<> struct foo<char> : true_type {};
      )",
      R"(
      struct true_type{};
      template <class T> struct foo;
      template<> struct foo<signed char> : true_type {};
      )",
      Lang_CXX);
  ClassTemplateSpecializationDecl *Spec0 =
      *cast<ClassTemplateDecl>(get<0>(Decls))->spec_begin();
  ClassTemplateSpecializationDecl *Spec1 =
      *cast<ClassTemplateDecl>(get<1>(Decls))->spec_begin();
  ASSERT_TRUE(Spec0 != nullptr);
  ASSERT_TRUE(Spec1 != nullptr);
  EXPECT_FALSE(testStructuralMatch(Spec0, Spec1));
}

// This test is disabled for now.
// FIXME Enable it, once the check is implemented.
TEST_F(StructuralEquivalenceTest, DISABLED_WrongOrderInNamespace) {
  auto Code =
      R"(
      namespace NS {
      template <class T> class Base {
          int a;
      };
      class Derived : Base<Derived> {
      };
      }
      void foo(NS::Derived &);
      )";
  auto Decls = makeNamedDecls(Code, Code, Lang_CXX);

  NamespaceDecl *NS =
      LastDeclMatcher<NamespaceDecl>().match(get<1>(Decls), namespaceDecl());
  ClassTemplateDecl *TD = LastDeclMatcher<ClassTemplateDecl>().match(
      get<1>(Decls), classTemplateDecl(hasName("Base")));

  // Reorder the decls, move the TD to the last place in the DC.
  NS->removeDecl(TD);
  NS->addDeclInternal(TD);

  EXPECT_FALSE(testStructuralMatch(Decls));
}

TEST_F(StructuralEquivalenceTest, WrongOrderOfFieldsInClass) {
  auto Code = "class X { int a; int b; };";
  auto Decls = makeNamedDecls(Code, Code, Lang_CXX, "X");

  CXXRecordDecl *RD = FirstDeclMatcher<CXXRecordDecl>().match(
      get<1>(Decls), cxxRecordDecl(hasName("X")));
  FieldDecl *FD =
      FirstDeclMatcher<FieldDecl>().match(get<1>(Decls), fieldDecl(hasName("a")));

  // Reorder the FieldDecls
  RD->removeDecl(FD);
  RD->addDeclInternal(FD);

  EXPECT_FALSE(testStructuralMatch(Decls));
}

} // end namespace ast_matchers
} // end namespace clang
OpenPOWER on IntegriCloud