[clang-tools-extra] 64c108c - [clangd] Better handling `\n` in the synthesized diagnostic message.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 17 00:28:09 PST 2022


Author: Haojian Wu
Date: 2022-01-17T09:27:58+01:00
New Revision: 64c108c9e4e0525328ceb4da55e4ab4b765d4c27

URL: https://github.com/llvm/llvm-project/commit/64c108c9e4e0525328ceb4da55e4ab4b765d4c27
DIFF: https://github.com/llvm/llvm-project/commit/64c108c9e4e0525328ceb4da55e4ab4b765d4c27.diff

LOG: [clangd] Better handling `\n` in the synthesized diagnostic message.

The newline-eof fix was rendered as "insert '...'", this patch
special-case it.

Reviewed By: sammccall

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/Diagnostics.cpp
    clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Diagnostics.cpp b/clang-tools-extra/clangd/Diagnostics.cpp
index 044ef1934426a..40eab3f647424 100644
--- a/clang-tools-extra/clangd/Diagnostics.cpp
+++ b/clang-tools-extra/clangd/Diagnostics.cpp
@@ -631,7 +631,10 @@ void StoreDiags::EndSourceFile() {
 /// the result is not too large and does not contain newlines.
 static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
   constexpr unsigned MaxLen = 50;
-
+  if (Code == "\n") {
+    OS << "\\n";
+    return;
+  }
   // Only show the first line if there are many.
   llvm::StringRef R = Code.split('\n').first;
   // Shorten the message if it's too long.

diff  --git a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
index 5303917402afd..76dd20177c47c 100644
--- a/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp
@@ -623,6 +623,15 @@ n]] = 10; // error-ok
                   Fix(Source.range(), "ident", "change 'ide\\…' to 'ident'"))));
 }
 
+TEST(DiagnosticTest, NewLineFixMessage) {
+  Annotations Source("int a;[[]]");
+  TestTU TU = TestTU::withCode(Source.code());
+  TU.ExtraArgs = {"-Wnewline-eof"};
+  EXPECT_THAT(
+      *TU.build().getDiagnostics(),
+      ElementsAre(WithFix((Fix(Source.range(), "\n", "insert '\\n'")))));
+}
+
 TEST(DiagnosticTest, ClangTidySuppressionCommentTrumpsWarningAsError) {
   Annotations Main(R"cpp(
     int main() {


        


More information about the cfe-commits mailing list