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