#include "clang/AST/ASTContext.h" #include "clang/AST/ASTStructuralEquivalence.h" #include "clang/ASTMatchers/ASTMatchers.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Testing/CommandLineArgs.h" #include "clang/Tooling/Tooling.h" #include "llvm/Support/Host.h" #include "DeclMatcher.h" #include "gtest/gtest.h" namespace clang { namespace ast_matchers { using std::get; struct StructuralEquivalenceTest : ::testing::Test { std::unique_ptr AST0, AST1; std::string Code0, Code1; // Buffers for SourceManager // Parses the source code in the specified language and sets the ASTs of // the current test instance to the parse result. void makeASTUnits(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang) { this->Code0 = SrcCode0; this->Code1 = SrcCode1; std::vector Args = getCommandLineArgsForTesting(Lang); const char *const InputFileName = "input.cc"; AST0 = tooling::buildASTFromCodeWithArgs(Code0, Args, InputFileName); AST1 = tooling::buildASTFromCodeWithArgs(Code1, Args, InputFileName); } // Get a pair of node pointers into the synthesized AST from the given code // snippets. To determine the returned node, a separate matcher is specified // for both snippets. The first matching node is returned. template std::tuple makeDecls(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const MatcherType &Matcher0, const MatcherType &Matcher1) { makeASTUnits(SrcCode0, SrcCode1, Lang); NodeType *D0 = FirstDeclMatcher().match( AST0->getASTContext().getTranslationUnitDecl(), Matcher0); NodeType *D1 = FirstDeclMatcher().match( AST1->getASTContext().getTranslationUnitDecl(), Matcher1); return std::make_tuple(D0, D1); } std::tuple makeTuDecls(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang) { makeASTUnits(SrcCode0, SrcCode1, Lang); return std::make_tuple(AST0->getASTContext().getTranslationUnitDecl(), AST1->getASTContext().getTranslationUnitDecl()); } // Get a pair of node pointers into the synthesized AST from the given code // snippets. The same matcher is used for both snippets. template std::tuple makeDecls(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const MatcherType &AMatcher) { return makeDecls( SrcCode0, SrcCode1, Lang, AMatcher, AMatcher); } // Get a pair of Decl pointers to the synthesized declarations from the given // code snippets. We search for the first NamedDecl with given name in both // snippets. std::tuple makeNamedDecls(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const char *const Identifier = "foo") { auto Matcher = namedDecl(hasName(Identifier)); return makeDecls(SrcCode0, SrcCode1, Lang, Matcher); } // Wraps a Stmt and the ASTContext that contains it. struct StmtWithASTContext { Stmt *S; ASTContext *Context; explicit StmtWithASTContext(Stmt &S, ASTContext &Context) : S(&S), Context(&Context) {} explicit StmtWithASTContext(FunctionDecl *FD) : S(FD->getBody()), Context(&FD->getASTContext()) {} }; // Get a pair of node pointers into the synthesized AST from the given code // snippets. To determine the returned node, a separate matcher is specified // for both snippets. The first matching node is returned. template std::tuple makeStmts(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const MatcherType &Matcher0, const MatcherType &Matcher1) { makeASTUnits(SrcCode0, SrcCode1, Lang); Stmt *S0 = FirstDeclMatcher().match( AST0->getASTContext().getTranslationUnitDecl(), Matcher0); Stmt *S1 = FirstDeclMatcher().match( AST1->getASTContext().getTranslationUnitDecl(), Matcher1); return std::make_tuple(StmtWithASTContext(*S0, AST0->getASTContext()), StmtWithASTContext(*S1, AST1->getASTContext())); } // Get a pair of node pointers into the synthesized AST from the given code // snippets. The same matcher is used for both snippets. template std::tuple makeStmts(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const MatcherType &AMatcher) { return makeStmts(SrcCode0, SrcCode1, Lang, AMatcher, AMatcher); } // Convenience function for makeStmts that wraps the code inside a function // body. template std::tuple makeWrappedStmts(const std::string &SrcCode0, const std::string &SrcCode1, TestLanguage Lang, const MatcherType &AMatcher) { auto Wrap = [](const std::string &Src) { return "void wrapped() {" + Src + ";}"; }; return makeStmts(Wrap(SrcCode0), Wrap(SrcCode1), Lang, AMatcher); } bool testStructuralMatch(Decl *D0, Decl *D1) { llvm::DenseSet> NonEquivalentDecls01; llvm::DenseSet> NonEquivalentDecls10; StructuralEquivalenceContext Ctx01( D0->getASTContext(), D1->getASTContext(), NonEquivalentDecls01, StructuralEquivalenceKind::Default, false, false); StructuralEquivalenceContext Ctx10( D1->getASTContext(), D0->getASTContext(), NonEquivalentDecls10, StructuralEquivalenceKind::Default, false, false); bool Eq01 = Ctx01.IsEquivalent(D0, D1); bool Eq10 = Ctx10.IsEquivalent(D1, D0); EXPECT_EQ(Eq01, Eq10); return Eq01; } bool testStructuralMatch(StmtWithASTContext S0, StmtWithASTContext S1) { llvm::DenseSet> NonEquivalentDecls01; llvm::DenseSet> NonEquivalentDecls10; StructuralEquivalenceContext Ctx01( *S0.Context, *S1.Context, NonEquivalentDecls01, StructuralEquivalenceKind::Default, false, false); StructuralEquivalenceContext Ctx10( *S1.Context, *S0.Context, NonEquivalentDecls10, StructuralEquivalenceKind::Default, false, false); bool Eq01 = Ctx01.IsEquivalent(S0.S, S1.S); bool Eq10 = Ctx10.IsEquivalent(S1.S, S0.S); EXPECT_EQ(Eq01, Eq10); return Eq01; } bool testStructuralMatch(std::tuple t) { return testStructuralMatch(get<0>(t), get<1>(t)); } bool testStructuralMatch(std::tuple t) { return testStructuralMatch(get<0>(t), get<1>(t)); } }; TEST_F(StructuralEquivalenceTest, Int) { auto Decls = makeNamedDecls("int foo;", "int foo;", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, IntVsSignedInt) { auto Decls = makeNamedDecls("int foo;", "signed int foo;", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, Char) { auto Decls = makeNamedDecls("char foo;", "char foo;", Lang_CXX03); 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_CXX03); EXPECT_FALSE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, ForwardRecordDecl) { auto Decls = makeNamedDecls("struct foo;", "struct foo;", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, IntVsSignedIntInStruct) { auto Decls = makeNamedDecls("struct foo { int x; };", "struct foo { signed int x; };", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, CharVsSignedCharInStruct) { auto Decls = makeNamedDecls("struct foo { char x; };", "struct foo { signed char x; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(Decls)); } TEST_F(StructuralEquivalenceTest, IntVsSignedIntTemplateSpec) { auto Decls = makeDecls( R"(template struct foo; template<> struct foo{};)", R"(template struct foo; template<> struct foo{};)", Lang_CXX03, classTemplateSpecializationDecl()); auto Spec0 = get<0>(Decls); auto Spec1 = get<1>(Decls); EXPECT_TRUE(testStructuralMatch(Spec0, Spec1)); } TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpec) { auto Decls = makeDecls( R"(template struct foo; template<> struct foo{};)", R"(template struct foo; template<> struct foo{};)", Lang_CXX03, classTemplateSpecializationDecl()); auto Spec0 = get<0>(Decls); auto Spec1 = get<1>(Decls); EXPECT_FALSE(testStructuralMatch(Spec0, Spec1)); } TEST_F(StructuralEquivalenceTest, CharVsSignedCharTemplateSpecWithInheritance) { auto Decls = makeDecls( R"( struct true_type{}; template struct foo; template<> struct foo : true_type {}; )", R"( struct true_type{}; template struct foo; template<> struct foo : true_type {}; )", Lang_CXX03, classTemplateSpecializationDecl()); EXPECT_FALSE(testStructuralMatch(Decls)); } // 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 Base { int a; }; class Derived : Base { }; } void foo(NS::Derived &); )"; auto Decls = makeNamedDecls(Code, Code, Lang_CXX03); NamespaceDecl *NS = LastDeclMatcher().match(get<1>(Decls), namespaceDecl()); ClassTemplateDecl *TD = LastDeclMatcher().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_CXX03, "X"); CXXRecordDecl *RD = FirstDeclMatcher().match( get<1>(Decls), cxxRecordDecl(hasName("X"))); FieldDecl *FD = FirstDeclMatcher().match(get<1>(Decls), fieldDecl(hasName("a"))); // Reorder the FieldDecls RD->removeDecl(FD); RD->addDeclInternal(FD); EXPECT_FALSE(testStructuralMatch(Decls)); } struct StructuralEquivalenceFunctionTest : StructuralEquivalenceTest { }; TEST_F(StructuralEquivalenceFunctionTest, TemplateVsNonTemplate) { auto t = makeNamedDecls("void foo();", "template void foo();", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, DifferentOperators) { auto t = makeDecls( "struct X{}; bool operator<(X, X);", "struct X{}; bool operator==(X, X);", Lang_CXX03, functionDecl(hasOverloadedOperatorName("<")), functionDecl(hasOverloadedOperatorName("=="))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, SameOperators) { auto t = makeDecls( "struct X{}; bool operator<(X, X);", "struct X{}; bool operator<(X, X);", Lang_CXX03, functionDecl(hasOverloadedOperatorName("<")), functionDecl(hasOverloadedOperatorName("<"))); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, CtorVsDtor) { auto t = makeDecls("struct X{ X(); };", "struct X{ ~X(); };", Lang_CXX03, cxxConstructorDecl(), cxxDestructorDecl()); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamConstWithRef) { auto t = makeNamedDecls("void foo(int&);", "void foo(const int&);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamConstSimple) { auto t = makeNamedDecls("void foo(int);", "void foo(const int);", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); // consider this OK } TEST_F(StructuralEquivalenceFunctionTest, Throw) { auto t = makeNamedDecls("void foo();", "void foo() throw();", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, Noexcept) { auto t = makeNamedDecls("void foo();", "void foo() noexcept;", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexcept) { auto t = makeNamedDecls("void foo() throw();", "void foo() noexcept;", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexceptFalse) { auto t = makeNamedDecls("void foo() throw();", "void foo() noexcept(false);", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ThrowVsNoexceptTrue) { auto t = makeNamedDecls("void foo() throw();", "void foo() noexcept(true);", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NoexceptNonMatch) { auto t = makeNamedDecls("void foo() noexcept(false);", "void foo() noexcept(true);", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NoexceptMatch) { auto t = makeNamedDecls("void foo() noexcept(false);", "void foo() noexcept(false);", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NoexceptVsNoexceptFalse) { auto t = makeNamedDecls("void foo() noexcept;", "void foo() noexcept(false);", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NoexceptVsNoexceptTrue) { auto t = makeNamedDecls("void foo() noexcept;", "void foo() noexcept(true);", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ReturnType) { auto t = makeNamedDecls("char foo();", "int foo();", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ReturnConst) { auto t = makeNamedDecls("char foo();", "const char foo();", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ReturnRef) { auto t = makeNamedDecls("char &foo();", "char &&foo();", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamCount) { auto t = makeNamedDecls("void foo(int);", "void foo(int, int);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamType) { auto t = makeNamedDecls("void foo(int);", "void foo(char);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamName) { auto t = makeNamedDecls("void foo(int a);", "void foo(int b);", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, Variadic) { auto t = makeNamedDecls("void foo(int x...);", "void foo(int x);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, ParamPtr) { auto t = makeNamedDecls("void foo(int *);", "void foo(int);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NameInParen) { auto t = makeNamedDecls("void ((foo))();", "void foo();", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NameInParenWithExceptionSpec) { auto t = makeNamedDecls( "void (foo)() throw(int);", "void (foo)() noexcept;", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, NameInParenWithConst) { auto t = makeNamedDecls( "struct A { void (foo)() const; };", "struct A { void (foo)(); };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, FunctionsWithDifferentNoreturnAttr) { auto t = makeNamedDecls("__attribute__((noreturn)) void foo();", " void foo();", Lang_C99); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, FunctionsWithDifferentCallingConventions) { // These attributes may not be available on certain platforms. if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getArch() != llvm::Triple::x86_64) return; auto t = makeNamedDecls("__attribute__((preserve_all)) void foo();", "__attribute__((ms_abi)) void foo();", Lang_C99); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceFunctionTest, FunctionsWithDifferentSavedRegsAttr) { if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getArch() != llvm::Triple::x86_64) return; auto t = makeNamedDecls( "__attribute__((no_caller_saved_registers)) void foo();", " void foo();", Lang_C99); EXPECT_FALSE(testStructuralMatch(t)); } struct StructuralEquivalenceCXXMethodTest : StructuralEquivalenceTest { }; TEST_F(StructuralEquivalenceCXXMethodTest, Virtual) { auto t = makeDecls("struct X { void foo(); };", "struct X { virtual void foo(); };", Lang_CXX03, cxxMethodDecl(hasName("foo"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Pure) { auto t = makeNamedDecls("struct X { virtual void foo(); };", "struct X { virtual void foo() = 0; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, DISABLED_Final) { // The final-ness is not checked yet. auto t = makeNamedDecls("struct X { virtual void foo(); };", "struct X { virtual void foo() final; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Const) { auto t = makeNamedDecls("struct X { void foo(); };", "struct X { void foo() const; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Static) { auto t = makeNamedDecls("struct X { void foo(); };", "struct X { static void foo(); };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Ref1) { auto t = makeNamedDecls("struct X { void foo(); };", "struct X { void foo() &&; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Ref2) { auto t = makeNamedDecls("struct X { void foo() &; };", "struct X { void foo() &&; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, AccessSpecifier) { auto t = makeDecls("struct X { public: void foo(); };", "struct X { private: void foo(); };", Lang_CXX03, cxxMethodDecl(hasName("foo"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Delete) { auto t = makeNamedDecls("struct X { void foo(); };", "struct X { void foo() = delete; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Constructor) { auto t = makeDecls("void foo();", "struct foo { foo(); };", Lang_CXX03, functionDecl(hasName("foo")), cxxConstructorDecl(hasName("foo"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorParam) { auto t = makeDecls("struct X { X(); };", "struct X { X(int); };", Lang_CXX03, cxxConstructorDecl(hasName("X"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorExplicit) { auto t = makeDecls("struct X { X(int); };", "struct X { explicit X(int); };", Lang_CXX11, cxxConstructorDecl(hasName("X"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, ConstructorDefault) { auto t = makeDecls("struct X { X(); };", "struct X { X() = default; };", Lang_CXX11, cxxConstructorDecl(hasName("X"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Conversion) { auto t = makeDecls("struct X { operator bool(); };", "struct X { operator char(); };", Lang_CXX11, cxxConversionDecl()); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, Operator) { auto t = makeDecls("struct X { int operator +(int); };", "struct X { int operator -(int); };", Lang_CXX03, functionDecl(hasOverloadedOperatorName("+")), functionDecl(hasOverloadedOperatorName("-"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, OutOfClass1) { auto t = makeDecls( "struct X { virtual void f(); }; void X::f() { }", "struct X { virtual void f() { }; };", Lang_CXX03, functionDecl(allOf(hasName("f"), isDefinition()))); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceCXXMethodTest, OutOfClass2) { auto t = makeDecls( "struct X { virtual void f(); }; void X::f() { }", "struct X { void f(); }; void X::f() { }", Lang_CXX03, functionDecl(allOf(hasName("f"), isDefinition()))); EXPECT_FALSE(testStructuralMatch(t)); } struct StructuralEquivalenceRecordTest : StructuralEquivalenceTest { // FIXME Use a common getRecordDecl with ASTImporterTest.cpp! RecordDecl *getRecordDecl(FieldDecl *FD) { auto *ET = cast(FD->getType().getTypePtr()); return cast(ET->getNamedType().getTypePtr())->getDecl(); }; }; TEST_F(StructuralEquivalenceRecordTest, Name) { auto t = makeDecls("struct A{ };", "struct B{ };", Lang_CXX03, cxxRecordDecl(hasName("A")), cxxRecordDecl(hasName("B"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, Fields) { auto t = makeNamedDecls("struct foo{ int x; };", "struct foo{ char x; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, DISABLED_Methods) { // Currently, methods of a class are not checked at class equivalence. auto t = makeNamedDecls("struct foo{ int x(); };", "struct foo{ char x(); };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, Bases) { auto t = makeNamedDecls("struct A{ }; struct foo: A { };", "struct B{ }; struct foo: B { };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, InheritanceVirtual) { auto t = makeNamedDecls("struct A{ }; struct foo: A { };", "struct A{ }; struct foo: virtual A { };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, DISABLED_InheritanceType) { // Access specifier in inheritance is not checked yet. auto t = makeNamedDecls("struct A{ }; struct foo: public A { };", "struct A{ }; struct foo: private A { };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, Match) { auto Code = R"( struct A{ }; struct B{ }; struct foo: A, virtual B { void x(); int a; }; )"; auto t = makeNamedDecls(Code, Code, Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, UnnamedRecordsShouldBeInequivalent) { auto t = makeTuDecls( R"( struct A { struct { struct A *next; } entry0; struct { struct A *next; } entry1; }; )", "", Lang_C99); auto *TU = get<0>(t); auto *Entry0 = FirstDeclMatcher().match(TU, fieldDecl(hasName("entry0"))); auto *Entry1 = FirstDeclMatcher().match(TU, fieldDecl(hasName("entry1"))); auto *R0 = getRecordDecl(Entry0); auto *R1 = getRecordDecl(Entry1); ASSERT_NE(R0, R1); EXPECT_TRUE(testStructuralMatch(R0, R0)); EXPECT_TRUE(testStructuralMatch(R1, R1)); EXPECT_FALSE(testStructuralMatch(R0, R1)); } TEST_F(StructuralEquivalenceRecordTest, AnonymousRecordsShouldBeInequivalent) { auto t = makeTuDecls( R"( struct X { struct { int a; }; struct { int b; }; }; )", "", Lang_C99); auto *TU = get<0>(t); auto *A = FirstDeclMatcher().match( TU, indirectFieldDecl(hasName("a"))); auto *FA = cast(A->chain().front()); RecordDecl *RA = cast(FA->getType().getTypePtr())->getDecl(); auto *B = FirstDeclMatcher().match( TU, indirectFieldDecl(hasName("b"))); auto *FB = cast(B->chain().front()); RecordDecl *RB = cast(FB->getType().getTypePtr())->getDecl(); ASSERT_NE(RA, RB); EXPECT_TRUE(testStructuralMatch(RA, RA)); EXPECT_TRUE(testStructuralMatch(RB, RB)); EXPECT_FALSE(testStructuralMatch(RA, RB)); } TEST_F(StructuralEquivalenceRecordTest, RecordsAreInequivalentIfOrderOfAnonRecordsIsDifferent) { auto t = makeTuDecls( R"( struct X { struct { int a; }; struct { int b; }; }; )", R"( struct X { // The order is reversed. struct { int b; }; struct { int a; }; }; )", Lang_C99); auto *TU = get<0>(t); auto *A = FirstDeclMatcher().match( TU, indirectFieldDecl(hasName("a"))); auto *FA = cast(A->chain().front()); RecordDecl *RA = cast(FA->getType().getTypePtr())->getDecl(); auto *TU1 = get<1>(t); auto *A1 = FirstDeclMatcher().match( TU1, indirectFieldDecl(hasName("a"))); auto *FA1 = cast(A1->chain().front()); RecordDecl *RA1 = cast(FA1->getType().getTypePtr())->getDecl(); RecordDecl *X = FirstDeclMatcher().match(TU, recordDecl(hasName("X"))); RecordDecl *X1 = FirstDeclMatcher().match(TU1, recordDecl(hasName("X"))); ASSERT_NE(X, X1); EXPECT_FALSE(testStructuralMatch(X, X1)); ASSERT_NE(RA, RA1); EXPECT_TRUE(testStructuralMatch(RA, RA)); EXPECT_TRUE(testStructuralMatch(RA1, RA1)); EXPECT_FALSE(testStructuralMatch(RA1, RA)); } TEST_F(StructuralEquivalenceRecordTest, UnnamedRecordsShouldBeInequivalentEvenIfTheSecondIsBeingDefined) { auto Code = R"( struct A { struct { struct A *next; } entry0; struct { struct A *next; } entry1; }; )"; auto t = makeTuDecls(Code, Code, Lang_C99); auto *FromTU = get<0>(t); auto *Entry1 = FirstDeclMatcher().match(FromTU, fieldDecl(hasName("entry1"))); auto *ToTU = get<1>(t); auto *Entry0 = FirstDeclMatcher().match(ToTU, fieldDecl(hasName("entry0"))); auto *A = FirstDeclMatcher().match(ToTU, recordDecl(hasName("A"))); A->startDefinition(); // Set isBeingDefined, getDefinition() will return a // nullptr. This may be the case during ASTImport. auto *R0 = getRecordDecl(Entry0); auto *R1 = getRecordDecl(Entry1); ASSERT_NE(R0, R1); EXPECT_TRUE(testStructuralMatch(R0, R0)); EXPECT_TRUE(testStructuralMatch(R1, R1)); EXPECT_FALSE(testStructuralMatch(R0, R1)); } TEST_F(StructuralEquivalenceRecordTest, TemplateVsNonTemplate) { auto t = makeDecls("struct A { };", "template struct A { };", Lang_CXX03, cxxRecordDecl(hasName("A"))); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, FwdDeclRecordShouldBeEqualWithFwdDeclRecord) { auto t = makeNamedDecls("class foo;", "class foo;", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, FwdDeclRecordShouldBeEqualWithRecordWhichHasDefinition) { auto t = makeNamedDecls("class foo;", "class foo { int A; };", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, RecordShouldBeEqualWithRecordWhichHasDefinition) { auto t = makeNamedDecls("class foo { int A; };", "class foo { int A; };", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, RecordsWithDifferentBody) { auto t = makeNamedDecls("class foo { int B; };", "class foo { int A; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, SameFriendMultipleTimes) { auto t = makeNamedDecls("struct foo { friend class X; };", "struct foo { friend class X; friend class X; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, SameFriendsDifferentOrder) { auto t = makeNamedDecls("struct foo { friend class X; friend class Y; };", "struct foo { friend class Y; friend class X; };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceRecordTest, SameFriendsSameOrder) { auto t = makeNamedDecls("struct foo { friend class X; friend class Y; };", "struct foo { friend class X; friend class Y; };", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } struct StructuralEquivalenceLambdaTest : StructuralEquivalenceTest {}; TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithDifferentMethods) { // Get the LambdaExprs, unfortunately we can't match directly the underlying // implicit CXXRecordDecl of the Lambda classes. auto t = makeDecls( "void f() { auto L0 = [](int){}; }", "void f() { auto L1 = [](){}; }", Lang_CXX11, lambdaExpr(), lambdaExpr()); CXXRecordDecl *L0 = get<0>(t)->getLambdaClass(); CXXRecordDecl *L1 = get<1>(t)->getLambdaClass(); EXPECT_FALSE(testStructuralMatch(L0, L1)); } TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithEqMethods) { auto t = makeDecls( "void f() { auto L0 = [](int){}; }", "void f() { auto L1 = [](int){}; }", Lang_CXX11, lambdaExpr(), lambdaExpr()); CXXRecordDecl *L0 = get<0>(t)->getLambdaClass(); CXXRecordDecl *L1 = get<1>(t)->getLambdaClass(); EXPECT_TRUE(testStructuralMatch(L0, L1)); } TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithDifferentFields) { auto t = makeDecls( "void f() { char* X; auto L0 = [X](){}; }", "void f() { float X; auto L1 = [X](){}; }", Lang_CXX11, lambdaExpr(), lambdaExpr()); CXXRecordDecl *L0 = get<0>(t)->getLambdaClass(); CXXRecordDecl *L1 = get<1>(t)->getLambdaClass(); EXPECT_FALSE(testStructuralMatch(L0, L1)); } TEST_F(StructuralEquivalenceLambdaTest, LambdaClassesWithEqFields) { auto t = makeDecls( "void f() { float X; auto L0 = [X](){}; }", "void f() { float X; auto L1 = [X](){}; }", Lang_CXX11, lambdaExpr(), lambdaExpr()); CXXRecordDecl *L0 = get<0>(t)->getLambdaClass(); CXXRecordDecl *L1 = get<1>(t)->getLambdaClass(); EXPECT_TRUE(testStructuralMatch(L0, L1)); } TEST_F(StructuralEquivalenceTest, CompareSameDeclWithMultiple) { auto t = makeNamedDecls("struct A{ }; struct B{ }; void foo(A a, A b);", "struct A{ }; struct B{ }; void foo(A a, B b);", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTest, ExplicitBoolDifferent) { auto Decls = makeNamedDecls("struct foo {explicit(false) foo(int);};", "struct foo {explicit(true) foo(int);};", Lang_CXX20); CXXConstructorDecl *First = FirstDeclMatcher().match( get<0>(Decls), cxxConstructorDecl(hasName("foo"))); CXXConstructorDecl *Second = FirstDeclMatcher().match( get<1>(Decls), cxxConstructorDecl(hasName("foo"))); EXPECT_FALSE(testStructuralMatch(First, Second)); } TEST_F(StructuralEquivalenceTest, ExplicitBoolSame) { auto Decls = makeNamedDecls("struct foo {explicit(true) foo(int);};", "struct foo {explicit(true) foo(int);};", Lang_CXX20); CXXConstructorDecl *First = FirstDeclMatcher().match( get<0>(Decls), cxxConstructorDecl(hasName("foo"))); CXXConstructorDecl *Second = FirstDeclMatcher().match( get<1>(Decls), cxxConstructorDecl(hasName("foo"))); EXPECT_TRUE(testStructuralMatch(First, Second)); } struct StructuralEquivalenceEnumTest : StructuralEquivalenceTest {}; TEST_F(StructuralEquivalenceEnumTest, FwdDeclEnumShouldBeEqualWithFwdDeclEnum) { auto t = makeNamedDecls("enum class foo;", "enum class foo;", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceEnumTest, FwdDeclEnumShouldBeEqualWithEnumWhichHasDefinition) { auto t = makeNamedDecls("enum class foo;", "enum class foo { A };", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceEnumTest, EnumShouldBeEqualWithEnumWhichHasDefinition) { auto t = makeNamedDecls("enum class foo { A };", "enum class foo { A };", Lang_CXX11); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceEnumTest, EnumsWithDifferentBody) { auto t = makeNamedDecls("enum class foo { B };", "enum class foo { A };", Lang_CXX11); EXPECT_FALSE(testStructuralMatch(t)); } struct StructuralEquivalenceTemplateTest : StructuralEquivalenceTest {}; TEST_F(StructuralEquivalenceTemplateTest, ExactlySameTemplates) { auto t = makeNamedDecls("template struct foo;", "template struct foo;", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, DifferentTemplateArgName) { auto t = makeNamedDecls("template struct foo;", "template struct foo;", Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, DifferentTemplateArgKind) { auto t = makeNamedDecls("template struct foo;", "template struct foo;", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, BitFieldDecl) { const char *Code = "class foo { int a : 2; };"; auto t = makeNamedDecls(Code, Code, Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, BitFieldDeclDifferentWidth) { auto t = makeNamedDecls("class foo { int a : 2; };", "class foo { int a : 4; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, DependentBitFieldDecl) { const char *Code = "template class foo { int a : sizeof(T); };"; auto t = makeNamedDecls(Code, Code, Lang_CXX03); EXPECT_TRUE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, DependentBitFieldDeclDifferentVal) { auto t = makeNamedDecls( "template class foo { int a : sizeof(A); };", "template class foo { int a : sizeof(B); };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, DependentBitFieldDeclDifferentVal2) { auto t = makeNamedDecls( "template class foo { int a : sizeof(A); };", "template class foo { int a : sizeof(A) + 1; };", Lang_CXX03); EXPECT_FALSE(testStructuralMatch(t)); } TEST_F(StructuralEquivalenceTemplateTest, ExplicitBoolSame) { auto Decls = makeNamedDecls( "template struct foo {explicit(b) foo(int);};", "template struct foo {explicit(b) foo(int);};", Lang_CXX20); CXXConstructorDecl *First = FirstDeclMatcher().match( get<0>(Decls), cxxConstructorDecl(hasName("foo"))); CXXConstructorDecl *Second = FirstDeclMatcher().match( get<1>(Decls), cxxConstructorDecl(hasName("foo"))); EXPECT_TRUE(testStructuralMatch(First, Second)); } TEST_F(StructuralEquivalenceTemplateTest, ExplicitBoolDifference) { auto Decls = makeNamedDecls( "template struct foo {explicit(b) foo(int);};", "template struct foo {explicit(!b) foo(int);};", Lang_CXX20); CXXConstructorDecl *First = FirstDeclMatcher().match( get<0>(Decls), cxxConstructorDecl(hasName("foo"))); CXXConstructorDecl *Second = FirstDeclMatcher().match( get<1>(Decls), cxxConstructorDecl(hasName("foo"))); EXPECT_FALSE(testStructuralMatch(First, Second)); } TEST_F(StructuralEquivalenceTemplateTest, TemplateVsSubstTemplateTemplateParmInArgEq) { auto t = makeDecls( R"( template class Arg { }; template