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"
16 #include "clang/Driver/Compilation.h"
17 #include "clang/Driver/Driver.h"
18 #include "clang/Driver/Tool.h"
19 #include "clang/Driver/ToolChain.h"
20 #include "clang/Frontend/ASTUnit.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/FrontendDiagnostic.h"
23 #include "clang/Frontend/TextDiagnosticPrinter.h"
24 #include "clang/Lex/PreprocessorOptions.h"
25 #include "clang/Tooling/ArgumentsAdjusters.h"
26 #include "clang/Tooling/CompilationDatabase.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/Config/llvm-config.h"
29 #include "llvm/Option/Option.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Host.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include <utility>
36 
37 #define DEBUG_TYPE "clang-tooling"
38 
39 namespace clang {
40 namespace tooling {
41 
42 ToolAction::~ToolAction() {}
43 
44 FrontendActionFactory::~FrontendActionFactory() {}
45 
46 // FIXME: This file contains structural duplication with other parts of the
47 // code that sets up a compiler to run tools on it, and we should refactor
48 // it to be based on the same framework.
49 
50 /// \brief Builds a clang driver initialized for running clang tools.
51 static clang::driver::Driver *newDriver(
52     clang::DiagnosticsEngine *Diagnostics, const char *BinaryName,
53     IntrusiveRefCntPtr<vfs::FileSystem> VFS) {
54   clang::driver::Driver *CompilerDriver =
55       new clang::driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
56                                 *Diagnostics, std::move(VFS));
57   CompilerDriver->setTitle("clang_based_tool");
58   return CompilerDriver;
59 }
60 
61 /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs.
62 ///
63 /// Returns NULL on error.
64 static const llvm::opt::ArgStringList *getCC1Arguments(
65     clang::DiagnosticsEngine *Diagnostics,
66     clang::driver::Compilation *Compilation) {
67   // We expect to get back exactly one Command job, if we didn't something
68   // failed. Extract that job from the Compilation.
69   const clang::driver::JobList &Jobs = Compilation->getJobs();
70   if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) {
71     SmallString<256> error_msg;
72     llvm::raw_svector_ostream error_stream(error_msg);
73     Jobs.Print(error_stream, "; ", true);
74     Diagnostics->Report(clang::diag::err_fe_expected_compiler_job)
75         << error_stream.str();
76     return nullptr;
77   }
78 
79   // The one job we find should be to invoke clang again.
80   const clang::driver::Command &Cmd =
81       cast<clang::driver::Command>(*Jobs.begin());
82   if (StringRef(Cmd.getCreator().getName()) != "clang") {
83     Diagnostics->Report(clang::diag::err_fe_expected_clang_command);
84     return nullptr;
85   }
86 
87   return &Cmd.getArguments();
88 }
89 
90 /// \brief Returns a clang build invocation initialized from the CC1 flags.
91 clang::CompilerInvocation *newInvocation(
92     clang::DiagnosticsEngine *Diagnostics,
93     const llvm::opt::ArgStringList &CC1Args) {
94   assert(!CC1Args.empty() && "Must at least contain the program name!");
95   clang::CompilerInvocation *Invocation = new clang::CompilerInvocation;
96   clang::CompilerInvocation::CreateFromArgs(
97       *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(),
98       *Diagnostics);
99   Invocation->getFrontendOpts().DisableFree = false;
100   Invocation->getCodeGenOpts().DisableFree = false;
101   Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
102   return Invocation;
103 }
104 
105 bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
106                    const Twine &FileName,
107                    std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
108   return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(),
109                                FileName, "clang-tool",
110                                std::move(PCHContainerOps));
111 }
112 
113 static std::vector<std::string>
114 getSyntaxOnlyToolArgs(const Twine &ToolName,
115                       const std::vector<std::string> &ExtraArgs,
116                       StringRef FileName) {
117   std::vector<std::string> Args;
118   Args.push_back(ToolName.str());
119   Args.push_back("-fsyntax-only");
120   Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
121   Args.push_back(FileName.str());
122   return Args;
123 }
124 
125 bool runToolOnCodeWithArgs(
126     clang::FrontendAction *ToolAction, const Twine &Code,
127     const std::vector<std::string> &Args, const Twine &FileName,
128     const Twine &ToolName,
129     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
130     const FileContentMappings &VirtualMappedFiles) {
131 
132   SmallString<16> FileNameStorage;
133   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
134   llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
135       new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
136   llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
137       new vfs::InMemoryFileSystem);
138   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
139   llvm::IntrusiveRefCntPtr<FileManager> Files(
140       new FileManager(FileSystemOptions(), OverlayFileSystem));
141   ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef),
142                             ToolAction, Files.get(),
143                             std::move(PCHContainerOps));
144 
145   SmallString<1024> CodeStorage;
146   InMemoryFileSystem->addFile(FileNameRef, 0,
147                               llvm::MemoryBuffer::getMemBuffer(
148                                   Code.toNullTerminatedStringRef(CodeStorage)));
149 
150   for (auto &FilenameWithContent : VirtualMappedFiles) {
151     InMemoryFileSystem->addFile(
152         FilenameWithContent.first, 0,
153         llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
154   }
155 
156   return Invocation.run();
157 }
158 
159 std::string getAbsolutePath(StringRef File) {
160   StringRef RelativePath(File);
161   // FIXME: Should '.\\' be accepted on Win32?
162   if (RelativePath.startswith("./")) {
163     RelativePath = RelativePath.substr(strlen("./"));
164   }
165 
166   SmallString<1024> AbsolutePath = RelativePath;
167   std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath);
168   assert(!EC);
169   (void)EC;
170   llvm::sys::path::native(AbsolutePath);
171   return AbsolutePath.str();
172 }
173 
174 void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
175                                     StringRef InvokedAs) {
176   if (!CommandLine.empty() && !InvokedAs.empty()) {
177     bool AlreadyHasTarget = false;
178     bool AlreadyHasMode = false;
179     // Skip CommandLine[0].
180     for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
181          ++Token) {
182       StringRef TokenRef(*Token);
183       AlreadyHasTarget |=
184           (TokenRef == "-target" || TokenRef.startswith("-target="));
185       AlreadyHasMode |= (TokenRef == "--driver-mode" ||
186                          TokenRef.startswith("--driver-mode="));
187     }
188     auto TargetMode =
189         clang::driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs);
190     if (!AlreadyHasMode && !TargetMode.second.empty()) {
191       CommandLine.insert(++CommandLine.begin(), TargetMode.second);
192     }
193     if (!AlreadyHasTarget && !TargetMode.first.empty()) {
194       CommandLine.insert(++CommandLine.begin(), {"-target", TargetMode.first});
195     }
196   }
197 }
198 
199 namespace {
200 
201 class SingleFrontendActionFactory : public FrontendActionFactory {
202   FrontendAction *Action;
203 
204 public:
205   SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
206 
207   FrontendAction *create() override { return Action; }
208 };
209 
210 }
211 
212 ToolInvocation::ToolInvocation(
213     std::vector<std::string> CommandLine, ToolAction *Action,
214     FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
215     : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false),
216       Files(Files), PCHContainerOps(std::move(PCHContainerOps)),
217       DiagConsumer(nullptr) {}
218 
219 ToolInvocation::ToolInvocation(
220     std::vector<std::string> CommandLine, FrontendAction *FAction,
221     FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
222     : CommandLine(std::move(CommandLine)),
223       Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true),
224       Files(Files), PCHContainerOps(std::move(PCHContainerOps)),
225       DiagConsumer(nullptr) {}
226 
227 ToolInvocation::~ToolInvocation() {
228   if (OwnsAction)
229     delete Action;
230 }
231 
232 void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) {
233   SmallString<1024> PathStorage;
234   llvm::sys::path::native(FilePath, PathStorage);
235   MappedFileContents[PathStorage] = Content;
236 }
237 
238 bool ToolInvocation::run() {
239   std::vector<const char*> Argv;
240   for (const std::string &Str : CommandLine)
241     Argv.push_back(Str.c_str());
242   const char *const BinaryName = Argv[0];
243   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
244   TextDiagnosticPrinter DiagnosticPrinter(
245       llvm::errs(), &*DiagOpts);
246   DiagnosticsEngine Diagnostics(
247       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
248       DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
249 
250   const std::unique_ptr<clang::driver::Driver> Driver(
251       newDriver(&Diagnostics, BinaryName, Files->getVirtualFileSystem()));
252   // Since the input might only be virtual, don't check whether it exists.
253   Driver->setCheckInputsExist(false);
254   const std::unique_ptr<clang::driver::Compilation> Compilation(
255       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
256   const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
257       &Diagnostics, Compilation.get());
258   if (!CC1Args) {
259     return false;
260   }
261   std::unique_ptr<clang::CompilerInvocation> Invocation(
262       newInvocation(&Diagnostics, *CC1Args));
263   // FIXME: remove this when all users have migrated!
264   for (const auto &It : MappedFileContents) {
265     // Inject the code as the given file name into the preprocessor options.
266     std::unique_ptr<llvm::MemoryBuffer> Input =
267         llvm::MemoryBuffer::getMemBuffer(It.getValue());
268     Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(),
269                                                       Input.release());
270   }
271   return runInvocation(BinaryName, Compilation.get(), Invocation.release(),
272                        std::move(PCHContainerOps));
273 }
274 
275 bool ToolInvocation::runInvocation(
276     const char *BinaryName, clang::driver::Compilation *Compilation,
277     clang::CompilerInvocation *Invocation,
278     std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
279   // Show the invocation, with -v.
280   if (Invocation->getHeaderSearchOpts().Verbose) {
281     llvm::errs() << "clang Invocation:\n";
282     Compilation->getJobs().Print(llvm::errs(), "\n", true);
283     llvm::errs() << "\n";
284   }
285 
286   return Action->runInvocation(Invocation, Files, std::move(PCHContainerOps),
287                                DiagConsumer);
288 }
289 
290 bool FrontendActionFactory::runInvocation(
291     CompilerInvocation *Invocation, FileManager *Files,
292     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
293     DiagnosticConsumer *DiagConsumer) {
294   // Create a compiler instance to handle the actual work.
295   clang::CompilerInstance Compiler(std::move(PCHContainerOps));
296   Compiler.setInvocation(Invocation);
297   Compiler.setFileManager(Files);
298 
299   // The FrontendAction can have lifetime requirements for Compiler or its
300   // members, and we need to ensure it's deleted earlier than Compiler. So we
301   // pass it to an std::unique_ptr declared after the Compiler variable.
302   std::unique_ptr<FrontendAction> ScopedToolAction(create());
303 
304   // Create the compiler's actual diagnostics engine.
305   Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
306   if (!Compiler.hasDiagnostics())
307     return false;
308 
309   Compiler.createSourceManager(*Files);
310 
311   const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
312 
313   Files->clearStatCaches();
314   return Success;
315 }
316 
317 ClangTool::ClangTool(const CompilationDatabase &Compilations,
318                      ArrayRef<std::string> SourcePaths,
319                      std::shared_ptr<PCHContainerOperations> PCHContainerOps)
320     : Compilations(Compilations), SourcePaths(SourcePaths),
321       PCHContainerOps(std::move(PCHContainerOps)),
322       OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())),
323       InMemoryFileSystem(new vfs::InMemoryFileSystem),
324       Files(new FileManager(FileSystemOptions(), OverlayFileSystem)),
325       DiagConsumer(nullptr) {
326   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
327   appendArgumentsAdjuster(getClangStripOutputAdjuster());
328   appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
329 }
330 
331 ClangTool::~ClangTool() {}
332 
333 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
334   MappedFileContents.push_back(std::make_pair(FilePath, Content));
335 }
336 
337 void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
338   if (ArgsAdjuster)
339     ArgsAdjuster =
340         combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
341   else
342     ArgsAdjuster = std::move(Adjuster);
343 }
344 
345 void ClangTool::clearArgumentsAdjusters() {
346   ArgsAdjuster = nullptr;
347 }
348 
349 static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
350                               void *MainAddr) {
351   // Allow users to override the resource dir.
352   for (StringRef Arg : Args)
353     if (Arg.startswith("-resource-dir"))
354       return;
355 
356   // If there's no override in place add our resource dir.
357   Args.push_back("-resource-dir=" +
358                  CompilerInvocation::GetResourcesPath(Argv0, MainAddr));
359 }
360 
361 int ClangTool::run(ToolAction *Action) {
362   // Exists solely for the purpose of lookup of the resource path.
363   // This just needs to be some symbol in the binary.
364   static int StaticSymbol;
365 
366   llvm::SmallString<128> InitialDirectory;
367   if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory))
368     llvm::report_fatal_error("Cannot detect current path: " +
369                              Twine(EC.message()));
370 
371   // First insert all absolute paths into the in-memory VFS. These are global
372   // for all compile commands.
373   if (SeenWorkingDirectories.insert("/").second)
374     for (const auto &MappedFile : MappedFileContents)
375       if (llvm::sys::path::is_absolute(MappedFile.first))
376         InMemoryFileSystem->addFile(
377             MappedFile.first, 0,
378             llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
379 
380   bool ProcessingFailed = false;
381   for (const auto &SourcePath : SourcePaths) {
382     std::string File(getAbsolutePath(SourcePath));
383 
384     // Currently implementations of CompilationDatabase::getCompileCommands can
385     // change the state of the file system (e.g.  prepare generated headers), so
386     // this method needs to run right before we invoke the tool, as the next
387     // file may require a different (incompatible) state of the file system.
388     //
389     // FIXME: Make the compilation database interface more explicit about the
390     // requirements to the order of invocation of its members.
391     std::vector<CompileCommand> CompileCommandsForFile =
392         Compilations.getCompileCommands(File);
393     if (CompileCommandsForFile.empty()) {
394       // FIXME: There are two use cases here: doing a fuzzy
395       // "find . -name '*.cc' |xargs tool" match, where as a user I don't care
396       // about the .cc files that were not found, and the use case where I
397       // specify all files I want to run over explicitly, where this should
398       // be an error. We'll want to add an option for this.
399       llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
400       continue;
401     }
402     for (CompileCommand &CompileCommand : CompileCommandsForFile) {
403       // FIXME: chdir is thread hostile; on the other hand, creating the same
404       // behavior as chdir is complex: chdir resolves the path once, thus
405       // guaranteeing that all subsequent relative path operations work
406       // on the same path the original chdir resulted in. This makes a
407       // difference for example on network filesystems, where symlinks might be
408       // switched during runtime of the tool. Fixing this depends on having a
409       // file system abstraction that allows openat() style interactions.
410       if (OverlayFileSystem->setCurrentWorkingDirectory(
411               CompileCommand.Directory))
412         llvm::report_fatal_error("Cannot chdir into \"" +
413                                  Twine(CompileCommand.Directory) + "\n!");
414 
415       // Now fill the in-memory VFS with the relative file mappings so it will
416       // have the correct relative paths. We never remove mappings but that
417       // should be fine.
418       if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
419         for (const auto &MappedFile : MappedFileContents)
420           if (!llvm::sys::path::is_absolute(MappedFile.first))
421             InMemoryFileSystem->addFile(
422                 MappedFile.first, 0,
423                 llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
424 
425       std::vector<std::string> CommandLine = CompileCommand.CommandLine;
426       if (ArgsAdjuster)
427         CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename);
428       assert(!CommandLine.empty());
429 
430       // Add the resource dir based on the binary of this tool. argv[0] in the
431       // compilation database may refer to a different compiler and we want to
432       // pick up the very same standard library that compiler is using. The
433       // builtin headers in the resource dir need to match the exact clang
434       // version the tool is using.
435       // FIXME: On linux, GetMainExecutable is independent of the value of the
436       // first argument, thus allowing ClangTool and runToolOnCode to just
437       // pass in made-up names here. Make sure this works on other platforms.
438       injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
439 
440       // FIXME: We need a callback mechanism for the tool writer to output a
441       // customized message for each file.
442       DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; });
443       ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
444                                 PCHContainerOps);
445       Invocation.setDiagnosticConsumer(DiagConsumer);
446 
447       if (!Invocation.run()) {
448         // FIXME: Diagnostics should be used instead.
449         llvm::errs() << "Error while processing " << File << ".\n";
450         ProcessingFailed = true;
451       }
452       // Return to the initial directory to correctly resolve next file by
453       // relative path.
454       if (OverlayFileSystem->setCurrentWorkingDirectory(InitialDirectory.c_str()))
455         llvm::report_fatal_error("Cannot chdir into \"" +
456                                  Twine(InitialDirectory) + "\n!");
457     }
458   }
459   return ProcessingFailed ? 1 : 0;
460 }
461 
462 namespace {
463 
464 class ASTBuilderAction : public ToolAction {
465   std::vector<std::unique_ptr<ASTUnit>> &ASTs;
466 
467 public:
468   ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
469 
470   bool runInvocation(CompilerInvocation *Invocation, FileManager *Files,
471                      std::shared_ptr<PCHContainerOperations> PCHContainerOps,
472                      DiagnosticConsumer *DiagConsumer) override {
473     std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
474         Invocation, std::move(PCHContainerOps),
475         CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
476                                             DiagConsumer,
477                                             /*ShouldOwnClient=*/false),
478         Files);
479     if (!AST)
480       return false;
481 
482     ASTs.push_back(std::move(AST));
483     return true;
484   }
485 };
486 }
487 
488 int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
489   ASTBuilderAction Action(ASTs);
490   return run(&Action);
491 }
492 
493 std::unique_ptr<ASTUnit>
494 buildASTFromCode(const Twine &Code, const Twine &FileName,
495                  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
496   return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName,
497                                   "clang-tool", std::move(PCHContainerOps));
498 }
499 
500 std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
501     const Twine &Code, const std::vector<std::string> &Args,
502     const Twine &FileName, const Twine &ToolName,
503     std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
504   SmallString<16> FileNameStorage;
505   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
506 
507   std::vector<std::unique_ptr<ASTUnit>> ASTs;
508   ASTBuilderAction Action(ASTs);
509   llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
510       new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
511   llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
512       new vfs::InMemoryFileSystem);
513   OverlayFileSystem->pushOverlay(InMemoryFileSystem);
514   llvm::IntrusiveRefCntPtr<FileManager> Files(
515       new FileManager(FileSystemOptions(), OverlayFileSystem));
516   ToolInvocation Invocation(getSyntaxOnlyToolArgs(ToolName, Args, FileNameRef),
517                             &Action, Files.get(), std::move(PCHContainerOps));
518 
519   SmallString<1024> CodeStorage;
520   InMemoryFileSystem->addFile(FileNameRef, 0,
521                               llvm::MemoryBuffer::getMemBuffer(
522                                   Code.toNullTerminatedStringRef(CodeStorage)));
523   if (!Invocation.run())
524     return nullptr;
525 
526   assert(ASTs.size() == 1);
527   return std::move(ASTs[0]);
528 }
529 
530 } // end namespace tooling
531 } // end namespace clang
532