blob: 64816f5d60468be6d14603e44f0cfcb3dac4a00b [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;
21using clang::tooling::runToolOnCode;
22using 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,
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 if (!runToolOnCode(Factory->create(), Code)) {
60 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
61 }
62 if (!Found && ExpectMatch) {
63 return testing::AssertionFailure()
64 << "Could not find match in \"" << Code << "\"";
65 } else if (Found && !ExpectMatch) {
66 return testing::AssertionFailure()
67 << "Found unexpected match in \"" << Code << "\"";
68 }
69 return testing::AssertionSuccess();
70}
71
72template <typename T>
73testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
74 return matchesConditionally(Code, AMatcher, true);
75}
76
77template <typename T>
78testing::AssertionResult notMatches(const std::string &Code,
79 const T &AMatcher) {
80 return matchesConditionally(Code, AMatcher, false);
81}
82
83template <typename T>
84testing::AssertionResult
85matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
86 BoundNodesCallback *FindResultVerifier,
87 bool ExpectResult) {
88 llvm::OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
89 bool VerifiedResult = false;
90 MatchFinder Finder;
91 Finder.addMatcher(
92 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
93 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
94 if (!runToolOnCode(Factory->create(), Code)) {
95 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
96 }
97 if (!VerifiedResult && ExpectResult) {
98 return testing::AssertionFailure()
99 << "Could not verify result in \"" << Code << "\"";
100 } else if (VerifiedResult && !ExpectResult) {
101 return testing::AssertionFailure()
102 << "Verified unexpected result in \"" << Code << "\"";
103 }
104 return testing::AssertionSuccess();
105}
106
107// FIXME: Find better names for these functions (or document what they
108// do more precisely).
109template <typename T>
110testing::AssertionResult
111matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
112 BoundNodesCallback *FindResultVerifier) {
113 return matchAndVerifyResultConditionally(
114 Code, AMatcher, FindResultVerifier, true);
115}
116
117template <typename T>
118testing::AssertionResult
119matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
120 BoundNodesCallback *FindResultVerifier) {
121 return matchAndVerifyResultConditionally(
122 Code, AMatcher, FindResultVerifier, false);
123}
124
125} // end namespace ast_matchers
126} // end namespace clang
127
128#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H