summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2012-07-16 12:46:48 +0000
committerAlexander Kornienko <alexfh@google.com>2012-07-16 12:46:48 +0000
commitdea8fba3c632745136017f3e8e9d9e0341d6feb9 (patch)
treee055799bf1f4cf94059f34136d03a6f64c9a0a77 /tools
parente994bb6b02f061c4e2442abe0c72913f6a74b961 (diff)
downloadclang-dea8fba3c632745136017f3e8e9d9e0341d6feb9.tar.gz
clang-dea8fba3c632745136017f3e8e9d9e0341d6feb9.tar.bz2
clang-dea8fba3c632745136017f3e8e9d9e0341d6feb9.tar.xz
The new clang-ast-dump tool for selective AST dumping. Moved common command-line tool stuff to CommandLineClangTool
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160265 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/CMakeLists.txt1
-rw-r--r--tools/Makefile2
-rw-r--r--tools/clang-ast-dump/CMakeLists.txt8
-rw-r--r--tools/clang-ast-dump/ClangASTDump.cpp141
-rw-r--r--tools/clang-ast-dump/Makefile23
-rw-r--r--tools/clang-check/ClangCheck.cpp74
6 files changed, 192 insertions, 57 deletions
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index ab4748d1b9..2e3c842263 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -5,3 +5,4 @@ add_subdirectory(c-arcmt-test)
add_subdirectory(diagtool)
add_subdirectory(driver)
add_subdirectory(clang-check)
+add_subdirectory(clang-ast-dump)
diff --git a/tools/Makefile b/tools/Makefile
index 5059ade930..95c1e4821d 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -9,7 +9,7 @@
CLANG_LEVEL := ..
DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool \
- clang-check
+ clang-check clang-ast-dump
include $(CLANG_LEVEL)/../../Makefile.config
diff --git a/tools/clang-ast-dump/CMakeLists.txt b/tools/clang-ast-dump/CMakeLists.txt
new file mode 100644
index 0000000000..7f73341010
--- /dev/null
+++ b/tools/clang-ast-dump/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_clang_executable(clang-ast-dump
+ ClangASTDump.cpp
+ )
+
+target_link_libraries(clang-ast-dump
+ clangTooling
+ clangBasic
+ )
diff --git a/tools/clang-ast-dump/ClangASTDump.cpp b/tools/clang-ast-dump/ClangASTDump.cpp
new file mode 100644
index 0000000000..2b86fd6d29
--- /dev/null
+++ b/tools/clang-ast-dump/ClangASTDump.cpp
@@ -0,0 +1,141 @@
+//===- tools/clang-ast-dump/ClangASTDump.cpp - Clang AST Dump tool --------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements a clang-ast-dump tool that dumps specified parts
+// of an AST of a number of translation units.
+//
+// Run with '-help' for details.
+//
+// This tool uses the Clang Tooling infrastructure, see
+// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
+// for details on setting it up with LLVM source tree.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CommandLine.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/CommandLineClangTool.h"
+#include "clang/Tooling/Tooling.h"
+
+using namespace clang::tooling;
+using namespace llvm;
+
+cl::opt<std::string> FilterString(
+ "f",
+ cl::desc("Filter string"),
+ cl::Optional);
+
+cl::opt<bool> ListAll(
+ "l",
+ cl::desc("List all filterable nodes"),
+ cl::init(false),
+ cl::Optional);
+
+static const char *MoreHelpText =
+ "-f <filter-string> can be used to dump only AST declaration nodes having\n"
+ "\ta certain substring in a qualified name.\n"
+ "\n"
+ "-l \tlists qualified names of all filterable declaration nodes.\n"
+ "\n";
+
+namespace {
+
+using namespace clang;
+
+class SelectiveDumpVisitor :
+ public RecursiveASTVisitor<SelectiveDumpVisitor> {
+ typedef RecursiveASTVisitor<SelectiveDumpVisitor> base;
+public:
+ SelectiveDumpVisitor(const std::string &FilterString, bool ListAll)
+ : FilterString(FilterString), ListAll(ListAll) {}
+
+ ASTConsumer* newASTConsumer() {
+ return new DumpConsumer(this);
+ }
+
+ bool shouldWalkTypesOfTypeLocs() const { return false; }
+
+ void Run(TranslationUnitDecl *D) {
+ if (ListAll) {
+ llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+ "Listing all filterable nodes:\n";
+ llvm::outs().resetColor();
+ TraverseDecl(D);
+ return;
+ }
+
+ if (FilterString.empty()) {
+ llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+ "Dumping translation unit:\n";
+ llvm::outs().resetColor();
+ D->dumpXML(llvm::outs());
+ return;
+ }
+
+ TraverseDecl(D);
+ }
+
+ bool TraverseDecl(Decl *D) {
+ if (ListAll) {
+ std::string Name = getName(D);
+ if (!Name.empty())
+ llvm::outs() << Name << "\n";
+ return base::TraverseDecl(D);
+ }
+
+ if (filterMatches(D)) {
+ llvm::outs().changeColor(llvm::raw_ostream::BLUE) <<
+ "Dumping " << getName(D) << ":\n";
+ llvm::outs().resetColor();
+ D->dumpXML(llvm::outs());
+ return true;
+ }
+ return base::TraverseDecl(D);
+ }
+
+private:
+ std::string getName(Decl *D) {
+ if (isa<NamedDecl>(D))
+ return cast<NamedDecl>(D)->getQualifiedNameAsString();
+ return "";
+ }
+ bool filterMatches(Decl *D) {
+ return getName(D).find(FilterString) != std::string::npos;
+ }
+
+ class DumpConsumer : public ASTConsumer {
+ public:
+ DumpConsumer(SelectiveDumpVisitor *Visitor) : Visitor(Visitor) {}
+
+ virtual void HandleTranslationUnit(ASTContext &Context) {
+ Visitor->Context = &Context;
+ Visitor->Run(Context.getTranslationUnitDecl());
+ }
+
+ private:
+ SelectiveDumpVisitor *Visitor;
+ };
+
+ ASTContext *Context;
+ std::string FilterString;
+ bool ListAll;
+};
+
+} // namespace
+
+int main(int argc, const char **argv) {
+ CommandLineClangTool Tool;
+ cl::extrahelp MoreHelp(MoreHelpText);
+ Tool.initialize(argc, argv);
+ SelectiveDumpVisitor Dumper(FilterString, ListAll);
+ return Tool.run(newFrontendActionFactory(&Dumper));
+}
diff --git a/tools/clang-ast-dump/Makefile b/tools/clang-ast-dump/Makefile
new file mode 100644
index 0000000000..74a0511a07
--- /dev/null
+++ b/tools/clang-ast-dump/Makefile
@@ -0,0 +1,23 @@
+##===- tools/clang-check/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 := ../..
+
+TOOLNAME = clang-ast-dump
+NO_INSTALL = 1
+
+# No plugins, optimize startup time.
+TOOL_NO_EXPORTS = 1
+
+LINK_COMPONENTS := support mc
+USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a \
+ clangTooling.a clangParse.a clangSema.a clangAnalysis.a \
+ clangEdit.a clangAST.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile
diff --git a/tools/clang-check/ClangCheck.cpp b/tools/clang-check/ClangCheck.cpp
index 36af01b2e8..d93c48ab01 100644
--- a/tools/clang-check/ClangCheck.cpp
+++ b/tools/clang-check/ClangCheck.cpp
@@ -1,4 +1,4 @@
-//===- examples/Tooling/ClangCheck.cpp - Clang check tool -----------------===//
+//===- tools/clang-check/ClangCheck.cpp - Clang check tool ----------------===//
//
// The LLVM Compiler Infrastructure
//
@@ -10,75 +10,37 @@
// This file implements a clang-check tool that runs the
// clang::SyntaxOnlyAction over a number of translation units.
//
+// This tool uses the Clang Tooling infrastructure, see
+// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
+// for details on setting it up with LLVM source tree.
+//
//===----------------------------------------------------------------------===//
#include "llvm/Support/CommandLine.h"
#include "clang/Frontend/FrontendActions.h"
-#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/CommandLineClangTool.h"
#include "clang/Tooling/Tooling.h"
using namespace clang::tooling;
using namespace llvm;
-cl::opt<std::string> BuildPath(
- "p",
- cl::desc("<build-path>"),
- cl::Optional);
-
-cl::list<std::string> SourcePaths(
- cl::Positional,
- cl::desc("<source0> [... <sourceN>]"),
- cl::OneOrMore);
-
-static cl::extrahelp MoreHelp(
- "\n"
- "<build-path> is used to read a compile command database.\n"
- "\n"
- "For example, it can be a CMake build directory in which a file named\n"
- "compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON\n"
- "CMake option to get this output). When no build path is specified,\n"
- "clang-check will attempt to locate it automatically using all parent\n"
- "paths of the first input file.\n"
- "\n"
- "<source0> ... specify the paths of source files. These paths are looked\n"
- "up in the compile command database. If the path of a file is absolute,\n"
- "it needs to point into CMake's source tree. If the path is relative,\n"
- "the current working directory needs to be in the CMake source tree and\n"
- "the file must be in a subdirectory of the current working directory.\n"
- "\"./\" prefixes in the relative files will be automatically removed,\n"
- "but the rest of a relative path must be a suffix of a path in the\n"
- "compile command database.\n"
- "\n"
- "For example, to use clang-check on all files in a subtree of the source\n"
- "tree, use:\n"
- "\n"
- " find path/in/subtree -name '*.cpp'|xargs clang-check\n"
+static const char *MoreHelpText =
+ "\tFor example, to run clang-check on all files in a subtree of the\n"
+ "\tsource tree, use:\n"
"\n"
- "or using a specific build path:\n"
+ "\t find path/in/subtree -name '*.cpp'|xargs clang-check\n"
"\n"
- " find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
+ "\tor using a specific build path:\n"
"\n"
- "Note, that path/in/subtree and current directory should follow the\n"
- "rules described above.\n"
+ "\t find path/in/subtree -name '*.cpp'|xargs clang-check -p build/path\n"
"\n"
-);
+ "\tNote, that path/in/subtree and current directory should follow the\n"
+ "\trules described above.\n"
+ "\n";
int main(int argc, const char **argv) {
- llvm::OwningPtr<CompilationDatabase> Compilations(
- FixedCompilationDatabase::loadFromCommandLine(argc, argv));
- cl::ParseCommandLineOptions(argc, argv);
- if (!Compilations) {
- std::string ErrorMessage;
- if (!BuildPath.empty()) {
- Compilations.reset(
- CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage));
- } else {
- Compilations.reset(CompilationDatabase::autoDetectFromSource(
- SourcePaths[0], ErrorMessage));
- }
- if (!Compilations)
- llvm::report_fatal_error(ErrorMessage);
- }
- ClangTool Tool(*Compilations, SourcePaths);
+ CommandLineClangTool Tool;
+ cl::extrahelp MoreHelp(MoreHelpText);
+ Tool.initialize(argc, argv);
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
}