1 //===- unittest/AST/ASTContextParentMapTest.cpp - AST parent map test -----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Tests for the getParents(...) methods of ASTContext. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/ASTContext.h" 15 #include "MatchVerifier.h" 16 #include "clang/ASTMatchers/ASTMatchFinder.h" 17 #include "clang/ASTMatchers/ASTMatchers.h" 18 #include "clang/Tooling/Tooling.h" 19 #include "gtest/gtest.h" 20 21 namespace clang { 22 namespace ast_matchers { 23 24 using clang::tooling::newFrontendActionFactory; 25 using clang::tooling::runToolOnCodeWithArgs; 26 using clang::tooling::FrontendActionFactory; 27 28 TEST(GetParents, ReturnsParentForDecl) { 29 MatchVerifier<Decl> Verifier; 30 EXPECT_TRUE( 31 Verifier.match("class C { void f(); };", 32 cxxMethodDecl(hasParent(recordDecl(hasName("C")))))); 33 } 34 35 TEST(GetParents, ReturnsParentForStmt) { 36 MatchVerifier<Stmt> Verifier; 37 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };", 38 ifStmt(hasParent(compoundStmt())))); 39 } 40 41 TEST(GetParents, ReturnsParentInsideTemplateInstantiations) { 42 MatchVerifier<Decl> DeclVerifier; 43 EXPECT_TRUE(DeclVerifier.match( 44 "template<typename T> struct C { void f() {} };" 45 "void g() { C<int> c; c.f(); }", 46 cxxMethodDecl(hasName("f"), 47 hasParent(cxxRecordDecl(isTemplateInstantiation()))))); 48 EXPECT_TRUE(DeclVerifier.match( 49 "template<typename T> struct C { void f() {} };" 50 "void g() { C<int> c; c.f(); }", 51 cxxMethodDecl(hasName("f"), 52 hasParent(cxxRecordDecl(unless(isTemplateInstantiation())))))); 53 EXPECT_FALSE(DeclVerifier.match( 54 "template<typename T> struct C { void f() {} };" 55 "void g() { C<int> c; c.f(); }", 56 cxxMethodDecl( 57 hasName("f"), 58 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))), 59 hasParent(cxxRecordDecl(isTemplateInstantiation())))))); 60 } 61 62 TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) { 63 MatchVerifier<Stmt> TemplateVerifier; 64 EXPECT_TRUE(TemplateVerifier.match( 65 "template<typename T> struct C { void f() {} };" 66 "void g() { C<int> c; c.f(); }", 67 compoundStmt(allOf( 68 hasAncestor(cxxRecordDecl(isTemplateInstantiation())), 69 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation()))))))); 70 } 71 72 } // end namespace ast_matchers 73 } // end namespace clang 74