Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 1 | //===--- 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 Kramer | a13d2bc | 2012-07-04 20:33:53 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 18 | #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 | |
| 25 | namespace clang { |
| 26 | |
Matt Beaumont-Gay | 8f35d23 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 27 | /// \brief Base class for simple RecursiveASTVisitor based tests. |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 28 | /// |
| 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. |
| 33 | template <typename T> |
| 34 | class TestVisitor : public RecursiveASTVisitor<T> { |
| 35 | public: |
| 36 | TestVisitor() { } |
| 37 | |
Matt Beaumont-Gay | 8f35d23 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 38 | virtual ~TestVisitor() { } |
| 39 | |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 40 | /// \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 | |
| 49 | protected: |
| 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 Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 59 | Visitor->Context = &Context; |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 60 | 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 Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 72 | CompilerInstance&, llvm::StringRef dummy) { |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 73 | /// 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. |
| 89 | template <typename T, template <typename> class Visitor = TestVisitor> |
| 90 | class ExpectedLocationVisitor : public Visitor<T> { |
| 91 | public: |
| 92 | ExpectedLocationVisitor() |
| 93 | : ExpectedLine(0), ExpectedColumn(0), Found(false) {} |
| 94 | |
Matt Beaumont-Gay | 8f35d23 | 2012-06-25 18:27:11 +0000 | [diff] [blame] | 95 | virtual ~ExpectedLocationVisitor() { |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 96 | 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 | |
| 108 | protected: |
| 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 */ |