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