blob: e65d8e792df596666b9f1843b9093db0f9501e0b [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"
14#include "clang/Tooling/Tooling.h"
15#include "gtest/gtest.h"
16
17namespace clang {
18namespace ast_matchers {
19
20using clang::tooling::newFrontendActionFactory;
Nico Weber50f88b92012-08-30 02:08:31 +000021using clang::tooling::runToolOnCodeWithArgs;
Manuel Klimek4da21662012-07-06 05:48:52 +000022using clang::tooling::FrontendActionFactory;
23
24class BoundNodesCallback {
25public:
26 virtual ~BoundNodesCallback() {}
Daniel Jasper452abbc2012-10-29 10:48:25 +000027 virtual bool run(const BoundNodes *BoundNodes) = 0;
28 virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
Peter Collingbourned2bd5892013-11-07 22:30:32 +000029 virtual void onEndOfTranslationUnit() {}
Manuel Klimek4da21662012-07-06 05:48:52 +000030};
31
32// If 'FindResultVerifier' is not NULL, sets *Verified to the result of
33// running 'FindResultVerifier' with the bound nodes as argument.
34// If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
35class VerifyMatch : public MatchFinder::MatchCallback {
36public:
37 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
38 : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
39
40 virtual void run(const MatchFinder::MatchResult &Result) {
41 if (FindResultReviewer != NULL) {
Daniel Jasper11c98772012-11-11 22:14:55 +000042 *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
Manuel Klimek4da21662012-07-06 05:48:52 +000043 } else {
44 *Verified = true;
45 }
46 }
47
Peter Collingbourned2bd5892013-11-07 22:30:32 +000048 void onEndOfTranslationUnit() LLVM_OVERRIDE {
49 if (FindResultReviewer)
50 FindResultReviewer->onEndOfTranslationUnit();
51 }
52
Manuel Klimek4da21662012-07-06 05:48:52 +000053private:
54 bool *const Verified;
55 BoundNodesCallback *const FindResultReviewer;
56};
57
58template <typename T>
59testing::AssertionResult matchesConditionally(const std::string &Code,
60 const T &AMatcher,
Daniel Jasper31f7c082012-10-01 13:40:41 +000061 bool ExpectMatch,
62 llvm::StringRef CompileArg) {
Peter Collingbourned2bd5892013-11-07 22:30:32 +000063 bool Found = false, DynamicFound = false;
Manuel Klimek4da21662012-07-06 05:48:52 +000064 MatchFinder Finder;
65 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
Peter Collingbourned2bd5892013-11-07 22:30:32 +000066 if (!Finder.addDynamicMatcher(AMatcher, new VerifyMatch(0, &DynamicFound)))
67 return testing::AssertionFailure() << "Could not add dynamic matcher";
Manuel Klimek4da21662012-07-06 05:48:52 +000068 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000069 // Some tests use typeof, which is a gnu extension.
Daniel Jasper31f7c082012-10-01 13:40:41 +000070 std::vector<std::string> Args(1, CompileArg);
Nico Weber50f88b92012-08-30 02:08:31 +000071 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000072 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
73 }
Peter Collingbourned2bd5892013-11-07 22:30:32 +000074 if (Found != DynamicFound) {
75 return testing::AssertionFailure() << "Dynamic match result ("
76 << DynamicFound
77 << ") does not match static result ("
78 << Found << ")";
79 }
Manuel Klimek4da21662012-07-06 05:48:52 +000080 if (!Found && ExpectMatch) {
81 return testing::AssertionFailure()
82 << "Could not find match in \"" << Code << "\"";
83 } else if (Found && !ExpectMatch) {
84 return testing::AssertionFailure()
85 << "Found unexpected match in \"" << Code << "\"";
86 }
87 return testing::AssertionSuccess();
88}
89
90template <typename T>
91testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000092 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000093}
94
95template <typename T>
96testing::AssertionResult notMatches(const std::string &Code,
97 const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000098 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000099}
100
101template <typename T>
102testing::AssertionResult
103matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
104 BoundNodesCallback *FindResultVerifier,
105 bool ExpectResult) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000106 OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
Manuel Klimek4da21662012-07-06 05:48:52 +0000107 bool VerifiedResult = false;
108 MatchFinder Finder;
109 Finder.addMatcher(
110 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
111 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +0000112 // Some tests use typeof, which is a gnu extension.
113 std::vector<std::string> Args(1, "-std=gnu++98");
114 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000115 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
116 }
117 if (!VerifiedResult && ExpectResult) {
118 return testing::AssertionFailure()
119 << "Could not verify result in \"" << Code << "\"";
120 } else if (VerifiedResult && !ExpectResult) {
121 return testing::AssertionFailure()
122 << "Verified unexpected result in \"" << Code << "\"";
123 }
124 return testing::AssertionSuccess();
125}
126
127// FIXME: Find better names for these functions (or document what they
128// do more precisely).
129template <typename T>
130testing::AssertionResult
131matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
132 BoundNodesCallback *FindResultVerifier) {
133 return matchAndVerifyResultConditionally(
134 Code, AMatcher, FindResultVerifier, true);
135}
136
137template <typename T>
138testing::AssertionResult
139matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
140 BoundNodesCallback *FindResultVerifier) {
141 return matchAndVerifyResultConditionally(
142 Code, AMatcher, FindResultVerifier, false);
143}
144
145} // end namespace ast_matchers
146} // end namespace clang
147
148#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H