diff --git a/clang/include/clang/Tooling/DependencyScanningTool.h b/clang/include/clang/Tooling/DependencyScanningTool.h index e796ed648db35..415dcf17c37b9 100644 --- a/clang/include/clang/Tooling/DependencyScanningTool.h +++ b/clang/include/clang/Tooling/DependencyScanningTool.h @@ -151,6 +151,20 @@ class DependencyScanningTool { llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); } + /// @brief Initialize the worker's compiler instance from the commandline. + /// The compiler instance only takes a `-cc1` job, so this method + /// builds the `-cc1` job from the CommandLine input. + /// @param Worker The dependency scanning worker whose compiler instance + /// with context is initialized. + /// @param CWD The current working directory. + /// @param CommandLine This command line may be a driver command or a cc1 + /// command. + /// @param DC A diagnostics consumer to report error if the initialization + /// fails. + static bool initializeWorkerCIWithContextFromCommandline( + clang::dependencies::DependencyScanningWorker &Worker, StringRef CWD, + ArrayRef CommandLine, DiagnosticConsumer &DC); + private: dependencies::DependencyScanningWorker Worker; std::unique_ptr diff --git a/clang/lib/Tooling/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanningTool.cpp index 74cc6af3551f8..85f261c8ebbf1 100644 --- a/clang/lib/Tooling/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanningTool.cpp @@ -217,19 +217,13 @@ static llvm::Error makeErrorFromDiagnosticsOS( DiagPrinterWithOS.DiagnosticsOS.str(), llvm::inconvertibleErrorCode()); } -llvm::Error -DependencyScanningTool::initializeCompilerInstanceWithContextOrError( - StringRef CWD, ArrayRef CommandLine) { - DiagPrinterWithOS = - std::make_unique(CommandLine); - +bool DependencyScanningTool::initializeWorkerCIWithContextFromCommandline( + DependencyScanningWorker &Worker, StringRef CWD, + ArrayRef CommandLine, DiagnosticConsumer &DC) { if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") { // The input command line is already a -cc1 invocation; initialize the // compiler instance directly from it. - if (Worker.initializeCompilerInstanceWithContext( - CWD, CommandLine, DiagPrinterWithOS->DiagPrinter)) - return llvm::Error::success(); - return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS); + return Worker.initializeCompilerInstanceWithContext(CWD, CommandLine, DC); } // The input command line is either a driver-style command line, or @@ -241,18 +235,31 @@ DependencyScanningTool::initializeCompilerInstanceWithContextOrError( const auto &ModifiedCommandLine = OverlayFSAndArgs.second; auto DiagEngineWithCmdAndOpts = - std::make_unique( - ModifiedCommandLine, OverlayFS, DiagPrinterWithOS->DiagPrinter); + std::make_unique(ModifiedCommandLine, + OverlayFS, DC); const auto MaybeFirstCC1 = getFirstCC1CommandLine( ModifiedCommandLine, *DiagEngineWithCmdAndOpts->DiagEngine, OverlayFS); if (!MaybeFirstCC1) - return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS); + return false; + + return Worker.initializeCompilerInstanceWithContext( + CWD, *MaybeFirstCC1, std::move(DiagEngineWithCmdAndOpts), OverlayFS); +} - if (Worker.initializeCompilerInstanceWithContext( - CWD, *MaybeFirstCC1, std::move(DiagEngineWithCmdAndOpts), OverlayFS)) +llvm::Error +DependencyScanningTool::initializeCompilerInstanceWithContextOrError( + StringRef CWD, ArrayRef CommandLine) { + DiagPrinterWithOS = + std::make_unique(CommandLine); + + bool Result = initializeWorkerCIWithContextFromCommandline( + Worker, CWD, CommandLine, DiagPrinterWithOS->DiagPrinter); + + if (Result) return llvm::Error::success(); - return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS); + else + return makeErrorFromDiagnosticsOS(*DiagPrinterWithOS); } llvm::Expected