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), + [&](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(); + }); } diff --git a/clang/test/ClangScanDeps/Inputs/diagnostics/cdb.json.template b/clang/test/ClangScanDeps/Inputs/diagnostics/cdb.json.template new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/diagnostics/cdb.json.template @@ -0,0 +1,7 @@ +[ + { + "directory": "DIR", + "command": "clang -c DIR/tu.c -target i386-apple-ios14.0-simulator -Wno-error=invalid-ios-deployment-target -o DIR/tu.o", + "file": "DIR/tu.c" + } +] diff --git a/clang/test/ClangScanDeps/Inputs/diagnostics/tu.c b/clang/test/ClangScanDeps/Inputs/diagnostics/tu.c new file mode 100644 diff --git a/clang/test/ClangScanDeps/diagnostics.c b/clang/test/ClangScanDeps/diagnostics.c new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/diagnostics.c @@ -0,0 +1,8 @@ +// RUN: rm -rf %t && mkdir %t +// RUN: cp %S/Inputs/diagnostics/* %t + +// RUN: sed "s|DIR|%/t|g" %S/Inputs/diagnostics/cdb.json.template > %t/cdb.json +// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full \ +// RUN: -generate-modules-path-args -module-files-dir %t/build 2>&1 | FileCheck %s + +// CHECK-NOT: error: invalid iOS deployment version diff --git a/clang/test/ClangScanDeps/strip_diag_serialize.cpp b/clang/test/ClangScanDeps/strip_diag_serialize.cpp --- a/clang/test/ClangScanDeps/strip_diag_serialize.cpp +++ b/clang/test/ClangScanDeps/strip_diag_serialize.cpp @@ -1,6 +1,3 @@ -// FIXME: Make this pass again. -// XFAIL: * - // RUN: rm -rf %t.dir // RUN: rm -rf %t.cdb // RUN: mkdir -p %t.dir