1 //===- unittest/Tooling/ASTMatchersTest.h - Matcher tests helpers ------===// 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 #ifndef LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H 11 #define LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H 12 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Tooling/Tooling.h" 15 #include "gtest/gtest.h" 16 17 namespace clang { 18 namespace ast_matchers { 19 20 using clang::tooling::newFrontendActionFactory; 21 using clang::tooling::runToolOnCodeWithArgs; 22 using clang::tooling::FrontendActionFactory; 23 24 class BoundNodesCallback { 25 public: 26 virtual ~BoundNodesCallback() {} 27 virtual bool run(const BoundNodes *BoundNodes) = 0; 28 virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0; 29 }; 30 31 // If 'FindResultVerifier' is not NULL, sets *Verified to the result of 32 // running 'FindResultVerifier' with the bound nodes as argument. 33 // If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called. 34 class VerifyMatch : public MatchFinder::MatchCallback { 35 public: 36 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified) 37 : Verified(Verified), FindResultReviewer(FindResultVerifier) {} 38 39 virtual void run(const MatchFinder::MatchResult &Result) { 40 if (FindResultReviewer != NULL) { 41 *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context); 42 } else { 43 *Verified = true; 44 } 45 } 46 47 private: 48 bool *const Verified; 49 BoundNodesCallback *const FindResultReviewer; 50 }; 51 52 template <typename T> 53 testing::AssertionResult matchesConditionally(const std::string &Code, 54 const T &AMatcher, 55 bool ExpectMatch, 56 llvm::StringRef CompileArg) { 57 bool Found = false; 58 MatchFinder Finder; 59 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found)); 60 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); 61 // Some tests use typeof, which is a gnu extension. 62 std::vector<std::string> Args(1, CompileArg); 63 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) { 64 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\""; 65 } 66 if (!Found && ExpectMatch) { 67 return testing::AssertionFailure() 68 << "Could not find match in \"" << Code << "\""; 69 } else if (Found && !ExpectMatch) { 70 return testing::AssertionFailure() 71 << "Found unexpected match in \"" << Code << "\""; 72 } 73 return testing::AssertionSuccess(); 74 } 75 76 template <typename T> 77 testing::AssertionResult matches(const std::string &Code, const T &AMatcher) { 78 return matchesConditionally(Code, AMatcher, true, "-std=c++11"); 79 } 80 81 template <typename T> 82 testing::AssertionResult notMatches(const std::string &Code, 83 const T &AMatcher) { 84 return matchesConditionally(Code, AMatcher, false, "-std=c++11"); 85 } 86 87 template <typename T> 88 testing::AssertionResult 89 matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher, 90 BoundNodesCallback *FindResultVerifier, 91 bool ExpectResult) { 92 OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier); 93 bool VerifiedResult = false; 94 MatchFinder Finder; 95 Finder.addMatcher( 96 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult)); 97 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); 98 // Some tests use typeof, which is a gnu extension. 99 std::vector<std::string> Args(1, "-std=gnu++98"); 100 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) { 101 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\""; 102 } 103 if (!VerifiedResult && ExpectResult) { 104 return testing::AssertionFailure() 105 << "Could not verify result in \"" << Code << "\""; 106 } else if (VerifiedResult && !ExpectResult) { 107 return testing::AssertionFailure() 108 << "Verified unexpected result in \"" << Code << "\""; 109 } 110 return testing::AssertionSuccess(); 111 } 112 113 // FIXME: Find better names for these functions (or document what they 114 // do more precisely). 115 template <typename T> 116 testing::AssertionResult 117 matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher, 118 BoundNodesCallback *FindResultVerifier) { 119 return matchAndVerifyResultConditionally( 120 Code, AMatcher, FindResultVerifier, true); 121 } 122 123 template <typename T> 124 testing::AssertionResult 125 matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher, 126 BoundNodesCallback *FindResultVerifier) { 127 return matchAndVerifyResultConditionally( 128 Code, AMatcher, FindResultVerifier, false); 129 } 130 131 } // end namespace ast_matchers 132 } // end namespace clang 133 134 #endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H 135