Tooling.h revision 0e2c34f92f00628d48968dfea096d36381f494cb
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
33176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#include "clang/AST/ASTConsumer.h"
34fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek#include "clang/Basic/Diagnostic.h"
35cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Basic/FileManager.h"
36cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Basic/LLVM.h"
37cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include "clang/Driver/Util.h"
38e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek#include "clang/Frontend/FrontendAction.h"
39a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan#include "clang/Tooling/ArgumentsAdjusters.h"
4000f3c4f499cc27c88005176226bb424f6db4a31bManuel Klimek#include "clang/Tooling/CompilationDatabase.h"
4130a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/StringMap.h"
4230a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "llvm/ADT/Twine.h"
436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include <memory>
44cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <string>
45cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <vector>
46cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
47cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace clang {
48cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
49cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace driver {
50cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass Compilation;
51cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace driver
52cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
53cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass CompilerInvocation;
54cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass SourceManager;
55cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass FrontendAction;
56cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
57cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace tooling {
58cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
598051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Interface to process a clang::CompilerInvocation.
608051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
618051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// If your tool is based on FrontendAction, you should be deriving from
628051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// FrontendActionFactory instead.
638051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass ToolAction {
648051db16aa9513333062ab3145f038429f66780fPeter Collingbournepublic:
658051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  virtual ~ToolAction();
668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Perform an action for an invocation.
688051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  virtual bool runInvocation(clang::CompilerInvocation *Invocation,
69fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek                             FileManager *Files,
70fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek                             DiagnosticConsumer *DiagConsumer) = 0;
718051db16aa9513333062ab3145f038429f66780fPeter Collingbourne};
728051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
73cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Interface to generate clang::FrontendActions.
74233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit///
75233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// Having a factory interface allows, for example, a new FrontendAction to be
768051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// created for each translation unit processed by ClangTool.  This class is
778051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// also a ToolAction which uses the FrontendActions created by create() to
788051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// process each translation unit.
798051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass FrontendActionFactory : public ToolAction {
80cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekpublic:
81cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  virtual ~FrontendActionFactory();
82cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
838051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Invokes the compiler with a FrontendAction created by create().
84fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  bool runInvocation(clang::CompilerInvocation *Invocation, FileManager *Files,
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     DiagnosticConsumer *DiagConsumer) override;
868051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
87cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Returns a new clang::FrontendAction.
88cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
89cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// The caller takes ownership of the returned action.
90cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  virtual clang::FrontendAction *create() = 0;
91cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
92cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
93cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for a given type.
94cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
95233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// T must derive from clang::FrontendAction.
96cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
97cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example:
98cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// FrontendActionFactory *Factory =
99cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///   newFrontendActionFactory<clang::SyntaxOnlyAction>();
100cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T>
1016bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory();
102cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
1033c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// \brief Callbacks called before and after each source file processed by a
1043c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// FrontendAction created by the FrontedActionFactory returned by \c
1053c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// newFrontendActionFactory.
1063c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vaneclass SourceFileCallbacks {
1079fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimekpublic:
1083c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  virtual ~SourceFileCallbacks() {}
1093c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane
1103c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \brief Called before a source file is processed by a FrontEndAction.
1113c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \see clang::FrontendAction::BeginSourceFileAction
1125ec9580f68661ffccff07177613b692d0acb4797Edwin Vane  virtual bool handleBeginSource(CompilerInstance &CI, StringRef Filename) {
1133c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    return true;
1143c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  }
1153c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane
1163c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \brief Called after a source file is processed by a FrontendAction.
1173c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \see clang::FrontendAction::EndSourceFileAction
1185ec9580f68661ffccff07177613b692d0acb4797Edwin Vane  virtual void handleEndSource() {}
1199fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek};
1209fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek
121cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for any type that provides an
122e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// implementation of newASTConsumer().
123cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
124e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// FactoryT must implement: ASTConsumer *newASTConsumer().
125cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
126cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example:
127e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// struct ProvidesASTConsumers {
128e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek///   clang::ASTConsumer *newASTConsumer();
129cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// } Factory;
1306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// std::unique_ptr<FrontendActionFactory> FactoryAdapter(
1316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///   newFrontendActionFactory(&Factory));
132cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT>
1336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
1346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = nullptr);
135cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
136cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
137cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
138cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param ToolAction The action to run over the code.
139cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param Code C++ code.
140cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param FileName The file name which 'Code' will be mapped as.
141cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
142cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \return - True if 'ToolAction' was successfully executed.
143cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekbool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
144cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                   const Twine &FileName = "input.cc");
145cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
1460e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// The first part of the pair is the filename, the second part the
1470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// file-content.
1480e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestypedef std::vector<std::pair<std::string, std::string>> FileContentMappings;
1490e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
150566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and
151566698851d76416129cd20ceea02bdd697934c5cNico Weber///        with additional other flags.
152566698851d76416129cd20ceea02bdd697934c5cNico Weber///
153566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param ToolAction The action to run over the code.
154566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Code C++ code.
155566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Args Additional flags to pass on.
156566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param FileName The file name which 'Code' will be mapped as.
157566698851d76416129cd20ceea02bdd697934c5cNico Weber///
158566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \return - True if 'ToolAction' was successfully executed.
1590e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesbool runToolOnCodeWithArgs(
1600e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    clang::FrontendAction *ToolAction, const Twine &Code,
1610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
1620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    const FileContentMappings &VirtualMappedFiles = FileContentMappings());
163566698851d76416129cd20ceea02bdd697934c5cNico Weber
1648051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code'.
1658051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code.
1678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as.
1688051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1698051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred.
1706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code,
1716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          const Twine &FileName = "input.cc");
1728051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
1738051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code' with additional flags.
1748051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1758051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code.
1768051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Args Additional flags to pass on.
1778051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as.
1788051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1798051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred.
1806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit>
1816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesbuildASTFromCodeWithArgs(const Twine &Code,
1826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         const std::vector<std::string> &Args,
1836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         const Twine &FileName = "input.cc");
1848051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
185cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction in a single clang invocation.
186cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ToolInvocation {
187cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public:
188cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Create a tool invocation.
189cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
19030c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// \param CommandLine The command line arguments to clang. Note that clang
19130c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// uses its binary name (CommandLine[0]) to locate its builtin headers.
19230c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// Callers have to ensure that they are installed in a compatible location
19330c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// (see clang driver implementation) or mapped in via mapVirtualFile.
194f907afed937b45f70df3b0f68b925d699992dfc0NAKAMURA Takumi  /// \param FAction The action to be executed. Class takes ownership.
195cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Files The FileManager used for the execution. Class does not take
196cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// ownership.
197651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction,
198cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                 FileManager *Files);
199cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
2008051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Create a tool invocation.
2018051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ///
2028051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param CommandLine The command line arguments to clang.
2038051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Action The action to be executed.
2048051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Files The FileManager used for the execution.
205651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ToolInvocation(std::vector<std::string> CommandLine, ToolAction *Action,
2068051db16aa9513333062ab3145f038429f66780fPeter Collingbourne                 FileManager *Files);
2078051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
2088051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ~ToolInvocation();
2098051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
210fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  /// \brief Set a \c DiagnosticConsumer to use during parsing.
211176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
212176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    this->DiagConsumer = DiagConsumer;
213176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
214fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
215cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Map a virtual file to be used while running the tool.
216cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
217cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param FilePath The path at which the content will be mapped.
218cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Content A null terminated buffer of the file's content.
219cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void mapVirtualFile(StringRef FilePath, StringRef Content);
220cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
221cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Run the clang invocation.
222cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
223cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \returns True if there were no errors during execution.
224cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  bool run();
225cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
226cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private:
227cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void addFileMappingsTo(SourceManager &SourceManager);
228cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
229cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  bool runInvocation(const char *BinaryName,
230cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                     clang::driver::Compilation *Compilation,
231d47afb96a3f988e6d21a92fe4dfe875ab227c7c0Sean Silva                     clang::CompilerInvocation *Invocation);
232cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
233cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  std::vector<std::string> CommandLine;
2348051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ToolAction *Action;
2358051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  bool OwnsAction;
236cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  FileManager *Files;
237cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  // Maps <file name> -> <file content>.
238cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  llvm::StringMap<StringRef> MappedFileContents;
239fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  DiagnosticConsumer *DiagConsumer;
240cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
241cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
242cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction over a set of files.
243cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
244cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// This class is written to be usable for command line utilities.
245a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// By default the class uses ClangSyntaxOnlyAdjuster to modify
246a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// command line arguments before the arguments are used to run
24748b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// a frontend action. One could install an additional command line
24848b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// arguments adjuster by calling the appendArgumentsAdjuster() method.
249cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ClangTool {
250cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public:
251cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Constructs a clang tool to run over a list of files.
252cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
253cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Compilations The CompilationDatabase which contains the compile
254cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///        command lines for the given source paths.
255cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param SourcePaths The source files to run over. If a source files is
256cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///        not found in Compilations, it is skipped.
257cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ClangTool(const CompilationDatabase &Compilations,
258cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek            ArrayRef<std::string> SourcePaths);
259cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
260176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ~ClangTool();
261d088a5f966c31462280d5ace29febc6889834611Edwin Vane
262fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  /// \brief Set a \c DiagnosticConsumer to use during parsing.
263176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    this->DiagConsumer = DiagConsumer;
265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
266fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
267cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Map a virtual file to be used while running the tool.
268cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
269cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param FilePath The path at which the content will be mapped.
270cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Content A null terminated buffer of the file's content.
271cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void mapVirtualFile(StringRef FilePath, StringRef Content);
272cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
27348b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \brief Append a command line arguments adjuster to the adjuster chain.
27448b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  ///
27548b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \param Adjuster An argument adjuster, which will be run on the output of
27648b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  ///        previous argument adjusters.
2770e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
27848b3f0f74da575a66802dc5019b7440914983885Manuel Klimek
27948b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \brief Clear the command line arguments adjuster chain.
28048b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  void clearArgumentsAdjusters();
28148b3f0f74da575a66802dc5019b7440914983885Manuel Klimek
2828051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// Runs an action over all files specified in the command line.
283cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
2848051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Action Tool action.
2858051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  int run(ToolAction *Action);
2868051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
2878051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Create an AST for each file specified in the command line and
2888051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// append them to ASTs.
2896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  int buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs);
290cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
291cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Returns the file manager used in the tool.
292cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
293cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// The file manager is shared between all translation units.
2948051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  FileManager &getFiles() { return *Files; }
295cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
296cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private:
297176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  const CompilationDatabase &Compilations;
298176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::vector<std::string> SourcePaths;
299cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3008051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  llvm::IntrusiveRefCntPtr<FileManager> Files;
301cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  // Contains a list of pairs (<file name>, <file content>).
302cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  std::vector< std::pair<StringRef, StringRef> > MappedFileContents;
303a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan
3040e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  ArgumentsAdjuster ArgsAdjuster;
305fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
306fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  DiagnosticConsumer *DiagConsumer;
307cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
308cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
309cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T>
3106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
311cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  class SimpleFrontendActionFactory : public FrontendActionFactory {
312cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  public:
313651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    clang::FrontendAction *create() override { return new T; }
314cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  };
315cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return std::unique_ptr<FrontendActionFactory>(
3176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      new SimpleFrontendActionFactory);
318cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}
319cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
320cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT>
3216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
3223c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) {
323cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  class FrontendActionFactoryAdapter : public FrontendActionFactory {
324cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  public:
3259fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek    explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory,
3263c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane                                          SourceFileCallbacks *Callbacks)
3273c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
328cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
329651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    clang::FrontendAction *create() override {
3303c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
331cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek    }
332cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
333cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  private:
334e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
335e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    public:
3369fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek      ConsumerFactoryAdaptor(FactoryT *ConsumerFactory,
3373c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane                             SourceFileCallbacks *Callbacks)
3383c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
339e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek
340176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      std::unique_ptr<clang::ASTConsumer>
341176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CreateASTConsumer(clang::CompilerInstance &, StringRef) override {
342e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek        return ConsumerFactory->newASTConsumer();
343e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek      }
344e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek
3459fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek    protected:
346651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool BeginSourceFileAction(CompilerInstance &CI,
347651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 StringRef Filename) override {
3483c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename))
3493c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane          return false;
3506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Callbacks)
3515ec9580f68661ffccff07177613b692d0acb4797Edwin Vane          return Callbacks->handleBeginSource(CI, Filename);
3523c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        return true;
3533c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      }
354651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      void EndSourceFileAction() override {
3556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Callbacks)
3565ec9580f68661ffccff07177613b692d0acb4797Edwin Vane          Callbacks->handleEndSource();
3579fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek        clang::ASTFrontendAction::EndSourceFileAction();
3589fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek      }
3599fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek
360e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    private:
361e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek      FactoryT *ConsumerFactory;
3623c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      SourceFileCallbacks *Callbacks;
363e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    };
364e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    FactoryT *ConsumerFactory;
3653c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    SourceFileCallbacks *Callbacks;
366cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  };
367cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return std::unique_ptr<FrontendActionFactory>(
3696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      new FrontendActionFactoryAdapter(ConsumerFactory, Callbacks));
370cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}
371cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3728fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \brief Returns the absolute path of \c File, by prepending it with
3738fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory if \c File is not absolute.
3748fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
3758fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise returns \c File.
3768fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// If 'File' starts with "./", the returned path will not contain the "./".
3778fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise, the returned path will contain the literal path-concatenation of
3788fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory and \c File.
3798fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
380a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// The difference to llvm::sys::fs::make_absolute is the canonicalization this
381a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// does by removing "./" and computing native paths.
3828fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
3838fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \param File Either an absolute or relative path.
3848fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimekstd::string getAbsolutePath(StringRef File);
3858fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek
386cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace tooling
387cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace clang
388cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
389cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#endif // LLVM_CLANG_TOOLING_TOOLING_H
390