[clang][Diagnostics] Print empty lines in multiline snippets
authorTimm Bäder <tbaeder@redhat.com>
Wed, 24 May 2023 06:45:31 +0000 (08:45 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Wed, 31 May 2023 08:21:24 +0000 (10:21 +0200)
We should preserve empty lines in output snippets.

Differential Revision: https://reviews.llvm.org/D151301

clang/lib/Frontend/TextDiagnostic.cpp
clang/test/Misc/diag-style.cpp [new file with mode: 0644]

index d285b88..d2cbb55 100644 (file)
@@ -1225,7 +1225,7 @@ void TextDiagnostic::emitSnippetAndCaret(
     // to produce easily machine parsable output.  Add a space before the
     // source line and the caret to make it trivial to tell the main diagnostic
     // line from what the user is intended to see.
-    if (DiagOpts->ShowSourceRanges) {
+    if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) {
       SourceLine = ' ' + SourceLine;
       CaretLine = ' ' + CaretLine;
     }
@@ -1262,9 +1262,6 @@ void TextDiagnostic::emitSnippetAndCaret(
 void TextDiagnostic::emitSnippet(StringRef SourceLine,
                                  unsigned MaxLineNoDisplayWidth,
                                  unsigned LineNo) {
-  if (SourceLine.empty())
-    return;
-
   // Emit line number.
   if (MaxLineNoDisplayWidth > 0) {
     unsigned LineNoDisplayWidth = getNumDisplayWidth(LineNo);
diff --git a/clang/test/Misc/diag-style.cpp b/clang/test/Misc/diag-style.cpp
new file mode 100644 (file)
index 0000000..b12afb2
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: not %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck -strict-whitespace %s
+
+/// empty lines in multi-line diagnostic snippets are preserved.
+static_assert(false &&
+
+              true, "");
+// CHECK: static assertion failed
+// CHECK-NEXT: {{^}}    4 | static_assert(false &&{{$}}
+// CHECK-NEXT: {{^}}      |               ^~~~~~~~{{$}}
+// CHECK-NEXT: {{^}}    5 | {{$}}
+// CHECK-NEXT: {{^}}    6 |               true, "");{{$}}
+// CHECK-NEXT: {{^}}      |               ~~~~{{$}}