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