blob: 77a42af55ab85004812b74c147690474cc227765 [file] [log] [blame]
Richard Smith87d8fb92012-06-24 23:56:26 +00001//===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
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//===----------------------------------------------------------------------===//
James Dennett75c100b2012-08-24 06:59:51 +00009///
10/// \file
11/// \brief Defines utility templates for RecursiveASTVisitor related tests.
12///
Richard Smith87d8fb92012-06-24 23:56:26 +000013//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
16#define LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
Richard Smith87d8fb92012-06-24 23:56:26 +000017
18#include "clang/AST/ASTConsumer.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000019#include "clang/AST/ASTContext.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000020#include "clang/AST/RecursiveASTVisitor.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000021#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000022#include "clang/Frontend/FrontendAction.h"
Richard Smith87d8fb92012-06-24 23:56:26 +000023#include "clang/Tooling/Tooling.h"
24#include "gtest/gtest.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000025#include <vector>
Richard Smith87d8fb92012-06-24 23:56:26 +000026
27namespace clang {
28
Matt Beaumont-Gaya0e2c042012-06-25 18:27:11 +000029/// \brief Base class for simple RecursiveASTVisitor based tests.
Richard Smith87d8fb92012-06-24 23:56:26 +000030///
31/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
32/// additional capability of running it over a snippet of code.
33///
Michael Hanc90d12d2013-09-11 15:53:29 +000034/// Visits template instantiations and implicit code by default.
Richard Smith87d8fb92012-06-24 23:56:26 +000035template <typename T>
36class TestVisitor : public RecursiveASTVisitor<T> {
37public:
38 TestVisitor() { }
39
Matt Beaumont-Gaya0e2c042012-06-25 18:27:11 +000040 virtual ~TestVisitor() { }
41
Alp Tokere3ad5312014-06-26 01:42:24 +000042 enum Language {
43 Lang_C,
44 Lang_CXX98,
45 Lang_CXX11,
46 Lang_OBJC,
47 Lang_OBJCXX11,
48 Lang_CXX = Lang_CXX98
49 };
Richard Smith87deab32012-08-17 21:23:17 +000050
Richard Smith87d8fb92012-06-24 23:56:26 +000051 /// \brief Runs the current AST visitor over the given code.
Richard Smith87deab32012-08-17 21:23:17 +000052 bool runOver(StringRef Code, Language L = Lang_CXX) {
Nico Weber077a53e2012-08-30 02:02:19 +000053 std::vector<std::string> Args;
54 switch (L) {
55 case Lang_C: Args.push_back("-std=c99"); break;
James Dennetteb576c02013-06-30 03:05:49 +000056 case Lang_CXX98: Args.push_back("-std=c++98"); break;
57 case Lang_CXX11: Args.push_back("-std=c++11"); break;
Alp Toker62438da2014-06-06 15:05:09 +000058 case Lang_OBJC: Args.push_back("-ObjC"); break;
Alp Tokere3ad5312014-06-26 01:42:24 +000059 case Lang_OBJCXX11:
60 Args.push_back("-ObjC++");
61 Args.push_back("-std=c++11");
Alp Toker0843bff2014-06-26 02:07:06 +000062 Args.push_back("-fblocks");
Alp Tokere3ad5312014-06-26 01:42:24 +000063 break;
Nico Weber077a53e2012-08-30 02:02:19 +000064 }
65 return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
Richard Smith87d8fb92012-06-24 23:56:26 +000066 }
67
68 bool shouldVisitTemplateInstantiations() const {
69 return true;
70 }
71
Michael Hanc90d12d2013-09-11 15:53:29 +000072 bool shouldVisitImplicitCode() const {
73 return true;
74 }
75
Richard Smith87d8fb92012-06-24 23:56:26 +000076protected:
77 virtual ASTFrontendAction* CreateTestAction() {
78 return new TestAction(this);
79 }
80
81 class FindConsumer : public ASTConsumer {
82 public:
83 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
84
Alexander Kornienko34eb2072015-04-11 02:00:23 +000085 void HandleTranslationUnit(clang::ASTContext &Context) override {
Manuel Klimek5da9dcb2012-07-05 18:13:01 +000086 Visitor->Context = &Context;
Richard Smith87d8fb92012-06-24 23:56:26 +000087 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
88 }
89
90 private:
91 TestVisitor *Visitor;
92 };
93
94 class TestAction : public ASTFrontendAction {
95 public:
96 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
97
Alexander Kornienko34eb2072015-04-11 02:00:23 +000098 std::unique_ptr<clang::ASTConsumer>
99 CreateASTConsumer(CompilerInstance &, llvm::StringRef dummy) override {
Richard Smith87d8fb92012-06-24 23:56:26 +0000100 /// TestConsumer will be deleted by the framework calling us.
David Blaikie6beb6aa2014-08-10 19:56:51 +0000101 return llvm::make_unique<FindConsumer>(Visitor);
Richard Smith87d8fb92012-06-24 23:56:26 +0000102 }
103
104 protected:
105 TestVisitor *Visitor;
106 };
107
108 ASTContext *Context;
109};
110
James Dennett75c100b2012-08-24 06:59:51 +0000111/// \brief A RecursiveASTVisitor to check that certain matches are (or are
112/// not) observed during visitation.
Richard Smith87d8fb92012-06-24 23:56:26 +0000113///
James Dennett75c100b2012-08-24 06:59:51 +0000114/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
115/// and allows simple creation of test visitors running matches on only a small
Richard Smith87d8fb92012-06-24 23:56:26 +0000116/// subset of the Visit* methods.
117template <typename T, template <typename> class Visitor = TestVisitor>
118class ExpectedLocationVisitor : public Visitor<T> {
119public:
James Dennett75c100b2012-08-24 06:59:51 +0000120 /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
121 ///
122 /// Any number of matches can be disallowed.
123 void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
124 DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
Richard Smith87d8fb92012-06-24 23:56:26 +0000125 }
126
127 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
James Dennett75c100b2012-08-24 06:59:51 +0000128 ///
129 /// Any number of expected matches can be set by calling this repeatedly.
130 /// Each is expected to be matched exactly once.
Richard Smith87d8fb92012-06-24 23:56:26 +0000131 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
James Dennett75c100b2012-08-24 06:59:51 +0000132 ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
133 }
134
135 /// \brief Checks that all expected matches have been found.
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000136 ~ExpectedLocationVisitor() override {
James Dennett75c100b2012-08-24 06:59:51 +0000137 for (typename std::vector<ExpectedMatch>::const_iterator
138 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
139 It != End; ++It) {
140 It->ExpectFound();
141 }
Richard Smith87d8fb92012-06-24 23:56:26 +0000142 }
143
144protected:
James Dennett75c100b2012-08-24 06:59:51 +0000145 /// \brief Checks an actual match against expected and disallowed matches.
Richard Smith87d8fb92012-06-24 23:56:26 +0000146 ///
147 /// Implementations are required to call this with appropriate values
148 /// for 'Name' during visitation.
149 void Match(StringRef Name, SourceLocation Location) {
James Dennett75c100b2012-08-24 06:59:51 +0000150 const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
151
152 for (typename std::vector<MatchCandidate>::const_iterator
153 It = DisallowedMatches.begin(), End = DisallowedMatches.end();
154 It != End; ++It) {
155 EXPECT_FALSE(It->Matches(Name, FullLocation))
156 << "Matched disallowed " << *It;
157 }
158
159 for (typename std::vector<ExpectedMatch>::iterator
160 It = ExpectedMatches.begin(), End = ExpectedMatches.end();
161 It != End; ++It) {
162 It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
Richard Smith87d8fb92012-06-24 23:56:26 +0000163 }
164 }
165
James Dennett75c100b2012-08-24 06:59:51 +0000166 private:
167 struct MatchCandidate {
168 std::string ExpectedName;
169 unsigned LineNumber;
170 unsigned ColumnNumber;
171
172 MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
173 : ExpectedName(Name.str()), LineNumber(LineNumber),
174 ColumnNumber(ColumnNumber) {
175 }
176
177 bool Matches(StringRef Name, FullSourceLoc const &Location) const {
178 return MatchesName(Name) && MatchesLocation(Location);
179 }
180
181 bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
182 return MatchesName(Name) || MatchesLocation(Location);
183 }
184
185 bool MatchesName(StringRef Name) const {
186 return Name == ExpectedName;
187 }
188
189 bool MatchesLocation(FullSourceLoc const &Location) const {
190 return Location.isValid() &&
191 Location.getSpellingLineNumber() == LineNumber &&
192 Location.getSpellingColumnNumber() == ColumnNumber;
193 }
194
195 friend std::ostream &operator<<(std::ostream &Stream,
196 MatchCandidate const &Match) {
197 return Stream << Match.ExpectedName
198 << " at " << Match.LineNumber << ":" << Match.ColumnNumber;
199 }
200 };
201
202 struct ExpectedMatch {
203 ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
204 : Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
205
206 void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
207 if (Candidate.Matches(Name, Location)) {
208 EXPECT_TRUE(!Found);
209 Found = true;
210 } else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
211 llvm::raw_string_ostream Stream(PartialMatches);
212 Stream << ", partial match: \"" << Name << "\" at ";
213 Location.print(Stream, SM);
214 }
215 }
216
217 void ExpectFound() const {
218 EXPECT_TRUE(Found)
219 << "Expected \"" << Candidate.ExpectedName
220 << "\" at " << Candidate.LineNumber
221 << ":" << Candidate.ColumnNumber << PartialMatches;
222 }
223
224 MatchCandidate Candidate;
225 std::string PartialMatches;
226 bool Found;
227 };
228
229 std::vector<MatchCandidate> DisallowedMatches;
230 std::vector<ExpectedMatch> ExpectedMatches;
Richard Smith87d8fb92012-06-24 23:56:26 +0000231};
Alexander Kornienko3d9d9292015-06-22 09:47:44 +0000232} // namespace clang
Richard Smith87d8fb92012-06-24 23:56:26 +0000233
Manuel Klimek20c1c892014-10-09 15:02:06 +0000234#endif