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/Frontend/ASTUnit.h"
15 #include "clang/Tooling/Tooling.h"
16 #include "gtest/gtest.h"
17 
18 namespace clang {
19 namespace ast_matchers {
20 
21 using clang::tooling::buildASTFromCodeWithArgs;
22 using clang::tooling::newFrontendActionFactory;
23 using clang::tooling::runToolOnCodeWithArgs;
24 using clang::tooling::FrontendActionFactory;
25 
26 class BoundNodesCallback {
27 public:
28   virtual ~BoundNodesCallback() {}
29   virtual bool run(const BoundNodes *BoundNodes) = 0;
30   virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
31   virtual void onEndOfTranslationUnit() {}
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) {
44       *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
45     } else {
46       *Verified = true;
47     }
48   }
49 
50   void onEndOfTranslationUnit() LLVM_OVERRIDE {
51     if (FindResultReviewer)
52       FindResultReviewer->onEndOfTranslationUnit();
53   }
54 
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,
63                                               bool ExpectMatch,
64                                               llvm::StringRef CompileArg) {
65   bool Found = false, DynamicFound = false;
66   MatchFinder Finder;
67   Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
68   if (!Finder.addDynamicMatcher(AMatcher, new VerifyMatch(0, &DynamicFound)))
69     return testing::AssertionFailure() << "Could not add dynamic matcher";
70   OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
71   // Some tests use typeof, which is a gnu extension.
72   std::vector<std::string> Args(1, CompileArg);
73   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
74     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
75   }
76   if (Found != DynamicFound) {
77     return testing::AssertionFailure() << "Dynamic match result ("
78                                        << DynamicFound
79                                        << ") does not match static result ("
80                                        << Found << ")";
81   }
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) {
94   return matchesConditionally(Code, AMatcher, true, "-std=c++11");
95 }
96 
97 template <typename T>
98 testing::AssertionResult notMatches(const std::string &Code,
99                                     const T &AMatcher) {
100   return matchesConditionally(Code, AMatcher, false, "-std=c++11");
101 }
102 
103 template <typename T>
104 testing::AssertionResult
105 matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
106                                   BoundNodesCallback *FindResultVerifier,
107                                   bool ExpectResult) {
108   OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
109   bool VerifiedResult = false;
110   MatchFinder Finder;
111   Finder.addMatcher(
112       AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
113   OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
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)) {
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   }
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 
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
166