blob: 6d872e8ea990934f85f9ba3a32a9abe6d78ae30e [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,
54 bool ExpectMatch) {
55 bool Found = false;
56 MatchFinder Finder;
57 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
58 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000059 // Some tests use typeof, which is a gnu extension.
60 std::vector<std::string> Args(1, "-std=gnu++98");
61 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000062 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
63 }
64 if (!Found && ExpectMatch) {
65 return testing::AssertionFailure()
66 << "Could not find match in \"" << Code << "\"";
67 } else if (Found && !ExpectMatch) {
68 return testing::AssertionFailure()
69 << "Found unexpected match in \"" << Code << "\"";
70 }
71 return testing::AssertionSuccess();
72}
73
74template <typename T>
75testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
76 return matchesConditionally(Code, AMatcher, true);
77}
78
79template <typename T>
80testing::AssertionResult notMatches(const std::string &Code,
81 const T &AMatcher) {
82 return matchesConditionally(Code, AMatcher, false);
83}
84
85template <typename T>
86testing::AssertionResult
87matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
88 BoundNodesCallback *FindResultVerifier,
89 bool ExpectResult) {
90 llvm::OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
91 bool VerifiedResult = false;
92 MatchFinder Finder;
93 Finder.addMatcher(
94 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
95 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000096 // Some tests use typeof, which is a gnu extension.
97 std::vector<std::string> Args(1, "-std=gnu++98");
98 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000099 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
100 }
101 if (!VerifiedResult && ExpectResult) {
102 return testing::AssertionFailure()
103 << "Could not verify result in \"" << Code << "\"";
104 } else if (VerifiedResult && !ExpectResult) {
105 return testing::AssertionFailure()
106 << "Verified unexpected result in \"" << Code << "\"";
107 }
108 return testing::AssertionSuccess();
109}
110
111// FIXME: Find better names for these functions (or document what they
112// do more precisely).
113template <typename T>
114testing::AssertionResult
115matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
116 BoundNodesCallback *FindResultVerifier) {
117 return matchAndVerifyResultConditionally(
118 Code, AMatcher, FindResultVerifier, true);
119}
120
121template <typename T>
122testing::AssertionResult
123matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
124 BoundNodesCallback *FindResultVerifier) {
125 return matchAndVerifyResultConditionally(
126 Code, AMatcher, FindResultVerifier, false);
127}
128
129} // end namespace ast_matchers
130} // end namespace clang
131
132#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H