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