[Bug preprocessor/41543] New: BUILTINS_LOCATION wrong with -fpreprocessed

jakub at gcc dot gnu dot org gcc-bugzilla@gcc.gnu.org
Fri Oct 2 09:53:00 GMT 2009


With -fpreprocessed, expand_location (BUILTINS_LOCATION) yields the
preprocessed source file name (e.g. bar.ii), line 1, column 1, instead of
<built-in> filename, line 0.  So e.g. ./cc1 -g -dA -fpreprocessed bar.ii
where bar.ii is:
# 1 "foo.C"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo.C"
int
foo (__builtin_va_list ap)
{
  return __builtin_va_arg (ap,int);
}
will have bar.ii in the filemap and e.g. on x86-64 va_list struct and its
fields will use that as location.

BUILTINS_LOCATION is 2, and for normal compilation without -fpreprocessed
there is just a:
cpp_read_main_file -> _cpp_stack_file -> _cpp_do_file_change -> linemap_add
call that bumps line_table->highest_location to 1, and then there is:
1491  if (!cpp_opts->preprocessed)
1492    {
1493      size_t i;
1494
1495      cb_file_change (parse_in,
1496                      linemap_add (line_table, LC_RENAME, 0,
1497                                   _("<built-in>"), 0));
in c-opts.c which ensures that location 2 maps to <built-in>.
But with -fpreprocessed cpp_read_main_file does additionally:
  /* For foo.i, read the original filename foo.c now, for the benefit
     of the front ends.  */
  if (CPP_OPTION (pfile, preprocessed))
    {
      read_original_filename (pfile);
      fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
    }
and read_original_filename during lexing of the first character already eats
some locations, bumping line_table->highest_location.

Not sure what would be best to fix it.

--- init.c.xx        2009-06-30 13:10:43.000000000 +0200
+++ init.c        2009-10-02 11:49:51.000000000 +0200
@@ -554,6 +554,13 @@ cpp_read_main_file (cpp_reader *pfile, c
      of the front ends.  */
   if (CPP_OPTION (pfile, preprocessed))
     {
+      /* Hack: GCC expects that (source_location) 2 is BUILTINS_LOCATION,
+         but after read_original_filename lexing it will be at the start of
+         the first line of the preprocessed file.  */
+      linemap_add (pfile->line_table, LC_RENAME, 0, _("<built-in>"), 0);
+      linemap_add (pfile->line_table, LC_RENAME, 0,
+           pfile->line_table->maps[pfile->line_table->used-2].to_file,
+           1);
       read_original_filename (pfile);
       fname = pfile->line_table->maps[pfile->line_table->used-1].to_file;
     }

cures this, but feels like a hack.  As it is gcc specific, it would need to be
some libcpp callback or something.  Changing BUILTINS_LOCATION to say
(source_location) -1, changing the places that check e.g. that location is <=
BUILTINS_LOCATION and special casing BUILTINS_LOCATION in expand_location could
perhaps work too.

Tom, what do you prefer?


-- 
           Summary: BUILTINS_LOCATION wrong with -fpreprocessed
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jakub at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41543



More information about the Gcc-bugs mailing list