ASTContextParentMapTest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "clang/AST/ASTContext.h"
  14. #include "clang/ASTMatchers/ASTMatchFinder.h"
  15. #include "clang/ASTMatchers/ASTMatchers.h"
  16. #include "clang/Tooling/Tooling.h"
  17. #include "gtest/gtest.h"
  18. #include "MatchVerifier.h"
  19. namespace clang {
  20. namespace ast_matchers {
  21. using clang::tooling::newFrontendActionFactory;
  22. using clang::tooling::runToolOnCodeWithArgs;
  23. using clang::tooling::FrontendActionFactory;
  24. TEST(GetParents, ReturnsParentForDecl) {
  25. MatchVerifier<Decl> Verifier;
  26. EXPECT_TRUE(Verifier.match("class C { void f(); };",
  27. methodDecl(hasParent(recordDecl(hasName("C"))))));
  28. }
  29. TEST(GetParents, ReturnsParentForStmt) {
  30. MatchVerifier<Stmt> Verifier;
  31. EXPECT_TRUE(Verifier.match("class C { void f() { if (true) {} } };",
  32. ifStmt(hasParent(compoundStmt()))));
  33. }
  34. TEST(GetParents, ReturnsParentInsideTemplateInstantiations) {
  35. MatchVerifier<Decl> DeclVerifier;
  36. EXPECT_TRUE(DeclVerifier.match(
  37. "template<typename T> struct C { void f() {} };"
  38. "void g() { C<int> c; c.f(); }",
  39. methodDecl(hasName("f"),
  40. hasParent(recordDecl(isTemplateInstantiation())))));
  41. EXPECT_TRUE(DeclVerifier.match(
  42. "template<typename T> struct C { void f() {} };"
  43. "void g() { C<int> c; c.f(); }",
  44. methodDecl(hasName("f"),
  45. hasParent(recordDecl(unless(isTemplateInstantiation()))))));
  46. EXPECT_FALSE(DeclVerifier.match(
  47. "template<typename T> struct C { void f() {} };"
  48. "void g() { C<int> c; c.f(); }",
  49. methodDecl(hasName("f"),
  50. allOf(hasParent(recordDecl(unless(isTemplateInstantiation()))),
  51. hasParent(recordDecl(isTemplateInstantiation()))))));
  52. }
  53. TEST(GetParents, ReturnsMultipleParentsInTemplateInstantiations) {
  54. MatchVerifier<Stmt> TemplateVerifier;
  55. EXPECT_TRUE(TemplateVerifier.match(
  56. "template<typename T> struct C { void f() {} };"
  57. "void g() { C<int> c; c.f(); }",
  58. compoundStmt(
  59. allOf(hasAncestor(recordDecl(isTemplateInstantiation())),
  60. hasAncestor(recordDecl(unless(isTemplateInstantiation())))))));
  61. }
  62. } // end namespace ast_matchers
  63. } // end namespace clang