//===- Tooling.h - Framework for standalone Clang tools ---------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements functions to run clang tools standalone instead // of running them as a plugin. // // A ClangTool is initialized with a CompilationDatabase and a set of files // to run over. The tool will then run a user-specified FrontendAction over // all TUs in which the given files are compiled. // // It is also possible to run a FrontendAction over a snippet of code by // calling runToolOnCode, which is useful for unit testing. // // Applications that need more fine grained control over how to run // multiple FrontendActions over code can use ToolInvocation. // // Example tools: // - running clang -fsyntax-only over source code from an editor to get // fast syntax checks // - running match/replace tools over C++ code // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLING_TOOLING_H #define LLVM_CLANG_TOOLING_TOOLING_H #include "clang/AST/ASTConsumer.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/LLVM.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/PCHContainerOperations.h" #include "clang/Tooling/ArgumentsAdjusters.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/IntrusiveRefCntPtr.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Twine.h" #include "llvm/Option/Option.h" #include "llvm/Support/VirtualFileSystem.h" #include <memory> #include <string> #include <utility> #include <vector> namespace clang { class CompilerInstance; class CompilerInvocation; class DiagnosticConsumer; class DiagnosticsEngine; namespace driver { class Compilation; } // namespace driver namespace tooling { class CompilationDatabase; /// Retrieves the flags of the `-cc1` job in `Compilation` that has only source /// files as its inputs. /// Returns nullptr if there are no such jobs or multiple of them. Note that /// offloading jobs are ignored. const llvm::opt::ArgStringList * getCC1Arguments(DiagnosticsEngine *Diagnostics, driver::Compilation *Compilation); /// Interface to process a clang::CompilerInvocation. /// /// If your tool is based on FrontendAction, you should be deriving from /// FrontendActionFactory instead. class ToolAction { … }; /// Interface to generate clang::FrontendActions. /// /// Having a factory interface allows, for example, a new FrontendAction to be /// created for each translation unit processed by ClangTool. This class is /// also a ToolAction which uses the FrontendActions created by create() to /// process each translation unit. class FrontendActionFactory : public ToolAction { … }; /// Returns a new FrontendActionFactory for a given type. /// /// T must derive from clang::FrontendAction. /// /// Example: /// std::unique_ptr<FrontendActionFactory> Factory = /// newFrontendActionFactory<clang::SyntaxOnlyAction>(); template <typename T> std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(); /// Callbacks called before and after each source file processed by a /// FrontendAction created by the FrontedActionFactory returned by \c /// newFrontendActionFactory. class SourceFileCallbacks { … }; /// Returns a new FrontendActionFactory for any type that provides an /// implementation of newASTConsumer(). /// /// FactoryT must implement: ASTConsumer *newASTConsumer(). /// /// Example: /// struct ProvidesASTConsumers { /// std::unique_ptr<clang::ASTConsumer> newASTConsumer(); /// } Factory; /// std::unique_ptr<FrontendActionFactory> FactoryAdapter( /// newFrontendActionFactory(&Factory)); template <typename FactoryT> inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = nullptr); /// Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag. /// /// \param ToolAction The action to run over the code. /// \param Code C++ code. /// \param FileName The file name which 'Code' will be mapped as. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. /// /// \return - True if 'ToolAction' was successfully executed. bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, const Twine &FileName = "input.cc", std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); /// The first part of the pair is the filename, the second part the /// file-content. FileContentMappings; /// Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and /// with additional other flags. /// /// \param ToolAction The action to run over the code. /// \param Code C++ code. /// \param Args Additional flags to pass on. /// \param FileName The file name which 'Code' will be mapped as. /// \param ToolName The name of the binary running the tool. Standard library /// header paths will be resolved relative to this. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. /// /// \return - True if 'ToolAction' was successfully executed. bool runToolOnCodeWithArgs( std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, const std::vector<std::string> &Args, const Twine &FileName = "input.cc", const Twine &ToolName = "clang-tool", std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>(), const FileContentMappings &VirtualMappedFiles = FileContentMappings()); // Similar to the overload except this takes a VFS. bool runToolOnCodeWithArgs( std::unique_ptr<FrontendAction> ToolAction, const Twine &Code, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, const std::vector<std::string> &Args, const Twine &FileName = "input.cc", const Twine &ToolName = "clang-tool", std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); /// Builds an AST for 'Code'. /// /// \param Code C++ code. /// \param FileName The file name which 'Code' will be mapped as. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. /// /// \return The resulting AST or null if an error occurred. std::unique_ptr<ASTUnit> buildASTFromCode(StringRef Code, StringRef FileName = "input.cc", std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>()); /// Builds an AST for 'Code' with additional flags. /// /// \param Code C++ code. /// \param Args Additional flags to pass on. /// \param FileName The file name which 'Code' will be mapped as. /// \param ToolName The name of the binary running the tool. Standard library /// header paths will be resolved relative to this. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. /// /// \param Adjuster A function to filter the command line arguments as specified. /// /// \return The resulting AST or null if an error occurred. std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( StringRef Code, const std::vector<std::string> &Args, StringRef FileName = "input.cc", StringRef ToolName = "clang-tool", std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>(), ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(), const FileContentMappings &VirtualMappedFiles = FileContentMappings(), DiagnosticConsumer *DiagConsumer = nullptr); /// Utility to run a FrontendAction in a single clang invocation. class ToolInvocation { … }; /// Utility to run a FrontendAction over a set of files. /// /// This class is written to be usable for command line utilities. /// By default the class uses ClangSyntaxOnlyAdjuster to modify /// command line arguments before the arguments are used to run /// a frontend action. One could install an additional command line /// arguments adjuster by calling the appendArgumentsAdjuster() method. class ClangTool { … }; template <typename T> std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() { … } template <typename FactoryT> inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) { … } /// Returns the absolute path of \c File, by prepending it with /// the current directory if \c File is not absolute. /// /// Otherwise returns \c File. /// If 'File' starts with "./", the returned path will not contain the "./". /// Otherwise, the returned path will contain the literal path-concatenation of /// the current directory and \c File. /// /// The difference to llvm::sys::fs::make_absolute is the canonicalization this /// does by removing "./" and computing native paths. /// /// \param File Either an absolute or relative path. std::string getAbsolutePath(StringRef File); /// An overload of getAbsolutePath that works over the provided \p FS. llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS, StringRef File); /// Changes CommandLine to contain implicit flags that would have been /// defined had the compiler driver been invoked through the path InvokedAs. /// /// For example, when called with \c InvokedAs set to `i686-linux-android-g++`, /// the arguments '-target', 'i686-linux-android`, `--driver-mode=g++` will /// be inserted after the first argument in \c CommandLine. /// /// This function will not add new `-target` or `--driver-mode` flags if they /// are already present in `CommandLine` (even if they have different settings /// than would have been inserted). /// /// \pre `llvm::InitializeAllTargets()` has been called. /// /// \param CommandLine the command line used to invoke the compiler driver or /// Clang tool, including the path to the executable as \c CommandLine[0]. /// \param InvokedAs the path to the driver used to infer implicit flags. /// /// \note This will not set \c CommandLine[0] to \c InvokedAs. The tooling /// infrastructure expects that CommandLine[0] is a tool path relative to which /// the builtin headers can be found. void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, StringRef InvokedAs); /// Helper function that expands response files in command line. void addExpandedResponseFiles(std::vector<std::string> &CommandLine, llvm::StringRef WorkingDir, llvm::cl::TokenizerCallback Tokenizer, llvm::vfs::FileSystem &FS); /// Creates a \c CompilerInvocation. CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics, ArrayRef<const char *> CC1Args, const char *const BinaryName); } // namespace tooling } // namespace clang #endif // LLVM_CLANG_TOOLING_TOOLING_H