From 81e557b1ded0e3249f784172c54728469baad1db Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Mon, 16 May 2011 21:33:46 +0000 Subject: [PATCH] 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 +++----------------------------- include/clang/Tooling/Tooling.h | 61 +++++++++++++++++++++++++ lib/Tooling/Tooling.cpp | 81 +++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 73 deletions(-) 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); } diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 6ccccd0bff..a32147c3eb 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -16,6 +16,8 @@ #define LLVM_CLANG_TOOLING_TOOLING_H #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Support/MemoryBuffer.h" #include #include @@ -75,6 +77,65 @@ CompileCommand FindCompileArgsInJsonDatabase( llvm::StringRef FileName, llvm::StringRef JsonDatabase, std::string &ErrorMessage); +// Interface to generate clang::FrontendActions. +class FrontendActionFactory { + public: + virtual ~FrontendActionFactory(); + + // Returns a new clang::FrontendAction. The caller takes ownership of the + // returned action. + virtual clang::FrontendAction* New() = 0; +}; + +/// \brief Utility to run a FrontendAction over a set of files. +/// +/// This class is written to be usable for command line utilities. +class ClangTool { + public: + /// \brief Construct a clang tool from a command line. + /// + /// This will parse the command line parameters and print an error message + /// and exit the program if the command line does not specify the required + /// parameters. + /// + /// Usage: + /// $ tool-name ... + /// + /// where 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). + /// + /// ... 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 a tool on all files in a subtree of the source + /// tree, use: + /// + /// /path/in/subtree $ find . -name '*.cpp' | + /// xargs tool-name /path/to/source + /// + /// \param argc The argc argument from main. + /// \param argv The argv argument from main. + ClangTool(int argc, char **argv); + + /// Runs a frontend action over all files specified in the command line. + /// + /// \param ActionFactory Factory generating the frontend actions. The function + /// takes ownership of this parameter. A new action is generated for every + /// processed translation unit. + int Run(FrontendActionFactory *ActionFactory); + + private: + std::vector Files; + llvm::OwningPtr JsonDatabase; +}; + } // end namespace tooling } // end namespace clang 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] << " " + << " ...\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(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 -- 2.40.0