| 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" |
| Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame^] | 14 | #include "clang/Frontend/ASTUnit.h" |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 15 | #include "clang/Tooling/Tooling.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace ast_matchers { |
| 20 | |
| Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame^] | 21 | using clang::tooling::buildASTFromCodeWithArgs; |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 22 | using clang::tooling::newFrontendActionFactory; |
| Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 23 | using clang::tooling::runToolOnCodeWithArgs; |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 24 | using clang::tooling::FrontendActionFactory; |
| 25 | |
| 26 | class BoundNodesCallback { |
| 27 | public: |
| 28 | virtual ~BoundNodesCallback() {} |
| Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 29 | virtual bool run(const BoundNodes *BoundNodes) = 0; |
| 30 | virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0; |
| Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 31 | virtual void onEndOfTranslationUnit() {} |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | // If 'FindResultVerifier' is not NULL, sets *Verified to the result of |
| 35 | // running 'FindResultVerifier' with the bound nodes as argument. |
| 36 | // If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called. |
| 37 | class VerifyMatch : public MatchFinder::MatchCallback { |
| 38 | public: |
| 39 | VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified) |
| 40 | : Verified(Verified), FindResultReviewer(FindResultVerifier) {} |
| 41 | |
| 42 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 43 | if (FindResultReviewer != NULL) { |
| Daniel Jasper | 11c9877 | 2012-11-11 22:14:55 +0000 | [diff] [blame] | 44 | *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context); |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 45 | } else { |
| 46 | *Verified = true; |
| 47 | } |
| 48 | } |
| 49 | |
| Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 50 | void onEndOfTranslationUnit() LLVM_OVERRIDE { |
| 51 | if (FindResultReviewer) |
| 52 | FindResultReviewer->onEndOfTranslationUnit(); |
| 53 | } |
| 54 | |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 55 | private: |
| 56 | bool *const Verified; |
| 57 | BoundNodesCallback *const FindResultReviewer; |
| 58 | }; |
| 59 | |
| 60 | template <typename T> |
| 61 | testing::AssertionResult matchesConditionally(const std::string &Code, |
| 62 | const T &AMatcher, |
| Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 63 | bool ExpectMatch, |
| 64 | llvm::StringRef CompileArg) { |
| Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 65 | bool Found = false, DynamicFound = false; |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 66 | MatchFinder Finder; |
| 67 | Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found)); |
| Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 68 | if (!Finder.addDynamicMatcher(AMatcher, new VerifyMatch(0, &DynamicFound))) |
| 69 | return testing::AssertionFailure() << "Could not add dynamic matcher"; |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 70 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 71 | // Some tests use typeof, which is a gnu extension. |
| Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 72 | std::vector<std::string> Args(1, CompileArg); |
| Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 73 | if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) { |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 74 | return testing::AssertionFailure() << "Parsing error in \"" << Code << "\""; |
| 75 | } |
| Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 76 | if (Found != DynamicFound) { |
| 77 | return testing::AssertionFailure() << "Dynamic match result (" |
| 78 | << DynamicFound |
| 79 | << ") does not match static result (" |
| 80 | << Found << ")"; |
| 81 | } |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 82 | if (!Found && ExpectMatch) { |
| 83 | return testing::AssertionFailure() |
| 84 | << "Could not find match in \"" << Code << "\""; |
| 85 | } else if (Found && !ExpectMatch) { |
| 86 | return testing::AssertionFailure() |
| 87 | << "Found unexpected match in \"" << Code << "\""; |
| 88 | } |
| 89 | return testing::AssertionSuccess(); |
| 90 | } |
| 91 | |
| 92 | template <typename T> |
| 93 | testing::AssertionResult matches(const std::string &Code, const T &AMatcher) { |
| Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 94 | return matchesConditionally(Code, AMatcher, true, "-std=c++11"); |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | testing::AssertionResult notMatches(const std::string &Code, |
| 99 | const T &AMatcher) { |
| Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 100 | return matchesConditionally(Code, AMatcher, false, "-std=c++11"); |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | template <typename T> |
| 104 | testing::AssertionResult |
| 105 | matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher, |
| 106 | BoundNodesCallback *FindResultVerifier, |
| 107 | bool ExpectResult) { |
| Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 108 | OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier); |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 109 | bool VerifiedResult = false; |
| 110 | MatchFinder Finder; |
| 111 | Finder.addMatcher( |
| 112 | AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult)); |
| 113 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| Nico Weber | 50f88b9 | 2012-08-30 02:08:31 +0000 | [diff] [blame] | 114 | // Some tests use typeof, which is a gnu extension. |
| 115 | std::vector<std::string> Args(1, "-std=gnu++98"); |
| 116 | if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) { |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 117 | return testing::AssertionFailure() << "Parsing error in \"" << Code << "\""; |
| 118 | } |
| 119 | if (!VerifiedResult && ExpectResult) { |
| 120 | return testing::AssertionFailure() |
| 121 | << "Could not verify result in \"" << Code << "\""; |
| 122 | } else if (VerifiedResult && !ExpectResult) { |
| 123 | return testing::AssertionFailure() |
| 124 | << "Verified unexpected result in \"" << Code << "\""; |
| 125 | } |
| Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame^] | 126 | |
| 127 | VerifiedResult = false; |
| 128 | OwningPtr<ASTUnit> AST(buildASTFromCodeWithArgs(Code, Args)); |
| 129 | if (!AST.get()) |
| 130 | return testing::AssertionFailure() << "Parsing error in \"" << Code |
| 131 | << "\" while building AST"; |
| 132 | Finder.matchAST(AST->getASTContext()); |
| 133 | if (!VerifiedResult && ExpectResult) { |
| 134 | return testing::AssertionFailure() |
| 135 | << "Could not verify result in \"" << Code << "\" with AST"; |
| 136 | } else if (VerifiedResult && !ExpectResult) { |
| 137 | return testing::AssertionFailure() |
| 138 | << "Verified unexpected result in \"" << Code << "\" with AST"; |
| 139 | } |
| 140 | |
| Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 141 | return testing::AssertionSuccess(); |
| 142 | } |
| 143 | |
| 144 | // FIXME: Find better names for these functions (or document what they |
| 145 | // do more precisely). |
| 146 | template <typename T> |
| 147 | testing::AssertionResult |
| 148 | matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher, |
| 149 | BoundNodesCallback *FindResultVerifier) { |
| 150 | return matchAndVerifyResultConditionally( |
| 151 | Code, AMatcher, FindResultVerifier, true); |
| 152 | } |
| 153 | |
| 154 | template <typename T> |
| 155 | testing::AssertionResult |
| 156 | matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher, |
| 157 | BoundNodesCallback *FindResultVerifier) { |
| 158 | return matchAndVerifyResultConditionally( |
| 159 | Code, AMatcher, FindResultVerifier, false); |
| 160 | } |
| 161 | |
| 162 | } // end namespace ast_matchers |
| 163 | } // end namespace clang |
| 164 | |
| 165 | #endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H |