[clang][driver] Ensure we don't accumulate entries in -MJ files

Previously, each job would overwrite the -MJ file. This didn't quite work for Clang invocations with multiple architectures, which got fixed in D121997 by always appending to the -MJ file. That's not correct either, since the file would grow indefinitely on subsequent Clang invocations. This patch ensures the driver always removes the file before jobs fill it in by appending.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D128098
This commit is contained in:
Jan Svoboda 2022-06-17 23:31:07 +02:00
parent 7bc00ce5cd
commit 92c6ffa14c
2 changed files with 11 additions and 0 deletions

View File

@ -1314,6 +1314,10 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model);
} }
// Remove existing compilation database so that each job can append to it.
if (Arg *A = Args.getLastArg(options::OPT_MJ))
llvm::sys::fs::remove(A->getValue());
// Setting up the jobs for some precompile cases depends on whether we are // Setting up the jobs for some precompile cases depends on whether we are
// treating them as PCH, implicit modules or C++20 ones. // treating them as PCH, implicit modules or C++20 ones.
// TODO: inferring the mode like this seems fragile (it meets the objective // TODO: inferring the mode like this seems fragile (it meets the objective

View File

@ -2,7 +2,14 @@
// RUN: rm -rf %t && mkdir -p %t // RUN: rm -rf %t && mkdir -p %t
// RUN: %clang -fdriver-only -o %t/out %s -mtargetos=macos12 -arch arm64 -arch x86_64 -MJ %t/compilation_database.json // RUN: %clang -fdriver-only -o %t/out %s -mtargetos=macos12 -arch arm64 -arch x86_64 -MJ %t/compilation_database.json
// Let's run that again and verify we're not accumulating redundant entries in the same file.
//
// RUN: %clang -fdriver-only -o %t/out %s -mtargetos=macos12 -arch arm64 -arch x86_64 -MJ %t/compilation_database.json
// RUN: echo EOF >> %t/compilation_database.json
// RUN: FileCheck --input-file=%t/compilation_database.json %s // RUN: FileCheck --input-file=%t/compilation_database.json %s
// CHECK: { "directory": "{{.*}}", "file": "{{.*}}", "output": "[[OUTPUT_X86_64:.*]]", "arguments": [{{.*}}, "-o", "[[OUTPUT_X86_64]]", {{.*}} "--target=x86_64-apple-macosx12.0.0"]}, // CHECK: { "directory": "{{.*}}", "file": "{{.*}}", "output": "[[OUTPUT_X86_64:.*]]", "arguments": [{{.*}}, "-o", "[[OUTPUT_X86_64]]", {{.*}} "--target=x86_64-apple-macosx12.0.0"]},
// CHECK-NEXT: { "directory": "{{.*}}", "file": "{{.*}}", "output": "[[OUTPUT_ARM64:.*]]", "arguments": [{{.*}}, "-o", "[[OUTPUT_ARM64]]", {{.*}} "--target=arm64-apple-macosx12.0.0"]}, // CHECK-NEXT: { "directory": "{{.*}}", "file": "{{.*}}", "output": "[[OUTPUT_ARM64:.*]]", "arguments": [{{.*}}, "-o", "[[OUTPUT_ARM64]]", {{.*}} "--target=arm64-apple-macosx12.0.0"]},
// CHECK-NEXT: EOF