Skip to content

Commit 64c108c

Browse files
committed
[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
1 parent 00b77d9 commit 64c108c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang-tools-extra/clangd/Diagnostics.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,10 @@ void StoreDiags::EndSourceFile() {
631631
/// the result is not too large and does not contain newlines.
632632
static void writeCodeToFixMessage(llvm::raw_ostream &OS, llvm::StringRef Code) {
633633
constexpr unsigned MaxLen = 50;
634-
634+
if (Code == "\n") {
635+
OS << "\\n";
636+
return;
637+
}
635638
// Only show the first line if there are many.
636639
llvm::StringRef R = Code.split('\n').first;
637640
// Shorten the message if it's too long.

clang-tools-extra/clangd/unittests/DiagnosticsTests.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,15 @@ n]] = 10; // error-ok
623623
Fix(Source.range(), "ident", "change 'ide\\…' to 'ident'"))));
624624
}
625625

626+
TEST(DiagnosticTest, NewLineFixMessage) {
627+
Annotations Source("int a;[[]]");
628+
TestTU TU = TestTU::withCode(Source.code());
629+
TU.ExtraArgs = {"-Wnewline-eof"};
630+
EXPECT_THAT(
631+
*TU.build().getDiagnostics(),
632+
ElementsAre(WithFix((Fix(Source.range(), "\n", "insert '\\n'")))));
633+
}
634+
626635
TEST(DiagnosticTest, ClangTidySuppressionCommentTrumpsWarningAsError) {
627636
Annotations Main(R"cpp(
628637
int main() {

0 commit comments

Comments
 (0)