]> granicus.if.org Git - clang/commitdiff
Implemented -ast-dump, -ast-print, -ast-dump-filter options in clang-check
authorAlexander Kornienko <alexfh@google.com>
Mon, 13 Aug 2012 10:50:08 +0000 (10:50 +0000)
committerAlexander Kornienko <alexfh@google.com>
Mon, 13 Aug 2012 10:50:08 +0000 (10:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161753 91177308-0d34-0410-b5e6-96231b3b80d8

test/Tooling/clang-check-ast-dump.cpp [new file with mode: 0644]
tools/clang-check/ClangCheck.cpp

diff --git a/test/Tooling/clang-check-ast-dump.cpp b/test/Tooling/clang-check-ast-dump.cpp
new file mode 100644 (file)
index 0000000..86533af
--- /dev/null
@@ -0,0 +1,35 @@
+// RUN: clang-check -ast-dump "%s" -- 2>&1 | FileCheck %s
+// CHECK: namespace test_namespace
+// CHECK-NEXT: class TheClass
+// CHECK: int theMethod(int x) (CompoundStmt
+// CHECK-NEXT:   (ReturnStmt
+// CHECK-NEXT:     (BinaryOperator
+//
+// RUN: clang-check -ast-dump -ast-dump-filter test_namespace::TheClass::theMethod "%s" -- 2>&1 | FileCheck -check-prefix CHECK-FILTER %s
+// CHECK-FILTER-NOT: namespace test_namespace
+// CHECK-FILTER-NOT: class TheClass
+// CHECK-FILTER: int theMethod(int x) (CompoundStmt
+// CHECK-FILTER-NEXT:   (ReturnStmt
+// CHECK-FILTER-NEXT:     (BinaryOperator
+//
+// RUN: clang-check -ast-print "%s" -- 2>&1 | FileCheck -check-prefix CHECK-PRINT %s
+// CHECK-PRINT: namespace test_namespace
+// CHECK-PRINT: class TheClass
+// CHECK-PRINT: int theMethod(int x)
+//
+// RUN: clang-check -ast-list "%s" -- 2>&1 | FileCheck -check-prefix CHECK-LIST %s
+// CHECK-LIST: test_namespace
+// CHECK-LIST-NEXT: test_namespace::TheClass
+// CHECK-LIST-NEXT: test_namespace::TheClass::theMethod
+// CHECK-LIST-NEXT: x
+
+namespace test_namespace {
+
+class TheClass {
+public:
+  int theMethod(int x) {
+    return x + x;
+  }
+};
+
+}
index d93c48ab01ed4bfcab2a64b3374eccd06783e7a2..9e58077b5a0ea204a02929471c7e694b65b3fe71 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/CommandLine.h"
+#include "clang/AST/ASTConsumer.h"
+#include "clang/Driver/OptTable.h"
+#include "clang/Driver/Options.h"
+#include "clang/Frontend/ASTConsumers.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Tooling/CommandLineClangTool.h"
 #include "clang/Tooling/Tooling.h"
 
+using namespace clang::driver;
 using namespace clang::tooling;
 using namespace llvm;
 
@@ -38,9 +43,46 @@ static const char *MoreHelpText =
     "\trules described above.\n"
     "\n";
 
+namespace {
+class ActionFactory {
+public:
+  ActionFactory()
+    : Options(createDriverOptTable()),
+      ASTDump(
+        "ast-dump",
+        cl::desc(Options->getOptionHelpText(options::OPT_ast_dump))),
+      ASTList(
+        "ast-list",
+        cl::desc(Options->getOptionHelpText(options::OPT_ast_list))),
+      ASTPrint(
+        "ast-print",
+        cl::desc(Options->getOptionHelpText(options::OPT_ast_print))),
+      ASTDumpFilter(
+        "ast-dump-filter",
+        cl::desc(Options->getOptionHelpText(options::OPT_ast_dump_filter))) {}
+
+  clang::ASTConsumer *newASTConsumer() {
+    if (ASTList)
+      return clang::CreateASTDeclNodeLister();
+    if (ASTDump)
+      return clang::CreateASTDumper(ASTDumpFilter);
+    if (ASTPrint)
+      return clang::CreateASTPrinter(&llvm::outs(), ASTDumpFilter);
+    return new clang::ASTConsumer();
+  }
+private:
+  OwningPtr<OptTable> Options;
+  cl::opt<bool> ASTDump;
+  cl::opt<bool> ASTList;
+  cl::opt<bool> ASTPrint;
+  cl::opt<std::string> ASTDumpFilter;
+};
+}
+
 int main(int argc, const char **argv) {
+  ActionFactory Factory;
   CommandLineClangTool Tool;
   cl::extrahelp MoreHelp(MoreHelpText);
   Tool.initialize(argc, argv);
-  return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
+  return Tool.run(newFrontendActionFactory(&Factory));
 }