diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -68,7 +68,6 @@ llvm::Optional ModuleName = None); private: - IntrusiveRefCntPtr DiagOpts; std::shared_ptr PCHContainerOps; std::unique_ptr PPSkipMappings; diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -276,8 +276,6 @@ DependencyScanningWorker::DependencyScanningWorker( DependencyScanningService &Service) : Format(Service.getFormat()) { - DiagOpts = new DiagnosticOptions(); - PCHContainerOps = std::make_shared(); PCHContainerOps->registerReader( std::make_unique()); @@ -302,16 +300,20 @@ Files = new FileManager(FileSystemOptions(), RealFS); } -static llvm::Error runWithDiags( - DiagnosticOptions *DiagOpts, - llvm::function_ref BodyShouldSucceed) { +static llvm::Error +runWithDiags(DiagnosticOptions *DiagOpts, + llvm::function_ref + BodyShouldSucceed) { + // Avoid serializing diagnostics. + DiagOpts->DiagnosticSerializationFile.clear(); + // Capture the emitted diagnostics and report them to the client // in the case of a failure. std::string DiagnosticOutput; llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); TextDiagnosticPrinter DiagPrinter(DiagnosticsOS, DiagOpts); - if (BodyShouldSucceed(DiagPrinter)) + if (BodyShouldSucceed(DiagPrinter, *DiagOpts)) return llvm::Error::success(); return llvm::make_error(DiagnosticsOS.str(), llvm::inconvertibleErrorCode()); @@ -338,15 +340,24 @@ const std::vector &FinalCommandLine = ModifiedCommandLine ? *ModifiedCommandLine : CommandLine; - return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) { - DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS, - PPSkipMappings.get(), Format, ModuleName); - // Create an invocation that uses the underlying file system to ensure that - // any file system requests that are made by the driver do not go through - // the dependency scanning filesystem. - ToolInvocation Invocation(FinalCommandLine, &Action, CurrentFiles.get(), - PCHContainerOps); - Invocation.setDiagnosticConsumer(&DC); - return Invocation.run(); - }); + std::vector FinalCCommandLine(CommandLine.size(), nullptr); + llvm::transform(CommandLine, FinalCCommandLine.begin(), + [](const std::string &Str) { return Str.c_str(); }); + + return runWithDiags(CreateAndPopulateDiagOpts(FinalCCommandLine).release(), + [&](DiagnosticConsumer &DC, DiagnosticOptions &DiagOpts) { + DependencyScanningAction Action( + WorkingDirectory, Consumer, DepFS, + PPSkipMappings.get(), Format, ModuleName); + // Create an invocation that uses the underlying file + // system to ensure that any file system requests that + // are made by the driver do not go through the + // dependency scanning filesystem. + ToolInvocation Invocation(FinalCommandLine, &Action, + CurrentFiles.get(), + PCHContainerOps); + Invocation.setDiagnosticConsumer(&DC); + Invocation.setDiagnosticOptions(&DiagOpts); + return Invocation.run(); + }); }