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() {} |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame^] | 27 | virtual bool run(const BoundNodes *BoundNodes) = 0; |
| 28 | virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 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) { |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 41 | *Verified = FindResultReviewer->run(&Result.Nodes, Result.Context); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 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, |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 55 | bool ExpectMatch, |
| 56 | llvm::StringRef CompileArg) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 57 | bool Found = false; |
| 58 | MatchFinder Finder; |
| 59 | Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found)); |
| 60 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 61 | // Some tests use typeof, which is a gnu extension. |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 62 | std::vector<std::string> Args(1, CompileArg); |
Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 63 | if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 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) { |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 78 | return matchesConditionally(Code, AMatcher, true, "-std=c++11"); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template <typename T> |
| 82 | testing::AssertionResult notMatches(const std::string &Code, |
| 83 | const T &AMatcher) { |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 84 | return matchesConditionally(Code, AMatcher, false, "-std=c++11"); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 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 | llvm::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)); |
Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 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)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 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 |