blob: 66cc9bd45074d931ef805a14451d418dc9ae02be [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() {}
Manuel Klimek3e2aa992012-10-24 14:47:44 +000027 virtual bool run(const BoundNodes *BoundNodes) { return false; }
28 virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) {
29 return run(BoundNodes);
30 }
Manuel Klimek4da21662012-07-06 05:48:52 +000031};
32
33// If 'FindResultVerifier' is not NULL, sets *Verified to the result of
34// running 'FindResultVerifier' with the bound nodes as argument.
35// If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
36class VerifyMatch : public MatchFinder::MatchCallback {
37public:
38 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
39 : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
40
41 virtual void run(const MatchFinder::MatchResult &Result) {
42 if (FindResultReviewer != NULL) {
Manuel Klimek3e2aa992012-10-24 14:47:44 +000043 *Verified = FindResultReviewer->run(&Result.Nodes, Result.Context);
Manuel Klimek4da21662012-07-06 05:48:52 +000044 } else {
45 *Verified = true;
46 }
47 }
48
49private:
50 bool *const Verified;
51 BoundNodesCallback *const FindResultReviewer;
52};
53
54template <typename T>
55testing::AssertionResult matchesConditionally(const std::string &Code,
56 const T &AMatcher,
Daniel Jasper31f7c082012-10-01 13:40:41 +000057 bool ExpectMatch,
58 llvm::StringRef CompileArg) {
Manuel Klimek4da21662012-07-06 05:48:52 +000059 bool Found = false;
60 MatchFinder Finder;
61 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
62 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000063 // Some tests use typeof, which is a gnu extension.
Daniel Jasper31f7c082012-10-01 13:40:41 +000064 std::vector<std::string> Args(1, CompileArg);
Nico Weber50f88b92012-08-30 02:08:31 +000065 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000066 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
67 }
68 if (!Found && ExpectMatch) {
69 return testing::AssertionFailure()
70 << "Could not find match in \"" << Code << "\"";
71 } else if (Found && !ExpectMatch) {
72 return testing::AssertionFailure()
73 << "Found unexpected match in \"" << Code << "\"";
74 }
75 return testing::AssertionSuccess();
76}
77
78template <typename T>
79testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000080 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000081}
82
83template <typename T>
84testing::AssertionResult notMatches(const std::string &Code,
85 const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000086 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000087}
88
89template <typename T>
90testing::AssertionResult
91matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
92 BoundNodesCallback *FindResultVerifier,
93 bool ExpectResult) {
94 llvm::OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
95 bool VerifiedResult = false;
96 MatchFinder Finder;
97 Finder.addMatcher(
98 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
99 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +0000100 // Some tests use typeof, which is a gnu extension.
101 std::vector<std::string> Args(1, "-std=gnu++98");
102 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000103 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
104 }
105 if (!VerifiedResult && ExpectResult) {
106 return testing::AssertionFailure()
107 << "Could not verify result in \"" << Code << "\"";
108 } else if (VerifiedResult && !ExpectResult) {
109 return testing::AssertionFailure()
110 << "Verified unexpected result in \"" << Code << "\"";
111 }
112 return testing::AssertionSuccess();
113}
114
115// FIXME: Find better names for these functions (or document what they
116// do more precisely).
117template <typename T>
118testing::AssertionResult
119matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
120 BoundNodesCallback *FindResultVerifier) {
121 return matchAndVerifyResultConditionally(
122 Code, AMatcher, FindResultVerifier, true);
123}
124
125template <typename T>
126testing::AssertionResult
127matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
128 BoundNodesCallback *FindResultVerifier) {
129 return matchAndVerifyResultConditionally(
130 Code, AMatcher, FindResultVerifier, false);
131}
132
133} // end namespace ast_matchers
134} // end namespace clang
135
136#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H