Skip to content

Commit 7f954b0

Browse files
committed
[clang] Fix assertion failure using -MJ with -fsyntax-only
If there is no output filename we should not assert when writing output for -MJ. Differential Revision: https://reviews.llvm.org/D159016 (cherry picked from commit aca23d8)
1 parent 6abb5d1 commit 7f954b0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
24462446
CWD = ".";
24472447
CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
24482448
CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
2449-
CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
2449+
if (Output.isFilename())
2450+
CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
24502451
CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
24512452
SmallString<128> Buf;
24522453
Buf = "-x";
@@ -2458,7 +2459,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
24582459
CDB << ", \"" << escape(Buf) << "\"";
24592460
}
24602461
CDB << ", \"" << escape(Input.getFilename()) << "\"";
2461-
CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\"";
2462+
if (Output.isFilename())
2463+
CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\"";
24622464
for (auto &A: Args) {
24632465
auto &O = A->getOption();
24642466
// Skip language selection, which is positional.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// RUN: mkdir -p %t.workdir && cd %t.workdir
22
// RUN: %clang -no-canonical-prefixes -fintegrated-as -MD -MP --sysroot=somewhere -c -x c %s -xc++ %s -Wall -MJ - 2>&1 | FileCheck %s
33
// RUN: not %clang -no-canonical-prefixes -c -x c %s -MJ %s/non-existant 2>&1 | FileCheck --check-prefix=ERROR %s
4+
// RUN: %clang -fsyntax-only %s -MJ - 2>&1 | FileCheck %s -check-prefix=SYNTAX_ONLY
45

56
// CHECK: { "directory": "{{[^"]*}}workdir", "file": "[[SRC:[^"]+[/|\\]compilation_database.c]]", "output": "compilation_database.o", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc", "[[SRC]]", "-o", "compilation_database.o", "-no-canonical-prefixes", "-fintegrated-as", "--sysroot=somewhere", "-c", "-Wall",{{.*}} "--target={{[^"]+}}"]},
67
// CHECK: { "directory": "{{.*}}", "file": "[[SRC:[^"]+[/|\\]compilation_database.c]]", "output": "compilation_database.o", "arguments": ["{{[^"]*}}clang{{[^"]*}}", "-xc++", "[[SRC]]", "-o", "compilation_database.o", "-no-canonical-prefixes", "-fintegrated-as", "--sysroot=somewhere", "-c", "-Wall",{{.*}} "--target={{[^"]+}}"]},
78
// ERROR: error: compilation database '{{.*}}/non-existant' could not be opened:
89

10+
// SYNTAX_ONLY: {
11+
// SYNTAX_ONLY-SAME: "directory": "{{[^"]*}}workdir",
12+
// SYNTAX_ONLY-SAME: "file": "{{[^"]*}}compilation_database.c"
13+
// SYNTAX_ONLY-NOT: "output"
14+
// SYNTAX_ONLY-SAME: "arguments": [
15+
// SYNTAX_ONLY-NOT: "-o"
16+
// SYNTAX_ONLY-SAME: ]
17+
// SYNTAX_ONLY-SAME: }
18+
919
int main(void) {
1020
return 0;
1121
}

0 commit comments

Comments
 (0)