• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
21 using clang::tooling::runToolOnCodeWithArgs;
22 using clang::tooling::FrontendActionFactory;
23 
24 class BoundNodesCallback {
25 public:
~BoundNodesCallback()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:
VerifyMatch(BoundNodesCallback * FindResultVerifier,bool * Verified)35   VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
36       : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
37 
run(const MatchFinder::MatchResult & Result)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>
matchesConditionally(const std::string & Code,const T & AMatcher,bool ExpectMatch)52 testing::AssertionResult matchesConditionally(const std::string &Code,
53                                               const T &AMatcher,
54                                               bool ExpectMatch) {
55   bool Found = false;
56   MatchFinder Finder;
57   Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
58   OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
59   // Some tests use typeof, which is a gnu extension.
60   std::vector<std::string> Args(1, "-std=gnu++98");
61   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
62     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
63   }
64   if (!Found && ExpectMatch) {
65     return testing::AssertionFailure()
66       << "Could not find match in \"" << Code << "\"";
67   } else if (Found && !ExpectMatch) {
68     return testing::AssertionFailure()
69       << "Found unexpected match in \"" << Code << "\"";
70   }
71   return testing::AssertionSuccess();
72 }
73 
74 template <typename T>
matches(const std::string & Code,const T & AMatcher)75 testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
76   return matchesConditionally(Code, AMatcher, true);
77 }
78 
79 template <typename T>
notMatches(const std::string & Code,const T & AMatcher)80 testing::AssertionResult notMatches(const std::string &Code,
81                                     const T &AMatcher) {
82   return matchesConditionally(Code, AMatcher, false);
83 }
84 
85 template <typename T>
86 testing::AssertionResult
matchAndVerifyResultConditionally(const std::string & Code,const T & AMatcher,BoundNodesCallback * FindResultVerifier,bool ExpectResult)87 matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
88                                   BoundNodesCallback *FindResultVerifier,
89                                   bool ExpectResult) {
90   llvm::OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
91   bool VerifiedResult = false;
92   MatchFinder Finder;
93   Finder.addMatcher(
94       AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
95   OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
96   // Some tests use typeof, which is a gnu extension.
97   std::vector<std::string> Args(1, "-std=gnu++98");
98   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
99     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
100   }
101   if (!VerifiedResult && ExpectResult) {
102     return testing::AssertionFailure()
103       << "Could not verify result in \"" << Code << "\"";
104   } else if (VerifiedResult && !ExpectResult) {
105     return testing::AssertionFailure()
106       << "Verified unexpected result in \"" << Code << "\"";
107   }
108   return testing::AssertionSuccess();
109 }
110 
111 // FIXME: Find better names for these functions (or document what they
112 // do more precisely).
113 template <typename T>
114 testing::AssertionResult
matchAndVerifyResultTrue(const std::string & Code,const T & AMatcher,BoundNodesCallback * FindResultVerifier)115 matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
116                          BoundNodesCallback *FindResultVerifier) {
117   return matchAndVerifyResultConditionally(
118       Code, AMatcher, FindResultVerifier, true);
119 }
120 
121 template <typename T>
122 testing::AssertionResult
matchAndVerifyResultFalse(const std::string & Code,const T & AMatcher,BoundNodesCallback * FindResultVerifier)123 matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
124                           BoundNodesCallback *FindResultVerifier) {
125   return matchAndVerifyResultConditionally(
126       Code, AMatcher, FindResultVerifier, false);
127 }
128 
129 } // end namespace ast_matchers
130 } // end namespace clang
131 
132 #endif  // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
133