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"
4333337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar#include "llvm/Option/Option.h"
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#include <memory>
45cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <string>
46cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#include <vector>
47cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
48cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace clang {
49cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
50cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace driver {
51cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass Compilation;
52cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace driver
53cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
54cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass CompilerInvocation;
55cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass SourceManager;
56cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass FrontendAction;
57cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
58cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimeknamespace tooling {
59cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
608051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Interface to process a clang::CompilerInvocation.
618051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
628051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// If your tool is based on FrontendAction, you should be deriving from
638051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// FrontendActionFactory instead.
648051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass ToolAction {
658051db16aa9513333062ab3145f038429f66780fPeter Collingbournepublic:
668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  virtual ~ToolAction();
678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
688051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Perform an action for an invocation.
698051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  virtual bool runInvocation(clang::CompilerInvocation *Invocation,
70fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek                             FileManager *Files,
71fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek                             DiagnosticConsumer *DiagConsumer) = 0;
728051db16aa9513333062ab3145f038429f66780fPeter Collingbourne};
738051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
74cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Interface to generate clang::FrontendActions.
75233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit///
76233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// Having a factory interface allows, for example, a new FrontendAction to be
778051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// created for each translation unit processed by ClangTool.  This class is
788051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// also a ToolAction which uses the FrontendActions created by create() to
798051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// process each translation unit.
808051db16aa9513333062ab3145f038429f66780fPeter Collingbourneclass FrontendActionFactory : public ToolAction {
81cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekpublic:
8233337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar  ~FrontendActionFactory() override;
83cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
848051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Invokes the compiler with a FrontendAction created by create().
85fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  bool runInvocation(clang::CompilerInvocation *Invocation, FileManager *Files,
86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                     DiagnosticConsumer *DiagConsumer) override;
878051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
88cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Returns a new clang::FrontendAction.
89cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
90cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// The caller takes ownership of the returned action.
91cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  virtual clang::FrontendAction *create() = 0;
92cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
93cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
94cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for a given type.
95cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
96233fbe1f56183878167a56be53425a6dd25ba334Stefanus Du Toit/// T must derive from clang::FrontendAction.
97cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
98cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example:
99cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// FrontendActionFactory *Factory =
100cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///   newFrontendActionFactory<clang::SyntaxOnlyAction>();
101cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T>
1026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory();
103cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
1043c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// \brief Callbacks called before and after each source file processed by a
1053c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// FrontendAction created by the FrontedActionFactory returned by \c
1063c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane/// newFrontendActionFactory.
1073c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vaneclass SourceFileCallbacks {
1089fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimekpublic:
1093c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  virtual ~SourceFileCallbacks() {}
1103c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane
1113c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \brief Called before a source file is processed by a FrontEndAction.
1123c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \see clang::FrontendAction::BeginSourceFileAction
1135ec9580f68661ffccff07177613b692d0acb4797Edwin Vane  virtual bool handleBeginSource(CompilerInstance &CI, StringRef Filename) {
1143c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    return true;
1153c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  }
1163c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane
1173c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \brief Called after a source file is processed by a FrontendAction.
1183c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane  /// \see clang::FrontendAction::EndSourceFileAction
1195ec9580f68661ffccff07177613b692d0acb4797Edwin Vane  virtual void handleEndSource() {}
1209fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek};
1219fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek
122cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Returns a new FrontendActionFactory for any type that provides an
123e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// implementation of newASTConsumer().
124cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
125e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// FactoryT must implement: ASTConsumer *newASTConsumer().
126cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
127cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// Example:
128e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek/// struct ProvidesASTConsumers {
129e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek///   clang::ASTConsumer *newASTConsumer();
130cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// } Factory;
1316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// std::unique_ptr<FrontendActionFactory> FactoryAdapter(
1326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///   newFrontendActionFactory(&Factory));
133cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT>
1346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
1356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = nullptr);
136cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
137cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
138cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
139cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param ToolAction The action to run over the code.
140cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param Code C++ code.
141cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \param FileName The file name which 'Code' will be mapped as.
142cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
143cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \return - True if 'ToolAction' was successfully executed.
144cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekbool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
145cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                   const Twine &FileName = "input.cc");
146cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
1470e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// The first part of the pair is the filename, the second part the
1480e2c34f92f00628d48968dfea096d36381f494cbStephen Hines/// file-content.
1490e2c34f92f00628d48968dfea096d36381f494cbStephen Hinestypedef std::vector<std::pair<std::string, std::string>> FileContentMappings;
1500e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
151566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and
152566698851d76416129cd20ceea02bdd697934c5cNico Weber///        with additional other flags.
153566698851d76416129cd20ceea02bdd697934c5cNico Weber///
154566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param ToolAction The action to run over the code.
155566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Code C++ code.
156566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param Args Additional flags to pass on.
157566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \param FileName The file name which 'Code' will be mapped as.
158566698851d76416129cd20ceea02bdd697934c5cNico Weber///
159566698851d76416129cd20ceea02bdd697934c5cNico Weber/// \return - True if 'ToolAction' was successfully executed.
1600e2c34f92f00628d48968dfea096d36381f494cbStephen Hinesbool runToolOnCodeWithArgs(
1610e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    clang::FrontendAction *ToolAction, const Twine &Code,
1620e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
1630e2c34f92f00628d48968dfea096d36381f494cbStephen Hines    const FileContentMappings &VirtualMappedFiles = FileContentMappings());
164566698851d76416129cd20ceea02bdd697934c5cNico Weber
1658051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code'.
1668051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1678051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code.
1688051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as.
1698051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1708051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred.
1716bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code,
1726bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          const Twine &FileName = "input.cc");
1738051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
1748051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \brief Builds an AST for 'Code' with additional flags.
1758051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1768051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Code C++ code.
1778051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param Args Additional flags to pass on.
1788051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \param FileName The file name which 'Code' will be mapped as.
1798051db16aa9513333062ab3145f038429f66780fPeter Collingbourne///
1808051db16aa9513333062ab3145f038429f66780fPeter Collingbourne/// \return The resulting AST or null if an error occurred.
1816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<ASTUnit>
1826bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesbuildASTFromCodeWithArgs(const Twine &Code,
1836bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         const std::vector<std::string> &Args,
1846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                         const Twine &FileName = "input.cc");
1858051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
186cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction in a single clang invocation.
187cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ToolInvocation {
188cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public:
189cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Create a tool invocation.
190cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
19130c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// \param CommandLine The command line arguments to clang. Note that clang
19230c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// uses its binary name (CommandLine[0]) to locate its builtin headers.
19330c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// Callers have to ensure that they are installed in a compatible location
19430c009b5fa4d22ddabfe6ad6ba4158a664a4bd83Alexander Kornienko  /// (see clang driver implementation) or mapped in via mapVirtualFile.
195f907afed937b45f70df3b0f68b925d699992dfc0NAKAMURA Takumi  /// \param FAction The action to be executed. Class takes ownership.
196cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Files The FileManager used for the execution. Class does not take
197cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// ownership.
198651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction,
199cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                 FileManager *Files);
200cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
2018051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Create a tool invocation.
2028051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ///
2038051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param CommandLine The command line arguments to clang.
2048051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Action The action to be executed.
2058051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Files The FileManager used for the execution.
206651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ToolInvocation(std::vector<std::string> CommandLine, ToolAction *Action,
2078051db16aa9513333062ab3145f038429f66780fPeter Collingbourne                 FileManager *Files);
2088051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
2098051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ~ToolInvocation();
2108051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
211fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  /// \brief Set a \c DiagnosticConsumer to use during parsing.
212176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
213176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    this->DiagConsumer = DiagConsumer;
214176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
215fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
216cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Map a virtual file to be used while running the tool.
217cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
218cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param FilePath The path at which the content will be mapped.
219cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Content A null terminated buffer of the file's content.
220cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void mapVirtualFile(StringRef FilePath, StringRef Content);
221cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
222cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Run the clang invocation.
223cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
224cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \returns True if there were no errors during execution.
225cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  bool run();
226cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
227cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private:
228cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void addFileMappingsTo(SourceManager &SourceManager);
229cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
230cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  bool runInvocation(const char *BinaryName,
231cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek                     clang::driver::Compilation *Compilation,
232d47afb96a3f988e6d21a92fe4dfe875ab227c7c0Sean Silva                     clang::CompilerInvocation *Invocation);
233cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
234cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  std::vector<std::string> CommandLine;
2358051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  ToolAction *Action;
2368051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  bool OwnsAction;
237cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  FileManager *Files;
238cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  // Maps <file name> -> <file content>.
239cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  llvm::StringMap<StringRef> MappedFileContents;
240fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  DiagnosticConsumer *DiagConsumer;
241cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
242cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
243cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// \brief Utility to run a FrontendAction over a set of files.
244cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek///
245cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek/// This class is written to be usable for command line utilities.
246a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// By default the class uses ClangSyntaxOnlyAdjuster to modify
247a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan/// command line arguments before the arguments are used to run
24848b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// a frontend action. One could install an additional command line
24948b3f0f74da575a66802dc5019b7440914983885Manuel Klimek/// arguments adjuster by calling the appendArgumentsAdjuster() method.
250cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimekclass ClangTool {
251cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek public:
252cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Constructs a clang tool to run over a list of files.
253cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
254cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Compilations The CompilationDatabase which contains the compile
255cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///        command lines for the given source paths.
256cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param SourcePaths The source files to run over. If a source files is
257cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///        not found in Compilations, it is skipped.
258cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ClangTool(const CompilationDatabase &Compilations,
259cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek            ArrayRef<std::string> SourcePaths);
260cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
261176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ~ClangTool();
262d088a5f966c31462280d5ace29febc6889834611Edwin Vane
263fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  /// \brief Set a \c DiagnosticConsumer to use during parsing.
264176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
265176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    this->DiagConsumer = DiagConsumer;
266176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
267fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
268cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Map a virtual file to be used while running the tool.
269cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
270cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param FilePath The path at which the content will be mapped.
271cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \param Content A null terminated buffer of the file's content.
272cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  void mapVirtualFile(StringRef FilePath, StringRef Content);
273cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
27448b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \brief Append a command line arguments adjuster to the adjuster chain.
27548b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  ///
27648b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \param Adjuster An argument adjuster, which will be run on the output of
27748b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  ///        previous argument adjusters.
2780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster);
27948b3f0f74da575a66802dc5019b7440914983885Manuel Klimek
28048b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  /// \brief Clear the command line arguments adjuster chain.
28148b3f0f74da575a66802dc5019b7440914983885Manuel Klimek  void clearArgumentsAdjusters();
28248b3f0f74da575a66802dc5019b7440914983885Manuel Klimek
2838051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// Runs an action over all files specified in the command line.
284cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
2858051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \param Action Tool action.
2868051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  int run(ToolAction *Action);
2878051db16aa9513333062ab3145f038429f66780fPeter Collingbourne
2888051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// \brief Create an AST for each file specified in the command line and
2898051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  /// append them to ASTs.
2906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  int buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs);
291cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
292cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// \brief Returns the file manager used in the tool.
293cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  ///
294cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  /// The file manager is shared between all translation units.
2958051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  FileManager &getFiles() { return *Files; }
296cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
297cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek private:
298176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  const CompilationDatabase &Compilations;
299176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::vector<std::string> SourcePaths;
300cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3018051db16aa9513333062ab3145f038429f66780fPeter Collingbourne  llvm::IntrusiveRefCntPtr<FileManager> Files;
302cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  // Contains a list of pairs (<file name>, <file content>).
303cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  std::vector< std::pair<StringRef, StringRef> > MappedFileContents;
304a01ddc787b5c8b1cf11e975d3586908d3629954cSimon Atanasyan
3050e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  ArgumentsAdjuster ArgsAdjuster;
306fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek
307fa8e7d344fc6ac9bf464419cc9cc2743b5036047Manuel Klimek  DiagnosticConsumer *DiagConsumer;
308cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek};
309cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
310cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename T>
3116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstd::unique_ptr<FrontendActionFactory> newFrontendActionFactory() {
312cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  class SimpleFrontendActionFactory : public FrontendActionFactory {
313cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  public:
314651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    clang::FrontendAction *create() override { return new T; }
315cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  };
316cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return std::unique_ptr<FrontendActionFactory>(
3186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      new SimpleFrontendActionFactory);
319cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}
320cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
321cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimektemplate <typename FactoryT>
3226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesinline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(
3233c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) {
324cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  class FrontendActionFactoryAdapter : public FrontendActionFactory {
325cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  public:
3269fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek    explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory,
3273c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane                                          SourceFileCallbacks *Callbacks)
3283c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
329cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
330651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    clang::FrontendAction *create() override {
3313c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks);
332cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek    }
333cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
334cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  private:
335e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    class ConsumerFactoryAdaptor : public clang::ASTFrontendAction {
336e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    public:
3379fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek      ConsumerFactoryAdaptor(FactoryT *ConsumerFactory,
3383c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane                             SourceFileCallbacks *Callbacks)
3393c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {}
340e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek
341176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      std::unique_ptr<clang::ASTConsumer>
342176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines      CreateASTConsumer(clang::CompilerInstance &, StringRef) override {
343e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek        return ConsumerFactory->newASTConsumer();
344e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek      }
345e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek
3469fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek    protected:
347651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      bool BeginSourceFileAction(CompilerInstance &CI,
348651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                 StringRef Filename) override {
3493c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename))
3503c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane          return false;
3516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Callbacks)
3525ec9580f68661ffccff07177613b692d0acb4797Edwin Vane          return Callbacks->handleBeginSource(CI, Filename);
3533c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane        return true;
3543c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      }
355651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      void EndSourceFileAction() override {
3566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        if (Callbacks)
3575ec9580f68661ffccff07177613b692d0acb4797Edwin Vane          Callbacks->handleEndSource();
3589fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek        clang::ASTFrontendAction::EndSourceFileAction();
3599fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek      }
3609fb6b27e6d584ac339363357335f8d6de3a652acManuel Klimek
361e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    private:
362e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek      FactoryT *ConsumerFactory;
3633c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane      SourceFileCallbacks *Callbacks;
364e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    };
365e6df0ce08fcdf9f906556d98cca96d38ab440bbaManuel Klimek    FactoryT *ConsumerFactory;
3663c16e699b2a6926017b5f7a1516dc1be67468713Edwin Vane    SourceFileCallbacks *Callbacks;
367cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek  };
368cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3696bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return std::unique_ptr<FrontendActionFactory>(
3706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      new FrontendActionFactoryAdapter(ConsumerFactory, Callbacks));
371cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek}
372cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
3738fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \brief Returns the absolute path of \c File, by prepending it with
3748fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory if \c File is not absolute.
3758fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
3768fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise returns \c File.
3778fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// If 'File' starts with "./", the returned path will not contain the "./".
3788fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// Otherwise, the returned path will contain the literal path-concatenation of
3798fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// the current directory and \c File.
3808fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
381a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// The difference to llvm::sys::fs::make_absolute is the canonicalization this
382a2148240bd94ef659a9195b87e9cd0023536c01fRafael Espindola/// does by removing "./" and computing native paths.
3838fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek///
3848fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek/// \param File Either an absolute or relative path.
3858fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimekstd::string getAbsolutePath(StringRef File);
3868fa2fb859a4cb8e67d9763225281d9b0aa9cb59fManuel Klimek
38733337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar/// \brief Creates a \c CompilerInvocation.
38833337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainarclang::CompilerInvocation *newInvocation(
38933337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar    clang::DiagnosticsEngine *Diagnostics,
39033337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar    const llvm::opt::ArgStringList &CC1Args);
39133337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar
392cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace tooling
393cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek} // end namespace clang
394cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek
395cb971c6726d16e12ecd2a340941d7f5c06698332Manuel Klimek#endif // LLVM_CLANG_TOOLING_TOOLING_H
396