[clang] clang: Add -fno-diagnostics-show-note-snippets flag (PR #216250)
Nico Weber via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 13 22:28:09 PDT 2026
https://github.com/nico updated https://github.com/llvm/llvm-project/pull/216250
>From 3872da16a97d3668c273b5dd0c49c8b4d57cb482 Mon Sep 17 00:00:00 2001
From: Nico Weber <thakis at chromium.org>
Date: Thu, 13 Aug 2026 22:04:30 -0700
Subject: [PATCH] clang: Add -fno-diagnostics-show-note-snippets
It disables showing snippets on notes (but not on warnings and errors).
---
clang/docs/ReleaseNotes.md | 5 +++
clang/docs/UsersManual.md | 11 +++++
.../include/clang/Basic/DiagnosticOptions.def | 1 +
clang/include/clang/Options/Options.td | 6 +++
clang/lib/Driver/ToolChains/Clang.cpp | 2 +
clang/lib/Frontend/TextDiagnostic.cpp | 4 ++
.../Driver/fdiagnostics-show-note-snippets.c | 11 +++++
clang/test/Misc/diag-note-snippets.cpp | 41 +++++++++++++++++++
8 files changed, 81 insertions(+)
create mode 100644 clang/test/Driver/fdiagnostics-show-note-snippets.c
create mode 100644 clang/test/Misc/diag-note-snippets.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 4962f9f137b5a..93bdd24d9fce0 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -210,6 +210,11 @@ features cannot lower the translation-unit ABI level;
### Improvements to Clang's diagnostics
+- Added `-fno-diagnostics-show-note-snippets` (and corresponding
+ `-fdiagnostics-show-note-snippets`, the default) to suppress snippets on
+ notes. Errors and warnings keep printing snippets. This makes output much
+ shorter for diagnostics with many notes, such as overload resolution failures.
+
- More consistent rendering of Unicode characters in diagnostic messages.
- Fixed bug in `-Wdocumentation` so that it correctly handles explicit
diff --git a/clang/docs/UsersManual.md b/clang/docs/UsersManual.md
index 40834746e1218..b7949de748e01 100644
--- a/clang/docs/UsersManual.md
+++ b/clang/docs/UsersManual.md
@@ -268,6 +268,17 @@ test.c:28:8: warning: extra tokens at end of #endif directive [-Wextra-tokens]
```
:::
+:::{option} -f[no-]diagnostics-show-note-snippets
+
+Print source line and caret snippets for note diagnostics.
+This option, which defaults to on, controls whether or not Clang
+prints the source line, source ranges, and caret for notes. When
+disabled, errors and warnings still show their snippets, but notes
+only show their message line. This makes output much shorter for
+diagnostics with many notes, such as overload resolution failures
+or template instantiation backtraces.
+:::
+
:::{option} -f[no-]color-diagnostics
This option, which defaults to on when a color-capable terminal is
diff --git a/clang/include/clang/Basic/DiagnosticOptions.def b/clang/include/clang/Basic/DiagnosticOptions.def
index 764e2f1fedbdd..54e389c5f6bf5 100644
--- a/clang/include/clang/Basic/DiagnosticOptions.def
+++ b/clang/include/clang/Basic/DiagnosticOptions.def
@@ -53,6 +53,7 @@ DIAGOPT(ShowLocation, 1, 1) /// Show source location information.
DIAGOPT(ShowLevel, 1, 1) /// Show diagnostic level.
DIAGOPT(AbsolutePath, 1, 0) /// Use absolute paths.
DIAGOPT(ShowCarets, 1, 1) /// Show carets in diagnostics.
+DIAGOPT(ShowNoteSnippets, 1, 1) /// Show source snippets for notes.
DIAGOPT(ShowFixits, 1, 1) /// Show fixit information.
DIAGOPT(ShowSourceRanges, 1, 0) /// Show source ranges in numeric form.
DIAGOPT(ShowParseableFixits, 1, 0) /// Show machine parseable fix-its.
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 6af0f578b6d63..9f1757419acd2 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -2326,6 +2326,12 @@ def fdiagnostics_misexpect_tolerance_EQ : Joined<["-"], "fdiagnostics-misexpect-
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
MetaVarName<"<value>">,
HelpText<"Prevent misexpect diagnostics from being output if the profile counts are within N% of the expected. ">;
+defm diagnostics_show_note_snippets : BoolFOption<"diagnostics-show-note-snippets",
+ DiagnosticOpts<"ShowNoteSnippets">, DefaultTrue,
+ NegFlag<SetFalse, [], [ClangOption, CC1Option],
+ "Do not show source line and caret snippets for notes">,
+ PosFlag<SetTrue, [], [ClangOption],
+ "Show source line and caret snippets for notes (default)">>;
defm diagnostics_show_option : BoolFOption<"diagnostics-show-option",
DiagnosticOpts<"ShowOptionNames">, DefaultTrue,
NegFlag<SetFalse, [], [ClangOption, CC1Option]>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 19d37b2ed7017..86318d084c38f 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4538,6 +4538,8 @@ static void RenderDiagnosticsOptions(const Driver &D, const ArgList &Args,
options::OPT_fno_caret_diagnostics, CaretDefault))
CmdArgs.push_back("-fno-caret-diagnostics");
+ Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_note_snippets,
+ options::OPT_fno_diagnostics_show_note_snippets);
Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_fixit_info,
options::OPT_fno_diagnostics_fixit_info);
Args.addOptOutFlag(CmdArgs, options::OPT_fdiagnostics_show_option,
diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 5221ed4de02ff..3e7ccccfec5e2 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -1368,6 +1368,10 @@ void TextDiagnostic::emitSnippetAndCaret(
// multiple times if one loc has multiple diagnostics.
if (!DiagOpts.ShowCarets)
return;
+ if (Level == DiagnosticsEngine::Note && !DiagOpts.ShowNoteSnippets) {
+ emitParseableFixits(Hints, Loc.getManager());
+ return;
+ }
if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
(LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
return;
diff --git a/clang/test/Driver/fdiagnostics-show-note-snippets.c b/clang/test/Driver/fdiagnostics-show-note-snippets.c
new file mode 100644
index 0000000000000..05b704b8f6f83
--- /dev/null
+++ b/clang/test/Driver/fdiagnostics-show-note-snippets.c
@@ -0,0 +1,11 @@
+// Default: nothing is passed to cc1.
+// RUN: %clang -### -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
+// DEFAULT-NOT: "-fno-diagnostics-show-note-snippets"
+// DEFAULT-NOT: "-fdiagnostics-show-note-snippets"
+
+// RUN: %clang -### -fsyntax-only -fno-diagnostics-show-note-snippets %s 2>&1 \
+// RUN: | FileCheck %s --check-prefix=DISABLED
+// DISABLED: "-fno-diagnostics-show-note-snippets"
+
+// RUN: %clang -### -fsyntax-only -fno-diagnostics-show-note-snippets \
+// RUN: -fdiagnostics-show-note-snippets %s 2>&1 | FileCheck %s --check-prefix=DEFAULT
diff --git a/clang/test/Misc/diag-note-snippets.cpp b/clang/test/Misc/diag-note-snippets.cpp
new file mode 100644
index 0000000000000..b7495bffbcec4
--- /dev/null
+++ b/clang/test/Misc/diag-note-snippets.cpp
@@ -0,0 +1,41 @@
+// By default, both the error and the note show a snippet.
+// RUN: not %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix=SNIPPET
+// SNIPPET: error: redefinition of 'x'
+// SNIPPET-NEXT: {{[0-9]+}} | float x;
+// SNIPPET-NEXT: | {{.*}}^
+// SNIPPET-NEXT: note: previous definition is here
+// SNIPPET-NEXT: {{[0-9]+}} | int x;
+// SNIPPET-NEXT: | {{.*}}^
+
+// With -fno-diagnostics-show-note-snippets, the error keeps its snippet but the
+// note only shows its message line.
+// RUN: not %clang_cc1 -fsyntax-only -fno-diagnostics-show-note-snippets %s 2>&1 | FileCheck %s --check-prefix=NOSNIPPET
+// NOSNIPPET: error: redefinition of 'x'
+// NOSNIPPET-NEXT: {{[0-9]+}} | float x;
+// NOSNIPPET-NEXT: | {{.*}}^
+// NOSNIPPET-NEXT: note: previous definition is here
+// NOSNIPPET-NOT: int x;
+// NOSNIPPET: error: no matching function for call to 'f'
+// NOSNIPPET-NEXT: {{[0-9]+}} | f();
+// NOSNIPPET-NEXT: | ^
+// NOSNIPPET-NEXT: note: candidate function not viable
+// NOSNIPPET-NOT: void f(int);
+
+
+// With -fno-diagnostics-show-note-snippets, parseable fixits on notes are still
+// emitted.
+// RUN: not %clang_cc1 -fsyntax-only -fno-diagnostics-show-note-snippets -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s --check-prefix=FIXIT
+// FIXIT: warning: using the result of an assignment
+// FIXIT: note: place parentheses around the assignment
+// FIXIT-NEXT: fix-it:{{.*}}diag-note-snippets.cpp
+
+int x;
+float x;
+
+void f(int);
+
+void g(int a) {
+ f();
+ if (a = 5) {
+ }
+}
More information about the cfe-commits
mailing list