[llvm-branch-commits] [cfe-branch] r154923 - in /cfe/branches/tooling/unittests: AST/ AST/Makefile AST/RecursiveASTVisitorTest.cpp CMakeLists.txt

Manuel Klimek klimek at google.com
Tue Apr 17 05:19:07 PDT 2012


Author: klimek
Date: Tue Apr 17 07:19:07 2012
New Revision: 154923

URL: http://llvm.org/viewvc/llvm-project?rev=154923&view=rev
Log:
First version of RecursiveASTVisitorTest. Still needs some refactoring to make it maintainable.

Added:
    cfe/branches/tooling/unittests/AST/
    cfe/branches/tooling/unittests/AST/Makefile   (with props)
    cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp   (with props)
Modified:
    cfe/branches/tooling/unittests/CMakeLists.txt

Added: cfe/branches/tooling/unittests/AST/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/AST/Makefile?rev=154923&view=auto
==============================================================================
--- cfe/branches/tooling/unittests/AST/Makefile (added)
+++ cfe/branches/tooling/unittests/AST/Makefile Tue Apr 17 07:19:07 2012
@@ -0,0 +1,17 @@
+##===- unittests/ASTMatchers/Makefile ----------------------*- Makefile -*-===##
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+CLANG_LEVEL = ../..
+TESTNAME = RecursiveASTVistor
+LINK_COMPONENTS := support mc
+USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \
+           clangParse.a clangSema.a clangAnalysis.a clangAST.a \
+           clangASTMatchers.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/unittests/Makefile

Propchange: cfe/branches/tooling/unittests/AST/Makefile
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp?rev=154923&view=auto
==============================================================================
--- cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp (added)
+++ cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp Tue Apr 17 07:19:07 2012
@@ -0,0 +1,158 @@
+//===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/DeclGroup.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Frontend/FrontendAction.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;
+
+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;
+  }
+
+ private:
+  ASTConsumer * const TestConsumer;
+};
+
+class ExpectTypeVisitor : public RecursiveASTVisitor<ExpectTypeVisitor> {
+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) {}
+
+  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;
+    }
+    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;
+  }
+
+  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;*/
+    }
+    return true;
+  }
+
+  bool shouldVisitTemplateInstantiations() {
+    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;
+};
+} // 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);
+}
+
+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);
+}
+
+/* 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);
+}
+*/
+
+} // end namespace tooling
+} // end namespace clang
+

Propchange: cfe/branches/tooling/unittests/AST/RecursiveASTVisitorTest.cpp
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: cfe/branches/tooling/unittests/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/tooling/unittests/CMakeLists.txt?rev=154923&r1=154922&r2=154923&view=diff
==============================================================================
--- cfe/branches/tooling/unittests/CMakeLists.txt (original)
+++ cfe/branches/tooling/unittests/CMakeLists.txt Tue Apr 17 07:19:07 2012
@@ -77,3 +77,8 @@
   Tooling/RefactoringTest.cpp
   USED_LIBS gtest gtest_main clangTooling
  )
+
+add_clang_unittest(AST
+  AST/RecursiveASTVisitorTest.cpp
+  USED_LIBS gtest gtest_main clangAST clangTooling
+ )





More information about the llvm-branch-commits mailing list