1 //===- Tooling.cpp - Running clang standalone tools -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements functions to run clang tools standalone instead
10 //  of running them as a plugin.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Tooling/Tooling.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/DiagnosticIDs.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/FileSystemOptions.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Driver/Compilation.h"
22 #include "clang/Driver/Driver.h"
23 #include "clang/Driver/Job.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.h"
26 #include "clang/Driver/ToolChain.h"
27 #include "clang/Frontend/ASTUnit.h"
28 #include "clang/Frontend/CompilerInstance.h"
29 #include "clang/Frontend/CompilerInvocation.h"
30 #include "clang/Frontend/FrontendDiagnostic.h"
31 #include "clang/Frontend/FrontendOptions.h"
32 #include "clang/Frontend/TextDiagnosticPrinter.h"
33 #include "clang/Lex/HeaderSearchOptions.h"
34 #include "clang/Lex/PreprocessorOptions.h"
35 #include "clang/Tooling/ArgumentsAdjusters.h"
36 #include "clang/Tooling/CompilationDatabase.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/IntrusiveRefCntPtr.h"
39 #include "llvm/ADT/SmallString.h"
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/ADT/Twine.h"
42 #include "llvm/Option/ArgList.h"
43 #include "llvm/Option/OptTable.h"
44 #include "llvm/Option/Option.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FileSystem.h"
49 #include "llvm/Support/Host.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/Path.h"
52 #include "llvm/Support/VirtualFileSystem.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include <cassert>
55 #include <cstring>
56 #include <memory>
57 #include <string>
58 #include <system_error>
59 #include <utility>
60 #include <vector>
61 
62 #define DEBUG_TYPE "clang-tooling"
63 
64 using namespace clang;
65 using namespace tooling;
66 
67 ToolAction::~ToolAction() = default;
68 
69 FrontendActionFactory::~FrontendActionFactory() = default;
70 
71 // FIXME: This file contains structural duplication with other parts of the
72 // code that sets up a compiler to run tools on it, and we should refactor
73 // it to be based on the same framework.
74 
75 /// Builds a clang driver initialized for running clang tools.
76 static driver::Driver *
77 newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName,
78           IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
79   driver::Driver *CompilerDriver =
80       new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
81                          *Diagnostics, "clang LLVM compiler", std::move(VFS));
82   CompilerDriver->setTitle("clang_based_tool");
83   return CompilerDriver;
84 }
85 
86 /// Decide whether extra compiler frontend commands can be ignored.
87 static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation) {
88   const driver::JobList &Jobs = Compilation->getJobs();
89   const driver::ActionList &Actions = Compilation->getActions();
90 
91   bool OffloadCompilation = false;
92 
93   // Jobs and Actions look very different depending on whether the Clang tool
94   // injected -fsyntax-only or not. Try to handle both cases here.
95 
96   for (const auto &Job : Jobs)
97     if (StringRef(Job.getExecutable()) == "clang-offload-bundler")
98       OffloadCompilation = true;
99 
100   if (Jobs.size() > 1) {
101     for (auto A : Actions){
102       // On MacOSX real actions may end up being wrapped in BindArchAction
103       if (isa<driver::BindArchAction>(A))
104         A = *A->input_begin();
105       if (isa<driver::OffloadAction>(A)) {
106         // Offload compilation has 2 top-level actions, one (at the front) is
107         // the original host compilation and the other is offload action
108         // composed of at least one device compilation. For such case, general
109         // tooling will consider host-compilation only. For tooling on device
110         // compilation, device compilation only option, such as
111         // `--cuda-device-only`, needs specifying.
112         assert(Actions.size() > 1);
113         assert(
114             isa<driver::CompileJobAction>(Actions.front()) ||
115             // On MacOSX real actions may end up being wrapped in
116             // BindArchAction.
117             (isa<driver::BindArchAction>(Actions.front()) &&
118              isa<driver::CompileJobAction>(*Actions.front()->input_begin())));
119         OffloadCompilation = true;
120         break;
121       }
122     }
123   }
124 
125   return OffloadCompilation;
126 }
127 
128 namespace clang {
129 namespace tooling {
130 
131 const llvm::opt::ArgStringList *
132 getCC1Arguments(DiagnosticsEngine *Diagnostics,
133                 driver::Compilation *Compilation) {
134   const driver::JobList &Jobs = Compilation->getJobs();
135 
136   auto IsCC1Command = [](const driver::Command &Cmd) {
137     return StringRef(Cmd.getCreator().getName()) == "clang";
138   };
139 
140   auto IsSrcFile = [](const driver::InputInfo &II) {
141     return isSrcFile(II.getType());
142   };
143 
144   llvm::SmallVector<const driver::Command *, 1> CC1Jobs;
145   for (const driver::Command &Job : Jobs)
146     if (IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
147       CC1Jobs.push_back(&Job);
148 
149   if (CC1Jobs.empty() ||
150       (CC1Jobs.size() > 1 && !ignoreExtraCC1Commands(Compilation))) {
151     SmallString<256> error_msg;
152     llvm::raw_svector_ostream error_stream(error_msg);
153     Jobs.Print(error_stream, "; ", true);
154     Diagnostics->Report(diag::err_fe_expected_compiler_job)
155         << error_stream.str();
156     return nullptr;
157   }
158 
159   return &CC1Jobs[0]->getArguments();
160 }
161 
162 /// Returns a clang build invocation initialized from the CC1 flags.
163 CompilerInvocation *newInvocation(DiagnosticsEngine *Diagnostics,
164                                   const llvm::opt::ArgStringList &CC1Args,
165                                   const char *const BinaryName) {
166   assert(!CC1Args.empty() && "Must at least contain the program name!");
167   CompilerInvocation *Invocation = new CompilerInvocation;
168   CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics,
169                                      BinaryName);
170   Invocation->getFrontendOpts().DisableFree = false;
171   Invocation->getCodeGenOpts().DisableFree = false;
172   return Invocation;
173 }
174 
175 bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
176                    const Twine &Code, const Twine &FileName,
177                    std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
178   return runToolOnCodeWithArgs(std::move(ToolAction), Code,
179                                std::vector<std::string>(), FileName,
180                                "clang-tool", std::move(PCHContainerOps));
181 }
182 
183 } // namespace tooling
184 } // namespace clang
185 
186 static std::vector<std::string>
187 getSyntaxOnlyToolArgs(const Twine &ToolName,
188                       const std::vector<std::string> &ExtraArgs,
189                       StringRef FileName) {
190   std::vector<std::string> Args;
191   Args.push_back(ToolName.str());
192   Args.push_back("-fsyntax-only");
193   Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
194   Args.push_back(FileName.str());
195   return Args;
196 }
197 
198 namespace clang {
199 namespace tooling {
200 
201 bool runToolOnCodeWithArgs(
202     std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
203     llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
204     const std::vector<std::string> &Args, const Twine &FileName,
205     const Twine &ToolName,
206     std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
207   SmallString<16> FileNameStorage;
208   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
209 
210   llvm::IntrusiveRefCntPtr<FileManager> Files(
211       new FileManager(FileSystemOptions(), VFS));
212   ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
213   ToolInvocation Invocation(
214       getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
215       std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
216   return Invocation.run();
217 }
218 
219 bool runToolOnCodeWithArgs(
220     std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
221     const std::vector<std::string> &Args, const Twine &FileName,
222     const Twine &ToolName,
223     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
224     const FileContentMappings &VirtualMappedFiles) {
225   llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
226       new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
227   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
228       new llvm::vfs::InMemoryFileSystem);
229   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
230 
231   SmallString<1024> CodeStorage;
232   InMemoryFileSystem->addFile(FileName, 0,
233                               llvm::MemoryBuffer::getMemBuffer(
234                                   Code.toNullTerminatedStringRef(CodeStorage)));
235 
236   for (auto &FilenameWithContent : VirtualMappedFiles) {
237     InMemoryFileSystem->addFile(
238         FilenameWithContent.first, 0,
239         llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
240   }
241 
242   return runToolOnCodeWithArgs(std::move(ToolAction), Code, OverlayFileSystem,
243                                Args, FileName, ToolName);
244 }
245 
246 llvm::Expected<std::string> getAbsolutePath(llvm::vfs::FileSystem &FS,
247                                             StringRef File) {
248   StringRef RelativePath(File);
249   // FIXME: Should '.\\' be accepted on Win32?
250   if (RelativePath.startswith("./")) {
251     RelativePath = RelativePath.substr(strlen("./"));
252   }
253 
254   SmallString<1024> AbsolutePath = RelativePath;
255   if (auto EC = FS.makeAbsolute(AbsolutePath))
256     return llvm::errorCodeToError(EC);
257   llvm::sys::path::native(AbsolutePath);
258   return std::string(AbsolutePath.str());
259 }
260 
261 std::string getAbsolutePath(StringRef File) {
262   return llvm::cantFail(getAbsolutePath(*llvm::vfs::getRealFileSystem(), File));
263 }
264 
265 void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
266                                     StringRef InvokedAs) {
267   if (CommandLine.empty() || InvokedAs.empty())
268     return;
269   const auto &Table = driver::getDriverOptTable();
270   // --target=X
271   const std::string TargetOPT =
272       Table.getOption(driver::options::OPT_target).getPrefixedName();
273   // -target X
274   const std::string TargetOPTLegacy =
275       Table.getOption(driver::options::OPT_target_legacy_spelling)
276           .getPrefixedName();
277   // --driver-mode=X
278   const std::string DriverModeOPT =
279       Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
280   auto TargetMode =
281       driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
282   // No need to search for target args if we don't have a target/mode to insert.
283   bool ShouldAddTarget = TargetMode.TargetIsValid;
284   bool ShouldAddMode = TargetMode.DriverMode != nullptr;
285   // Skip CommandLine[0].
286   for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
287        ++Token) {
288     StringRef TokenRef(*Token);
289     ShouldAddTarget = ShouldAddTarget && !TokenRef.startswith(TargetOPT) &&
290                       !TokenRef.equals(TargetOPTLegacy);
291     ShouldAddMode = ShouldAddMode && !TokenRef.startswith(DriverModeOPT);
292   }
293   if (ShouldAddMode) {
294     CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
295   }
296   if (ShouldAddTarget) {
297     CommandLine.insert(++CommandLine.begin(),
298                        TargetOPT + TargetMode.TargetPrefix);
299   }
300 }
301 
302 } // namespace tooling
303 } // namespace clang
304 
305 namespace {
306 
307 class SingleFrontendActionFactory : public FrontendActionFactory {
308   std::unique_ptr<FrontendAction> Action;
309 
310 public:
311   SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
312       : Action(std::move(Action)) {}
313 
314   std::unique_ptr<FrontendAction> create() override {
315     return std::move(Action);
316   }
317 };
318 
319 } // namespace
320 
321 ToolInvocation::ToolInvocation(
322     std::vector<std::string> CommandLine, ToolAction *Action,
323     FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
324     : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false),
325       Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
326 
327 ToolInvocation::ToolInvocation(
328     std::vector<std::string> CommandLine,
329     std::unique_ptr<FrontendAction> FAction, FileManager *Files,
330     std::shared_ptr<PCHContainerOperations> PCHContainerOps)
331     : CommandLine(std::move(CommandLine)),
332       Action(new SingleFrontendActionFactory(std::move(FAction))),
333       OwnsAction(true), Files(Files),
334       PCHContainerOps(std::move(PCHContainerOps)) {}
335 
336 ToolInvocation::~ToolInvocation() {
337   if (OwnsAction)
338     delete Action;
339 }
340 
341 bool ToolInvocation::run() {
342   std::vector<const char*> Argv;
343   for (const std::string &Str : CommandLine)
344     Argv.push_back(Str.c_str());
345   const char *const BinaryName = Argv[0];
346   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
347       CreateAndPopulateDiagOpts(Argv);
348   TextDiagnosticPrinter DiagnosticPrinter(
349       llvm::errs(), &*DiagOpts);
350   DiagnosticsEngine Diagnostics(
351       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
352       DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
353   // Although `Diagnostics` are used only for command-line parsing, the custom
354   // `DiagConsumer` might expect a `SourceManager` to be present.
355   SourceManager SrcMgr(Diagnostics, *Files);
356   Diagnostics.setSourceManager(&SrcMgr);
357 
358   const std::unique_ptr<driver::Driver> Driver(
359       newDriver(&Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
360   // The "input file not found" diagnostics from the driver are useful.
361   // The driver is only aware of the VFS working directory, but some clients
362   // change this at the FileManager level instead.
363   // In this case the checks have false positives, so skip them.
364   if (!Files->getFileSystemOpts().WorkingDir.empty())
365     Driver->setCheckInputsExist(false);
366   const std::unique_ptr<driver::Compilation> Compilation(
367       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
368   if (!Compilation)
369     return false;
370   const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
371       &Diagnostics, Compilation.get());
372   if (!CC1Args)
373     return false;
374   std::unique_ptr<CompilerInvocation> Invocation(
375       newInvocation(&Diagnostics, *CC1Args, BinaryName));
376   return runInvocation(BinaryName, Compilation.get(), std::move(Invocation),
377                        std::move(PCHContainerOps));
378 }
379 
380 bool ToolInvocation::runInvocation(
381     const char *BinaryName, driver::Compilation *Compilation,
382     std::shared_ptr<CompilerInvocation> Invocation,
383     std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
384   // Show the invocation, with -v.
385   if (Invocation->getHeaderSearchOpts().Verbose) {
386     llvm::errs() << "clang Invocation:\n";
387     Compilation->getJobs().Print(llvm::errs(), "\n", true);
388     llvm::errs() << "\n";
389   }
390 
391   return Action->runInvocation(std::move(Invocation), Files,
392                                std::move(PCHContainerOps), DiagConsumer);
393 }
394 
395 bool FrontendActionFactory::runInvocation(
396     std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files,
397     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
398     DiagnosticConsumer *DiagConsumer) {
399   // Create a compiler instance to handle the actual work.
400   CompilerInstance Compiler(std::move(PCHContainerOps));
401   Compiler.setInvocation(std::move(Invocation));
402   Compiler.setFileManager(Files);
403 
404   // The FrontendAction can have lifetime requirements for Compiler or its
405   // members, and we need to ensure it's deleted earlier than Compiler. So we
406   // pass it to an std::unique_ptr declared after the Compiler variable.
407   std::unique_ptr<FrontendAction> ScopedToolAction(create());
408 
409   // Create the compiler's actual diagnostics engine.
410   Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
411   if (!Compiler.hasDiagnostics())
412     return false;
413 
414   Compiler.createSourceManager(*Files);
415 
416   const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
417 
418   Files->clearStatCache();
419   return Success;
420 }
421 
422 ClangTool::ClangTool(const CompilationDatabase &Compilations,
423                      ArrayRef<std::string> SourcePaths,
424                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
425                      IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
426                      IntrusiveRefCntPtr<FileManager> Files)
427     : Compilations(Compilations), SourcePaths(SourcePaths),
428       PCHContainerOps(std::move(PCHContainerOps)),
429       OverlayFileSystem(new llvm::vfs::OverlayFileSystem(std::move(BaseFS))),
430       InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
431       Files(Files ? Files
432                   : new FileManager(FileSystemOptions(), OverlayFileSystem)) {
433   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
434   appendArgumentsAdjuster(getClangStripOutputAdjuster());
435   appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
436   appendArgumentsAdjuster(getClangStripDependencyFileAdjuster());
437   if (Files)
438     Files->setVirtualFileSystem(OverlayFileSystem);
439 }
440 
441 ClangTool::~ClangTool() = default;
442 
443 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
444   MappedFileContents.push_back(std::make_pair(FilePath, Content));
445 }
446 
447 void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
448   ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
449 }
450 
451 void ClangTool::clearArgumentsAdjusters() {
452   ArgsAdjuster = nullptr;
453 }
454 
455 static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
456                               void *MainAddr) {
457   // Allow users to override the resource dir.
458   for (StringRef Arg : Args)
459     if (Arg.startswith("-resource-dir"))
460       return;
461 
462   // If there's no override in place add our resource dir.
463   Args = getInsertArgumentAdjuster(
464       ("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
465           .c_str())(Args, "");
466 }
467 
468 int ClangTool::run(ToolAction *Action) {
469   // Exists solely for the purpose of lookup of the resource path.
470   // This just needs to be some symbol in the binary.
471   static int StaticSymbol;
472 
473   // First insert all absolute paths into the in-memory VFS. These are global
474   // for all compile commands.
475   if (SeenWorkingDirectories.insert("/").second)
476     for (const auto &MappedFile : MappedFileContents)
477       if (llvm::sys::path::is_absolute(MappedFile.first))
478         InMemoryFileSystem->addFile(
479             MappedFile.first, 0,
480             llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
481 
482   bool ProcessingFailed = false;
483   bool FileSkipped = false;
484   // Compute all absolute paths before we run any actions, as those will change
485   // the working directory.
486   std::vector<std::string> AbsolutePaths;
487   AbsolutePaths.reserve(SourcePaths.size());
488   for (const auto &SourcePath : SourcePaths) {
489     auto AbsPath = getAbsolutePath(*OverlayFileSystem, SourcePath);
490     if (!AbsPath) {
491       llvm::errs() << "Skipping " << SourcePath
492                    << ". Error while getting an absolute path: "
493                    << llvm::toString(AbsPath.takeError()) << "\n";
494       continue;
495     }
496     AbsolutePaths.push_back(std::move(*AbsPath));
497   }
498 
499   // Remember the working directory in case we need to restore it.
500   std::string InitialWorkingDir;
501   if (RestoreCWD) {
502     if (auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
503       InitialWorkingDir = std::move(*CWD);
504     } else {
505       llvm::errs() << "Could not get working directory: "
506                    << CWD.getError().message() << "\n";
507     }
508   }
509 
510   for (llvm::StringRef File : AbsolutePaths) {
511     // Currently implementations of CompilationDatabase::getCompileCommands can
512     // change the state of the file system (e.g.  prepare generated headers), so
513     // this method needs to run right before we invoke the tool, as the next
514     // file may require a different (incompatible) state of the file system.
515     //
516     // FIXME: Make the compilation database interface more explicit about the
517     // requirements to the order of invocation of its members.
518     std::vector<CompileCommand> CompileCommandsForFile =
519         Compilations.getCompileCommands(File);
520     if (CompileCommandsForFile.empty()) {
521       llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
522       FileSkipped = true;
523       continue;
524     }
525     for (CompileCommand &CompileCommand : CompileCommandsForFile) {
526       // FIXME: chdir is thread hostile; on the other hand, creating the same
527       // behavior as chdir is complex: chdir resolves the path once, thus
528       // guaranteeing that all subsequent relative path operations work
529       // on the same path the original chdir resulted in. This makes a
530       // difference for example on network filesystems, where symlinks might be
531       // switched during runtime of the tool. Fixing this depends on having a
532       // file system abstraction that allows openat() style interactions.
533       if (OverlayFileSystem->setCurrentWorkingDirectory(
534               CompileCommand.Directory))
535         llvm::report_fatal_error("Cannot chdir into \"" +
536                                  Twine(CompileCommand.Directory) + "\"!");
537 
538       // Now fill the in-memory VFS with the relative file mappings so it will
539       // have the correct relative paths. We never remove mappings but that
540       // should be fine.
541       if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
542         for (const auto &MappedFile : MappedFileContents)
543           if (!llvm::sys::path::is_absolute(MappedFile.first))
544             InMemoryFileSystem->addFile(
545                 MappedFile.first, 0,
546                 llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
547 
548       std::vector<std::string> CommandLine = CompileCommand.CommandLine;
549       if (ArgsAdjuster)
550         CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename);
551       assert(!CommandLine.empty());
552 
553       // Add the resource dir based on the binary of this tool. argv[0] in the
554       // compilation database may refer to a different compiler and we want to
555       // pick up the very same standard library that compiler is using. The
556       // builtin headers in the resource dir need to match the exact clang
557       // version the tool is using.
558       // FIXME: On linux, GetMainExecutable is independent of the value of the
559       // first argument, thus allowing ClangTool and runToolOnCode to just
560       // pass in made-up names here. Make sure this works on other platforms.
561       injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
562 
563       // FIXME: We need a callback mechanism for the tool writer to output a
564       // customized message for each file.
565       LLVM_DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; });
566       ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
567                                 PCHContainerOps);
568       Invocation.setDiagnosticConsumer(DiagConsumer);
569 
570       if (!Invocation.run()) {
571         // FIXME: Diagnostics should be used instead.
572         if (PrintErrorMessage)
573           llvm::errs() << "Error while processing " << File << ".\n";
574         ProcessingFailed = true;
575       }
576     }
577   }
578 
579   if (!InitialWorkingDir.empty()) {
580     if (auto EC =
581             OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
582       llvm::errs() << "Error when trying to restore working dir: "
583                    << EC.message() << "\n";
584   }
585   return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
586 }
587 
588 namespace {
589 
590 class ASTBuilderAction : public ToolAction {
591   std::vector<std::unique_ptr<ASTUnit>> &ASTs;
592 
593 public:
594   ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
595 
596   bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
597                      FileManager *Files,
598                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
599                      DiagnosticConsumer *DiagConsumer) override {
600     std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
601         Invocation, std::move(PCHContainerOps),
602         CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
603                                             DiagConsumer,
604                                             /*ShouldOwnClient=*/false),
605         Files);
606     if (!AST)
607       return false;
608 
609     ASTs.push_back(std::move(AST));
610     return true;
611   }
612 };
613 
614 } // namespace
615 
616 int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
617   ASTBuilderAction Action(ASTs);
618   return run(&Action);
619 }
620 
621 void ClangTool::setRestoreWorkingDir(bool RestoreCWD) {
622   this->RestoreCWD = RestoreCWD;
623 }
624 
625 void ClangTool::setPrintErrorMessage(bool PrintErrorMessage) {
626   this->PrintErrorMessage = PrintErrorMessage;
627 }
628 
629 namespace clang {
630 namespace tooling {
631 
632 std::unique_ptr<ASTUnit>
633 buildASTFromCode(StringRef Code, StringRef FileName,
634                  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
635   return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName,
636                                   "clang-tool", std::move(PCHContainerOps));
637 }
638 
639 std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
640     StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
641     StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
642     ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
643     DiagnosticConsumer *DiagConsumer) {
644   std::vector<std::unique_ptr<ASTUnit>> ASTs;
645   ASTBuilderAction Action(ASTs);
646   llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
647       new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
648   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
649       new llvm::vfs::InMemoryFileSystem);
650   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
651   llvm::IntrusiveRefCntPtr<FileManager> Files(
652       new FileManager(FileSystemOptions(), OverlayFileSystem));
653 
654   ToolInvocation Invocation(
655       getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),
656       &Action, Files.get(), std::move(PCHContainerOps));
657   Invocation.setDiagnosticConsumer(DiagConsumer);
658 
659   InMemoryFileSystem->addFile(FileName, 0,
660                               llvm::MemoryBuffer::getMemBufferCopy(Code));
661   for (auto &FilenameWithContent : VirtualMappedFiles) {
662     InMemoryFileSystem->addFile(
663         FilenameWithContent.first, 0,
664         llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
665   }
666 
667   if (!Invocation.run())
668     return nullptr;
669 
670   assert(ASTs.size() == 1);
671   return std::move(ASTs[0]);
672 }
673 
674 } // namespace tooling
675 } // namespace clang
676