[llvm-branch-commits] [cfe-branch] r155003 - in /cfe/branches/tooling/unittests: AST/RecursiveASTVisitorTest.cpp Tooling/RefactoringTest.cpp

Manuel Klimek klimek at google.com
Wed Apr 18 07:18:26 PDT 2012


Author: klimek
Date: Wed Apr 18 09:18:26 2012
New Revision: 155003

URL: http://llvm.org/viewvc/llvm-project?rev=155003&view=rev
Log:
Getting the RAV test in shape to be upstreamable.


Modified:
    cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp
    cfe/branches/tooling/unittests/Tooling/RefactoringTest.cpp

Modified: cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp?rev=155003&r1=155002&r2=155003&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp (original)
+++ cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp Wed Apr 18 09:18:26 2012
@@ -1,4 +1,4 @@
-//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
+//===- unittest/AST/RecursiveASTMatcherTest.cpp ---------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,151 +8,208 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/AST/ASTConsumer.h"
-#include "clang/AST/DeclGroup.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Frontend/FrontendAction.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "clang/Tooling/Tooling.h"
-#include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
 
 namespace clang {
-namespace ast_matchers {
 
-using clang::tooling::runToolOnCode;
+/// \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);
+  }
 
-namespace {
-/// Takes an ast consumer and returns it from CreateASTConsumer. This only
-/// works with single translation unit compilations.
-class TestAction : public ASTFrontendAction {
- public:
-  /// Takes ownership of TestConsumer.
-  explicit TestAction(ASTConsumer *TestConsumer)
-    : TestConsumer(TestConsumer) {}
-
- protected:
-  virtual ASTConsumer *CreateASTConsumer(CompilerInstance &Compiler,
-                                         StringRef Dummy) {
-    /// TestConsumer will be deleted by the framework calling us.
-    return TestConsumer;
+  bool shouldVisitTemplateInstantiations() const {
+    return true;
   }
 
- private:
-  ASTConsumer * const TestConsumer;
+protected:
+  clang::ASTContext *Context;
+  clang::SourceManager *SM;
+
+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->SM = &compiler.getSourceManager();
+      Visitor->Context = &compiler.getASTContext();
+      /// TestConsumer will be deleted by the framework calling us.
+      return new FindConsumer(Visitor);
+    }
+
+  private:
+    TestVisitor *Visitor;
+  };
 };
 
-class ExpectTypeVisitor : public RecursiveASTVisitor<ExpectTypeVisitor> {
+/// \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:
-  ExpectTypeVisitor(bool *Found, ASTContext *Context, SourceManager *SM,
-                    StringRef ExpectedType, int Line, int Column)
-    : Found(Found), Context(Context), SM(SM), ExpectedType(ExpectedType),
-      Line(Line), Column(Column) {}
+  ExpectedLocationVisitor()
+    : ExpectedLine(0), ExpectedColumn(0), Found(false) {}
 
-  bool VisitTypeLoc(TypeLoc TypeLocation) {
-    FullSourceLoc Location = Context->getFullLoc(TypeLocation.getBeginLoc());
-    std::string LocationText;
-    llvm::raw_string_ostream Stream(LocationText);
-    Location.print(Stream, *SM);
-    if (Location.isValid()) {
-      llvm::errs() << Stream.str() << "\n" << TypeLocation.getType().getAsString() << "\n";
-      if (TypeLocation.getType().getAsString() == ExpectedType &&
-          Line == Location.getSpellingLineNumber() &&
-          Column == Location.getSpellingColumnNumber())
-        *Found = true;
+  ~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) {
+      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->SM);
     }
-    return true;
   }
 
-  bool VisitDeclRefExpr(DeclRefExpr *Reference) {
-    FullSourceLoc Location = Context->getFullLoc(Reference->getLocation());
-    std::string LocationText;
-    llvm::raw_string_ostream Stream(LocationText);
-    Location.print(Stream, *SM);
-    if (Location.isValid()) {
-      llvm::errs() << Stream.str() << "\n" << Reference->getNameInfo().getAsString() << "\n";
-      if (Reference->getNameInfo().getAsString() == ExpectedType &&
-          Line == Location.getSpellingLineNumber() &&
-          Column == Location.getSpellingColumnNumber())
-        *Found = true;
-    }
-    return true;
-  }
+  std::string ExpectedMatch;
+  unsigned ExpectedLine;
+  unsigned ExpectedColumn;
+  std::string PartialMatches;
+  bool Found;
+};
 
-  bool VisitDecl(Decl *Declaration) {
-    FullSourceLoc Location = Context->getFullLoc(Declaration->getLocation());
-    std::string LocationText;
-    llvm::raw_string_ostream Stream(LocationText);
-    Location.print(Stream, *SM);
-    if (Location.isValid()) {
-      llvm::errs() << Stream.str() << "\n"; // << Declaration->getNameAsString() << "\n";
-/*      if (TypeLocation.getType().getAsString() == ExpectedType &&
-          Line == Location.getSpellingLineNumber() &&
-          Column == Location.getSpellingColumnNumber())
-        *Found = true;*/
-    }
+class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> {
+public:
+  bool VisitTypeLoc(TypeLoc TypeLocation) {
+    Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc());
     return true;
   }
+};
 
-  bool shouldVisitTemplateInstantiations() {
+class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> {
+public:
+  bool VisitDeclRefExpr(DeclRefExpr *Reference) {
+    Match(Reference->getNameInfo().getAsString(), Reference->getLocation());
     return true;
   }
-
-private:
-  bool * const Found;
-  ASTContext * const Context;
-  SourceManager * const SM;
-  const std::string ExpectedType;
-  const int Line;
-  const int Column;
 };
 
-class RunVisitorConsumer : public ASTConsumer {
- public:
-  explicit RunVisitorConsumer(bool *Found,
-                              StringRef ExpectedType, int Line, int Column)
-    : Found(Found), ExpectedType(ExpectedType), Line(Line), Column(Column) {}
-
-  virtual void HandleTranslationUnit(ASTContext &Context) {
-    ExpectTypeVisitor Visitor(Found, &Context, &Context.getSourceManager(),
-                              ExpectedType, Line, Column);
-    Visitor.TraverseDecl(Context.getTranslationUnitDecl());
-  }
- private:
-  bool * const Found;
-  const std::string ExpectedType;
-  const int Line;
-  const int Column;
+class CXXMemberCallVisitor
+  : public ExpectedLocationVisitor<CXXMemberCallVisitor> {
+public:
+  bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) {
+    Match(Call->getMethodDecl()->getQualifiedNameAsString(),
+          Call->getLocStart());
+    return true;
+  }
 };
-} // end namespace
 
 TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) {
-  bool Found = false;
-  EXPECT_TRUE(runToolOnCode(new TestAction(
-                new RunVisitorConsumer(&Found, "class X", 1, 30)),
-                  "class X {}; class Y : public X {};"));
-  EXPECT_TRUE(Found);
+  TypeLocVisitor Visitor;
+  Visitor.ExpectMatch("class X", 1, 30);
+  EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};"));
 }
 
 TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) {
-  bool Found = false;
-  EXPECT_TRUE(runToolOnCode(new TestAction(
-                new RunVisitorConsumer(&Found, "x", 2, 3)),
-                  "void x(); template <void (*T)()> class X {};\nX<x> y;"));
-  EXPECT_TRUE(Found);
+  DeclRefExprVisitor Visitor;
+  Visitor.ExpectMatch("x", 2, 3);
+  EXPECT_TRUE(Visitor.runOver(
+    "void x(); template <void (*T)()> class X {};\nX<x> y;"));
+}
+
+TEST(RecursiveASTVisitor, VisitsCallExpr) {
+  DeclRefExprVisitor Visitor;
+  Visitor.ExpectMatch("x", 1, 22);
+  EXPECT_TRUE(Visitor.runOver(
+    "void x(); void y() { x(); }"));
+}
+
+TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) {
+  CXXMemberCallVisitor Visitor;
+  Visitor.ExpectMatch("Y::x", 3, 3);
+  EXPECT_TRUE(Visitor.runOver(
+    "struct Y { void x(); };\n"
+    "template<typename T> void y(T t) {\n"
+    "  t.x();\n"
+    "}\n"
+    "void foo() { y<Y>(Y()); }"));
+}
+
+/* FIXME:
+TEST(RecursiveASTVisitor, VisitsCallInNestedTemplateInstantiation) {
+  CXXMemberCallVisitor Visitor;
+  Visitor.ExpectMatch("Y::x", 4, 5);
+  EXPECT_TRUE(Visitor.runOver(
+    "struct Y { void x(); };\n"
+    "template<typename T> struct Z {\n"
+    "  template<typename U> static void f() {\n"
+    "    T().x();\n"
+    "  }\n"
+    "};\n"
+    "void foo() { Z<Y>::f<int>(); }"));
 }
+*/
 
 /* FIXME: According to Richard Smith this is a bug in the AST.
 TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) {
-  bool Found = false;
-  EXPECT_TRUE(runToolOnCode(new TestAction(
-                new RunVisitorConsumer(&Found, "x", 3, 43)),
-                  "template <typename T> void x();\n"
-                  "template <void (*T)()> class X {};\n"
-                  "template <typename T> class Y : public X< x<T> > {};\n"
-                  "Y<int> y;"));
-  EXPECT_TRUE(Found);
+  DeclRefExprVisitor Visitor;
+  Visitor.ExpectMatch("x", 3, 43);
+  EXPECT_TRUE(Visitor.runOver(
+    "template <typename T> void x();\n"
+    "template <void (*T)()> class X {};\n"
+    "template <typename T> class Y : public X< x<T> > {};\n"
+    "Y<int> y;"));
 }
 */
 
-} // end namespace tooling
 } // end namespace clang
 

Modified: cfe/branches/tooling/unittests/Tooling/RefactoringTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/Tooling/RefactoringTest.cpp?rev=155003&r1=155002&r2=155003&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/Tooling/RefactoringTest.cpp (original)
+++ cfe/branches/tooling/unittests/Tooling/RefactoringTest.cpp Wed Apr 18 09:18:26 2012
@@ -262,6 +262,7 @@
   class FindConsumer : public clang::ASTConsumer {
   public:
     FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
+
     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
       Visitor->TraverseDecl(Context.getTranslationUnitDecl());
     }
@@ -284,9 +285,7 @@
   private:
     TestVisitor *Visitor;
   };
-
 };
-
 } // end namespace
 
 void expectReplacementAt(const Replacement &Replace,





More information about the llvm-branch-commits mailing list