On 5/6/25 6:49 AM, Jakub Jelinek wrote:
Hi!

The backport of the PR108900 fix to 14 branch broke building chromium
because static_assert (__LINE__ == expected_line_number, ""); now triggers
as the __LINE__ values are off by one.
This isn't the case on the trunk and 15 branch because we've switched
to 64-bit location_t and so one actually needs far longer header files
to trigger it.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120061#c11
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120061#c12
contain (large) testcases in patch form which show on the 14 branch
that the first one used to fail before the PR108900 backport and now
works correctly, while the second one attempts to match the chromium
behavior and it used to pass before the PR108900 backport and now it
FAILs.
The two testcases show rare problematic cases, because
do_include_common -> parse_include -> check_eol -> check_eol_1 ->
cpp_get_token_1 -> _cpp_lex_token -> _cpp_lex_direct -> linemap_line_start
triggers there
       /* Allocate the new line_map.  However, if the current map only has a
          single line we can sometimes just increase its column_bits instead. */
       if (line_delta < 0
           || last_line != ORDINARY_MAP_STARTING_LINE_NUMBER (map)
           || SOURCE_COLUMN (map, highest) >= (1U << (column_bits - range_bits))
           || ( /* We can't reuse the map if the line offset is sufficiently
                   large to cause overflow when computing location_t values.  */
               (to_line - ORDINARY_MAP_STARTING_LINE_NUMBER (map))
               >= (((uint64_t) 1)
                   << (CHAR_BIT * sizeof (linenum_type) - column_bits)))
           || range_bits < map->m_range_bits)
         map = linemap_check_ordinary
                 (const_cast <line_map *>
                   (linemap_add (set, LC_RENAME,
                                 ORDINARY_MAP_IN_SYSTEM_HEADER_P (map),
                                 ORDINARY_MAP_FILE_NAME (map),
                                 to_line)));
and so creates a new ordinary map on the line right after the (problematic)
#include line.
Now, in the spot that r14-11679-g8a884140c2bcb7 patched,
pfile->line_table->highest_location in all 3 tests (also
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120061#c13
) is before the decrement the start of the line after the #include line and so
the decrement is really desirable in that case to put highest_location
somewhere on the line where the #include actually is.
But at the same time it is also undesirable, because if we do decrement it,
then linemap_add LC_ENTER called from _cpp_do_file_change will then
   /* Generate a start_location above the current highest_location.
      If possible, make the low range bits be zero.  */
   location_t start_location = set->highest_location + 1;
   unsigned range_bits = 0;
   if (start_location < LINE_MAP_MAX_LOCATION_WITH_COLS)
     range_bits = set->default_range_bits;
   start_location += (1 << range_bits) - 1;
   start_location &=  ~((1 << range_bits) - 1);

   linemap_assert (!LINEMAPS_ORDINARY_USED (set)
                   || (start_location
                       >= MAP_START_LOCATION (LINEMAPS_LAST_ORDINARY_MAP 
(set))));
and we can end up with the new LC_ENTER ordinary map having the same
start_location as the preceding LC_RENAME one.
Next thing that happens is computation of included_from:
   if (reason == LC_ENTER)
     {
       if (set->depth == 0)
         map->included_from = 0;
       else
         /* The location of the end of the just-closed map.  */
         map->included_from
           = (((map[0].start_location - 1 - map[-1].start_location)
               & ~((1 << map[-1].m_column_and_range_bits) - 1))
              + map[-1].start_location);
The normal case (e.g. with the testcase included at the start of this comment) 
is
that map[-1] starts somewhere earlier and so map->included_from computation 
above
nicely computes location_t which expands to the start of the #include line.
With r14-11679 reverted, for #c11 as well as #c12
map[0].start_location == map[-1].start_location above, and so it is
((location_t) -1 & ~((1 << map[-1].m_column_and_range_bits) - 1)))
+ map[-1].start_location,
which happens to be start of the #include line.
For #c11 map[0].start_location is 0x500003a0 and map[-1] has
m_column_and_range_bits 7 and map[-2] has m_column_and_range_bits 12 and
map[0].included_from is set to 0x50000320.
For #c12 map[0].start_location is 0x606c0402 and map[-2].start_location is
0x606c0400 and m_column_and_range_bits is 0 for all 3 maps.
map[0].included_from is set to 0x606c0401.
The last important part is again in linemap_add when doing LC_LEAVE:
       /* (MAP - 1) points to the map we are leaving. The
          map from which (MAP - 1) got included should be the map
          that comes right before MAP in the same file.  */
       from = linemap_included_from_linemap (set, map - 1);

       /* A TO_FILE of NULL is special - we use the natural values.  */
       if (to_file == NULL)
         {
           to_file = ORDINARY_MAP_FILE_NAME (from);
           to_line = SOURCE_LINE (from, from[1].start_location);
           sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
         }
Here it wants to compute the right to_line which ought to be the line after
the #include directive.
On the #c11 testcase that doesn't work correctly though, because
map[-1].included_from is 0x50000320, from[0] for that is LC_ENTER with
start_location 0x4080 and m_column_and_range_bits 12 but note that we've
earlier computed map[-1].start_location + (-1 & 0xffffff80) and so only
decreased by 7 bits, so to_line is still on the line with #include and not
after it.  In the #c12 that doesn't happen, all the ordinary maps involved
there had 0 m_column_and_range_bits and so this computes correct line.

Below is a fix for the trunk including testcases using the
location_overflow_plugin hack to simulate the bugs without needing huge
files (in the 14 case it is just 330KB and almost 10MB, but in the 15
case it would need to be far bigger).
The pre- r15-9018 trunk has
FAIL: gcc.dg/plugin/location-overflow-test-pr116047.c 
-fplugin=./location_overflow_plugin.so  scan-file 
static_assert[^\n\r]*6[^\n\r]*== 6
and current trunk
FAIL: gcc.dg/plugin/location-overflow-test-pr116047.c 
-fplugin=./location_overflow_plugin.so  scan-file 
static_assert[^\n\r]*6[^\n\r]*== 6
FAIL: gcc.dg/plugin/location-overflow-test-pr120061.c 
-fplugin=./location_overflow_plugin.so  scan-file 
static_assert[^\n\r]*5[^\n\r]*== 5
and with the patch everything PASSes.
I'll post afterwards a 14 version of the patch.

The patch reverts the r15-9018 change, because it is incorrect,
we really need to decrement it even when crossing ordinary map
boundaries, so that the location is not on the line after the #include
line but somewhere on the #include line.  It also patches two spots
in linemap_add mentioned above to make sure we get correct locations
both in the included_from location_t when doing LC_ENTER (second
line-map.cc hunk) and when doing LC_LEAVE to compute the right to_line
(first line-map.cc hunk), both in presence of an added LC_RENAME
with the same start_location as the following LC_ENTER (i.e. the
problematic cases).
The LC_ENTER hunk is mostly to ensure included_form location_t is
at the start of the #include line (column 0), without it we can
decrease include_from not enough and end up at some random column
in the middle of the line, because it is masking away
map[-1].m_column_and_range_bits bits even when in the end the resulting
include_from location_t will be found in map[-2] map with perhaps
different m_column_and_range_bits.  That alone doesn't fix the bug
though.
The more important is the LC_LEAVE hunk and the problem there is
caused by linemap_line_start not actually doing
     r = set->highest_line + (line_delta << map->m_column_and_range_bits);
when adding a new map (the LC_RENAME one because we need to switch to
different number of directly encoded ranges, or columns, etc.).
So, in the original PR108900 case that
   to_line = SOURCE_LINE (from, from[1].start_location);
doesn't do the right thing, from there is the last < 0x50000000 map
with m_column_and_range_bits 12, from[1] is the first one above it
and map[-1].included_from is the correct location of column 0 on
the #include line, but as the new LC_RENAME map has been created without
actually increasing highest_location to be on the new line (we've just
set to_line of the new LC_RENAME map to the correct line),
   to_line = SOURCE_LINE (from, from[1].start_location);
stays on the same source line.  I've tried to just replace that with
   to_line = SOURCE_LINE (from, linemap_included_from (map - 1)) + 1;
i.e. just find out the #include line from map[-1].included_from and
add 1 to it, unfortunately that breaks the
c-c++-common/cpp/line-4.c
test where we expect to stay on the same 0 line for LC_LEAVE from
<command line> and gcc.dg/cpp/trad/Wunused.c, gcc.dg/cpp/trad/builtins.c
and c-c++-common/analyzer/named-constants-via-macros-traditional.c tests
all with -traditional-cpp preprocessing where to_line is also off-by-one
from the expected one.
So, this patch instead conditionalizes it, uses the
   to_line = SOURCE_LINE (from, linemap_included_from (map - 1)) + 1;
way only if from[1] is a LC_RENAME map (rather than the usual
LC_ENTER one), that should limit it to the problematic cases of when
parse_include peeked after EOL and had to create LC_RENAME map with
the same start_location as the LC_ENTER after it.

Well, that's all very complicated but seems to make sense. Can you also add short rationale comments to the changes in linemap_add and plugin_init?

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
and 15 branch?

2025-05-06  Jakub Jelinek  <ja...@redhat.com>

        PR preprocessor/108900
        PR preprocessor/116047
        PR preprocessor/120061
        * files.cc (_cpp_stack_file): Revert 2025-03-28 change.
        * line-map.cc (linemap_add): Use
        SOURCE_LINE (from, linemap_included_from (map - 1)) + 1; instead of
        SOURCE_LINE (from, from[1].start_location); to compute to_line
        for LC_LEAVE.  For LC_ENTER included_from computation, look at
        map[-2] or even lower if map[-1] has the same start_location as
        map[0].

        * gcc.dg/plugin/plugin.exp: Add location-overflow-test-pr116047.c
        and location-overflow-test-pr120061.c.
        * gcc.dg/plugin/location_overflow_plugin.c (plugin_init): Don't error
        on unknown values, instead just break.  Handle 0x4fHHHHHH arguments
        differently.
        * gcc.dg/plugin/location-overflow-test-pr116047.c: New test.
        * gcc.dg/plugin/location-overflow-test-pr116047-1.h: New test.
        * gcc.dg/plugin/location-overflow-test-pr116047-2.h: New test.
        * gcc.dg/plugin/location-overflow-test-pr120061.c: New test.
        * gcc.dg/plugin/location-overflow-test-pr120061-1.h: New test.
        * gcc.dg/plugin/location-overflow-test-pr120061-2.h: New test.

--- libcpp/files.cc.jj  2025-05-03 11:02:02.502647404 +0200
+++ libcpp/files.cc     2025-05-05 21:09:18.042680877 +0200
@@ -1006,14 +1006,6 @@ _cpp_stack_file (cpp_reader *pfile, _cpp
                    && (pfile->line_table->highest_location
                        != LINE_MAP_MAX_LOCATION - 1));
- if (decrement && LINEMAPS_ORDINARY_USED (pfile->line_table))
-    {
-      const line_map_ordinary *map
-       = LINEMAPS_LAST_ORDINARY_MAP (pfile->line_table);
-      if (map && map->start_location == pfile->line_table->highest_location)
-       decrement = false;
-    }
-
    if (decrement)
      pfile->line_table->highest_location--;
--- libcpp/line-map.cc.jj 2024-04-26 11:47:02.244168816 +0200
+++ libcpp/line-map.cc  2025-05-05 21:57:07.412793519 +0200
@@ -630,7 +630,10 @@ linemap_add (line_maps *set, enum lc_rea
        if (to_file == NULL)
        {
          to_file = ORDINARY_MAP_FILE_NAME (from);
-         to_line = SOURCE_LINE (from, from[1].start_location);
+         if (from[1].reason == LC_RENAME)
+           to_line = SOURCE_LINE (from, linemap_included_from (map - 1)) + 1;
+         else
+           to_line = SOURCE_LINE (from, from[1].start_location);
          sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (from);
        }
        else
@@ -660,11 +663,16 @@ linemap_add (line_maps *set, enum lc_rea
        if (set->depth == 0)
        map->included_from = 0;
        else
-       /* The location of the end of the just-closed map.  */
-       map->included_from
-         = (((map[0].start_location - 1 - map[-1].start_location)
-             & ~((loc_one << map[-1].m_column_and_range_bits) - 1))
-            + map[-1].start_location);
+       {
+         /* The location of the end of the just-closed map.  */
+         int i = -1;
+         while (map[i].start_location == map[0].start_location)
+           --i;
+         map->included_from
+           = (((map[0].start_location - 1 - map[i].start_location)
+               & ~((loc_one << map[i].m_column_and_range_bits) - 1))
+              + map[i].start_location);
+       }
        set->depth++;
        if (set->trace_includes)
        trace_include (set, map);
--- gcc/testsuite/gcc.dg/plugin/plugin.exp.jj   2025-01-02 20:54:32.508124675 
+0100
+++ gcc/testsuite/gcc.dg/plugin/plugin.exp      2025-05-06 10:49:48.768350236 
+0200
@@ -138,7 +138,9 @@ set plugin_test_list [list \
      { location_overflow_plugin.cc \
          location-overflow-test-1.c \
          location-overflow-test-2.c \
-         location-overflow-test-pr83173.c } \
+         location-overflow-test-pr83173.c \
+         location-overflow-test-pr116047.c \
+         location-overflow-test-pr120061.c } \
      { must_tail_call_plugin.cc \
          must-tail-call-1.c \
          must-tail-call-2.c } \
--- gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc.jj  2024-12-09 
21:04:17.059805398 +0100
+++ gcc/testsuite/gcc.dg/plugin/location_overflow_plugin.cc     2025-05-06 
10:46:29.432071833 +0200
@@ -87,7 +87,10 @@ plugin_init (struct plugin_name_args *pl
    /* With 64-bit locations, the thresholds are larger, so shift the base
       location argument accordingly.  */
    gcc_assert (sizeof (location_t) == sizeof (uint64_t));
-  base_location = 1 + ((base_location - 1) << 31);
+  if (base_location >= 0x4f000000 && base_location <= 0x4fffffff)
+    base_location += (((location_t) 0x50000000) << 31) - 0x50000000;
+  else
+    base_location = 1 + ((base_location - 1) << 31);
register_callback (plugin_info->base_name,
                     PLUGIN_PRAGMAS,
@@ -107,7 +110,7 @@ plugin_init (struct plugin_name_args *pl
        break;
default:
-      error_at (UNKNOWN_LOCATION, "unrecognized value for plugin argument");
+      break;
      }
return 0;
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047.c.jj    
2025-05-06 10:46:29.431071847 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047.c       
2025-05-06 11:27:06.003811731 +0200
@@ -0,0 +1,5 @@
+/* PR preprocessor/116047 */
+/* { dg-do preprocess } */
+/* { dg-options "-nostdinc -std=c23 
-fplugin-arg-location_overflow_plugin-value=0x4ffe0180" } */
+#include "location-overflow-test-pr116047-1.h"
+/* { dg-final { scan-file location-overflow-test-pr116047.i 
"static_assert\[^\n\r]\*6\[^\n\r]\*== 6" } } */
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047-1.h.jj  
2025-05-06 10:46:29.431071847 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047-1.h     
2025-05-06 11:21:54.172065679 +0200
@@ -0,0 +1,6 @@
+
+
+
+
+#include "location-overflow-test-pr116047-2.h"
+static_assert (__LINE__ == 6, "");
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047-2.h.jj  
2025-05-06 10:46:29.431071847 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr116047-2.h     
2025-05-06 10:46:29.431071847 +0200
@@ -0,0 +1 @@
+int i;
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061.c.jj    
2025-05-06 10:46:29.432071833 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061.c       
2025-05-06 10:46:29.432071833 +0200
@@ -0,0 +1,6 @@
+/* PR preprocessor/120061 */
+/* { dg-do preprocess } */
+/* { dg-options "-nostdinc -std=c23 
-fplugin-arg-location_overflow_plugin-value=0x61000000" } */
+#include "location-overflow-test-pr120061-1.h"
+static_assert (__LINE__ == 5, "");
+/* { dg-final { scan-file location-overflow-test-pr120061.i 
"static_assert\[^\n\r]\*5\[^\n\r]\*== 5" } } */
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061-1.h.jj  
2025-05-06 10:46:29.429071874 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061-1.h     
2025-05-06 10:46:29.429071874 +0200
@@ -0,0 +1,6 @@
+
+
+
+
+#include "location-overflow-test-pr120061-2.h"
+
--- gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061-2.h.jj  
2025-05-06 10:46:29.431071847 +0200
+++ gcc/testsuite/gcc.dg/plugin/location-overflow-test-pr120061-2.h     
2025-05-06 10:46:29.431071847 +0200
@@ -0,0 +1 @@
+int i;

        Jakub


Reply via email to