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 @@ -66,7 +66,6 @@ DependencyConsumer &Consumer); 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 @@ -269,8 +269,6 @@ DependencyScanningWorker::DependencyScanningWorker( DependencyScanningService &Service) : Format(Service.getFormat()) { - DiagOpts = new DiagnosticOptions(); - PCHContainerOps = std::make_shared(); PCHContainerOps->registerReader( std::make_unique()); @@ -290,16 +288,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()); @@ -313,15 +315,22 @@ llvm::IntrusiveRefCntPtr CurrentFiles = Files ? Files : new FileManager(FileSystemOptions(), RealFS); - return runWithDiags(DiagOpts.get(), [&](DiagnosticConsumer &DC) { - DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS, - PPSkipMappings.get(), Format); - // 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(CommandLine, &Action, CurrentFiles.get(), - PCHContainerOps); - Invocation.setDiagnosticConsumer(&DC); - return Invocation.run(); - }); + std::vector CCommandLine(CommandLine.size(), nullptr); + llvm::transform(CommandLine, CCommandLine.begin(), + [](const std::string &Str) { return Str.c_str(); }); + + return runWithDiags( + CreateAndPopulateDiagOpts(CCommandLine).release(), + [&](DiagnosticConsumer &DC, DiagnosticOptions &DiagOpts) { + DependencyScanningAction Action(WorkingDirectory, Consumer, DepFS, + PPSkipMappings.get(), Format); + // 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(CommandLine, &Action, CurrentFiles.get(), + PCHContainerOps); + Invocation.setDiagnosticConsumer(&DC); + Invocation.setDiagnosticOptions(&DiagOpts); + return Invocation.run(); + }); }