blob: b92a1bac4c0369ed40ee0a572fda97ff2e566c36 [file] [log] [blame]
Richard Smithbc9e5582012-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//===----------------------------------------------------------------------===//
9//
10// This file defines a utility class for RecursiveASTVisitor related tests.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TEST_VISITOR_H
15#define LLVM_CLANG_TEST_VISITOR_H
16
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/RecursiveASTVisitor.h"
19#include "clang/Frontend/FrontendAction.h"
20#include "clang/Frontend/CompilerInstance.h"
21#include "clang/Tooling/Tooling.h"
22#include "gtest/gtest.h"
23
24namespace clang {
25
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000026/// \brief Base class for simple RecursiveASTVisitor based tests.
Richard Smithbc9e5582012-06-24 23:56:26 +000027///
28/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
29/// additional capability of running it over a snippet of code.
30///
31/// Visits template instantiations by default.
32template <typename T>
33class TestVisitor : public RecursiveASTVisitor<T> {
34public:
35 TestVisitor() { }
36
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000037 virtual ~TestVisitor() { }
38
Richard Smithbc9e5582012-06-24 23:56:26 +000039 /// \brief Runs the current AST visitor over the given code.
40 bool runOver(StringRef Code) {
41 return tooling::runToolOnCode(CreateTestAction(), Code);
42 }
43
44 bool shouldVisitTemplateInstantiations() const {
45 return true;
46 }
47
48protected:
49 virtual ASTFrontendAction* CreateTestAction() {
50 return new TestAction(this);
51 }
52
53 class FindConsumer : public ASTConsumer {
54 public:
55 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
56
57 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
58 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
59 }
60
61 private:
62 TestVisitor *Visitor;
63 };
64
65 class TestAction : public ASTFrontendAction {
66 public:
67 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
68
69 virtual clang::ASTConsumer* CreateASTConsumer(
70 CompilerInstance& compiler, llvm::StringRef dummy) {
71 Visitor->Context = &compiler.getASTContext();
72 /// TestConsumer will be deleted by the framework calling us.
73 return new FindConsumer(Visitor);
74 }
75
76 protected:
77 TestVisitor *Visitor;
78 };
79
80 ASTContext *Context;
81};
82
83
84/// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself.
85///
86/// Allows simple creation of test visitors running matches on only a small
87/// subset of the Visit* methods.
88template <typename T, template <typename> class Visitor = TestVisitor>
89class ExpectedLocationVisitor : public Visitor<T> {
90public:
91 ExpectedLocationVisitor()
92 : ExpectedLine(0), ExpectedColumn(0), Found(false) {}
93
Matt Beaumont-Gay8f35d232012-06-25 18:27:11 +000094 virtual ~ExpectedLocationVisitor() {
Richard Smithbc9e5582012-06-24 23:56:26 +000095 EXPECT_TRUE(Found)
96 << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine
97 << ":" << ExpectedColumn << PartialMatches;
98 }
99
100 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
101 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
102 ExpectedMatch = Match.str();
103 ExpectedLine = Line;
104 ExpectedColumn = Column;
105 }
106
107protected:
108 /// \brief Convenience method to simplify writing test visitors.
109 ///
110 /// Sets 'Found' to true if 'Name' and 'Location' match the expected
111 /// values. If only a partial match is found, record the information
112 /// to produce nice error output when a test fails.
113 ///
114 /// Implementations are required to call this with appropriate values
115 /// for 'Name' during visitation.
116 void Match(StringRef Name, SourceLocation Location) {
117 FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
118 if (Name == ExpectedMatch &&
119 FullLocation.isValid() &&
120 FullLocation.getSpellingLineNumber() == ExpectedLine &&
121 FullLocation.getSpellingColumnNumber() == ExpectedColumn) {
122 EXPECT_TRUE(!Found);
123 Found = true;
124 } else if (Name == ExpectedMatch ||
125 (FullLocation.isValid() &&
126 FullLocation.getSpellingLineNumber() == ExpectedLine &&
127 FullLocation.getSpellingColumnNumber() == ExpectedColumn)) {
128 // If we did not match, record information about partial matches.
129 llvm::raw_string_ostream Stream(PartialMatches);
130 Stream << ", partial match: \"" << Name << "\" at ";
131 Location.print(Stream, this->Context->getSourceManager());
132 }
133 }
134
135 std::string ExpectedMatch;
136 unsigned ExpectedLine;
137 unsigned ExpectedColumn;
138 std::string PartialMatches;
139 bool Found;
140};
141}
142
143#endif /* LLVM_CLANG_TEST_VISITOR_H */