summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2012-04-04 12:07:46 +0000
committerManuel Klimek <klimek@google.com>2012-04-04 12:07:46 +0000
commitcb971c6726d16e12ecd2a340941d7f5c06698332 (patch)
tree63f54a02c06faaf2297917a6e475852f1e959747 /tools
parentc9aa9c00fc99ded37a064d607b71815484e20652 (diff)
Adds a tooling library.
Provides an API to run clang tools (FrontendActions) as standalone tools, or repeatedly in-memory in a process. This is useful for unit-testing, map-reduce style applications, source transformation daemons or command line tools. The ability to run over multiple translation units with different command line arguments enables building up refactoring tools that need to apply transformations across translation unit boundaries. See tools/clang-check/ClangCheck.cpp for an example. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154008 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/CMakeLists.txt1
-rw-r--r--tools/Makefile3
-rw-r--r--tools/clang-check/CMakeLists.txt5
-rw-r--r--tools/clang-check/ClangCheck.cpp62
-rw-r--r--tools/clang-check/Makefile24
5 files changed, 94 insertions, 1 deletions
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 117d10a603..ab4748d1b9 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -4,3 +4,4 @@ add_subdirectory(arcmt-test)
add_subdirectory(c-arcmt-test)
add_subdirectory(diagtool)
add_subdirectory(driver)
+add_subdirectory(clang-check)
diff --git a/tools/Makefile b/tools/Makefile
index f647f56a51..5059ade930 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -8,7 +8,8 @@
##===----------------------------------------------------------------------===##
CLANG_LEVEL := ..
-DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool
+DIRS := driver libclang c-index-test arcmt-test c-arcmt-test diagtool \
+ clang-check
include $(CLANG_LEVEL)/../../Makefile.config
diff --git a/tools/clang-check/CMakeLists.txt b/tools/clang-check/CMakeLists.txt
new file mode 100644
index 0000000000..851d6cdd16
--- /dev/null
+++ b/tools/clang-check/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_USED_LIBS clangTooling clangBasic)
+
+add_clang_executable(clang-check
+ ClangCheck.cpp
+ )
diff --git a/tools/clang-check/ClangCheck.cpp b/tools/clang-check/ClangCheck.cpp
new file mode 100644
index 0000000000..b5b6bd5a14
--- /dev/null
+++ b/tools/clang-check/ClangCheck.cpp
@@ -0,0 +1,62 @@
+//===- examples/Tooling/ClangCheck.cpp - Clang check 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-check tool that runs the
+// clang::SyntaxOnlyAction over a number of translation units.
+//
+// Usage:
+// clang-check <cmake-output-dir> <file1> <file2> ...
+//
+// Where <cmake-output-dir> is a CMake build directory in which a file named
+// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
+// CMake to get this output).
+//
+// <file1> ... specify the paths of files in the CMake source tree. This path
+// is looked up in the compile command database. If the path of a file is
+// absolute, it needs to point into CMake's source tree. If the path is
+// relative, the current working directory needs to be in the CMake source
+// tree and the file must be in a subdirectory of the current working
+// directory. "./" prefixes in the relative files will be automatically
+// removed, but the rest of a relative path must be a suffix of a path in
+// the compile command line database.
+//
+// For example, to use clang-check on all files in a subtree of the source
+// tree, use:
+//
+// /path/in/subtree $ find . -name '*.cpp'| xargs clang-check /path/to/source
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CommandLine.h"
+#include "clang/Frontend/FrontendActions.h"
+#include "clang/Tooling/CompilationDatabase.h"
+#include "clang/Tooling/Tooling.h"
+
+using namespace clang::tooling;
+using namespace llvm;
+
+cl::opt<std::string> BuildPath(
+ cl::Positional,
+ cl::desc("<build-path>"));
+
+cl::list<std::string> SourcePaths(
+ cl::Positional,
+ cl::desc("<source0> [... <sourceN>]"),
+ cl::OneOrMore);
+
+int main(int argc, char **argv) {
+ cl::ParseCommandLineOptions(argc, argv);
+ std::string ErrorMessage;
+ llvm::OwningPtr<CompilationDatabase> Compilations(
+ CompilationDatabase::loadFromDirectory(BuildPath, ErrorMessage));
+ if (!Compilations)
+ llvm::report_fatal_error(ErrorMessage);
+ ClangTool Tool(*Compilations, SourcePaths);
+ return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>());
+}
diff --git a/tools/clang-check/Makefile b/tools/clang-check/Makefile
new file mode 100644
index 0000000000..11dc05e670
--- /dev/null
+++ b/tools/clang-check/Makefile
@@ -0,0 +1,24 @@
+##===- 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-check
+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 \
+ clangAST.a clangLex.a clangBasic.a
+
+include $(CLANG_LEVEL)/Makefile
+