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