clang API Documentation

Tooling.cpp
Go to the documentation of this file.
00001 //===--- Tooling.cpp - Running clang standalone tools ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements functions to run clang tools standalone instead
00011 //  of running them as a plugin.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Tooling/Tooling.h"
00016 #include "clang/AST/ASTConsumer.h"
00017 #include "clang/Driver/Compilation.h"
00018 #include "clang/Driver/Driver.h"
00019 #include "clang/Driver/Tool.h"
00020 #include "clang/Frontend/ASTUnit.h"
00021 #include "clang/Frontend/CompilerInstance.h"
00022 #include "clang/Frontend/FrontendDiagnostic.h"
00023 #include "clang/Frontend/TextDiagnosticPrinter.h"
00024 #include "clang/Tooling/ArgumentsAdjusters.h"
00025 #include "clang/Tooling/CompilationDatabase.h"
00026 #include "llvm/ADT/STLExtras.h"
00027 #include "llvm/Config/llvm-config.h"
00028 #include "llvm/Option/Option.h"
00029 #include "llvm/Support/Debug.h"
00030 #include "llvm/Support/FileSystem.h"
00031 #include "llvm/Support/Host.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 
00034 // For chdir, see the comment in ClangTool::run for more information.
00035 #ifdef LLVM_ON_WIN32
00036 #  include <direct.h>
00037 #else
00038 #  include <unistd.h>
00039 #endif
00040 
00041 #define DEBUG_TYPE "clang-tooling"
00042 
00043 namespace clang {
00044 namespace tooling {
00045 
00046 ToolAction::~ToolAction() {}
00047 
00048 FrontendActionFactory::~FrontendActionFactory() {}
00049 
00050 // FIXME: This file contains structural duplication with other parts of the
00051 // code that sets up a compiler to run tools on it, and we should refactor
00052 // it to be based on the same framework.
00053 
00054 /// \brief Builds a clang driver initialized for running clang tools.
00055 static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics,
00056                                         const char *BinaryName) {
00057   clang::driver::Driver *CompilerDriver = new clang::driver::Driver(
00058       BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics);
00059   CompilerDriver->setTitle("clang_based_tool");
00060   return CompilerDriver;
00061 }
00062 
00063 /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs.
00064 ///
00065 /// Returns NULL on error.
00066 static const llvm::opt::ArgStringList *getCC1Arguments(
00067     clang::DiagnosticsEngine *Diagnostics,
00068     clang::driver::Compilation *Compilation) {
00069   // We expect to get back exactly one Command job, if we didn't something
00070   // failed. Extract that job from the Compilation.
00071   const clang::driver::JobList &Jobs = Compilation->getJobs();
00072   if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) {
00073     SmallString<256> error_msg;
00074     llvm::raw_svector_ostream error_stream(error_msg);
00075     Jobs.Print(error_stream, "; ", true);
00076     Diagnostics->Report(clang::diag::err_fe_expected_compiler_job)
00077         << error_stream.str();
00078     return nullptr;
00079   }
00080 
00081   // The one job we find should be to invoke clang again.
00082   const clang::driver::Command &Cmd =
00083       cast<clang::driver::Command>(*Jobs.begin());
00084   if (StringRef(Cmd.getCreator().getName()) != "clang") {
00085     Diagnostics->Report(clang::diag::err_fe_expected_clang_command);
00086     return nullptr;
00087   }
00088 
00089   return &Cmd.getArguments();
00090 }
00091 
00092 /// \brief Returns a clang build invocation initialized from the CC1 flags.
00093 static clang::CompilerInvocation *newInvocation(
00094     clang::DiagnosticsEngine *Diagnostics,
00095     const llvm::opt::ArgStringList &CC1Args) {
00096   assert(!CC1Args.empty() && "Must at least contain the program name!");
00097   clang::CompilerInvocation *Invocation = new clang::CompilerInvocation;
00098   clang::CompilerInvocation::CreateFromArgs(
00099       *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(),
00100       *Diagnostics);
00101   Invocation->getFrontendOpts().DisableFree = false;
00102   Invocation->getCodeGenOpts().DisableFree = false;
00103   Invocation->getDependencyOutputOpts() = DependencyOutputOptions();
00104   return Invocation;
00105 }
00106 
00107 bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
00108                    const Twine &FileName) {
00109   return runToolOnCodeWithArgs(
00110       ToolAction, Code, std::vector<std::string>(), FileName);
00111 }
00112 
00113 static std::vector<std::string>
00114 getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs,
00115                       StringRef FileName) {
00116   std::vector<std::string> Args;
00117   Args.push_back("clang-tool");
00118   Args.push_back("-fsyntax-only");
00119   Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
00120   Args.push_back(FileName.str());
00121   return Args;
00122 }
00123 
00124 bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code,
00125                            const std::vector<std::string> &Args,
00126                            const Twine &FileName) {
00127   SmallString<16> FileNameStorage;
00128   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
00129   llvm::IntrusiveRefCntPtr<FileManager> Files(
00130       new FileManager(FileSystemOptions()));
00131   ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction,
00132                             Files.get());
00133 
00134   SmallString<1024> CodeStorage;
00135   Invocation.mapVirtualFile(FileNameRef,
00136                             Code.toNullTerminatedStringRef(CodeStorage));
00137   return Invocation.run();
00138 }
00139 
00140 std::string getAbsolutePath(StringRef File) {
00141   StringRef RelativePath(File);
00142   // FIXME: Should '.\\' be accepted on Win32?
00143   if (RelativePath.startswith("./")) {
00144     RelativePath = RelativePath.substr(strlen("./"));
00145   }
00146 
00147   SmallString<1024> AbsolutePath = RelativePath;
00148   std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath);
00149   assert(!EC);
00150   (void)EC;
00151   llvm::sys::path::native(AbsolutePath);
00152   return AbsolutePath.str();
00153 }
00154 
00155 namespace {
00156 
00157 class SingleFrontendActionFactory : public FrontendActionFactory {
00158   FrontendAction *Action;
00159 
00160 public:
00161   SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {}
00162 
00163   FrontendAction *create() override { return Action; }
00164 };
00165 
00166 }
00167 
00168 ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine,
00169                                ToolAction *Action, FileManager *Files)
00170     : CommandLine(std::move(CommandLine)),
00171       Action(Action),
00172       OwnsAction(false),
00173       Files(Files),
00174       DiagConsumer(nullptr) {}
00175 
00176 ToolInvocation::ToolInvocation(std::vector<std::string> CommandLine,
00177                                FrontendAction *FAction, FileManager *Files)
00178     : CommandLine(std::move(CommandLine)),
00179       Action(new SingleFrontendActionFactory(FAction)),
00180       OwnsAction(true),
00181       Files(Files),
00182       DiagConsumer(nullptr) {}
00183 
00184 ToolInvocation::~ToolInvocation() {
00185   if (OwnsAction)
00186     delete Action;
00187 }
00188 
00189 void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) {
00190   SmallString<1024> PathStorage;
00191   llvm::sys::path::native(FilePath, PathStorage);
00192   MappedFileContents[PathStorage] = Content;
00193 }
00194 
00195 bool ToolInvocation::run() {
00196   std::vector<const char*> Argv;
00197   for (const std::string &Str : CommandLine)
00198     Argv.push_back(Str.c_str());
00199   const char *const BinaryName = Argv[0];
00200   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
00201   TextDiagnosticPrinter DiagnosticPrinter(
00202       llvm::errs(), &*DiagOpts);
00203   DiagnosticsEngine Diagnostics(
00204       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
00205       DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
00206 
00207   const std::unique_ptr<clang::driver::Driver> Driver(
00208       newDriver(&Diagnostics, BinaryName));
00209   // Since the input might only be virtual, don't check whether it exists.
00210   Driver->setCheckInputsExist(false);
00211   const std::unique_ptr<clang::driver::Compilation> Compilation(
00212       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
00213   const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
00214       &Diagnostics, Compilation.get());
00215   if (!CC1Args) {
00216     return false;
00217   }
00218   std::unique_ptr<clang::CompilerInvocation> Invocation(
00219       newInvocation(&Diagnostics, *CC1Args));
00220   for (const auto &It : MappedFileContents) {
00221     // Inject the code as the given file name into the preprocessor options.
00222     std::unique_ptr<llvm::MemoryBuffer> Input =
00223         llvm::MemoryBuffer::getMemBuffer(It.getValue());
00224     Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(),
00225                                                       Input.release());
00226   }
00227   return runInvocation(BinaryName, Compilation.get(), Invocation.release());
00228 }
00229 
00230 bool ToolInvocation::runInvocation(
00231     const char *BinaryName,
00232     clang::driver::Compilation *Compilation,
00233     clang::CompilerInvocation *Invocation) {
00234   // Show the invocation, with -v.
00235   if (Invocation->getHeaderSearchOpts().Verbose) {
00236     llvm::errs() << "clang Invocation:\n";
00237     Compilation->getJobs().Print(llvm::errs(), "\n", true);
00238     llvm::errs() << "\n";
00239   }
00240 
00241   return Action->runInvocation(Invocation, Files, DiagConsumer);
00242 }
00243 
00244 bool FrontendActionFactory::runInvocation(CompilerInvocation *Invocation,
00245                                           FileManager *Files,
00246                                           DiagnosticConsumer *DiagConsumer) {
00247   // Create a compiler instance to handle the actual work.
00248   clang::CompilerInstance Compiler;
00249   Compiler.setInvocation(Invocation);
00250   Compiler.setFileManager(Files);
00251 
00252   // The FrontendAction can have lifetime requirements for Compiler or its
00253   // members, and we need to ensure it's deleted earlier than Compiler. So we
00254   // pass it to an std::unique_ptr declared after the Compiler variable.
00255   std::unique_ptr<FrontendAction> ScopedToolAction(create());
00256 
00257   // Create the compiler's actual diagnostics engine.
00258   Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
00259   if (!Compiler.hasDiagnostics())
00260     return false;
00261 
00262   Compiler.createSourceManager(*Files);
00263 
00264   const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
00265 
00266   Files->clearStatCaches();
00267   return Success;
00268 }
00269 
00270 ClangTool::ClangTool(const CompilationDatabase &Compilations,
00271                      ArrayRef<std::string> SourcePaths)
00272     : Compilations(Compilations), SourcePaths(SourcePaths),
00273       Files(new FileManager(FileSystemOptions())), DiagConsumer(nullptr) {
00274   appendArgumentsAdjuster(new ClangStripOutputAdjuster());
00275   appendArgumentsAdjuster(new ClangSyntaxOnlyAdjuster());
00276 }
00277 
00278 ClangTool::~ClangTool() {}
00279 
00280 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
00281   MappedFileContents.push_back(std::make_pair(FilePath, Content));
00282 }
00283 
00284 void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster) {
00285   ArgsAdjusters.push_back(std::unique_ptr<ArgumentsAdjuster>(Adjuster));
00286 }
00287 
00288 void ClangTool::clearArgumentsAdjusters() {
00289   ArgsAdjusters.clear();
00290 }
00291 
00292 int ClangTool::run(ToolAction *Action) {
00293   // Exists solely for the purpose of lookup of the resource path.
00294   // This just needs to be some symbol in the binary.
00295   static int StaticSymbol;
00296   // The driver detects the builtin header path based on the path of the
00297   // executable.
00298   // FIXME: On linux, GetMainExecutable is independent of the value of the
00299   // first argument, thus allowing ClangTool and runToolOnCode to just
00300   // pass in made-up names here. Make sure this works on other platforms.
00301   std::string MainExecutable =
00302       llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol);
00303 
00304   llvm::SmallString<128> InitialDirectory;
00305   if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory))
00306     llvm::report_fatal_error("Cannot detect current path: " +
00307                              Twine(EC.message()));
00308   bool ProcessingFailed = false;
00309   for (const auto &SourcePath : SourcePaths) {
00310     std::string File(getAbsolutePath(SourcePath));
00311 
00312     // Currently implementations of CompilationDatabase::getCompileCommands can
00313     // change the state of the file system (e.g.  prepare generated headers), so
00314     // this method needs to run right before we invoke the tool, as the next
00315     // file may require a different (incompatible) state of the file system.
00316     //
00317     // FIXME: Make the compilation database interface more explicit about the
00318     // requirements to the order of invocation of its members.
00319     std::vector<CompileCommand> CompileCommandsForFile =
00320         Compilations.getCompileCommands(File);
00321     if (CompileCommandsForFile.empty()) {
00322       // FIXME: There are two use cases here: doing a fuzzy
00323       // "find . -name '*.cc' |xargs tool" match, where as a user I don't care
00324       // about the .cc files that were not found, and the use case where I
00325       // specify all files I want to run over explicitly, where this should
00326       // be an error. We'll want to add an option for this.
00327       llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
00328       continue;
00329     }
00330     for (CompileCommand &CompileCommand : CompileCommandsForFile) {
00331       // FIXME: chdir is thread hostile; on the other hand, creating the same
00332       // behavior as chdir is complex: chdir resolves the path once, thus
00333       // guaranteeing that all subsequent relative path operations work
00334       // on the same path the original chdir resulted in. This makes a
00335       // difference for example on network filesystems, where symlinks might be
00336       // switched during runtime of the tool. Fixing this depends on having a
00337       // file system abstraction that allows openat() style interactions.
00338       if (chdir(CompileCommand.Directory.c_str()))
00339         llvm::report_fatal_error("Cannot chdir into \"" +
00340                                  Twine(CompileCommand.Directory) + "\n!");
00341       std::vector<std::string> CommandLine = CompileCommand.CommandLine;
00342       for (const auto &Adjuster : ArgsAdjusters)
00343         CommandLine = Adjuster->Adjust(CommandLine);
00344       assert(!CommandLine.empty());
00345       CommandLine[0] = MainExecutable;
00346       // FIXME: We need a callback mechanism for the tool writer to output a
00347       // customized message for each file.
00348       DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; });
00349       ToolInvocation Invocation(std::move(CommandLine), Action, Files.get());
00350       Invocation.setDiagnosticConsumer(DiagConsumer);
00351       for (const auto &MappedFile : MappedFileContents)
00352         Invocation.mapVirtualFile(MappedFile.first, MappedFile.second);
00353       if (!Invocation.run()) {
00354         // FIXME: Diagnostics should be used instead.
00355         llvm::errs() << "Error while processing " << File << ".\n";
00356         ProcessingFailed = true;
00357       }
00358       // Return to the initial directory to correctly resolve next file by
00359       // relative path.
00360       if (chdir(InitialDirectory.c_str()))
00361         llvm::report_fatal_error("Cannot chdir into \"" +
00362                                  Twine(InitialDirectory) + "\n!");
00363     }
00364   }
00365   return ProcessingFailed ? 1 : 0;
00366 }
00367 
00368 namespace {
00369 
00370 class ASTBuilderAction : public ToolAction {
00371   std::vector<std::unique_ptr<ASTUnit>> &ASTs;
00372 
00373 public:
00374   ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
00375 
00376   bool runInvocation(CompilerInvocation *Invocation, FileManager *Files,
00377                      DiagnosticConsumer *DiagConsumer) override {
00378     // FIXME: This should use the provided FileManager.
00379     std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
00380         Invocation, CompilerInstance::createDiagnostics(
00381                         &Invocation->getDiagnosticOpts(), DiagConsumer,
00382                         /*ShouldOwnClient=*/false));
00383     if (!AST)
00384       return false;
00385 
00386     ASTs.push_back(std::move(AST));
00387     return true;
00388   }
00389 };
00390 
00391 }
00392 
00393 int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
00394   ASTBuilderAction Action(ASTs);
00395   return run(&Action);
00396 }
00397 
00398 std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code,
00399                                           const Twine &FileName) {
00400   return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName);
00401 }
00402 
00403 std::unique_ptr<ASTUnit>
00404 buildASTFromCodeWithArgs(const Twine &Code,
00405                          const std::vector<std::string> &Args,
00406                          const Twine &FileName) {
00407   SmallString<16> FileNameStorage;
00408   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
00409 
00410   std::vector<std::unique_ptr<ASTUnit>> ASTs;
00411   ASTBuilderAction Action(ASTs);
00412   ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action,
00413                             nullptr);
00414 
00415   SmallString<1024> CodeStorage;
00416   Invocation.mapVirtualFile(FileNameRef,
00417                             Code.toNullTerminatedStringRef(CodeStorage));
00418   if (!Invocation.run())
00419     return nullptr;
00420 
00421   assert(ASTs.size() == 1);
00422   return std::move(ASTs[0]);
00423 }
00424 
00425 } // end namespace tooling
00426 } // end namespace clang