diff mbox series

diagnostics: fix ICE in apply_fixit with -ftrack-macro-expansion=0 [PR106207]

Message ID 1793752590.36340319@f173.i.mail.ru
State New
Headers show
Series diagnostics: fix ICE in apply_fixit with -ftrack-macro-expansion=0 [PR106207] | expand

Commit Message

илья ролдугин Nov. 4, 2026, 12:36 a.m. UTC
Hello,

This patch fixes PR c++/106207, an internal compiler error in
changed_line::apply_fixit when compiling code with both
-ftrack-macro-expansion=0 and -fdiagnostics-generate-patch.

When macro tracking is disabled, macro tokens receive coarse locations.
Subsequent shifts by get_effective_column can cause start_offset or
next_offset to become negative. Instead of asserting via gcc_assert,
handle this as an invalid edit and return false (matching the behavior
already present for offsets that exceed the line length).

Tested on x86_64-pc-linux-gnu:
- make check-c++ RUNTESTFLAGS="dg.exp=pr106207.C" (passes across all standards)
- make check-c++ RUNTESTFLAGS="dg.exp=diagnostic/*" (3328 passes, 0 unexpected failures)

As I do not have write access to the GCC repository, could someone
please commit this for me if approved?

PR c++/106207

gcc/ChangeLog:

* diagnostics/changes.cc (changed_line::apply_fixit): Return false
instead of asserting if start_offset or next_offset is negative.

gcc/testsuite/ChangeLog:

* g++.dg/diagnostic/pr106207.C: New test.

Signed-off-by: Ilia Roldugin <ilia.roldugin@mail.ru>
diff mbox series

Patch

From 93699c89b86a2028c7672c83d9adf49aea2aa2f4 Mon Sep 17 00:00:00 2001
From: Ilia Roldugin <ilia.roldugin@mail.ru>
Date: Wed, 4 Nov 2026 03:08:41 +0300
Subject: [PATCH] diagnostics: fix ICE in apply_fixit with
 -ftrack-macro-expansion=0 [PR106207]

When -ftrack-macro-expansion=0 is used together with
-fdiagnostics-generate-patch, coordinates from macro expansions can be
shifted by get_effective_column such that start_offset or next_offset
becomes negative. Previously, this triggered a gcc_assert in
changed_line::apply_fixit, causing an ICE.

Since changed_line::apply_fixit is documented to return false on errors
(and already does so for offsets exceeding line length), replace the
assertions with boundary checks returning false.

	PR c++/106207

gcc/ChangeLog:

	* diagnostics/changes.cc (changed_line::apply_fixit): Return false
	instead of asserting if start_offset or next_offset is negative.

gcc/testsuite/ChangeLog:

	* g++.dg/diagnostic/pr106207.C: New test.

Signed-off-by: Ilia Roldugin <ilia.roldugin@mail.ru>
---
 gcc/diagnostics/changes.cc                 |  7 ++++---
 gcc/testsuite/g++.dg/diagnostic/pr106207.C | 15 +++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/diagnostic/pr106207.C

diff --git a/gcc/diagnostics/changes.cc b/gcc/diagnostics/changes.cc
index a416e4b4c..cca9f0cb3 100644
--- a/gcc/diagnostics/changes.cc
+++ b/gcc/diagnostics/changes.cc
@@ -782,9 +782,10 @@  changed_line::apply_fixit (int start_column,
   int start_offset = start_column - 1;
   int next_offset = next_column - 1;
 
-  gcc_assert (start_offset >= 0);
-  gcc_assert (next_offset >= 0);
-
+  if (start_offset < 0)
+    return false;
+  if (next_offset < 0)
+    return false;
   if (start_column > next_column)
     return false;
   if (start_offset >= (m_len + 1))
diff --git a/gcc/testsuite/g++.dg/diagnostic/pr106207.C b/gcc/testsuite/g++.dg/diagnostic/pr106207.C
new file mode 100644
index 000000000..9dd0b4b4a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/pr106207.C
@@ -0,0 +1,15 @@ 
+// PR c++/106207
+// { dg-do compile }
+// { dg-options "-ftrack-macro-expansion=0 -fdiagnostics-generate-patch" }
+
+#define FOO(no)  \
+void f_##no() \
+{ \
+  int gen_##no(); \
+}
+
+#define GEN_FOO \
+FOO(f##1) \
+FOO(f##2)
+
+GEN_FOO // { dg-warning "empty parentheses were disambiguated" }
-- 
2.55.0