blob: 94e9735654c219adee038f2dfdc257331de097bf [file] [log] [blame]
Manuel Klimekd4be4082013-02-28 13:21:39 +00001//===- 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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000015#include "MatchVerifier.h"
Manuel Klimekd4be4082013-02-28 13:21:39 +000016#include "clang/ASTMatchers/ASTMatchFinder.h"
17#include "clang/ASTMatchers/ASTMatchers.h"
18#include "clang/Tooling/Tooling.h"
19#include "gtest/gtest.h"
Manuel Klimekd4be4082013-02-28 13:21:39 +000020
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;
Aaron Ballman512fb642015-09-17 13:30:52 +000030 EXPECT_TRUE(
31 Verifier.match("class C { void f(); };",
32 cxxMethodDecl(hasParent(recordDecl(hasName("C"))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000033}
34
35TEST(GetParents, ReturnsParentForStmt) {
36 MatchVerifier<Stmt> Verifier;
37 EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
38 ifStmt(hasParent(compoundStmt()))));
39}
40
41TEST(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(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000046 cxxMethodDecl(hasName("f"),
47 hasParent(cxxRecordDecl(isTemplateInstantiation())))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000048 EXPECT_TRUE(DeclVerifier.match(
49 "template<typename T> struct C { void f() {} };"
50 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000051 cxxMethodDecl(hasName("f"),
52 hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000053 EXPECT_FALSE(DeclVerifier.match(
54 "template<typename T> struct C { void f() {} };"
55 "void g() { C<int> c; c.f(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000056 cxxMethodDecl(
57 hasName("f"),
58 allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))),
59 hasParent(cxxRecordDecl(isTemplateInstantiation()))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000060}
61
62TEST(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(); }",
Aaron Ballman512fb642015-09-17 13:30:52 +000067 compoundStmt(allOf(
68 hasAncestor(cxxRecordDecl(isTemplateInstantiation())),
69 hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
Manuel Klimekd4be4082013-02-28 13:21:39 +000070}
71
72} // end namespace ast_matchers
73} // end namespace clang