1 //===- unittest/Tooling/ASTMatchersTest.h - Matcher tests helpers ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
11 #define LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Tooling/Tooling.h"
15 #include "gtest/gtest.h"
18 namespace ast_matchers {
20 using clang::tooling::newFrontendActionFactory;
21 using clang::tooling::runToolOnCodeWithArgs;
22 using clang::tooling::FrontendActionFactory;
24 class BoundNodesCallback {
26 virtual ~BoundNodesCallback() {}
27 virtual bool run(const BoundNodes *BoundNodes) = 0;
28 virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
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 {
36 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
37 : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
39 virtual void run(const MatchFinder::MatchResult &Result) {
40 if (FindResultReviewer != NULL) {
41 *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
49 BoundNodesCallback *const FindResultReviewer;
53 testing::AssertionResult matchesConditionally(const std::string &Code,
56 llvm::StringRef CompileArg) {
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 << "\"";
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 << "\"";
73 return testing::AssertionSuccess();
77 testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
78 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
82 testing::AssertionResult notMatches(const std::string &Code,
84 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
88 testing::AssertionResult
89 matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
90 BoundNodesCallback *FindResultVerifier,
92 OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
93 bool VerifiedResult = false;
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 << "\"";
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 << "\"";
110 return testing::AssertionSuccess();
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);
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);
131 } // end namespace ast_matchers
132 } // end namespace clang
134 #endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H