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