[PATCH] D159016: [clang] Fix assertion failure using -MJ with -fsyntax-only

Ben Langmuir via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 28 14:13:16 PDT 2023


benlangmuir created this revision.
benlangmuir added a reviewer: MaskRay.
Herald added a project: All.
benlangmuir requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If there is no output filename we should not assert when writing output for -MJ.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159016

Files:
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/compilation_database_fsyntax_only.c


Index: clang/test/Driver/compilation_database_fsyntax_only.c
===================================================================
--- /dev/null
+++ clang/test/Driver/compilation_database_fsyntax_only.c
@@ -0,0 +1,16 @@
+// RUN: mkdir -p %t.workdir && cd %t.workdir
+// RUN: %clang -fsyntax-only %s -MJ - 2>&1 | FileCheck %s
+
+// CHECK:      {
+// CHECK-SAME: "directory": "{{[^"]*}}workdir",
+// CHECK-SAME: "file": "{{[^"]*}}compilation_database_fsyntax_only.c"
+// CHECK-NOT:  "output"
+// CHECK-SAME: "arguments": [
+// CHECK-NOT:    "-o"
+// CHECK-SAME: ]
+// CHECK-SAME: }
+
+
+int main(void) {
+  return 0;
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -2426,7 +2426,8 @@
     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";
@@ -2438,7 +2439,8 @@
     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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159016.554046.patch
Type: text/x-patch
Size: 1655 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230828/7364ef2b/attachment-0001.bin>


More information about the cfe-commits mailing list