Skip to content

Commit

Permalink
[clang] Fix assertion failure using -MJ with -fsyntax-only
Browse files Browse the repository at this point in the history
If there is no output filename we should not assert when writing output
for -MJ.

Differential Revision: https://reviews.llvm.org/D159016
  • Loading branch information
benlangmuir committed Aug 30, 2023
1 parent acc572f commit aca23d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
CWD = ".";
CDB << "{ \"directory\": \"" << escape(*CWD) << "\"";
CDB << ", \"file\": \"" << escape(Input.getFilename()) << "\"";
CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
if (Output.isFilename())
CDB << ", \"output\": \"" << escape(Output.getFilename()) << "\"";
CDB << ", \"arguments\": [\"" << escape(D.ClangExecutable) << "\"";
SmallString<128> Buf;
Buf = "-x";
Expand All @@ -2445,7 +2446,8 @@ void Clang::DumpCompilationDatabase(Compilation &C, StringRef Filename,
CDB << ", \"" << escape(Buf) << "\"";
}
CDB << ", \"" << escape(Input.getFilename()) << "\"";
CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\"";
if (Output.isFilename())
CDB << ", \"-o\", \"" << escape(Output.getFilename()) << "\"";
for (auto &A: Args) {
auto &O = A->getOption();
// Skip language selection, which is positional.
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Driver/compilation_database.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
// RUN: mkdir -p %t.workdir && cd %t.workdir
// RUN: %clang -no-canonical-prefixes -fintegrated-as -MD -MP --sysroot=somewhere -c -x c %s -xc++ %s -Wall -MJ - 2>&1 | FileCheck %s
// RUN: not %clang -no-canonical-prefixes -c -x c %s -MJ %s/non-existant 2>&1 | FileCheck --check-prefix=ERROR %s
// RUN: %clang -fsyntax-only %s -MJ - 2>&1 | FileCheck %s -check-prefix=SYNTAX_ONLY

// 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={{[^"]+}}"]},
// 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={{[^"]+}}"]},
// ERROR: error: compilation database '{{.*}}/non-existant' could not be opened:

// SYNTAX_ONLY: {
// SYNTAX_ONLY-SAME: "directory": "{{[^"]*}}workdir",
// SYNTAX_ONLY-SAME: "file": "{{[^"]*}}compilation_database.c"
// SYNTAX_ONLY-NOT: "output"
// SYNTAX_ONLY-SAME: "arguments": [
// SYNTAX_ONLY-NOT: "-o"
// SYNTAX_ONLY-SAME: ]
// SYNTAX_ONLY-SAME: }

int main(void) {
return 0;
}

0 comments on commit aca23d8

Please sign in to comment.