diff options
author | Manuel Klimek <klimek@google.com> | 2011-05-16 21:33:46 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2011-05-16 21:33:46 +0000 |
commit | 81e557b1ded0e3249f784172c54728469baad1db (patch) | |
tree | 2ad3bb4dd699afb46c115c71f3d3228766499b16 /lib | |
parent | 182564cd14a2105fff05fd52f5940eff96161d57 (diff) |
Pulls the common part of the clang-check example into Tooling, to allow new tools to be implemented without duplicating the boilerplate.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@131425 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Tooling/Tooling.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index c1714a9be7..9cc92f1ae6 100644 --- a/lib/Tooling/Tooling.cpp +++ b/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 |