Manuel Klimek | cb971c6 | 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" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 16 | #include "clang/Driver/Compilation.h" |
| 17 | #include "clang/Driver/Driver.h" |
| 18 | #include "clang/Driver/Tool.h" |
| 19 | #include "clang/Frontend/CompilerInstance.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Tooling/ArgumentsAdjusters.h" |
| 23 | #include "clang/Tooling/CompilationDatabase.h" |
NAKAMURA Takumi | 01b8ca5 | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Edwin Vane | ad7e160 | 2013-03-15 20:14:01 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/Debug.h" |
NAKAMURA Takumi | b175d0f | 2012-04-04 13:59:36 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
NAKAMURA Takumi | 01b8ca5 | 2012-04-04 13:59:41 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Host.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 29 | |
Manuel Klimek | 3b6e319 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 30 | // For chdir, see the comment in ClangTool::run for more information. |
Manuel Klimek | ed5ee48 | 2012-05-07 10:02:55 +0000 | [diff] [blame] | 31 | #ifdef _WIN32 |
Manuel Klimek | 3b6e319 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 32 | # include <direct.h> |
Manuel Klimek | ed5ee48 | 2012-05-07 10:02:55 +0000 | [diff] [blame] | 33 | #else |
| 34 | # include <unistd.h> |
Manuel Klimek | 3b6e319 | 2012-05-07 09:45:46 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 37 | namespace clang { |
| 38 | namespace tooling { |
| 39 | |
| 40 | FrontendActionFactory::~FrontendActionFactory() {} |
| 41 | |
| 42 | // FIXME: This file contains structural duplication with other parts of the |
| 43 | // code that sets up a compiler to run tools on it, and we should refactor |
| 44 | // it to be based on the same framework. |
| 45 | |
| 46 | /// \brief Builds a clang driver initialized for running clang tools. |
| 47 | static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, |
| 48 | const char *BinaryName) { |
| 49 | const std::string DefaultOutputName = "a.out"; |
| 50 | clang::driver::Driver *CompilerDriver = new clang::driver::Driver( |
Alexander Kornienko | 30c009b | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 51 | BinaryName, llvm::sys::getDefaultTargetTriple(), |
Rafael Espindola | 17c874a | 2012-11-27 16:10:37 +0000 | [diff] [blame] | 52 | DefaultOutputName, *Diagnostics); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 53 | CompilerDriver->setTitle("clang_based_tool"); |
| 54 | return CompilerDriver; |
| 55 | } |
| 56 | |
| 57 | /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. |
| 58 | /// |
| 59 | /// Returns NULL on error. |
| 60 | static const clang::driver::ArgStringList *getCC1Arguments( |
| 61 | clang::DiagnosticsEngine *Diagnostics, |
| 62 | clang::driver::Compilation *Compilation) { |
| 63 | // We expect to get back exactly one Command job, if we didn't something |
| 64 | // failed. Extract that job from the Compilation. |
| 65 | const clang::driver::JobList &Jobs = Compilation->getJobs(); |
| 66 | if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 67 | SmallString<256> error_msg; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 68 | llvm::raw_svector_ostream error_stream(error_msg); |
| 69 | Compilation->PrintJob(error_stream, Compilation->getJobs(), "; ", true); |
| 70 | Diagnostics->Report(clang::diag::err_fe_expected_compiler_job) |
| 71 | << error_stream.str(); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | // The one job we find should be to invoke clang again. |
| 76 | const clang::driver::Command *Cmd = |
| 77 | cast<clang::driver::Command>(*Jobs.begin()); |
| 78 | if (StringRef(Cmd->getCreator().getName()) != "clang") { |
| 79 | Diagnostics->Report(clang::diag::err_fe_expected_clang_command); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | return &Cmd->getArguments(); |
| 84 | } |
| 85 | |
| 86 | /// \brief Returns a clang build invocation initialized from the CC1 flags. |
| 87 | static clang::CompilerInvocation *newInvocation( |
| 88 | clang::DiagnosticsEngine *Diagnostics, |
| 89 | const clang::driver::ArgStringList &CC1Args) { |
| 90 | assert(!CC1Args.empty() && "Must at least contain the program name!"); |
| 91 | clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; |
| 92 | clang::CompilerInvocation::CreateFromArgs( |
| 93 | *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), |
| 94 | *Diagnostics); |
| 95 | Invocation->getFrontendOpts().DisableFree = false; |
| 96 | return Invocation; |
| 97 | } |
| 98 | |
| 99 | bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, |
| 100 | const Twine &FileName) { |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 101 | return runToolOnCodeWithArgs( |
| 102 | ToolAction, Code, std::vector<std::string>(), FileName); |
| 103 | } |
| 104 | |
| 105 | bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, |
| 106 | const std::vector<std::string> &Args, |
| 107 | const Twine &FileName) { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 108 | SmallString<16> FileNameStorage; |
| 109 | StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 110 | std::vector<std::string> Commands; |
| 111 | Commands.push_back("clang-tool"); |
| 112 | Commands.push_back("-fsyntax-only"); |
| 113 | Commands.insert(Commands.end(), Args.begin(), Args.end()); |
| 114 | Commands.push_back(FileNameRef.data()); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 115 | FileManager Files((FileSystemOptions())); |
Nico Weber | 5666988 | 2012-08-30 02:02:19 +0000 | [diff] [blame] | 116 | ToolInvocation Invocation(Commands, ToolAction, &Files); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 117 | |
| 118 | SmallString<1024> CodeStorage; |
| 119 | Invocation.mapVirtualFile(FileNameRef, |
| 120 | Code.toNullTerminatedStringRef(CodeStorage)); |
| 121 | return Invocation.run(); |
| 122 | } |
| 123 | |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 124 | std::string getAbsolutePath(StringRef File) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 125 | SmallString<1024> BaseDirectory; |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 126 | if (const char *PWD = ::getenv("PWD")) |
| 127 | BaseDirectory = PWD; |
| 128 | else |
| 129 | llvm::sys::fs::current_path(BaseDirectory); |
NAKAMURA Takumi | 62d198c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 130 | SmallString<1024> PathStorage; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 131 | if (llvm::sys::path::is_absolute(File)) { |
NAKAMURA Takumi | 62d198c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 132 | llvm::sys::path::native(File, PathStorage); |
| 133 | return PathStorage.str(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 134 | } |
| 135 | StringRef RelativePath(File); |
NAKAMURA Takumi | 62d198c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 136 | // FIXME: Should '.\\' be accepted on Win32? |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 137 | if (RelativePath.startswith("./")) { |
| 138 | RelativePath = RelativePath.substr(strlen("./")); |
| 139 | } |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 140 | SmallString<1024> AbsolutePath(BaseDirectory); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 141 | llvm::sys::path::append(AbsolutePath, RelativePath); |
NAKAMURA Takumi | 62d198c | 2012-05-23 22:24:20 +0000 | [diff] [blame] | 142 | llvm::sys::path::native(Twine(AbsolutePath), PathStorage); |
| 143 | return PathStorage.str(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ToolInvocation::ToolInvocation( |
| 147 | ArrayRef<std::string> CommandLine, FrontendAction *ToolAction, |
| 148 | FileManager *Files) |
| 149 | : CommandLine(CommandLine.vec()), ToolAction(ToolAction), Files(Files) { |
| 150 | } |
| 151 | |
| 152 | void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { |
NAKAMURA Takumi | 0ef8db2 | 2012-06-02 15:34:21 +0000 | [diff] [blame] | 153 | SmallString<1024> PathStorage; |
| 154 | llvm::sys::path::native(FilePath, PathStorage); |
| 155 | MappedFileContents[PathStorage] = Content; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool ToolInvocation::run() { |
| 159 | std::vector<const char*> Argv; |
| 160 | for (int I = 0, E = CommandLine.size(); I != E; ++I) |
| 161 | Argv.push_back(CommandLine[I].c_str()); |
| 162 | const char *const BinaryName = Argv[0]; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 163 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 164 | TextDiagnosticPrinter DiagnosticPrinter( |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 165 | llvm::errs(), &*DiagOpts); |
| 166 | DiagnosticsEngine Diagnostics( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 167 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 168 | &*DiagOpts, &DiagnosticPrinter, false); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 169 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 170 | const OwningPtr<clang::driver::Driver> Driver( |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 171 | newDriver(&Diagnostics, BinaryName)); |
| 172 | // Since the input might only be virtual, don't check whether it exists. |
| 173 | Driver->setCheckInputsExist(false); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 174 | const OwningPtr<clang::driver::Compilation> Compilation( |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 175 | Driver->BuildCompilation(llvm::makeArrayRef(Argv))); |
| 176 | const clang::driver::ArgStringList *const CC1Args = getCC1Arguments( |
| 177 | &Diagnostics, Compilation.get()); |
| 178 | if (CC1Args == NULL) { |
| 179 | return false; |
| 180 | } |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 181 | OwningPtr<clang::CompilerInvocation> Invocation( |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 182 | newInvocation(&Diagnostics, *CC1Args)); |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 183 | return runInvocation(BinaryName, Compilation.get(), Invocation.take()); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 186 | bool ToolInvocation::runInvocation( |
| 187 | const char *BinaryName, |
| 188 | clang::driver::Compilation *Compilation, |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 189 | clang::CompilerInvocation *Invocation) { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 190 | // Show the invocation, with -v. |
| 191 | if (Invocation->getHeaderSearchOpts().Verbose) { |
| 192 | llvm::errs() << "clang Invocation:\n"; |
| 193 | Compilation->PrintJob(llvm::errs(), Compilation->getJobs(), "\n", true); |
| 194 | llvm::errs() << "\n"; |
| 195 | } |
| 196 | |
| 197 | // Create a compiler instance to handle the actual work. |
| 198 | clang::CompilerInstance Compiler; |
| 199 | Compiler.setInvocation(Invocation); |
| 200 | Compiler.setFileManager(Files); |
| 201 | // FIXME: What about LangOpts? |
| 202 | |
Alexander Kornienko | 14a1924 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 203 | // ToolAction can have lifetime requirements for Compiler or its members, and |
| 204 | // we need to ensure it's deleted earlier than Compiler. So we pass it to an |
| 205 | // OwningPtr declared after the Compiler variable. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 206 | OwningPtr<FrontendAction> ScopedToolAction(ToolAction.take()); |
Alexander Kornienko | 14a1924 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 207 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 208 | // Create the compilers actual diagnostics engine. |
Sean Silva | d47afb9 | 2013-01-20 01:58:28 +0000 | [diff] [blame] | 209 | Compiler.createDiagnostics(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 210 | if (!Compiler.hasDiagnostics()) |
| 211 | return false; |
| 212 | |
| 213 | Compiler.createSourceManager(*Files); |
| 214 | addFileMappingsTo(Compiler.getSourceManager()); |
| 215 | |
Alexander Kornienko | 14a1924 | 2012-05-31 17:58:43 +0000 | [diff] [blame] | 216 | const bool Success = Compiler.ExecuteAction(*ScopedToolAction); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 217 | |
| 218 | Compiler.resetAndLeakFileManager(); |
Manuel Klimek | 98be860 | 2012-07-31 13:56:54 +0000 | [diff] [blame] | 219 | Files->clearStatCaches(); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 220 | return Success; |
| 221 | } |
| 222 | |
| 223 | void ToolInvocation::addFileMappingsTo(SourceManager &Sources) { |
| 224 | for (llvm::StringMap<StringRef>::const_iterator |
| 225 | It = MappedFileContents.begin(), End = MappedFileContents.end(); |
| 226 | It != End; ++It) { |
| 227 | // Inject the code as the given file name into the preprocessor options. |
| 228 | const llvm::MemoryBuffer *Input = |
| 229 | llvm::MemoryBuffer::getMemBuffer(It->getValue()); |
| 230 | // FIXME: figure out what '0' stands for. |
| 231 | const FileEntry *FromFile = Files->getVirtualFile( |
| 232 | It->getKey(), Input->getBufferSize(), 0); |
Alexander Kornienko | 240193b | 2012-05-30 12:10:28 +0000 | [diff] [blame] | 233 | Sources.overrideFileContents(FromFile, Input); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | ClangTool::ClangTool(const CompilationDatabase &Compilations, |
| 238 | ArrayRef<std::string> SourcePaths) |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 239 | : Files((FileSystemOptions())), |
| 240 | ArgsAdjuster(new ClangSyntaxOnlyAdjuster()) { |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 241 | for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 242 | SmallString<1024> File(getAbsolutePath(SourcePaths[I])); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 243 | |
Manuel Klimek | 00f3c4f | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 244 | std::vector<CompileCommand> CompileCommandsForFile = |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 245 | Compilations.getCompileCommands(File.str()); |
Manuel Klimek | 00f3c4f | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 246 | if (!CompileCommandsForFile.empty()) { |
| 247 | for (int I = 0, E = CompileCommandsForFile.size(); I != E; ++I) { |
| 248 | CompileCommands.push_back(std::make_pair(File.str(), |
| 249 | CompileCommandsForFile[I])); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 250 | } |
| 251 | } else { |
| 252 | // FIXME: There are two use cases here: doing a fuzzy |
| 253 | // "find . -name '*.cc' |xargs tool" match, where as a user I don't care |
| 254 | // about the .cc files that were not found, and the use case where I |
| 255 | // specify all files I want to run over explicitly, where this should |
| 256 | // be an error. We'll want to add an option for this. |
| 257 | llvm::outs() << "Skipping " << File << ". Command line not found.\n"; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { |
| 263 | MappedFileContents.push_back(std::make_pair(FilePath, Content)); |
| 264 | } |
| 265 | |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 266 | void ClangTool::setArgumentsAdjuster(ArgumentsAdjuster *Adjuster) { |
| 267 | ArgsAdjuster.reset(Adjuster); |
| 268 | } |
| 269 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 270 | int ClangTool::run(FrontendActionFactory *ActionFactory) { |
Alexander Kornienko | 30c009b | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 271 | // Exists solely for the purpose of lookup of the resource path. |
| 272 | // This just needs to be some symbol in the binary. |
| 273 | static int StaticSymbol; |
| 274 | // The driver detects the builtin header path based on the path of the |
| 275 | // executable. |
| 276 | // FIXME: On linux, GetMainExecutable is independent of the value of the |
| 277 | // first argument, thus allowing ClangTool and runToolOnCode to just |
| 278 | // pass in made-up names here. Make sure this works on other platforms. |
| 279 | std::string MainExecutable = |
| 280 | llvm::sys::Path::GetMainExecutable("clang_tool", &StaticSymbol).str(); |
| 281 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 282 | bool ProcessingFailed = false; |
Manuel Klimek | 00f3c4f | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 283 | for (unsigned I = 0; I < CompileCommands.size(); ++I) { |
| 284 | std::string File = CompileCommands[I].first; |
| 285 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 286 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 287 | // guaranteeing that all subsequent relative path operations work |
| 288 | // on the same path the original chdir resulted in. This makes a difference |
Alexander Kornienko | 30c009b | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 289 | // for example on network filesystems, where symlinks might be switched |
Manuel Klimek | 00f3c4f | 2012-05-07 09:17:48 +0000 | [diff] [blame] | 290 | // during runtime of the tool. Fixing this depends on having a file system |
| 291 | // abstraction that allows openat() style interactions. |
| 292 | if (chdir(CompileCommands[I].second.Directory.c_str())) |
| 293 | llvm::report_fatal_error("Cannot chdir into \"" + |
| 294 | CompileCommands[I].second.Directory + "\n!"); |
Simon Atanasyan | a01ddc7 | 2012-05-09 16:18:30 +0000 | [diff] [blame] | 295 | std::vector<std::string> CommandLine = |
| 296 | ArgsAdjuster->Adjust(CompileCommands[I].second.CommandLine); |
Alexander Kornienko | 30c009b | 2012-06-04 19:02:59 +0000 | [diff] [blame] | 297 | assert(!CommandLine.empty()); |
| 298 | CommandLine[0] = MainExecutable; |
Edwin Vane | ad7e160 | 2013-03-15 20:14:01 +0000 | [diff] [blame^] | 299 | // FIXME: We need a callback mechanism for the tool writer to output a |
| 300 | // customized message for each file. |
| 301 | DEBUG({ |
| 302 | llvm::dbgs() << "Processing: " << File << ".\n"; |
| 303 | }); |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 304 | ToolInvocation Invocation(CommandLine, ActionFactory->create(), &Files); |
| 305 | for (int I = 0, E = MappedFileContents.size(); I != E; ++I) { |
| 306 | Invocation.mapVirtualFile(MappedFileContents[I].first, |
| 307 | MappedFileContents[I].second); |
| 308 | } |
| 309 | if (!Invocation.run()) { |
Edwin Vane | ad7e160 | 2013-03-15 20:14:01 +0000 | [diff] [blame^] | 310 | // FIXME: Diagnostics should be used instead. |
| 311 | llvm::errs() << "Error while processing " << File << ".\n"; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 312 | ProcessingFailed = true; |
| 313 | } |
| 314 | } |
| 315 | return ProcessingFailed ? 1 : 0; |
| 316 | } |
| 317 | |
| 318 | } // end namespace tooling |
| 319 | } // end namespace clang |