diff options
Diffstat (limited to 'unittests/Tooling/RecursiveASTVisitorTest.cpp')
-rw-r--r-- | unittests/Tooling/RecursiveASTVisitorTest.cpp | 117 |
1 files changed, 1 insertions, 116 deletions
diff --git a/unittests/Tooling/RecursiveASTVisitorTest.cpp b/unittests/Tooling/RecursiveASTVisitorTest.cpp index 9ef86b552c..f3ba6468d0 100644 --- a/unittests/Tooling/RecursiveASTVisitorTest.cpp +++ b/unittests/Tooling/RecursiveASTVisitorTest.cpp @@ -7,125 +7,10 @@ // //===----------------------------------------------------------------------===// -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/RecursiveASTVisitor.h" -#include "clang/Frontend/FrontendAction.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Tooling/Tooling.h" -#include "gtest/gtest.h" +#include "TestVisitor.h" namespace clang { -/// \brief Base class for sipmle RecursiveASTVisitor based tests. -/// -/// This is a drop-in replacement for RecursiveASTVisitor itself, with the -/// additional capability of running it over a snippet of code. -/// -/// Visits template instantiations by default. -/// -/// FIXME: Put into a common location. -template <typename T> -class TestVisitor : public clang::RecursiveASTVisitor<T> { -public: - /// \brief Runs the current AST visitor over the given code. - bool runOver(StringRef Code) { - return tooling::runToolOnCode(new TestAction(this), Code); - } - - bool shouldVisitTemplateInstantiations() const { - return true; - } - -protected: - clang::ASTContext *Context; - -private: - class FindConsumer : public clang::ASTConsumer { - public: - FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {} - - virtual void HandleTranslationUnit(clang::ASTContext &Context) { - Visitor->TraverseDecl(Context.getTranslationUnitDecl()); - } - - private: - TestVisitor *Visitor; - }; - - class TestAction : public clang::ASTFrontendAction { - public: - TestAction(TestVisitor *Visitor) : Visitor(Visitor) {} - - virtual clang::ASTConsumer* CreateASTConsumer( - clang::CompilerInstance& compiler, llvm::StringRef dummy) { - Visitor->Context = &compiler.getASTContext(); - /// TestConsumer will be deleted by the framework calling us. - return new FindConsumer(Visitor); - } - - private: - TestVisitor *Visitor; - }; -}; - -/// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself. -/// -/// Allows simple creation of test visitors running matches on only a small -/// subset of the Visit* methods. -template <typename T> -class ExpectedLocationVisitor : public TestVisitor<T> { -public: - ExpectedLocationVisitor() - : ExpectedLine(0), ExpectedColumn(0), Found(false) {} - - ~ExpectedLocationVisitor() { - EXPECT_TRUE(Found) - << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine - << ":" << ExpectedColumn << PartialMatches; - } - - /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. - void ExpectMatch(Twine Match, unsigned Line, unsigned Column) { - ExpectedMatch = Match.str(); - ExpectedLine = Line; - ExpectedColumn = Column; - } - -protected: - /// \brief Convenience method to simplify writing test visitors. - /// - /// Sets 'Found' to true if 'Name' and 'Location' match the expected - /// values. If only a partial match is found, record the information - /// to produce nice error output when a test fails. - /// - /// Implementations are required to call this with appropriate values - /// for 'Name' during visitation. - void Match(StringRef Name, SourceLocation Location) { - FullSourceLoc FullLocation = this->Context->getFullLoc(Location); - if (Name == ExpectedMatch && - FullLocation.isValid() && - FullLocation.getSpellingLineNumber() == ExpectedLine && - FullLocation.getSpellingColumnNumber() == ExpectedColumn) { - EXPECT_TRUE(!Found); - Found = true; - } else if (Name == ExpectedMatch || - (FullLocation.isValid() && - FullLocation.getSpellingLineNumber() == ExpectedLine && - FullLocation.getSpellingColumnNumber() == ExpectedColumn)) { - // If we did not match, record information about partial matches. - llvm::raw_string_ostream Stream(PartialMatches); - Stream << ", partial match: \"" << Name << "\" at "; - Location.print(Stream, this->Context->getSourceManager()); - } - } - - std::string ExpectedMatch; - unsigned ExpectedLine; - unsigned ExpectedColumn; - std::string PartialMatches; - bool Found; -}; - class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> { public: bool VisitTypeLoc(TypeLoc TypeLocation) { |