Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 1 | //===--- Tooling.cpp - Running clang standalone tools ---------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Tooling/Tooling.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 17 | #include "clang/Driver/Compilation.h" |
| 18 | #include "clang/Driver/Driver.h" |
| 19 | #include "clang/Driver/Tool.h" |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/ASTUnit.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 23 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Tooling/ArgumentsAdjusters.h" |
| 25 | #include "clang/Tooling/CompilationDatabase.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 27 | #include "llvm/Config/config.h" |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 28 | #include "llvm/Option/Option.h" |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
NAKAMURA Takumi | f0c8779 | 2012-04-04 13:59:36 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileSystem.h" |
NAKAMURA Takumi | 3a64a4b | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Host.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 33 | |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 34 | // For chdir, see the comment in ClangTool::run for more information. |
Hans Wennborg | 501eadb | 2014-03-12 16:07:46 +0000 | [diff] [blame] | 35 | #ifdef LLVM_ON_WIN32 |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 36 | # include <direct.h> |
Manuel Klimek | f33dcb0 | 2012-05-07 10:02:55 +0000 | [diff] [blame] | 37 | #else |
| 38 | # include <unistd.h> |
Manuel Klimek | 74cec54 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 41 | namespace clang { |
| 42 | namespace tooling { |
| 43 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 44 | ToolAction::~ToolAction() {} |
| 45 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 46 | FrontendActionFactory::~FrontendActionFactory() {} |
| 47 | |
| 48 | // FIXME: This file contains structural duplication with other parts of the |
| 49 | // code that sets up a compiler to run tools on it, and we should refactor |
| 50 | // it to be based on the same framework. |
| 51 | |
| 52 | /// \brief Builds a clang driver initialized for running clang tools. |
| 53 | static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, |
| 54 | const char *BinaryName) { |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 55 | const char *DefaultOutputName = "a.out"; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 56 | clang::driver::Driver *CompilerDriver = new clang::driver::Driver( |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 57 | BinaryName, llvm::sys::getDefaultTargetTriple(), |
Rafael Espindola | b0448cd | 2012-11-27 16:10:37 +0000 | [diff] [blame] | 58 | DefaultOutputName, *Diagnostics); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 59 | CompilerDriver->setTitle("clang_based_tool"); |
| 60 | return CompilerDriver; |
| 61 | } |
| 62 | |
| 63 | /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 64 | /// |
| 65 | /// Returns NULL on error. |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 66 | static const llvm::opt::ArgStringList *getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 67 | clang::DiagnosticsEngine *Diagnostics, |
| 68 | clang::driver::Compilation *Compilation) { |
| 69 | // We expect to get back exactly one Command job, if we didn't something |
| 70 | // failed. Extract that job from the Compilation. |
| 71 | const clang::driver::JobList &Jobs = Compilation->getJobs(); |
| 72 | if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 73 | SmallString<256> error_msg; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 74 | llvm::raw_svector_ostream error_stream(error_msg); |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 75 | Jobs.Print(error_stream, "; ", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 76 | Diagnostics->Report(clang::diag::err_fe_expected_compiler_job) |
| 77 | << error_stream.str(); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | // The one job we find should be to invoke clang again. |
| 82 | const clang::driver::Command *Cmd = |
| 83 | cast<clang::driver::Command>(*Jobs.begin()); |
| 84 | if (StringRef(Cmd->getCreator().getName()) != "clang") { |
| 85 | Diagnostics->Report(clang::diag::err_fe_expected_clang_command); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | return &Cmd->getArguments(); |
| 90 | } |
| 91 | |
| 92 | /// \brief Returns a clang build invocation initialized from the CC1 flags. |
| 93 | static clang::CompilerInvocation *newInvocation( |
| 94 | clang::DiagnosticsEngine *Diagnostics, |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 95 | const llvm::opt::ArgStringList &CC1Args) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 96 | assert(!CC1Args.empty() && "Must at least contain the program name!"); |
| 97 | clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; |
| 98 | clang::CompilerInvocation::CreateFromArgs( |
| 99 | *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), |
| 100 | *Diagnostics); |
| 101 | Invocation->getFrontendOpts().DisableFree = false; |
Nick Lewycky | 39dc9f8 | 2013-06-25 17:01:21 +0000 | [diff] [blame] | 102 | Invocation->getCodeGenOpts().DisableFree = false; |
Peter Collingbourne | c0423b3 | 2014-03-02 23:37:26 +0000 | [diff] [blame] | 103 | Invocation->getDependencyOutputOpts() = DependencyOutputOptions(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 104 | return Invocation; |
| 105 | } |
| 106 | |
| 107 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
| 108 | const Twine &FileName) { |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 109 | return runToolOnCodeWithArgs( |
| 110 | ToolAction, Code, std::vector<std::string>(), FileName); |
| 111 | } |
| 112 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 113 | static std::vector<std::string> |
| 114 | getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, |
| 115 | StringRef FileName) { |
| 116 | std::vector<std::string> Args; |
| 117 | Args.push_back("clang-tool"); |
| 118 | Args.push_back("-fsyntax-only"); |
| 119 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
| 120 | Args.push_back(FileName.str()); |
| 121 | return Args; |
| 122 | } |
| 123 | |
Nico Weber | 077a53e | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 124 | bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, |
| 125 | const std::vector<std::string> &Args, |
| 126 | const Twine &FileName) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 127 | SmallString<16> FileNameStorage; |
| 128 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 129 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 130 | new FileManager(FileSystemOptions())); |
| 131 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction, |
| 132 | Files.getPtr()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 133 | |
| 134 | SmallString<1024> CodeStorage; |
| 135 | Invocation.mapVirtualFile(FileNameRef, |
| 136 | Code.toNullTerminatedStringRef(CodeStorage)); |
| 137 | return Invocation.run(); |
| 138 | } |
| 139 | |
Manuel Klimek | 65fd0e1 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 140 | std::string getAbsolutePath(StringRef File) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 141 | StringRef RelativePath(File); |
NAKAMURA Takumi | 9b2d17c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 142 | // FIXME: Should '.\\' be accepted on Win32? |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 143 | if (RelativePath.startswith("./")) { |
| 144 | RelativePath = RelativePath.substr(strlen("./")); |
| 145 | } |
Rafael Espindola | c7367ff | 2013-08-10 01:40:10 +0000 | [diff] [blame] | 146 | |
| 147 | SmallString<1024> AbsolutePath = RelativePath; |
| 148 | llvm::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); |
| 149 | assert(!EC); |
Rafael Espindola | 0b8e4a1 | 2013-08-10 04:25:53 +0000 | [diff] [blame] | 150 | (void)EC; |
Benjamin Kramer | 2d4d8cb | 2013-09-11 11:23:15 +0000 | [diff] [blame] | 151 | llvm::sys::path::native(AbsolutePath); |
| 152 | return AbsolutePath.str(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 155 | namespace { |
| 156 | |
| 157 | class SingleFrontendActionFactory : public FrontendActionFactory { |
| 158 | FrontendAction *Action; |
| 159 | |
| 160 | public: |
| 161 | SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} |
| 162 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 163 | FrontendAction *create() override { return Action; } |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } |
| 167 | |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 168 | ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine, |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 169 | ToolAction *Action, FileManager *Files) |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 170 | : CommandLine(std::move(CommandLine)), |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 171 | Action(Action), |
| 172 | OwnsAction(false), |
| 173 | Files(Files), |
| 174 | DiagConsumer(NULL) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 175 | |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 176 | ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine, |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 177 | FrontendAction *FAction, FileManager *Files) |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 178 | : CommandLine(std::move(CommandLine)), |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 179 | Action(new SingleFrontendActionFactory(FAction)), |
| 180 | OwnsAction(true), |
| 181 | Files(Files), |
| 182 | DiagConsumer(NULL) {} |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 183 | |
| 184 | ToolInvocation::~ToolInvocation() { |
| 185 | if (OwnsAction) |
| 186 | delete Action; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 189 | void ToolInvocation::setDiagnosticConsumer(DiagnosticConsumer *D) { |
| 190 | DiagConsumer = D; |
| 191 | } |
| 192 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 193 | void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { |
NAKAMURA Takumi | 4de3165 | 2012-06-02 15:34:21 +0000 | [diff] [blame] | 194 | SmallString<1024> PathStorage; |
| 195 | llvm::sys::path::native(FilePath, PathStorage); |
| 196 | MappedFileContents[PathStorage] = Content; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool ToolInvocation::run() { |
| 200 | std::vector<const char*> Argv; |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 201 | for (const std::string &Str : CommandLine) |
| 202 | Argv.push_back(Str.c_str()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 203 | const char *const BinaryName = Argv[0]; |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 204 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 205 | TextDiagnosticPrinter DiagnosticPrinter( |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 206 | llvm::errs(), &*DiagOpts); |
| 207 | DiagnosticsEngine Diagnostics( |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 208 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
| 209 | DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 210 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 211 | const std::unique_ptr<clang::driver::Driver> Driver( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 212 | newDriver(&Diagnostics, BinaryName)); |
| 213 | // Since the input might only be virtual, don't check whether it exists. |
| 214 | Driver->setCheckInputsExist(false); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 215 | const std::unique_ptr<clang::driver::Compilation> Compilation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 216 | Driver->BuildCompilation(llvm::makeArrayRef(Argv))); |
Reid Kleckner | 898229a | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 217 | const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 218 | &Diagnostics, Compilation.get()); |
| 219 | if (CC1Args == NULL) { |
| 220 | return false; |
| 221 | } |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 222 | std::unique_ptr<clang::CompilerInvocation> Invocation( |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 223 | newInvocation(&Diagnostics, *CC1Args)); |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 224 | for (const auto &It : MappedFileContents) { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 225 | // Inject the code as the given file name into the preprocessor options. |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 226 | auto *Input = llvm::MemoryBuffer::getMemBuffer(It.getValue()); |
| 227 | Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), Input); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 228 | } |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 229 | return runInvocation(BinaryName, Compilation.get(), Invocation.release()); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 232 | bool ToolInvocation::runInvocation( |
| 233 | const char *BinaryName, |
| 234 | clang::driver::Compilation *Compilation, |
Sean Silva | f1b49e2 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 235 | clang::CompilerInvocation *Invocation) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 236 | // Show the invocation, with -v. |
| 237 | if (Invocation->getHeaderSearchOpts().Verbose) { |
| 238 | llvm::errs() << "clang Invocation:\n"; |
Hans Wennborg | b212b34 | 2013-09-12 18:23:34 +0000 | [diff] [blame] | 239 | Compilation->getJobs().Print(llvm::errs(), "\n", true); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 240 | llvm::errs() << "\n"; |
| 241 | } |
| 242 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 243 | return Action->runInvocation(Invocation, Files, DiagConsumer); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | bool FrontendActionFactory::runInvocation(CompilerInvocation *Invocation, |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 247 | FileManager *Files, |
| 248 | DiagnosticConsumer *DiagConsumer) { |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 249 | // Create a compiler instance to handle the actual work. |
| 250 | clang::CompilerInstance Compiler; |
| 251 | Compiler.setInvocation(Invocation); |
| 252 | Compiler.setFileManager(Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 253 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 254 | // The FrontendAction can have lifetime requirements for Compiler or its |
| 255 | // members, and we need to ensure it's deleted earlier than Compiler. So we |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 256 | // pass it to an std::unique_ptr declared after the Compiler variable. |
| 257 | std::unique_ptr<FrontendAction> ScopedToolAction(create()); |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 258 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 259 | // Create the compilers actual diagnostics engine. |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 260 | Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 261 | if (!Compiler.hasDiagnostics()) |
| 262 | return false; |
| 263 | |
| 264 | Compiler.createSourceManager(*Files); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 265 | |
Alexander Kornienko | 21d6ec9 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 266 | const bool Success = Compiler.ExecuteAction(*ScopedToolAction); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 267 | |
Manuel Klimek | 3aad855 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 268 | Files->clearStatCaches(); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 269 | return Success; |
| 270 | } |
| 271 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 272 | ClangTool::ClangTool(const CompilationDatabase &Compilations, |
| 273 | ArrayRef<std::string> SourcePaths) |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 274 | : Files(new FileManager(FileSystemOptions())), DiagConsumer(NULL) { |
Pavel Labath | dee20c1 | 2013-06-06 11:52:19 +0000 | [diff] [blame] | 275 | ArgsAdjusters.push_back(new ClangStripOutputAdjuster()); |
| 276 | ArgsAdjusters.push_back(new ClangSyntaxOnlyAdjuster()); |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 277 | for (const auto &SourcePath : SourcePaths) { |
| 278 | std::string File(getAbsolutePath(SourcePath)); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 279 | |
Manuel Klimek | 805d8dc | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 280 | std::vector<CompileCommand> CompileCommandsForFile = |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 281 | Compilations.getCompileCommands(File); |
Manuel Klimek | 805d8dc | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 282 | if (!CompileCommandsForFile.empty()) { |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 283 | for (CompileCommand &CompileCommand : CompileCommandsForFile) { |
| 284 | CompileCommands.push_back( |
| 285 | std::make_pair(File, std::move(CompileCommand))); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 286 | } |
| 287 | } else { |
| 288 | // FIXME: There are two use cases here: doing a fuzzy |
| 289 | // "find . -name '*.cc' |xargs tool" match, where as a user I don't care |
| 290 | // about the .cc files that were not found, and the use case where I |
| 291 | // specify all files I want to run over explicitly, where this should |
| 292 | // be an error. We'll want to add an option for this. |
| 293 | llvm::outs() << "Skipping " << File << ". Command line not found.\n"; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 298 | void ClangTool::setDiagnosticConsumer(DiagnosticConsumer *D) { |
| 299 | DiagConsumer = D; |
| 300 | } |
| 301 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 302 | void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { |
| 303 | MappedFileContents.push_back(std::make_pair(FilePath, Content)); |
| 304 | } |
| 305 | |
Simon Atanasyan | 32df72d | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 306 | void ClangTool::setArgumentsAdjuster(ArgumentsAdjuster *Adjuster) { |
Manuel Klimek | d91ac93 | 2013-06-04 14:44:44 +0000 | [diff] [blame] | 307 | clearArgumentsAdjusters(); |
| 308 | appendArgumentsAdjuster(Adjuster); |
| 309 | } |
| 310 | |
| 311 | void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster) { |
| 312 | ArgsAdjusters.push_back(Adjuster); |
| 313 | } |
| 314 | |
| 315 | void ClangTool::clearArgumentsAdjusters() { |
| 316 | for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I) |
| 317 | delete ArgsAdjusters[I]; |
| 318 | ArgsAdjusters.clear(); |
Simon Atanasyan | 32df72d | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 321 | int ClangTool::run(ToolAction *Action) { |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 322 | // Exists solely for the purpose of lookup of the resource path. |
| 323 | // This just needs to be some symbol in the binary. |
| 324 | static int StaticSymbol; |
| 325 | // The driver detects the builtin header path based on the path of the |
| 326 | // executable. |
| 327 | // FIXME: On linux, GetMainExecutable is independent of the value of the |
| 328 | // first argument, thus allowing ClangTool and runToolOnCode to just |
| 329 | // pass in made-up names here. Make sure this works on other platforms. |
| 330 | std::string MainExecutable = |
Rafael Espindola | 9678d27 | 2013-06-26 05:03:40 +0000 | [diff] [blame] | 331 | llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 332 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 333 | bool ProcessingFailed = false; |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 334 | for (const auto &Command : CompileCommands) { |
Manuel Klimek | 805d8dc | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 335 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 336 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 337 | // guaranteeing that all subsequent relative path operations work |
| 338 | // on the same path the original chdir resulted in. This makes a difference |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 339 | // for example on network filesystems, where symlinks might be switched |
Manuel Klimek | 805d8dc | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 340 | // during runtime of the tool. Fixing this depends on having a file system |
| 341 | // abstraction that allows openat() style interactions. |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 342 | if (chdir(Command.second.Directory.c_str())) |
Manuel Klimek | 805d8dc | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 343 | llvm::report_fatal_error("Cannot chdir into \"" + |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 344 | Twine(Command.second.Directory) + "\n!"); |
| 345 | std::vector<std::string> CommandLine = Command.second.CommandLine; |
| 346 | for (ArgumentsAdjuster *Adjuster : ArgsAdjusters) |
| 347 | CommandLine = Adjuster->Adjust(CommandLine); |
Alexander Kornienko | 8388d24 | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 348 | assert(!CommandLine.empty()); |
| 349 | CommandLine[0] = MainExecutable; |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 350 | // FIXME: We need a callback mechanism for the tool writer to output a |
| 351 | // customized message for each file. |
| 352 | DEBUG({ |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 353 | llvm::dbgs() << "Processing: " << Command.first << ".\n"; |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 354 | }); |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 355 | ToolInvocation Invocation(std::move(CommandLine), Action, Files.getPtr()); |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 356 | Invocation.setDiagnosticConsumer(DiagConsumer); |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 357 | for (const auto &MappedFile : MappedFileContents) { |
| 358 | Invocation.mapVirtualFile(MappedFile.first, MappedFile.second); |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 359 | } |
| 360 | if (!Invocation.run()) { |
Edwin Vane | 34794c5 | 2013-03-15 20:14:01 +0000 | [diff] [blame] | 361 | // FIXME: Diagnostics should be used instead. |
Benjamin Kramer | efb1eb9 | 2014-03-20 12:48:36 +0000 | [diff] [blame^] | 362 | llvm::errs() << "Error while processing " << Command.first << ".\n"; |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 363 | ProcessingFailed = true; |
| 364 | } |
| 365 | } |
| 366 | return ProcessingFailed ? 1 : 0; |
| 367 | } |
| 368 | |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 369 | namespace { |
| 370 | |
| 371 | class ASTBuilderAction : public ToolAction { |
| 372 | std::vector<ASTUnit *> &ASTs; |
| 373 | |
| 374 | public: |
| 375 | ASTBuilderAction(std::vector<ASTUnit *> &ASTs) : ASTs(ASTs) {} |
| 376 | |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 377 | bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 378 | DiagnosticConsumer *DiagConsumer) override { |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 379 | // FIXME: This should use the provided FileManager. |
| 380 | ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation( |
Manuel Klimek | 6408301 | 2013-11-07 23:18:05 +0000 | [diff] [blame] | 381 | Invocation, CompilerInstance::createDiagnostics( |
| 382 | &Invocation->getDiagnosticOpts(), DiagConsumer, |
| 383 | /*ShouldOwnClient=*/false)); |
Peter Collingbourne | c689ee7 | 2013-11-06 20:12:45 +0000 | [diff] [blame] | 384 | if (!AST) |
| 385 | return false; |
| 386 | |
| 387 | ASTs.push_back(AST); |
| 388 | return true; |
| 389 | } |
| 390 | }; |
| 391 | |
| 392 | } |
| 393 | |
| 394 | int ClangTool::buildASTs(std::vector<ASTUnit *> &ASTs) { |
| 395 | ASTBuilderAction Action(ASTs); |
| 396 | return run(&Action); |
| 397 | } |
| 398 | |
| 399 | ASTUnit *buildASTFromCode(const Twine &Code, const Twine &FileName) { |
| 400 | return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName); |
| 401 | } |
| 402 | |
| 403 | ASTUnit *buildASTFromCodeWithArgs(const Twine &Code, |
| 404 | const std::vector<std::string> &Args, |
| 405 | const Twine &FileName) { |
| 406 | SmallString<16> FileNameStorage; |
| 407 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
| 408 | |
| 409 | std::vector<ASTUnit *> ASTs; |
| 410 | ASTBuilderAction Action(ASTs); |
| 411 | ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, 0); |
| 412 | |
| 413 | SmallString<1024> CodeStorage; |
| 414 | Invocation.mapVirtualFile(FileNameRef, |
| 415 | Code.toNullTerminatedStringRef(CodeStorage)); |
| 416 | if (!Invocation.run()) |
| 417 | return 0; |
| 418 | |
| 419 | assert(ASTs.size() == 1); |
| 420 | return ASTs[0]; |
| 421 | } |
| 422 | |
Manuel Klimek | 47c245a | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 423 | } // end namespace tooling |
| 424 | } // end namespace clang |