blob: 35098d94b83c157acb692c7e1a36e5e232da81f9 [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
26/// \brief Base class for sipmle RecursiveASTVisitor based tests.
27///
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
37 /// \brief Runs the current AST visitor over the given code.
38 bool runOver(StringRef Code) {
39 return tooling::runToolOnCode(CreateTestAction(), Code);
40 }
41
42 bool shouldVisitTemplateInstantiations() const {
43 return true;
44 }
45
46protected:
47 virtual ASTFrontendAction* CreateTestAction() {
48 return new TestAction(this);
49 }
50
51 class FindConsumer : public ASTConsumer {
52 public:
53 FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
54
55 virtual void HandleTranslationUnit(clang::ASTContext &Context) {
56 Visitor->TraverseDecl(Context.getTranslationUnitDecl());
57 }
58
59 private:
60 TestVisitor *Visitor;
61 };
62
63 class TestAction : public ASTFrontendAction {
64 public:
65 TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
66
67 virtual clang::ASTConsumer* CreateASTConsumer(
68 CompilerInstance& compiler, llvm::StringRef dummy) {
69 Visitor->Context = &compiler.getASTContext();
70 /// TestConsumer will be deleted by the framework calling us.
71 return new FindConsumer(Visitor);
72 }
73
74 protected:
75 TestVisitor *Visitor;
76 };
77
78 ASTContext *Context;
79};
80
81
82/// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself.
83///
84/// Allows simple creation of test visitors running matches on only a small
85/// subset of the Visit* methods.
86template <typename T, template <typename> class Visitor = TestVisitor>
87class ExpectedLocationVisitor : public Visitor<T> {
88public:
89 ExpectedLocationVisitor()
90 : ExpectedLine(0), ExpectedColumn(0), Found(false) {}
91
92 ~ExpectedLocationVisitor() {
93 EXPECT_TRUE(Found)
94 << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine
95 << ":" << ExpectedColumn << PartialMatches;
96 }
97
98 /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
99 void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
100 ExpectedMatch = Match.str();
101 ExpectedLine = Line;
102 ExpectedColumn = Column;
103 }
104
105protected:
106 /// \brief Convenience method to simplify writing test visitors.
107 ///
108 /// Sets 'Found' to true if 'Name' and 'Location' match the expected
109 /// values. If only a partial match is found, record the information
110 /// to produce nice error output when a test fails.
111 ///
112 /// Implementations are required to call this with appropriate values
113 /// for 'Name' during visitation.
114 void Match(StringRef Name, SourceLocation Location) {
115 FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
116 if (Name == ExpectedMatch &&
117 FullLocation.isValid() &&
118 FullLocation.getSpellingLineNumber() == ExpectedLine &&
119 FullLocation.getSpellingColumnNumber() == ExpectedColumn) {
120 EXPECT_TRUE(!Found);
121 Found = true;
122 } else if (Name == ExpectedMatch ||
123 (FullLocation.isValid() &&
124 FullLocation.getSpellingLineNumber() == ExpectedLine &&
125 FullLocation.getSpellingColumnNumber() == ExpectedColumn)) {
126 // If we did not match, record information about partial matches.
127 llvm::raw_string_ostream Stream(PartialMatches);
128 Stream << ", partial match: \"" << Name << "\" at ";
129 Location.print(Stream, this->Context->getSourceManager());
130 }
131 }
132
133 std::string ExpectedMatch;
134 unsigned ExpectedLine;
135 unsigned ExpectedColumn;
136 std::string PartialMatches;
137 bool Found;
138};
139}
140
141#endif /* LLVM_CLANG_TEST_VISITOR_H */