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