summaryrefslogtreecommitdiffstats
path: root/clang/lib/Tooling/Tooling.cpp
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2011-05-16 21:33:46 +0000
committerManuel Klimek <klimek@google.com>2011-05-16 21:33:46 +0000
commitc9675152b3b569562c1b6754427b45b8177d3e10 (patch)
tree566a1f89e8cfe4758e82ce170a4dc0bab3f36ad2 /clang/lib/Tooling/Tooling.cpp
parentd4a3609d307137a058d4f1e95947cd222d6e8d5c (diff)
downloadbcm5719-llvm-c9675152b3b569562c1b6754427b45b8177d3e10.tar.gz
bcm5719-llvm-c9675152b3b569562c1b6754427b45b8177d3e10.zip
Pulls the common part of the clang-check example into Tooling, to allow new tools to be implemented without duplicating the boilerplate.
llvm-svn: 131425
Diffstat (limited to 'clang/lib/Tooling/Tooling.cpp')
-rw-r--r--clang/lib/Tooling/Tooling.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index c1714a9be71..9cc92f1ae67 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -21,6 +21,7 @@
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
#include "clang/Basic/DiagnosticIDs.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
@@ -317,6 +318,86 @@ CompileCommand FindCompileArgsInJsonDatabase(
return find_handler.MatchingCommand;
}
+/// \brief Returns the absolute path of 'File', by prepending it with
+/// 'BaseDirectory' if 'File' is not absolute. Otherwise returns 'File'.
+/// If 'File' starts with "./", the returned path will not contain the "./".
+/// Otherwise, the returned path will contain the literal path-concatenation of
+/// 'BaseDirectory' and 'File'.
+///
+/// \param File Either an absolute or relative path.
+/// \param BaseDirectory An absolute path.
+static std::string GetAbsolutePath(
+ llvm::StringRef File, llvm::StringRef BaseDirectory) {
+ assert(llvm::sys::path::is_absolute(BaseDirectory));
+ if (llvm::sys::path::is_absolute(File)) {
+ return File;
+ }
+ llvm::StringRef RelativePath(File);
+ if (RelativePath.startswith("./")) {
+ RelativePath = RelativePath.substr(strlen("./"));
+ }
+ llvm::SmallString<1024> AbsolutePath(BaseDirectory);
+ llvm::sys::path::append(AbsolutePath, RelativePath);
+ return AbsolutePath.str();
+}
+
+FrontendActionFactory::~FrontendActionFactory() {}
+
+ClangTool::ClangTool(int argc, char **argv) {
+ if (argc < 3) {
+ llvm::outs() << "Usage: " << argv[0] << " <cmake-output-dir> "
+ << "<file1> <file2> ...\n";
+ exit(1);
+ }
+ llvm::SmallString<1024> JsonDatabasePath(argv[1]);
+ llvm::sys::path::append(JsonDatabasePath, "compile_commands.json");
+ llvm::error_code Result =
+ llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase);
+ if (Result != 0) {
+ llvm::outs() << "Error while opening JSON database: " << Result.message()
+ << "\n";
+ exit(1);
+ }
+ Files = std::vector<std::string>(argv + 2, argv + argc);
+}
+
+int ClangTool::Run(FrontendActionFactory *ActionFactory) {
+ llvm::StringRef BaseDirectory(::getenv("PWD"));
+ bool ProcessingFailed = false;
+ for (unsigned I = 0; I < Files.size(); ++I) {
+ llvm::SmallString<1024> File(GetAbsolutePath(Files[I], BaseDirectory));
+ llvm::outs() << "Processing " << File << ".\n";
+ std::string ErrorMessage;
+ clang::tooling::CompileCommand LookupResult =
+ clang::tooling::FindCompileArgsInJsonDatabase(
+ File.str(), JsonDatabase->getBuffer(), ErrorMessage);
+ if (!LookupResult.CommandLine.empty()) {
+ if (!LookupResult.Directory.empty()) {
+ // FIXME: What should happen if CommandLine includes -working-directory
+ // as well?
+ LookupResult.CommandLine.push_back(
+ "-working-directory=" + LookupResult.Directory);
+ }
+ if (!clang::tooling::RunToolWithFlags(
+ ActionFactory->New(),
+ LookupResult.CommandLine.size(),
+ &clang::tooling::CommandLineToArgv(
+ &LookupResult.CommandLine)[0])) {
+ llvm::outs() << "Error while processing " << File << ".\n";
+ ProcessingFailed = true;
+ }
+ } else {
+ // FIXME: There are two use cases here: doing a fuzzy
+ // "find . -name '*.cc' |xargs tool" match, where as a user I don't care
+ // about the .cc files that were not found, and the use case where I
+ // specify all files I want to run over explicitly, where this should
+ // be an error. We'll want to add an option for this.
+ llvm::outs() << "Skipping " << File << ". Command line not found.\n";
+ }
+ }
+ return ProcessingFailed ? 1 : 0;
+}
+
} // end namespace tooling
} // end namespace clang
OpenPOWER on IntegriCloud