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