xref: /aosp_15_r20/external/clang/unittests/ASTMatchers/ASTMatchersTest.h (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===- unittest/Tooling/ASTMatchersTest.h - Matcher tests helpers ------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li 
10*67e74705SXin Li #ifndef LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
11*67e74705SXin Li #define LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
12*67e74705SXin Li 
13*67e74705SXin Li #include "clang/ASTMatchers/ASTMatchFinder.h"
14*67e74705SXin Li #include "clang/Frontend/ASTUnit.h"
15*67e74705SXin Li #include "clang/Tooling/Tooling.h"
16*67e74705SXin Li #include "gtest/gtest.h"
17*67e74705SXin Li 
18*67e74705SXin Li namespace clang {
19*67e74705SXin Li namespace ast_matchers {
20*67e74705SXin Li 
21*67e74705SXin Li using clang::tooling::buildASTFromCodeWithArgs;
22*67e74705SXin Li using clang::tooling::newFrontendActionFactory;
23*67e74705SXin Li using clang::tooling::runToolOnCodeWithArgs;
24*67e74705SXin Li using clang::tooling::FrontendActionFactory;
25*67e74705SXin Li using clang::tooling::FileContentMappings;
26*67e74705SXin Li 
27*67e74705SXin Li class BoundNodesCallback {
28*67e74705SXin Li public:
~BoundNodesCallback()29*67e74705SXin Li   virtual ~BoundNodesCallback() {}
30*67e74705SXin Li   virtual bool run(const BoundNodes *BoundNodes) = 0;
31*67e74705SXin Li   virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
onEndOfTranslationUnit()32*67e74705SXin Li   virtual void onEndOfTranslationUnit() {}
33*67e74705SXin Li };
34*67e74705SXin Li 
35*67e74705SXin Li // If 'FindResultVerifier' is not NULL, sets *Verified to the result of
36*67e74705SXin Li // running 'FindResultVerifier' with the bound nodes as argument.
37*67e74705SXin Li // If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
38*67e74705SXin Li class VerifyMatch : public MatchFinder::MatchCallback {
39*67e74705SXin Li public:
VerifyMatch(std::unique_ptr<BoundNodesCallback> FindResultVerifier,bool * Verified)40*67e74705SXin Li   VerifyMatch(std::unique_ptr<BoundNodesCallback> FindResultVerifier, bool *Verified)
41*67e74705SXin Li       : Verified(Verified), FindResultReviewer(std::move(FindResultVerifier)) {}
42*67e74705SXin Li 
run(const MatchFinder::MatchResult & Result)43*67e74705SXin Li   void run(const MatchFinder::MatchResult &Result) override {
44*67e74705SXin Li     if (FindResultReviewer != nullptr) {
45*67e74705SXin Li       *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
46*67e74705SXin Li     } else {
47*67e74705SXin Li       *Verified = true;
48*67e74705SXin Li     }
49*67e74705SXin Li   }
50*67e74705SXin Li 
onEndOfTranslationUnit()51*67e74705SXin Li   void onEndOfTranslationUnit() override {
52*67e74705SXin Li     if (FindResultReviewer)
53*67e74705SXin Li       FindResultReviewer->onEndOfTranslationUnit();
54*67e74705SXin Li   }
55*67e74705SXin Li 
56*67e74705SXin Li private:
57*67e74705SXin Li   bool *const Verified;
58*67e74705SXin Li   const std::unique_ptr<BoundNodesCallback> FindResultReviewer;
59*67e74705SXin Li };
60*67e74705SXin Li 
61*67e74705SXin Li template <typename T>
62*67e74705SXin Li testing::AssertionResult matchesConditionally(
63*67e74705SXin Li     const std::string &Code, const T &AMatcher, bool ExpectMatch,
64*67e74705SXin Li     llvm::StringRef CompileArg,
65*67e74705SXin Li     const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
66*67e74705SXin Li     const std::string &Filename = "input.cc") {
67*67e74705SXin Li   bool Found = false, DynamicFound = false;
68*67e74705SXin Li   MatchFinder Finder;
69*67e74705SXin Li   VerifyMatch VerifyFound(nullptr, &Found);
70*67e74705SXin Li   Finder.addMatcher(AMatcher, &VerifyFound);
71*67e74705SXin Li   VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
72*67e74705SXin Li   if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
73*67e74705SXin Li     return testing::AssertionFailure() << "Could not add dynamic matcher";
74*67e74705SXin Li   std::unique_ptr<FrontendActionFactory> Factory(
75*67e74705SXin Li       newFrontendActionFactory(&Finder));
76*67e74705SXin Li   // Some tests need rtti/exceptions on.  Use an unknown-unknown triple so we
77*67e74705SXin Li   // don't instantiate the full system toolchain.  On Linux, instantiating the
78*67e74705SXin Li   // toolchain involves stat'ing large portions of /usr/lib, and this slows down
79*67e74705SXin Li   // not only this test, but all other tests, via contention in the kernel.
80*67e74705SXin Li   //
81*67e74705SXin Li   // FIXME: This is a hack to work around the fact that there's no way to do the
82*67e74705SXin Li   // equivalent of runToolOnCodeWithArgs without instantiating a full Driver.
83*67e74705SXin Li   // We should consider having a function, at least for tests, that invokes cc1.
84*67e74705SXin Li   std::vector<std::string> Args = {CompileArg, "-frtti", "-fexceptions",
85*67e74705SXin Li                                    "-target", "i386-unknown-unknown"};
86*67e74705SXin Li   if (!runToolOnCodeWithArgs(
87*67e74705SXin Li           Factory->create(), Code, Args, Filename, "clang-tool",
88*67e74705SXin Li           std::make_shared<PCHContainerOperations>(), VirtualMappedFiles)) {
89*67e74705SXin Li     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
90*67e74705SXin Li   }
91*67e74705SXin Li   if (Found != DynamicFound) {
92*67e74705SXin Li     return testing::AssertionFailure() << "Dynamic match result ("
93*67e74705SXin Li                                        << DynamicFound
94*67e74705SXin Li                                        << ") does not match static result ("
95*67e74705SXin Li                                        << Found << ")";
96*67e74705SXin Li   }
97*67e74705SXin Li   if (!Found && ExpectMatch) {
98*67e74705SXin Li     return testing::AssertionFailure()
99*67e74705SXin Li       << "Could not find match in \"" << Code << "\"";
100*67e74705SXin Li   } else if (Found && !ExpectMatch) {
101*67e74705SXin Li     return testing::AssertionFailure()
102*67e74705SXin Li       << "Found unexpected match in \"" << Code << "\"";
103*67e74705SXin Li   }
104*67e74705SXin Li   return testing::AssertionSuccess();
105*67e74705SXin Li }
106*67e74705SXin Li 
107*67e74705SXin Li template <typename T>
matches(const std::string & Code,const T & AMatcher)108*67e74705SXin Li testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
109*67e74705SXin Li   return matchesConditionally(Code, AMatcher, true, "-std=c++11");
110*67e74705SXin Li }
111*67e74705SXin Li 
112*67e74705SXin Li template <typename T>
notMatches(const std::string & Code,const T & AMatcher)113*67e74705SXin Li testing::AssertionResult notMatches(const std::string &Code,
114*67e74705SXin Li                                     const T &AMatcher) {
115*67e74705SXin Li   return matchesConditionally(Code, AMatcher, false, "-std=c++11");
116*67e74705SXin Li }
117*67e74705SXin Li 
118*67e74705SXin Li template <typename T>
matchesObjC(const std::string & Code,const T & AMatcher)119*67e74705SXin Li testing::AssertionResult matchesObjC(const std::string &Code,
120*67e74705SXin Li                                      const T &AMatcher) {
121*67e74705SXin Li   return matchesConditionally(
122*67e74705SXin Li     Code, AMatcher, true,
123*67e74705SXin Li     "", FileContentMappings(), "input.m");
124*67e74705SXin Li }
125*67e74705SXin Li 
126*67e74705SXin Li template <typename T>
matchesC(const std::string & Code,const T & AMatcher)127*67e74705SXin Li testing::AssertionResult matchesC(const std::string &Code, const T &AMatcher) {
128*67e74705SXin Li   return matchesConditionally(Code, AMatcher, true, "", FileContentMappings(),
129*67e74705SXin Li                               "input.c");
130*67e74705SXin Li }
131*67e74705SXin Li 
132*67e74705SXin Li template <typename T>
matchesC99(const std::string & Code,const T & AMatcher)133*67e74705SXin Li testing::AssertionResult matchesC99(const std::string &Code,
134*67e74705SXin Li                                     const T &AMatcher) {
135*67e74705SXin Li   return matchesConditionally(Code, AMatcher, true, "-std=c99",
136*67e74705SXin Li                               FileContentMappings(), "input.c");
137*67e74705SXin Li }
138*67e74705SXin Li 
139*67e74705SXin Li template <typename T>
notMatchesC(const std::string & Code,const T & AMatcher)140*67e74705SXin Li testing::AssertionResult notMatchesC(const std::string &Code,
141*67e74705SXin Li                                      const T &AMatcher) {
142*67e74705SXin Li   return matchesConditionally(Code, AMatcher, false, "", FileContentMappings(),
143*67e74705SXin Li                               "input.c");
144*67e74705SXin Li }
145*67e74705SXin Li 
146*67e74705SXin Li template <typename T>
notMatchesObjC(const std::string & Code,const T & AMatcher)147*67e74705SXin Li testing::AssertionResult notMatchesObjC(const std::string &Code,
148*67e74705SXin Li                                      const T &AMatcher) {
149*67e74705SXin Li   return matchesConditionally(
150*67e74705SXin Li     Code, AMatcher, false,
151*67e74705SXin Li     "", FileContentMappings(), "input.m");
152*67e74705SXin Li }
153*67e74705SXin Li 
154*67e74705SXin Li 
155*67e74705SXin Li // Function based on matchesConditionally with "-x cuda" argument added and
156*67e74705SXin Li // small CUDA header prepended to the code string.
157*67e74705SXin Li template <typename T>
matchesConditionallyWithCuda(const std::string & Code,const T & AMatcher,bool ExpectMatch,llvm::StringRef CompileArg)158*67e74705SXin Li testing::AssertionResult matchesConditionallyWithCuda(
159*67e74705SXin Li     const std::string &Code, const T &AMatcher, bool ExpectMatch,
160*67e74705SXin Li     llvm::StringRef CompileArg) {
161*67e74705SXin Li   const std::string CudaHeader =
162*67e74705SXin Li       "typedef unsigned int size_t;\n"
163*67e74705SXin Li       "#define __constant__ __attribute__((constant))\n"
164*67e74705SXin Li       "#define __device__ __attribute__((device))\n"
165*67e74705SXin Li       "#define __global__ __attribute__((global))\n"
166*67e74705SXin Li       "#define __host__ __attribute__((host))\n"
167*67e74705SXin Li       "#define __shared__ __attribute__((shared))\n"
168*67e74705SXin Li       "struct dim3 {"
169*67e74705SXin Li       "  unsigned x, y, z;"
170*67e74705SXin Li       "  __host__ __device__ dim3(unsigned x, unsigned y = 1, unsigned z = 1)"
171*67e74705SXin Li       "      : x(x), y(y), z(z) {}"
172*67e74705SXin Li       "};"
173*67e74705SXin Li       "typedef struct cudaStream *cudaStream_t;"
174*67e74705SXin Li       "int cudaConfigureCall(dim3 gridSize, dim3 blockSize,"
175*67e74705SXin Li       "                      size_t sharedSize = 0,"
176*67e74705SXin Li       "                      cudaStream_t stream = 0);";
177*67e74705SXin Li 
178*67e74705SXin Li   bool Found = false, DynamicFound = false;
179*67e74705SXin Li   MatchFinder Finder;
180*67e74705SXin Li   VerifyMatch VerifyFound(nullptr, &Found);
181*67e74705SXin Li   Finder.addMatcher(AMatcher, &VerifyFound);
182*67e74705SXin Li   VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
183*67e74705SXin Li   if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
184*67e74705SXin Li     return testing::AssertionFailure() << "Could not add dynamic matcher";
185*67e74705SXin Li   std::unique_ptr<FrontendActionFactory> Factory(
186*67e74705SXin Li       newFrontendActionFactory(&Finder));
187*67e74705SXin Li   // Some tests use typeof, which is a gnu extension.  Using an explicit
188*67e74705SXin Li   // unknown-unknown triple is good for a large speedup, because it lets us
189*67e74705SXin Li   // avoid constructing a full system triple.
190*67e74705SXin Li   std::vector<std::string> Args = {
191*67e74705SXin Li       "-xcuda",  "-fno-ms-extensions",      "--cuda-host-only", "-nocudainc",
192*67e74705SXin Li       "-target", "nvptx64-unknown-unknown", CompileArg};
193*67e74705SXin Li   if (!runToolOnCodeWithArgs(Factory->create(),
194*67e74705SXin Li                              CudaHeader + Code, Args)) {
195*67e74705SXin Li     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
196*67e74705SXin Li   }
197*67e74705SXin Li   if (Found != DynamicFound) {
198*67e74705SXin Li     return testing::AssertionFailure() << "Dynamic match result ("
199*67e74705SXin Li                                        << DynamicFound
200*67e74705SXin Li                                        << ") does not match static result ("
201*67e74705SXin Li                                        << Found << ")";
202*67e74705SXin Li   }
203*67e74705SXin Li   if (!Found && ExpectMatch) {
204*67e74705SXin Li     return testing::AssertionFailure()
205*67e74705SXin Li       << "Could not find match in \"" << Code << "\"";
206*67e74705SXin Li   } else if (Found && !ExpectMatch) {
207*67e74705SXin Li     return testing::AssertionFailure()
208*67e74705SXin Li       << "Found unexpected match in \"" << Code << "\"";
209*67e74705SXin Li   }
210*67e74705SXin Li   return testing::AssertionSuccess();
211*67e74705SXin Li }
212*67e74705SXin Li 
213*67e74705SXin Li template <typename T>
matchesWithCuda(const std::string & Code,const T & AMatcher)214*67e74705SXin Li testing::AssertionResult matchesWithCuda(const std::string &Code,
215*67e74705SXin Li                                          const T &AMatcher) {
216*67e74705SXin Li   return matchesConditionallyWithCuda(Code, AMatcher, true, "-std=c++11");
217*67e74705SXin Li }
218*67e74705SXin Li 
219*67e74705SXin Li template <typename T>
notMatchesWithCuda(const std::string & Code,const T & AMatcher)220*67e74705SXin Li testing::AssertionResult notMatchesWithCuda(const std::string &Code,
221*67e74705SXin Li                                     const T &AMatcher) {
222*67e74705SXin Li   return matchesConditionallyWithCuda(Code, AMatcher, false, "-std=c++11");
223*67e74705SXin Li }
224*67e74705SXin Li 
225*67e74705SXin Li template <typename T>
226*67e74705SXin Li testing::AssertionResult
matchAndVerifyResultConditionally(const std::string & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier,bool ExpectResult)227*67e74705SXin Li matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
228*67e74705SXin Li                                   std::unique_ptr<BoundNodesCallback> FindResultVerifier,
229*67e74705SXin Li                                   bool ExpectResult) {
230*67e74705SXin Li   bool VerifiedResult = false;
231*67e74705SXin Li   MatchFinder Finder;
232*67e74705SXin Li   VerifyMatch VerifyVerifiedResult(std::move(FindResultVerifier), &VerifiedResult);
233*67e74705SXin Li   Finder.addMatcher(AMatcher, &VerifyVerifiedResult);
234*67e74705SXin Li   std::unique_ptr<FrontendActionFactory> Factory(
235*67e74705SXin Li       newFrontendActionFactory(&Finder));
236*67e74705SXin Li   // Some tests use typeof, which is a gnu extension.  Using an explicit
237*67e74705SXin Li   // unknown-unknown triple is good for a large speedup, because it lets us
238*67e74705SXin Li   // avoid constructing a full system triple.
239*67e74705SXin Li   std::vector<std::string> Args = {"-std=gnu++98", "-target",
240*67e74705SXin Li                                    "i386-unknown-unknown"};
241*67e74705SXin Li   if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
242*67e74705SXin Li     return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
243*67e74705SXin Li   }
244*67e74705SXin Li   if (!VerifiedResult && ExpectResult) {
245*67e74705SXin Li     return testing::AssertionFailure()
246*67e74705SXin Li       << "Could not verify result in \"" << Code << "\"";
247*67e74705SXin Li   } else if (VerifiedResult && !ExpectResult) {
248*67e74705SXin Li     return testing::AssertionFailure()
249*67e74705SXin Li       << "Verified unexpected result in \"" << Code << "\"";
250*67e74705SXin Li   }
251*67e74705SXin Li 
252*67e74705SXin Li   VerifiedResult = false;
253*67e74705SXin Li   std::unique_ptr<ASTUnit> AST(buildASTFromCodeWithArgs(Code, Args));
254*67e74705SXin Li   if (!AST.get())
255*67e74705SXin Li     return testing::AssertionFailure() << "Parsing error in \"" << Code
256*67e74705SXin Li                                        << "\" while building AST";
257*67e74705SXin Li   Finder.matchAST(AST->getASTContext());
258*67e74705SXin Li   if (!VerifiedResult && ExpectResult) {
259*67e74705SXin Li     return testing::AssertionFailure()
260*67e74705SXin Li       << "Could not verify result in \"" << Code << "\" with AST";
261*67e74705SXin Li   } else if (VerifiedResult && !ExpectResult) {
262*67e74705SXin Li     return testing::AssertionFailure()
263*67e74705SXin Li       << "Verified unexpected result in \"" << Code << "\" with AST";
264*67e74705SXin Li   }
265*67e74705SXin Li 
266*67e74705SXin Li   return testing::AssertionSuccess();
267*67e74705SXin Li }
268*67e74705SXin Li 
269*67e74705SXin Li // FIXME: Find better names for these functions (or document what they
270*67e74705SXin Li // do more precisely).
271*67e74705SXin Li template <typename T>
272*67e74705SXin Li testing::AssertionResult
matchAndVerifyResultTrue(const std::string & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier)273*67e74705SXin Li matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
274*67e74705SXin Li                          std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
275*67e74705SXin Li   return matchAndVerifyResultConditionally(
276*67e74705SXin Li       Code, AMatcher, std::move(FindResultVerifier), true);
277*67e74705SXin Li }
278*67e74705SXin Li 
279*67e74705SXin Li template <typename T>
280*67e74705SXin Li testing::AssertionResult
matchAndVerifyResultFalse(const std::string & Code,const T & AMatcher,std::unique_ptr<BoundNodesCallback> FindResultVerifier)281*67e74705SXin Li matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
282*67e74705SXin Li                           std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
283*67e74705SXin Li   return matchAndVerifyResultConditionally(
284*67e74705SXin Li       Code, AMatcher, std::move(FindResultVerifier), false);
285*67e74705SXin Li }
286*67e74705SXin Li 
287*67e74705SXin Li // Implements a run method that returns whether BoundNodes contains a
288*67e74705SXin Li // Decl bound to Id that can be dynamically cast to T.
289*67e74705SXin Li // Optionally checks that the check succeeded a specific number of times.
290*67e74705SXin Li template <typename T>
291*67e74705SXin Li class VerifyIdIsBoundTo : public BoundNodesCallback {
292*67e74705SXin Li public:
293*67e74705SXin Li   // Create an object that checks that a node of type \c T was bound to \c Id.
294*67e74705SXin Li   // Does not check for a certain number of matches.
VerifyIdIsBoundTo(llvm::StringRef Id)295*67e74705SXin Li   explicit VerifyIdIsBoundTo(llvm::StringRef Id)
296*67e74705SXin Li     : Id(Id), ExpectedCount(-1), Count(0) {}
297*67e74705SXin Li 
298*67e74705SXin Li   // Create an object that checks that a node of type \c T was bound to \c Id.
299*67e74705SXin Li   // Checks that there were exactly \c ExpectedCount matches.
VerifyIdIsBoundTo(llvm::StringRef Id,int ExpectedCount)300*67e74705SXin Li   VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
301*67e74705SXin Li     : Id(Id), ExpectedCount(ExpectedCount), Count(0) {}
302*67e74705SXin Li 
303*67e74705SXin Li   // Create an object that checks that a node of type \c T was bound to \c Id.
304*67e74705SXin Li   // Checks that there was exactly one match with the name \c ExpectedName.
305*67e74705SXin Li   // Note that \c T must be a NamedDecl for this to work.
306*67e74705SXin Li   VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
307*67e74705SXin Li                     int ExpectedCount = 1)
Id(Id)308*67e74705SXin Li     : Id(Id), ExpectedCount(ExpectedCount), Count(0),
309*67e74705SXin Li       ExpectedName(ExpectedName) {}
310*67e74705SXin Li 
onEndOfTranslationUnit()311*67e74705SXin Li   void onEndOfTranslationUnit() override {
312*67e74705SXin Li     if (ExpectedCount != -1)
313*67e74705SXin Li       EXPECT_EQ(ExpectedCount, Count);
314*67e74705SXin Li     if (!ExpectedName.empty())
315*67e74705SXin Li       EXPECT_EQ(ExpectedName, Name);
316*67e74705SXin Li     Count = 0;
317*67e74705SXin Li     Name.clear();
318*67e74705SXin Li   }
319*67e74705SXin Li 
~VerifyIdIsBoundTo()320*67e74705SXin Li   ~VerifyIdIsBoundTo() override {
321*67e74705SXin Li     EXPECT_EQ(0, Count);
322*67e74705SXin Li     EXPECT_EQ("", Name);
323*67e74705SXin Li   }
324*67e74705SXin Li 
run(const BoundNodes * Nodes)325*67e74705SXin Li   bool run(const BoundNodes *Nodes) override {
326*67e74705SXin Li     const BoundNodes::IDToNodeMap &M = Nodes->getMap();
327*67e74705SXin Li     if (Nodes->getNodeAs<T>(Id)) {
328*67e74705SXin Li       ++Count;
329*67e74705SXin Li       if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
330*67e74705SXin Li         Name = Named->getNameAsString();
331*67e74705SXin Li       } else if (const NestedNameSpecifier *NNS =
332*67e74705SXin Li         Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
333*67e74705SXin Li         llvm::raw_string_ostream OS(Name);
334*67e74705SXin Li         NNS->print(OS, PrintingPolicy(LangOptions()));
335*67e74705SXin Li       }
336*67e74705SXin Li       BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
337*67e74705SXin Li       EXPECT_NE(M.end(), I);
338*67e74705SXin Li       if (I != M.end())
339*67e74705SXin Li         EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
340*67e74705SXin Li       return true;
341*67e74705SXin Li     }
342*67e74705SXin Li     EXPECT_TRUE(M.count(Id) == 0 ||
343*67e74705SXin Li       M.find(Id)->second.template get<T>() == nullptr);
344*67e74705SXin Li     return false;
345*67e74705SXin Li   }
346*67e74705SXin Li 
run(const BoundNodes * Nodes,ASTContext * Context)347*67e74705SXin Li   bool run(const BoundNodes *Nodes, ASTContext *Context) override {
348*67e74705SXin Li     return run(Nodes);
349*67e74705SXin Li   }
350*67e74705SXin Li 
351*67e74705SXin Li private:
352*67e74705SXin Li   const std::string Id;
353*67e74705SXin Li   const int ExpectedCount;
354*67e74705SXin Li   int Count;
355*67e74705SXin Li   const std::string ExpectedName;
356*67e74705SXin Li   std::string Name;
357*67e74705SXin Li };
358*67e74705SXin Li 
359*67e74705SXin Li } // namespace ast_matchers
360*67e74705SXin Li } // namespace clang
361*67e74705SXin Li 
362*67e74705SXin Li #endif  // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
363