ASTMatchersInternalTest.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // unittests/ASTMatchers/ASTMatchersInternalTest.cpp - AST matcher unit tests //
  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. #include "ASTMatchersTest.h"
  10. #include "clang/AST/PrettyPrinter.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/ASTMatchers/ASTMatchers.h"
  13. #include "clang/Tooling/Tooling.h"
  14. #include "llvm/ADT/Triple.h"
  15. #include "llvm/Support/Host.h"
  16. #include "gtest/gtest.h"
  17. namespace clang {
  18. namespace ast_matchers {
  19. #if GTEST_HAS_DEATH_TEST
  20. TEST(HasNameDeathTest, DiesOnEmptyName) {
  21. ASSERT_DEBUG_DEATH({
  22. DeclarationMatcher HasEmptyName = recordDecl(hasName(""));
  23. EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
  24. }, "");
  25. }
  26. TEST(HasNameDeathTest, DiesOnEmptyPattern) {
  27. ASSERT_DEBUG_DEATH({
  28. DeclarationMatcher HasEmptyName = recordDecl(matchesName(""));
  29. EXPECT_TRUE(notMatches("class X {};", HasEmptyName));
  30. }, "");
  31. }
  32. TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) {
  33. ASSERT_DEBUG_DEATH({
  34. DeclarationMatcher IsDerivedFromEmpty = cxxRecordDecl(isDerivedFrom(""));
  35. EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty));
  36. }, "");
  37. }
  38. #endif
  39. TEST(ConstructVariadic, MismatchedTypes_Regression) {
  40. EXPECT_TRUE(
  41. matches("const int a = 0;",
  42. internal::DynTypedMatcher::constructVariadic(
  43. internal::DynTypedMatcher::VO_AnyOf,
  44. ast_type_traits::ASTNodeKind::getFromNodeKind<QualType>(),
  45. {isConstQualified(), arrayType()})
  46. .convertTo<QualType>()));
  47. }
  48. // For testing AST_MATCHER_P().
  49. AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) {
  50. // Make sure all special variables are used: node, match_finder,
  51. // bound_nodes_builder, and the parameter named 'AMatcher'.
  52. return AMatcher.matches(Node, Finder, Builder);
  53. }
  54. TEST(AstMatcherPMacro, Works) {
  55. DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b")));
  56. EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
  57. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("b")));
  58. EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
  59. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("a")));
  60. EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
  61. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("b")));
  62. }
  63. AST_POLYMORPHIC_MATCHER_P(polymorphicHas,
  64. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt),
  65. internal::Matcher<Decl>, AMatcher) {
  66. return Finder->matchesChildOf(
  67. Node, AMatcher, Builder,
  68. ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses,
  69. ASTMatchFinder::BK_First);
  70. }
  71. TEST(AstPolymorphicMatcherPMacro, Works) {
  72. DeclarationMatcher HasClassB =
  73. polymorphicHas(recordDecl(hasName("B")).bind("b"));
  74. EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };",
  75. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("b")));
  76. EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };",
  77. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("a")));
  78. EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };",
  79. HasClassB, llvm::make_unique<VerifyIdIsBoundTo<Decl>>("b")));
  80. StatementMatcher StatementHasClassB =
  81. polymorphicHas(recordDecl(hasName("B")));
  82. EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB));
  83. }
  84. TEST(MatchFinder, CheckProfiling) {
  85. MatchFinder::MatchFinderOptions Options;
  86. llvm::StringMap<llvm::TimeRecord> Records;
  87. Options.CheckProfiling.emplace(Records);
  88. MatchFinder Finder(std::move(Options));
  89. struct NamedCallback : public MatchFinder::MatchCallback {
  90. void run(const MatchFinder::MatchResult &Result) override {}
  91. StringRef getID() const override { return "MyID"; }
  92. } Callback;
  93. Finder.addMatcher(decl(), &Callback);
  94. std::unique_ptr<FrontendActionFactory> Factory(
  95. newFrontendActionFactory(&Finder));
  96. ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
  97. EXPECT_EQ(1u, Records.size());
  98. EXPECT_EQ("MyID", Records.begin()->getKey());
  99. }
  100. class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback {
  101. public:
  102. VerifyStartOfTranslationUnit() : Called(false) {}
  103. void run(const MatchFinder::MatchResult &Result) override {
  104. EXPECT_TRUE(Called);
  105. }
  106. void onStartOfTranslationUnit() override { Called = true; }
  107. bool Called;
  108. };
  109. TEST(MatchFinder, InterceptsStartOfTranslationUnit) {
  110. MatchFinder Finder;
  111. VerifyStartOfTranslationUnit VerifyCallback;
  112. Finder.addMatcher(decl(), &VerifyCallback);
  113. std::unique_ptr<FrontendActionFactory> Factory(
  114. newFrontendActionFactory(&Finder));
  115. ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
  116. EXPECT_TRUE(VerifyCallback.Called);
  117. VerifyCallback.Called = false;
  118. std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
  119. ASSERT_TRUE(AST.get());
  120. Finder.matchAST(AST->getASTContext());
  121. EXPECT_TRUE(VerifyCallback.Called);
  122. }
  123. class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback {
  124. public:
  125. VerifyEndOfTranslationUnit() : Called(false) {}
  126. void run(const MatchFinder::MatchResult &Result) override {
  127. EXPECT_FALSE(Called);
  128. }
  129. void onEndOfTranslationUnit() override { Called = true; }
  130. bool Called;
  131. };
  132. TEST(MatchFinder, InterceptsEndOfTranslationUnit) {
  133. MatchFinder Finder;
  134. VerifyEndOfTranslationUnit VerifyCallback;
  135. Finder.addMatcher(decl(), &VerifyCallback);
  136. std::unique_ptr<FrontendActionFactory> Factory(
  137. newFrontendActionFactory(&Finder));
  138. ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;"));
  139. EXPECT_TRUE(VerifyCallback.Called);
  140. VerifyCallback.Called = false;
  141. std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;"));
  142. ASSERT_TRUE(AST.get());
  143. Finder.matchAST(AST->getASTContext());
  144. EXPECT_TRUE(VerifyCallback.Called);
  145. }
  146. TEST(Matcher, matchOverEntireASTContext) {
  147. std::unique_ptr<ASTUnit> AST =
  148. clang::tooling::buildASTFromCode("struct { int *foo; };");
  149. ASSERT_TRUE(AST.get());
  150. auto PT = selectFirst<PointerType>(
  151. "x", match(pointerType().bind("x"), AST->getASTContext()));
  152. EXPECT_NE(nullptr, PT);
  153. }
  154. TEST(IsInlineMatcher, IsInline) {
  155. EXPECT_TRUE(matches("void g(); inline void f();",
  156. functionDecl(isInline(), hasName("f"))));
  157. EXPECT_TRUE(matches("namespace n { inline namespace m {} }",
  158. namespaceDecl(isInline(), hasName("m"))));
  159. }
  160. // FIXME: Figure out how to specify paths so the following tests pass on
  161. // Windows.
  162. #ifndef _WIN32
  163. TEST(Matcher, IsExpansionInMainFileMatcher) {
  164. EXPECT_TRUE(matches("class X {};",
  165. recordDecl(hasName("X"), isExpansionInMainFile())));
  166. EXPECT_TRUE(notMatches("", recordDecl(isExpansionInMainFile())));
  167. FileContentMappings M;
  168. M.push_back(std::make_pair("/other", "class X {};"));
  169. EXPECT_TRUE(matchesConditionally("#include <other>\n",
  170. recordDecl(isExpansionInMainFile()), false,
  171. "-isystem/", M));
  172. }
  173. TEST(Matcher, IsExpansionInSystemHeader) {
  174. FileContentMappings M;
  175. M.push_back(std::make_pair("/other", "class X {};"));
  176. EXPECT_TRUE(matchesConditionally(
  177. "#include \"other\"\n", recordDecl(isExpansionInSystemHeader()), true,
  178. "-isystem/", M));
  179. EXPECT_TRUE(matchesConditionally("#include \"other\"\n",
  180. recordDecl(isExpansionInSystemHeader()),
  181. false, "-I/", M));
  182. EXPECT_TRUE(notMatches("class X {};",
  183. recordDecl(isExpansionInSystemHeader())));
  184. EXPECT_TRUE(notMatches("", recordDecl(isExpansionInSystemHeader())));
  185. }
  186. TEST(Matcher, IsExpansionInFileMatching) {
  187. FileContentMappings M;
  188. M.push_back(std::make_pair("/foo", "class A {};"));
  189. M.push_back(std::make_pair("/bar", "class B {};"));
  190. EXPECT_TRUE(matchesConditionally(
  191. "#include <foo>\n"
  192. "#include <bar>\n"
  193. "class X {};",
  194. recordDecl(isExpansionInFileMatching("b.*"), hasName("B")), true,
  195. "-isystem/", M));
  196. EXPECT_TRUE(matchesConditionally(
  197. "#include <foo>\n"
  198. "#include <bar>\n"
  199. "class X {};",
  200. recordDecl(isExpansionInFileMatching("f.*"), hasName("X")), false,
  201. "-isystem/", M));
  202. }
  203. #endif // _WIN32
  204. } // end namespace ast_matchers
  205. } // end namespace clang