1cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek//===--- Tooling.h - Framework for standalone Clang tools -------*- C++ -*-===// 2cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 3cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// The LLVM Compiler Infrastructure 4cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 5cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// This file is distributed under the University of Illinois Open Source 6cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// License. See LICENSE.TXT for details. 7cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 8cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek//===----------------------------------------------------------------------===// 9cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 10cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// This file implements functions to run clang tools standalone instead 11cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// of running them as a plugin. 12cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 13cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// A ClangTool is initialized with a CompilationDatabase and a set of files 14cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// to run over. The tool will then run a user-specified FrontendAction over 15cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// all TUs in which the given files are compiled. 16cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 17cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// It is also possible to run a FrontendAction over a snippet of code by 18295413422d0c8d70e8861b71f7b622871ba1ba44Manuel Klimek// calling runToolOnCode, which is useful for unit testing. 19cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 20cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// Applications that need more fine grained control over how to run 21cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// multiple FrontendActions over code can use ToolInvocation. 22cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 23cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// Example tools: 24cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// - running clang -fsyntax-only over source code from an editor to get 25cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// fast syntax checks 26cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// - running match/replace tools over C++ code 27cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek// 28cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek//===----------------------------------------------------------------------===// 29cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 30cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#ifndef LLVM_CLANG_TOOLING_TOOLING_H 31cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#define LLVM_CLANG_TOOLING_TOOLING_H 32cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 33fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek#include "clang/Basic/Diagnostic.h" 34cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Basic/FileManager.h" 35cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Basic/LLVM.h" 36cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Driver/Util.h" 37e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek#include "clang/Frontend/FrontendAction.h" 38a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan#include "clang/Tooling/ArgumentsAdjusters.h" 3900f3c4f499cc27c88005176226bb424f6db4a31bManuel Klimek#include "clang/Tooling/CompilationDatabase.h" 4030a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/StringMap.h" 4130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/Twine.h" 426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include <memory> 43cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <string> 44cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <vector> 45cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 46cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace clang { 47cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 48cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace driver { 49cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass Compilation; 50cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace driver 51cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 52cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass CompilerInvocation; 53cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass SourceManager; 54cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass FrontendAction; 55cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 56cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace tooling { 57cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 588051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Interface to process a clang::CompilerInvocation. 598051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// 608051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// If your tool is based on FrontendAction, you should be deriving from 618051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// FrontendActionFactory instead. 628051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass ToolAction { 638051db16aa9513333062ab3145f038429f66780fPeter Collingbournepublic: 648051db16aa9513333062ab3145f038429f66780fPeter Collingbourne virtual ~ToolAction(); 658051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \brief Perform an action for an invocation. 678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne virtual bool runInvocation(clang::CompilerInvocation *Invocation, 68fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek FileManager *Files, 69fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek DiagnosticConsumer *DiagConsumer) = 0; 708051db16aa9513333062ab3145f038429f66780fPeter Collingbourne}; 718051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 72cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Interface to generate clang::FrontendActions. 73233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// 74233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// Having a factory interface allows, for example, a new FrontendAction to be 758051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// created for each translation unit processed by ClangTool. This class is 768051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// also a ToolAction which uses the FrontendActions created by create() to 778051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// process each translation unit. 788051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass FrontendActionFactory : public ToolAction { 79cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekpublic: 80cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek virtual ~FrontendActionFactory(); 81cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 828051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \brief Invokes the compiler with a FrontendAction created by create(). 83fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek bool runInvocation(clang::CompilerInvocation *Invocation, FileManager *Files, 84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DiagnosticConsumer *DiagConsumer) override; 858051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 86cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Returns a new clang::FrontendAction. 87cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 88cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// The caller takes ownership of the returned action. 89cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek virtual clang::FrontendAction *create() = 0; 90cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}; 91cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 92cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for a given type. 93cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 94233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// T must derive from clang::FrontendAction. 95cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 96cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example: 97cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// FrontendActionFactory *Factory = 98cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// newFrontendActionFactory<clang::SyntaxOnlyAction>(); 99cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T> 1006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory(); 101cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 1023c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// \brief Callbacks called before and after each source file processed by a 1033c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// FrontendAction created by the FrontedActionFactory returned by \c 1043c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// newFrontendActionFactory. 1053c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vaneclass SourceFileCallbacks { 1069fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimekpublic: 1073c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane virtual ~SourceFileCallbacks() {} 1083c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane 1093c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane /// \brief Called before a source file is processed by a FrontEndAction. 1103c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane /// \see clang::FrontendAction::BeginSourceFileAction 1115ec9580f68661ffccff07177613b692d0acb4797Edwin Vane virtual bool handleBeginSource(CompilerInstance &CI, StringRef Filename) { 1123c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane return true; 1133c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane } 1143c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane 1153c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane /// \brief Called after a source file is processed by a FrontendAction. 1163c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane /// \see clang::FrontendAction::EndSourceFileAction 1175ec9580f68661ffccff07177613b692d0acb4797Edwin Vane virtual void handleEndSource() {} 1189fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek}; 1199fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek 120cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for any type that provides an 121e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// implementation of newASTConsumer(). 122cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 123e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// FactoryT must implement: ASTConsumer *newASTConsumer(). 124cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 125cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example: 126e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// struct ProvidesASTConsumers { 127e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// clang::ASTConsumer *newASTConsumer(); 128cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// } Factory; 1296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// std::unique_ptr<FrontendActionFactory> FactoryAdapter( 1306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// newFrontendActionFactory(&Factory)); 131cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT> 1326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( 1336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = nullptr); 134cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 135cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag. 136cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 137cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param ToolAction The action to run over the code. 138cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param Code C++ code. 139cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param FileName The file name which 'Code' will be mapped as. 140cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 141cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \return - True if 'ToolAction' was successfully executed. 142cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekbool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, 143cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek const Twine &FileName = "input.cc"); 144cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 145566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and 146566698851d76416129cd20ceea02bdd697934c5cNico Weber/// with additional other flags. 147566698851d76416129cd20ceea02bdd697934c5cNico Weber/// 148566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param ToolAction The action to run over the code. 149566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Code C++ code. 150566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Args Additional flags to pass on. 151566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param FileName The file name which 'Code' will be mapped as. 152566698851d76416129cd20ceea02bdd697934c5cNico Weber/// 153566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \return - True if 'ToolAction' was successfully executed. 154566698851d76416129cd20ceea02bdd697934c5cNico Weberbool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, 155566698851d76416129cd20ceea02bdd697934c5cNico Weber const std::vector<std::string> &Args, 156566698851d76416129cd20ceea02bdd697934c5cNico Weber const Twine &FileName = "input.cc"); 157566698851d76416129cd20ceea02bdd697934c5cNico Weber 1588051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code'. 1598051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// 1608051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code. 1618051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as. 1628051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// 1638051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred. 1646bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code, 1656bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const Twine &FileName = "input.cc"); 1668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 1678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code' with additional flags. 1688051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// 1698051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code. 1708051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Args Additional flags to pass on. 1718051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as. 1728051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// 1738051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred. 1746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit> 1756bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesbuildASTFromCodeWithArgs(const Twine &Code, 1766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const std::vector<std::string> &Args, 1776bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines const Twine &FileName = "input.cc"); 1788051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 179cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction in a single clang invocation. 180cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ToolInvocation { 181cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public: 182cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Create a tool invocation. 183cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 18430c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko /// \param CommandLine The command line arguments to clang. Note that clang 18530c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko /// uses its binary name (CommandLine[0]) to locate its builtin headers. 18630c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko /// Callers have to ensure that they are installed in a compatible location 18730c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko /// (see clang driver implementation) or mapped in via mapVirtualFile. 188f907afed937b45f70df3b0f68b925d699992dfc0NAKAMURA Takumi /// \param FAction The action to be executed. Class takes ownership. 189cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param Files The FileManager used for the execution. Class does not take 190cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// ownership. 191651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction, 192cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek FileManager *Files); 193cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 1948051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \brief Create a tool invocation. 1958051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// 1968051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \param CommandLine The command line arguments to clang. 1978051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \param Action The action to be executed. 1988051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \param Files The FileManager used for the execution. 199651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ToolInvocation(std::vector<std::string> CommandLine, ToolAction *Action, 2008051db16aa9513333062ab3145f038429f66780fPeter Collingbourne FileManager *Files); 2018051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 2028051db16aa9513333062ab3145f038429f66780fPeter Collingbourne ~ToolInvocation(); 2038051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 204fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek /// \brief Set a \c DiagnosticConsumer to use during parsing. 205fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer); 206fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek 207cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Map a virtual file to be used while running the tool. 208cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 209cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param FilePath The path at which the content will be mapped. 210cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param Content A null terminated buffer of the file's content. 211cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek void mapVirtualFile(StringRef FilePath, StringRef Content); 212cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 213cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Run the clang invocation. 214cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 215cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \returns True if there were no errors during execution. 216cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek bool run(); 217cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 218cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private: 219cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek void addFileMappingsTo(SourceManager &SourceManager); 220cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 221cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek bool runInvocation(const char *BinaryName, 222cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek clang::driver::Compilation *Compilation, 223d47afb96a3f988e6d21a92fe4dfe875ab227c7c0Sean Silva clang::CompilerInvocation *Invocation); 224cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 225cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek std::vector<std::string> CommandLine; 2268051db16aa9513333062ab3145f038429f66780fPeter Collingbourne ToolAction *Action; 2278051db16aa9513333062ab3145f038429f66780fPeter Collingbourne bool OwnsAction; 228cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek FileManager *Files; 229cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek // Maps <file name> -> <file content>. 230cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek llvm::StringMap<StringRef> MappedFileContents; 231fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek DiagnosticConsumer *DiagConsumer; 232cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}; 233cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 234cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction over a set of files. 235cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// 236cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// This class is written to be usable for command line utilities. 237a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// By default the class uses ClangSyntaxOnlyAdjuster to modify 238a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// command line arguments before the arguments are used to run 23948b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// a frontend action. One could install an additional command line 24048b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// arguments adjuster by calling the appendArgumentsAdjuster() method. 241cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ClangTool { 242cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public: 243cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Constructs a clang tool to run over a list of files. 244cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 245cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param Compilations The CompilationDatabase which contains the compile 246cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// command lines for the given source paths. 247cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param SourcePaths The source files to run over. If a source files is 248cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// not found in Compilations, it is skipped. 249cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek ClangTool(const CompilationDatabase &Compilations, 250cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek ArrayRef<std::string> SourcePaths); 251cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 25248b3f0f74da575a66802dc5019b7440914983885Manuel Klimek virtual ~ClangTool() { clearArgumentsAdjusters(); } 253d088a5f966c31462280d5ace29febc6889834611Edwin Vane 254fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek /// \brief Set a \c DiagnosticConsumer to use during parsing. 255fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer); 256fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek 257cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Map a virtual file to be used while running the tool. 258cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 259cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param FilePath The path at which the content will be mapped. 260cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \param Content A null terminated buffer of the file's content. 261cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek void mapVirtualFile(StringRef FilePath, StringRef Content); 262cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 263a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan /// \brief Install command line arguments adjuster. 264a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan /// 265a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan /// \param Adjuster Command line arguments adjuster. 26648b3f0f74da575a66802dc5019b7440914983885Manuel Klimek // 26748b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// FIXME: Function is deprecated. Use (clear/append)ArgumentsAdjuster instead. 26848b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// Remove it once all callers are gone. 269a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan void setArgumentsAdjuster(ArgumentsAdjuster *Adjuster); 270a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan 27148b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// \brief Append a command line arguments adjuster to the adjuster chain. 27248b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// 27348b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// \param Adjuster An argument adjuster, which will be run on the output of 27448b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// previous argument adjusters. 27548b3f0f74da575a66802dc5019b7440914983885Manuel Klimek void appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster); 27648b3f0f74da575a66802dc5019b7440914983885Manuel Klimek 27748b3f0f74da575a66802dc5019b7440914983885Manuel Klimek /// \brief Clear the command line arguments adjuster chain. 27848b3f0f74da575a66802dc5019b7440914983885Manuel Klimek void clearArgumentsAdjusters(); 27948b3f0f74da575a66802dc5019b7440914983885Manuel Klimek 2808051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// Runs an action over all files specified in the command line. 281cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 2828051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \param Action Tool action. 2838051db16aa9513333062ab3145f038429f66780fPeter Collingbourne int run(ToolAction *Action); 2848051db16aa9513333062ab3145f038429f66780fPeter Collingbourne 2858051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// \brief Create an AST for each file specified in the command line and 2868051db16aa9513333062ab3145f038429f66780fPeter Collingbourne /// append them to ASTs. 2876bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines int buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs); 288cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 289cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// \brief Returns the file manager used in the tool. 290cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// 291cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek /// The file manager is shared between all translation units. 2928051db16aa9513333062ab3145f038429f66780fPeter Collingbourne FileManager &getFiles() { return *Files; } 293cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 294cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private: 29500f3c4f499cc27c88005176226bb424f6db4a31bManuel Klimek // We store compile commands as pair (file name, compile command). 29600f3c4f499cc27c88005176226bb424f6db4a31bManuel Klimek std::vector< std::pair<std::string, CompileCommand> > CompileCommands; 297cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 2988051db16aa9513333062ab3145f038429f66780fPeter Collingbourne llvm::IntrusiveRefCntPtr<FileManager> Files; 299cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek // Contains a list of pairs (<file name>, <file content>). 300cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek std::vector< std::pair<StringRef, StringRef> > MappedFileContents; 301a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan 30263d5335aea916584b76790af1f1ed7caeaf8efebPavel Labath SmallVector<ArgumentsAdjuster *, 2> ArgsAdjusters; 303fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek 304fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek DiagnosticConsumer *DiagConsumer; 305cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}; 306cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 307cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T> 3086bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory() { 309cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek class SimpleFrontendActionFactory : public FrontendActionFactory { 310cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public: 311651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines clang::FrontendAction *create() override { return new T; } 312cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek }; 313cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 3146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return std::unique_ptr<FrontendActionFactory>( 3156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines new SimpleFrontendActionFactory); 316cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} 317cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 318cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT> 3196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( 3203c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) { 321cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek class FrontendActionFactoryAdapter : public FrontendActionFactory { 322cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public: 3239fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory, 3243c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane SourceFileCallbacks *Callbacks) 3253c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} 326cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 327651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines clang::FrontendAction *create() override { 3283c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks); 329cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek } 330cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 331cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private: 332e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek class ConsumerFactoryAdaptor : public clang::ASTFrontendAction { 333e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek public: 3349fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek ConsumerFactoryAdaptor(FactoryT *ConsumerFactory, 3353c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane SourceFileCallbacks *Callbacks) 3363c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} 337e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek 338e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, 339651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef) override { 340e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek return ConsumerFactory->newASTConsumer(); 341e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek } 342e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek 3439fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek protected: 344651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines bool BeginSourceFileAction(CompilerInstance &CI, 345651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef Filename) override { 3463c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename)) 3473c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane return false; 3486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Callbacks) 3495ec9580f68661ffccff07177613b692d0acb4797Edwin Vane return Callbacks->handleBeginSource(CI, Filename); 3503c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane return true; 3513c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane } 352651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void EndSourceFileAction() override { 3536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines if (Callbacks) 3545ec9580f68661ffccff07177613b692d0acb4797Edwin Vane Callbacks->handleEndSource(); 3559fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek clang::ASTFrontendAction::EndSourceFileAction(); 3569fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek } 3579fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek 358e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek private: 359e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek FactoryT *ConsumerFactory; 3603c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane SourceFileCallbacks *Callbacks; 361e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek }; 362e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek FactoryT *ConsumerFactory; 3633c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane SourceFileCallbacks *Callbacks; 364cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek }; 365cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 3666bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines return std::unique_ptr<FrontendActionFactory>( 3676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines new FrontendActionFactoryAdapter(ConsumerFactory, Callbacks)); 368cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} 369cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 3708fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \brief Returns the absolute path of \c File, by prepending it with 3718fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory if \c File is not absolute. 3728fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// 3738fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise returns \c File. 3748fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// If 'File' starts with "./", the returned path will not contain the "./". 3758fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise, the returned path will contain the literal path-concatenation of 3768fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory and \c File. 3778fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// 378a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// The difference to llvm::sys::fs::make_absolute is the canonicalization this 379a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// does by removing "./" and computing native paths. 3808fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// 3818fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \param File Either an absolute or relative path. 3828fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimekstd::string getAbsolutePath(StringRef File); 3838fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek 384cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace tooling 385cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace clang 386cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek 387cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#endif // LLVM_CLANG_TOOLING_TOOLING_H 388