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 24TEST(GetParents, ReturnsParentForDecl) { 25 MatchVerifier<Decl> Verifier; 26 EXPECT_TRUE( 27 Verifier.match("class C { void f(); };", 28 cxxMethodDecl(hasParent(recordDecl(hasName("C")))))); 29} 30 31TEST(GetParents, ReturnsParentForStmt) { 32 MatchVerifier<Stmt> Verifier; 33 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };", 34 ifStmt(hasParent(compoundStmt())))); 35} 36 37TEST(GetParents, ReturnsParentForTypeLoc) { 38 MatchVerifier<TypeLoc> Verifier; 39 EXPECT_TRUE( 40 Verifier.match("namespace a { class b {}; } void f(a::b) {}", 41 typeLoc(hasParent(typeLoc(hasParent(functionDecl())))))); 42} 43 44TEST(GetParents, ReturnsParentForNestedNameSpecifierLoc) { 45 MatchVerifier<NestedNameSpecifierLoc> Verifier; 46 EXPECT_TRUE(Verifier.match("namespace a { class b {}; } void f(a::b) {}", 47 nestedNameSpecifierLoc(hasParent(typeLoc())))); 48} 49 50TEST(GetParents, ReturnsParentInsideTemplateInstantiations) { 51 MatchVerifier<Decl> DeclVerifier; 52 EXPECT_TRUE(DeclVerifier.match( 53 "template<typename T> struct C { void f() {} };" 54 "void g() { C<int> c; c.f(); }", 55 cxxMethodDecl(hasName("f"), 56 hasParent(cxxRecordDecl(isTemplateInstantiation()))))); 57 EXPECT_TRUE(DeclVerifier.match( 58 "template<typename T> struct C { void f() {} };" 59 "void g() { C<int> c; c.f(); }", 60 cxxMethodDecl(hasName("f"), 61 hasParent(cxxRecordDecl(unless(isTemplateInstantiation())))))); 62 EXPECT_FALSE(DeclVerifier.match( 63 "template<typename T> struct C { void f() {} };" 64 "void g() { C<int> c; c.f(); }", 65 cxxMethodDecl( 66 hasName("f"), 67 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))), 68 hasParent(cxxRecordDecl(isTemplateInstantiation())))))); 69} 70 71TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) { 72 MatchVerifier<Stmt> TemplateVerifier; 73 EXPECT_TRUE(TemplateVerifier.match( 74 "template<typename T> struct C { void f() {} };" 75 "void g() { C<int> c; c.f(); }", 76 compoundStmt(allOf( 77 hasAncestor(cxxRecordDecl(isTemplateInstantiation())), 78 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation()))))))); 79} 80 81} // end namespace ast_matchers 82} // end namespace clang 83