blob: f5bcd37f112dc0b23a048d335d84ce581e4c4c66 [file] [log] [blame]
Manuel Klimek4da21662012-07-06 05:48:52 +00001//===- 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 Collingbourne51fcdf82013-11-07 22:30:36 +000014#include "clang/Frontend/ASTUnit.h"
Manuel Klimek4da21662012-07-06 05:48:52 +000015#include "clang/Tooling/Tooling.h"
16#include "gtest/gtest.h"
17
18namespace clang {
19namespace ast_matchers {
20
Peter Collingbourne51fcdf82013-11-07 22:30:36 +000021using clang::tooling::buildASTFromCodeWithArgs;
Manuel Klimek4da21662012-07-06 05:48:52 +000022using clang::tooling::newFrontendActionFactory;
Nico Weber50f88b92012-08-30 02:08:31 +000023using clang::tooling::runToolOnCodeWithArgs;
Manuel Klimek4da21662012-07-06 05:48:52 +000024using clang::tooling::FrontendActionFactory;
25
26class BoundNodesCallback {
27public:
28 virtual ~BoundNodesCallback() {}
Daniel Jasper452abbc2012-10-29 10:48:25 +000029 virtual bool run(const BoundNodes *BoundNodes) = 0;
30 virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
Peter Collingbourned2bd5892013-11-07 22:30:32 +000031 virtual void onEndOfTranslationUnit() {}
Manuel Klimek4da21662012-07-06 05:48:52 +000032};
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.
37class VerifyMatch : public MatchFinder::MatchCallback {
38public:
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 Jasper11c98772012-11-11 22:14:55 +000044 *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
Manuel Klimek4da21662012-07-06 05:48:52 +000045 } else {
46 *Verified = true;
47 }
48 }
49
Peter Collingbourned2bd5892013-11-07 22:30:32 +000050 void onEndOfTranslationUnit() LLVM_OVERRIDE {
51 if (FindResultReviewer)
52 FindResultReviewer->onEndOfTranslationUnit();
53 }
54
Manuel Klimek4da21662012-07-06 05:48:52 +000055private:
56 bool *const Verified;
57 BoundNodesCallback *const FindResultReviewer;
58};
59
60template <typename T>
61testing::AssertionResult matchesConditionally(const std::string &Code,
62 const T &AMatcher,
Daniel Jasper31f7c082012-10-01 13:40:41 +000063 bool ExpectMatch,
64 llvm::StringRef CompileArg) {
Peter Collingbourned2bd5892013-11-07 22:30:32 +000065 bool Found = false, DynamicFound = false;
Manuel Klimek4da21662012-07-06 05:48:52 +000066 MatchFinder Finder;
67 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
Peter Collingbourned2bd5892013-11-07 22:30:32 +000068 if (!Finder.addDynamicMatcher(AMatcher, new VerifyMatch(0, &DynamicFound)))
69 return testing::AssertionFailure() << "Could not add dynamic matcher";
Manuel Klimek4da21662012-07-06 05:48:52 +000070 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000071 // Some tests use typeof, which is a gnu extension.
Daniel Jasper31f7c082012-10-01 13:40:41 +000072 std::vector<std::string> Args(1, CompileArg);
Nico Weber50f88b92012-08-30 02:08:31 +000073 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000074 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
75 }
Peter Collingbourned2bd5892013-11-07 22:30:32 +000076 if (Found != DynamicFound) {
77 return testing::AssertionFailure() << "Dynamic match result ("
78 << DynamicFound
79 << ") does not match static result ("
80 << Found << ")";
81 }
Manuel Klimek4da21662012-07-06 05:48:52 +000082 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
92template <typename T>
93testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000094 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000095}
96
97template <typename T>
98testing::AssertionResult notMatches(const std::string &Code,
99 const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +0000100 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +0000101}
102
103template <typename T>
104testing::AssertionResult
105matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
106 BoundNodesCallback *FindResultVerifier,
107 bool ExpectResult) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000108 OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
Manuel Klimek4da21662012-07-06 05:48:52 +0000109 bool VerifiedResult = false;
110 MatchFinder Finder;
111 Finder.addMatcher(
112 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
113 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +0000114 // 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 Klimek4da21662012-07-06 05:48:52 +0000117 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 Collingbourne51fcdf82013-11-07 22:30:36 +0000126
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 Klimek4da21662012-07-06 05:48:52 +0000141 return testing::AssertionSuccess();
142}
143
144// FIXME: Find better names for these functions (or document what they
145// do more precisely).
146template <typename T>
147testing::AssertionResult
148matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
149 BoundNodesCallback *FindResultVerifier) {
150 return matchAndVerifyResultConditionally(
151 Code, AMatcher, FindResultVerifier, true);
152}
153
154template <typename T>
155testing::AssertionResult
156matchAndVerifyResultFalse(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