From 81e557b1ded0e3249f784172c54728469baad1db Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Mon, 16 May 2011 21:33:46 +0000 Subject: 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 --- examples/Tooling/ClangCheck.cpp | 79 ++++------------------------------------- 1 file changed, 6 insertions(+), 73 deletions(-) (limited to 'examples') diff --git a/examples/Tooling/ClangCheck.cpp b/examples/Tooling/ClangCheck.cpp index db24ec31ec..ad88e023ca 100644 --- a/examples/Tooling/ClangCheck.cpp +++ b/examples/Tooling/ClangCheck.cpp @@ -35,80 +35,13 @@ #include "clang/Frontend/FrontendActions.h" #include "clang/Tooling/Tooling.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/Twine.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/system_error.h" -/// \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. -/// -/// FIXME: Put this somewhere where it is more generally available. -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(); -} +class SyntaxOnlyActionFactory : public clang::tooling::FrontendActionFactory { + public: + virtual clang::FrontendAction *New() { return new clang::SyntaxOnlyAction; } +}; int main(int argc, char **argv) { - if (argc < 3) { - llvm::outs() << "Usage: " << argv[0] << " " - << " ...\n"; - return 1; - } - // FIXME: We should pull how to find the database into the Tooling package. - llvm::OwningPtr JsonDatabase; - 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"; - return 1; - } - llvm::StringRef BaseDirectory(::getenv("PWD")); - for (int I = 2; I < argc; ++I) { - llvm::SmallString<1024> File(GetAbsolutePath(argv[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.size()) { - // FIXME: What should happen if CommandLine includes -working-directory - // as well? - LookupResult.CommandLine.push_back( - "-working-directory=" + LookupResult.Directory); - } - if (!clang::tooling::RunToolWithFlags( - new clang::SyntaxOnlyAction, - LookupResult.CommandLine.size(), - &clang::tooling::CommandLineToArgv( - &LookupResult.CommandLine)[0])) { - llvm::outs() << "Error while processing " << File << ".\n"; - } - } else { - llvm::outs() << "Skipping " << File << ". Command line not found.\n"; - } - } - return 0; + clang::tooling::ClangTool Tool(argc, argv); + return Tool.Run(new SyntaxOnlyActionFactory); } -- cgit v1.2.3-18-g5258