blob: 5fed85bb30bb6a78d9c1b6d55f6b6640588b7250 [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;
Manuel Klimek4da21662012-07-06 05:48:52 +000029};
30
31// If 'FindResultVerifier' is not NULL, sets *Verified to the result of
32// running 'FindResultVerifier' with the bound nodes as argument.
33// If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
34class VerifyMatch : public MatchFinder::MatchCallback {
35public:
36 VerifyMatch(BoundNodesCallback *FindResultVerifier, bool *Verified)
37 : Verified(Verified), FindResultReviewer(FindResultVerifier) {}
38
39 virtual void run(const MatchFinder::MatchResult &Result) {
40 if (FindResultReviewer != NULL) {
Daniel Jasper11c98772012-11-11 22:14:55 +000041 *Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
Manuel Klimek4da21662012-07-06 05:48:52 +000042 } else {
43 *Verified = true;
44 }
45 }
46
47private:
48 bool *const Verified;
49 BoundNodesCallback *const FindResultReviewer;
50};
51
52template <typename T>
53testing::AssertionResult matchesConditionally(const std::string &Code,
54 const T &AMatcher,
Daniel Jasper31f7c082012-10-01 13:40:41 +000055 bool ExpectMatch,
56 llvm::StringRef CompileArg) {
Manuel Klimek4da21662012-07-06 05:48:52 +000057 bool Found = false;
58 MatchFinder Finder;
59 Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
60 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000061 // Some tests use typeof, which is a gnu extension.
Daniel Jasper31f7c082012-10-01 13:40:41 +000062 std::vector<std::string> Args(1, CompileArg);
Nico Weber50f88b92012-08-30 02:08:31 +000063 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +000064 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
65 }
66 if (!Found && ExpectMatch) {
67 return testing::AssertionFailure()
68 << "Could not find match in \"" << Code << "\"";
69 } else if (Found && !ExpectMatch) {
70 return testing::AssertionFailure()
71 << "Found unexpected match in \"" << Code << "\"";
72 }
73 return testing::AssertionSuccess();
74}
75
76template <typename T>
77testing::AssertionResult matches(const std::string &Code, const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000078 return matchesConditionally(Code, AMatcher, true, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000079}
80
81template <typename T>
82testing::AssertionResult notMatches(const std::string &Code,
83 const T &AMatcher) {
Daniel Jasper31f7c082012-10-01 13:40:41 +000084 return matchesConditionally(Code, AMatcher, false, "-std=c++11");
Manuel Klimek4da21662012-07-06 05:48:52 +000085}
86
87template <typename T>
88testing::AssertionResult
89matchAndVerifyResultConditionally(const std::string &Code, const T &AMatcher,
90 BoundNodesCallback *FindResultVerifier,
91 bool ExpectResult) {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000092 OwningPtr<BoundNodesCallback> ScopedVerifier(FindResultVerifier);
Manuel Klimek4da21662012-07-06 05:48:52 +000093 bool VerifiedResult = false;
94 MatchFinder Finder;
95 Finder.addMatcher(
96 AMatcher, new VerifyMatch(FindResultVerifier, &VerifiedResult));
97 OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
Nico Weber50f88b92012-08-30 02:08:31 +000098 // Some tests use typeof, which is a gnu extension.
99 std::vector<std::string> Args(1, "-std=gnu++98");
100 if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
Manuel Klimek4da21662012-07-06 05:48:52 +0000101 return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
102 }
103 if (!VerifiedResult && ExpectResult) {
104 return testing::AssertionFailure()
105 << "Could not verify result in \"" << Code << "\"";
106 } else if (VerifiedResult && !ExpectResult) {
107 return testing::AssertionFailure()
108 << "Verified unexpected result in \"" << Code << "\"";
109 }
110 return testing::AssertionSuccess();
111}
112
113// FIXME: Find better names for these functions (or document what they
114// do more precisely).
115template <typename T>
116testing::AssertionResult
117matchAndVerifyResultTrue(const std::string &Code, const T &AMatcher,
118 BoundNodesCallback *FindResultVerifier) {
119 return matchAndVerifyResultConditionally(
120 Code, AMatcher, FindResultVerifier, true);
121}
122
123template <typename T>
124testing::AssertionResult
125matchAndVerifyResultFalse(const std::string &Code, const T &AMatcher,
126 BoundNodesCallback *FindResultVerifier) {
127 return matchAndVerifyResultConditionally(
128 Code, AMatcher, FindResultVerifier, false);
129}
130
131} // end namespace ast_matchers
132} // end namespace clang
133
134#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H