blob: 01a7c51016264aca42c3159cd34c4ebbb25128c4 [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() {}
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.
33class VerifyMatch : public MatchFinder::MatchCallback {
34public:
35 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
36 : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
37
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
46private:
47 bool *const Verified;
48 BoundNodesCallback *const FindResultReviewer;
49};
50
51template <typename T>
52testing::AssertionResult matchesConditionally(const std::string &Code,
53 const T &AMatcher,
Daniel Jasper31f7c082012-10-01 13:40:41 +000054 bool ExpectMatch,
55 llvm::StringRef CompileArg) {
Manuel Klimek4da21662012-07-06 05:48:52 +000056 bool Found = false;
57 MatchFinder Finder;
58 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
59 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000060 // Some tests use typeof, which is a gnu extension.
Daniel Jasper31f7c082012-10-01 13:40:41 +000061 std::vector<std::string> Args(1, CompileArg);
Nico Weber50f88b92012-08-30 02:08:31 +000062 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000063 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
64 }
65 if (!Found && ExpectMatch) {
66 return testing::AssertionFailure()
67 << "Could not find match in \"" << Code << "\"";
68 } else if (Found && !ExpectMatch) {
69 return testing::AssertionFailure()
70 << "Found unexpected match in \"" << Code << "\"";
71 }
72 return testing::AssertionSuccess();
73}
74
75template <typename T>
76testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000077 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000078}
79
80template <typename T>
81testing::AssertionResult notMatches(const std::string &Code,
82 const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000083 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000084}
85
86template <typename T>
87testing::AssertionResult
88matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
89 BoundNodesCallback *FindResultVerifier,
90 bool ExpectResult) {
91 llvm::OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
92 bool VerifiedResult = false;
93 MatchFinder Finder;
94 Finder.addMatcher(
95 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
96 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000097 // Some tests use typeof, which is a gnu extension.
98 std::vector<std::string> Args(1, "-std=gnu++98");
99 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000100 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
101 }
102 if (!VerifiedResult && ExpectResult) {
103 return testing::AssertionFailure()
104 << "Could not verify result in \"" << Code << "\"";
105 } else if (VerifiedResult && !ExpectResult) {
106 return testing::AssertionFailure()
107 << "Verified unexpected result in \"" << Code << "\"";
108 }
109 return testing::AssertionSuccess();
110}
111
112// FIXME: Find better names for these functions (or document what they
113// do more precisely).
114template <typename T>
115testing::AssertionResult
116matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
117 BoundNodesCallback *FindResultVerifier) {
118 return matchAndVerifyResultConditionally(
119 Code, AMatcher, FindResultVerifier, true);
120}
121
122template <typename T>
123testing::AssertionResult
124matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
125 BoundNodesCallback *FindResultVerifier) {
126 return matchAndVerifyResultConditionally(
127 Code, AMatcher, FindResultVerifier, false);
128}
129
130} // end namespace ast_matchers
131} // end namespace clang
132
133#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H