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 21namespace clang { 22namespace ast_matchers { 23 24using clang::tooling::newFrontendActionFactory; 25using clang::tooling::runToolOnCodeWithArgs; 26using clang::tooling::FrontendActionFactory; 27 28TEST(GetParents, ReturnsParentForDecl) { 29 MatchVerifier<Decl> Verifier; 30 EXPECT_TRUE(Verifier.match("class C { void f(); };", 31 methodDecl(hasParent(recordDecl(hasName("C")))))); 32} 33 34TEST(GetParents, ReturnsParentForStmt) { 35 MatchVerifier<Stmt> Verifier; 36 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };", 37 ifStmt(hasParent(compoundStmt())))); 38} 39 40TEST(GetParents, ReturnsParentInsideTemplateInstantiations) { 41 MatchVerifier<Decl> DeclVerifier; 42 EXPECT_TRUE(DeclVerifier.match( 43 "template<typename T> struct C { void f() {} };" 44 "void g() { C<int> c; c.f(); }", 45 methodDecl(hasName("f"), 46 hasParent(recordDecl(isTemplateInstantiation()))))); 47 EXPECT_TRUE(DeclVerifier.match( 48 "template<typename T> struct C { void f() {} };" 49 "void g() { C<int> c; c.f(); }", 50 methodDecl(hasName("f"), 51 hasParent(recordDecl(unless(isTemplateInstantiation())))))); 52 EXPECT_FALSE(DeclVerifier.match( 53 "template<typename T> struct C { void f() {} };" 54 "void g() { C<int> c; c.f(); }", 55 methodDecl(hasName("f"), 56 allOf(hasParent(recordDecl(unless(isTemplateInstantiation()))), 57 hasParent(recordDecl(isTemplateInstantiation())))))); 58} 59 60TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) { 61 MatchVerifier<Stmt> TemplateVerifier; 62 EXPECT_TRUE(TemplateVerifier.match( 63 "template<typename T> struct C { void f() {} };" 64 "void g() { C<int> c; c.f(); }", 65 compoundStmt( 66 allOf(hasAncestor(recordDecl(isTemplateInstantiation())), 67 hasAncestor(recordDecl(unless(isTemplateInstantiation()))))))); 68} 69 70} // end namespace ast_matchers 71} // end namespace clang 72