Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 15 | #include "MatchVerifier.h" |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 16 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 18 | #include "clang/Tooling/Tooling.h" |
| 19 | #include "gtest/gtest.h" |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace ast_matchers { |
| 23 | |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 24 | TEST(GetParents, ReturnsParentForDecl) { |
| 25 | MatchVerifier<Decl> Verifier; |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 26 | EXPECT_TRUE( |
| 27 | Verifier.match("class C { void f(); };", |
| 28 | cxxMethodDecl(hasParent(recordDecl(hasName("C")))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | TEST(GetParents, ReturnsParentForStmt) { |
| 32 | MatchVerifier<Stmt> Verifier; |
| 33 | EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };", |
| 34 | ifStmt(hasParent(compoundStmt())))); |
| 35 | } |
| 36 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 37 | TEST(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 | |
| 44 | TEST(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 | |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 50 | TEST(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(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 55 | cxxMethodDecl(hasName("f"), |
| 56 | hasParent(cxxRecordDecl(isTemplateInstantiation()))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 57 | EXPECT_TRUE(DeclVerifier.match( |
| 58 | "template<typename T> struct C { void f() {} };" |
| 59 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 60 | cxxMethodDecl(hasName("f"), |
| 61 | hasParent(cxxRecordDecl(unless(isTemplateInstantiation())))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 62 | EXPECT_FALSE(DeclVerifier.match( |
| 63 | "template<typename T> struct C { void f() {} };" |
| 64 | "void g() { C<int> c; c.f(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 65 | cxxMethodDecl( |
| 66 | hasName("f"), |
| 67 | allOf(hasParent(cxxRecordDecl(unless(isTemplateInstantiation()))), |
| 68 | hasParent(cxxRecordDecl(isTemplateInstantiation())))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST(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(); }", |
Aaron Ballman | 512fb64 | 2015-09-17 13:30:52 +0000 | [diff] [blame] | 76 | compoundStmt(allOf( |
| 77 | hasAncestor(cxxRecordDecl(isTemplateInstantiation())), |
| 78 | hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation()))))))); |
Manuel Klimek | d4be408 | 2013-02-28 13:21:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | } // end namespace ast_matchers |
| 82 | } // end namespace clang |