Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 1 | //===--- Tooling.h - Framework for standalone Clang tools -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements functions to run clang tools standalone instead |
| 11 | // of running them as a plugin. |
| 12 | // |
| 13 | // A ClangTool is initialized with a CompilationDatabase and a set of files |
| 14 | // to run over. The tool will then run a user-specified FrontendAction over |
| 15 | // all TUs in which the given files are compiled. |
| 16 | // |
| 17 | // It is also possible to run a FrontendAction over a snippet of code by |
Manuel Klimek | 2954134 | 2012-04-23 08:43:08 +0000 | [diff] [blame] | 18 | // calling runToolOnCode, which is useful for unit testing. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 19 | // |
| 20 | // Applications that need more fine grained control over how to run |
| 21 | // multiple FrontendActions over code can use ToolInvocation. |
| 22 | // |
| 23 | // Example tools: |
| 24 | // - running clang -fsyntax-only over source code from an editor to get |
| 25 | // fast syntax checks |
| 26 | // - running match/replace tools over C++ code |
| 27 | // |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #ifndef LLVM_CLANG_TOOLING_TOOLING_H |
| 31 | #define LLVM_CLANG_TOOLING_TOOLING_H |
| 32 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 33 | #include "clang/AST/ASTConsumer.h" |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 34 | #include "clang/Basic/Diagnostic.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 35 | #include "clang/Basic/FileManager.h" |
| 36 | #include "clang/Basic/LLVM.h" |
| 37 | #include "clang/Driver/Util.h" |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 38 | #include "clang/Frontend/FrontendAction.h" |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 39 | #include "clang/Tooling/ArgumentsAdjusters.h" |
Manuel Klimek | 00f3c4f | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 40 | #include "clang/Tooling/CompilationDatabase.h" |
Chandler Carruth | 30a2e16 | 2012-12-04 09:18:49 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringMap.h" |
| 42 | #include "llvm/ADT/Twine.h" |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 43 | #include "llvm/Option/Option.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 44 | #include <memory> |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | namespace clang { |
| 49 | |
| 50 | namespace driver { |
| 51 | class Compilation; |
| 52 | } // end namespace driver |
| 53 | |
| 54 | class CompilerInvocation; |
| 55 | class SourceManager; |
| 56 | class FrontendAction; |
| 57 | |
| 58 | namespace tooling { |
| 59 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 60 | /// \brief Interface to process a clang::CompilerInvocation. |
| 61 | /// |
| 62 | /// If your tool is based on FrontendAction, you should be deriving from |
| 63 | /// FrontendActionFactory instead. |
| 64 | class ToolAction { |
| 65 | public: |
| 66 | virtual ~ToolAction(); |
| 67 | |
| 68 | /// \brief Perform an action for an invocation. |
| 69 | virtual bool runInvocation(clang::CompilerInvocation *Invocation, |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 70 | FileManager *Files, |
| 71 | DiagnosticConsumer *DiagConsumer) = 0; |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 74 | /// \brief Interface to generate clang::FrontendActions. |
Stefanus Du Toit | 233fbe1 | 2013-08-15 00:35:46 +0000 | [diff] [blame] | 75 | /// |
| 76 | /// Having a factory interface allows, for example, a new FrontendAction to be |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 77 | /// created for each translation unit processed by ClangTool. This class is |
| 78 | /// also a ToolAction which uses the FrontendActions created by create() to |
| 79 | /// process each translation unit. |
| 80 | class FrontendActionFactory : public ToolAction { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 81 | public: |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 82 | ~FrontendActionFactory() override; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 83 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 84 | /// \brief Invokes the compiler with a FrontendAction created by create(). |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 85 | bool runInvocation(clang::CompilerInvocation *Invocation, FileManager *Files, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 86 | DiagnosticConsumer *DiagConsumer) override; |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 87 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 88 | /// \brief Returns a new clang::FrontendAction. |
| 89 | /// |
| 90 | /// The caller takes ownership of the returned action. |
| 91 | virtual clang::FrontendAction *create() = 0; |
| 92 | }; |
| 93 | |
| 94 | /// \brief Returns a new FrontendActionFactory for a given type. |
| 95 | /// |
Stefanus Du Toit | 233fbe1 | 2013-08-15 00:35:46 +0000 | [diff] [blame] | 96 | /// T must derive from clang::FrontendAction. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 97 | /// |
| 98 | /// Example: |
| 99 | /// FrontendActionFactory *Factory = |
| 100 | /// newFrontendActionFactory<clang::SyntaxOnlyAction>(); |
| 101 | template <typename T> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 102 | std::unique_ptr<FrontendActionFactory> newFrontendActionFactory(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 103 | |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 104 | /// \brief Callbacks called before and after each source file processed by a |
| 105 | /// FrontendAction created by the FrontedActionFactory returned by \c |
| 106 | /// newFrontendActionFactory. |
| 107 | class SourceFileCallbacks { |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 108 | public: |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 109 | virtual ~SourceFileCallbacks() {} |
| 110 | |
| 111 | /// \brief Called before a source file is processed by a FrontEndAction. |
| 112 | /// \see clang::FrontendAction::BeginSourceFileAction |
Edwin Vane | 5ec9580 | 2013-05-30 13:59:44 +0000 | [diff] [blame] | 113 | virtual bool handleBeginSource(CompilerInstance &CI, StringRef Filename) { |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /// \brief Called after a source file is processed by a FrontendAction. |
| 118 | /// \see clang::FrontendAction::EndSourceFileAction |
Edwin Vane | 5ec9580 | 2013-05-30 13:59:44 +0000 | [diff] [blame] | 119 | virtual void handleEndSource() {} |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 122 | /// \brief Returns a new FrontendActionFactory for any type that provides an |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 123 | /// implementation of newASTConsumer(). |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 124 | /// |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 125 | /// FactoryT must implement: ASTConsumer *newASTConsumer(). |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 126 | /// |
| 127 | /// Example: |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 128 | /// struct ProvidesASTConsumers { |
| 129 | /// clang::ASTConsumer *newASTConsumer(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 130 | /// } Factory; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 131 | /// std::unique_ptr<FrontendActionFactory> FactoryAdapter( |
| 132 | /// newFrontendActionFactory(&Factory)); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 133 | template <typename FactoryT> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 134 | inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( |
| 135 | FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks = nullptr); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 136 | |
| 137 | /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag. |
| 138 | /// |
| 139 | /// \param ToolAction The action to run over the code. |
| 140 | /// \param Code C++ code. |
| 141 | /// \param FileName The file name which 'Code' will be mapped as. |
| 142 | /// |
| 143 | /// \return - True if 'ToolAction' was successfully executed. |
| 144 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
| 145 | const Twine &FileName = "input.cc"); |
| 146 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 147 | /// The first part of the pair is the filename, the second part the |
| 148 | /// file-content. |
| 149 | typedef std::vector<std::pair<std::string, std::string>> FileContentMappings; |
| 150 | |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 151 | /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and |
| 152 | /// with additional other flags. |
| 153 | /// |
| 154 | /// \param ToolAction The action to run over the code. |
| 155 | /// \param Code C++ code. |
| 156 | /// \param Args Additional flags to pass on. |
| 157 | /// \param FileName The file name which 'Code' will be mapped as. |
| 158 | /// |
| 159 | /// \return - True if 'ToolAction' was successfully executed. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 160 | bool runToolOnCodeWithArgs( |
| 161 | clang::FrontendAction *ToolAction, const Twine &Code, |
| 162 | const std::vector<std::string> &Args, const Twine &FileName = "input.cc", |
| 163 | const FileContentMappings &VirtualMappedFiles = FileContentMappings()); |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 164 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 165 | /// \brief Builds an AST for 'Code'. |
| 166 | /// |
| 167 | /// \param Code C++ code. |
| 168 | /// \param FileName The file name which 'Code' will be mapped as. |
| 169 | /// |
| 170 | /// \return The resulting AST or null if an error occurred. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 171 | std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code, |
| 172 | const Twine &FileName = "input.cc"); |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 173 | |
| 174 | /// \brief Builds an AST for 'Code' with additional flags. |
| 175 | /// |
| 176 | /// \param Code C++ code. |
| 177 | /// \param Args Additional flags to pass on. |
| 178 | /// \param FileName The file name which 'Code' will be mapped as. |
| 179 | /// |
| 180 | /// \return The resulting AST or null if an error occurred. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 181 | std::unique_ptr<ASTUnit> |
| 182 | buildASTFromCodeWithArgs(const Twine &Code, |
| 183 | const std::vector<std::string> &Args, |
| 184 | const Twine &FileName = "input.cc"); |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 185 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 186 | /// \brief Utility to run a FrontendAction in a single clang invocation. |
| 187 | class ToolInvocation { |
| 188 | public: |
| 189 | /// \brief Create a tool invocation. |
| 190 | /// |
Alexander Kornienko | 30c009b | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 191 | /// \param CommandLine The command line arguments to clang. Note that clang |
| 192 | /// uses its binary name (CommandLine[0]) to locate its builtin headers. |
| 193 | /// Callers have to ensure that they are installed in a compatible location |
| 194 | /// (see clang driver implementation) or mapped in via mapVirtualFile. |
NAKAMURA Takumi | f907afe | 2013-11-06 22:37:12 +0000 | [diff] [blame] | 195 | /// \param FAction The action to be executed. Class takes ownership. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 196 | /// \param Files The FileManager used for the execution. Class does not take |
| 197 | /// ownership. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 198 | ToolInvocation(std::vector<std::string> CommandLine, FrontendAction *FAction, |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 199 | FileManager *Files); |
| 200 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 201 | /// \brief Create a tool invocation. |
| 202 | /// |
| 203 | /// \param CommandLine The command line arguments to clang. |
| 204 | /// \param Action The action to be executed. |
| 205 | /// \param Files The FileManager used for the execution. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 206 | ToolInvocation(std::vector<std::string> CommandLine, ToolAction *Action, |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 207 | FileManager *Files); |
| 208 | |
| 209 | ~ToolInvocation(); |
| 210 | |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 211 | /// \brief Set a \c DiagnosticConsumer to use during parsing. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 212 | void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { |
| 213 | this->DiagConsumer = DiagConsumer; |
| 214 | } |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 215 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 216 | /// \brief Map a virtual file to be used while running the tool. |
| 217 | /// |
| 218 | /// \param FilePath The path at which the content will be mapped. |
| 219 | /// \param Content A null terminated buffer of the file's content. |
| 220 | void mapVirtualFile(StringRef FilePath, StringRef Content); |
| 221 | |
| 222 | /// \brief Run the clang invocation. |
| 223 | /// |
| 224 | /// \returns True if there were no errors during execution. |
| 225 | bool run(); |
| 226 | |
| 227 | private: |
| 228 | void addFileMappingsTo(SourceManager &SourceManager); |
| 229 | |
| 230 | bool runInvocation(const char *BinaryName, |
| 231 | clang::driver::Compilation *Compilation, |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 232 | clang::CompilerInvocation *Invocation); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 233 | |
| 234 | std::vector<std::string> CommandLine; |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 235 | ToolAction *Action; |
| 236 | bool OwnsAction; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 237 | FileManager *Files; |
| 238 | // Maps <file name> -> <file content>. |
| 239 | llvm::StringMap<StringRef> MappedFileContents; |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 240 | DiagnosticConsumer *DiagConsumer; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | /// \brief Utility to run a FrontendAction over a set of files. |
| 244 | /// |
| 245 | /// This class is written to be usable for command line utilities. |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 246 | /// By default the class uses ClangSyntaxOnlyAdjuster to modify |
| 247 | /// command line arguments before the arguments are used to run |
Manuel Klimek | 48b3f0f | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 248 | /// a frontend action. One could install an additional command line |
| 249 | /// arguments adjuster by calling the appendArgumentsAdjuster() method. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 250 | class ClangTool { |
| 251 | public: |
| 252 | /// \brief Constructs a clang tool to run over a list of files. |
| 253 | /// |
| 254 | /// \param Compilations The CompilationDatabase which contains the compile |
| 255 | /// command lines for the given source paths. |
| 256 | /// \param SourcePaths The source files to run over. If a source files is |
| 257 | /// not found in Compilations, it is skipped. |
| 258 | ClangTool(const CompilationDatabase &Compilations, |
| 259 | ArrayRef<std::string> SourcePaths); |
| 260 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 261 | ~ClangTool(); |
Edwin Vane | d088a5f | 2013-01-11 17:04:55 +0000 | [diff] [blame] | 262 | |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 263 | /// \brief Set a \c DiagnosticConsumer to use during parsing. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 264 | void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { |
| 265 | this->DiagConsumer = DiagConsumer; |
| 266 | } |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 267 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 268 | /// \brief Map a virtual file to be used while running the tool. |
| 269 | /// |
| 270 | /// \param FilePath The path at which the content will be mapped. |
| 271 | /// \param Content A null terminated buffer of the file's content. |
| 272 | void mapVirtualFile(StringRef FilePath, StringRef Content); |
| 273 | |
Manuel Klimek | 48b3f0f | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 274 | /// \brief Append a command line arguments adjuster to the adjuster chain. |
| 275 | /// |
| 276 | /// \param Adjuster An argument adjuster, which will be run on the output of |
| 277 | /// previous argument adjusters. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 278 | void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster); |
Manuel Klimek | 48b3f0f | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 279 | |
| 280 | /// \brief Clear the command line arguments adjuster chain. |
| 281 | void clearArgumentsAdjusters(); |
| 282 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 283 | /// Runs an action over all files specified in the command line. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 284 | /// |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 285 | /// \param Action Tool action. |
| 286 | int run(ToolAction *Action); |
| 287 | |
| 288 | /// \brief Create an AST for each file specified in the command line and |
| 289 | /// append them to ASTs. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 290 | int buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 291 | |
| 292 | /// \brief Returns the file manager used in the tool. |
| 293 | /// |
| 294 | /// The file manager is shared between all translation units. |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 295 | FileManager &getFiles() { return *Files; } |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 296 | |
| 297 | private: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 298 | const CompilationDatabase &Compilations; |
| 299 | std::vector<std::string> SourcePaths; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 300 | |
Peter Collingbourne | 8051db1 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 301 | llvm::IntrusiveRefCntPtr<FileManager> Files; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 302 | // Contains a list of pairs (<file name>, <file content>). |
| 303 | std::vector< std::pair<StringRef, StringRef> > MappedFileContents; |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 304 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 305 | ArgumentsAdjuster ArgsAdjuster; |
Manuel Klimek | fa8e7d3 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 306 | |
| 307 | DiagnosticConsumer *DiagConsumer; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | template <typename T> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 311 | std::unique_ptr<FrontendActionFactory> newFrontendActionFactory() { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 312 | class SimpleFrontendActionFactory : public FrontendActionFactory { |
| 313 | public: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 314 | clang::FrontendAction *create() override { return new T; } |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 317 | return std::unique_ptr<FrontendActionFactory>( |
| 318 | new SimpleFrontendActionFactory); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | template <typename FactoryT> |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 322 | inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 323 | FactoryT *ConsumerFactory, SourceFileCallbacks *Callbacks) { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 324 | class FrontendActionFactoryAdapter : public FrontendActionFactory { |
| 325 | public: |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 326 | explicit FrontendActionFactoryAdapter(FactoryT *ConsumerFactory, |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 327 | SourceFileCallbacks *Callbacks) |
| 328 | : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 329 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 330 | clang::FrontendAction *create() override { |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 331 | return new ConsumerFactoryAdaptor(ConsumerFactory, Callbacks); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | private: |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 335 | class ConsumerFactoryAdaptor : public clang::ASTFrontendAction { |
| 336 | public: |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 337 | ConsumerFactoryAdaptor(FactoryT *ConsumerFactory, |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 338 | SourceFileCallbacks *Callbacks) |
| 339 | : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 340 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 341 | std::unique_ptr<clang::ASTConsumer> |
| 342 | CreateASTConsumer(clang::CompilerInstance &, StringRef) override { |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 343 | return ConsumerFactory->newASTConsumer(); |
| 344 | } |
| 345 | |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 346 | protected: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 347 | bool BeginSourceFileAction(CompilerInstance &CI, |
| 348 | StringRef Filename) override { |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 349 | if (!clang::ASTFrontendAction::BeginSourceFileAction(CI, Filename)) |
| 350 | return false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 351 | if (Callbacks) |
Edwin Vane | 5ec9580 | 2013-05-30 13:59:44 +0000 | [diff] [blame] | 352 | return Callbacks->handleBeginSource(CI, Filename); |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 355 | void EndSourceFileAction() override { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 356 | if (Callbacks) |
Edwin Vane | 5ec9580 | 2013-05-30 13:59:44 +0000 | [diff] [blame] | 357 | Callbacks->handleEndSource(); |
Manuel Klimek | 9fb6b27 | 2012-10-25 08:49:11 +0000 | [diff] [blame] | 358 | clang::ASTFrontendAction::EndSourceFileAction(); |
| 359 | } |
| 360 | |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 361 | private: |
| 362 | FactoryT *ConsumerFactory; |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 363 | SourceFileCallbacks *Callbacks; |
Manuel Klimek | e6df0ce | 2012-07-05 18:13:01 +0000 | [diff] [blame] | 364 | }; |
| 365 | FactoryT *ConsumerFactory; |
Edwin Vane | 3c16e69 | 2013-05-29 16:01:10 +0000 | [diff] [blame] | 366 | SourceFileCallbacks *Callbacks; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 367 | }; |
| 368 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 369 | return std::unique_ptr<FrontendActionFactory>( |
| 370 | new FrontendActionFactoryAdapter(ConsumerFactory, Callbacks)); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 373 | /// \brief Returns the absolute path of \c File, by prepending it with |
| 374 | /// the current directory if \c File is not absolute. |
| 375 | /// |
| 376 | /// Otherwise returns \c File. |
| 377 | /// If 'File' starts with "./", the returned path will not contain the "./". |
| 378 | /// Otherwise, the returned path will contain the literal path-concatenation of |
| 379 | /// the current directory and \c File. |
| 380 | /// |
Rafael Espindola | a214824 | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 381 | /// The difference to llvm::sys::fs::make_absolute is the canonicalization this |
| 382 | /// does by removing "./" and computing native paths. |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 383 | /// |
| 384 | /// \param File Either an absolute or relative path. |
| 385 | std::string getAbsolutePath(StringRef File); |
| 386 | |
Pirama Arumuga Nainar | 33337ca | 2015-05-06 11:48:57 -0700 | [diff] [blame] | 387 | /// \brief Creates a \c CompilerInvocation. |
| 388 | clang::CompilerInvocation *newInvocation( |
| 389 | clang::DiagnosticsEngine *Diagnostics, |
| 390 | const llvm::opt::ArgStringList &CC1Args); |
| 391 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 392 | } // end namespace tooling |
| 393 | } // end namespace clang |
| 394 | |
| 395 | #endif // LLVM_CLANG_TOOLING_TOOLING_H |