public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lhyatt at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug preprocessor/105608] [11/12/13/14 Regression] ICE: in linemap_add with a really long defined macro on the command line r11-338-g2a0225e47868fbfc
Date: Tue, 02 May 2023 20:56:19 +0000	[thread overview]
Message-ID: <bug-105608-4-yOctMHq0HY@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105608-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105608

--- Comment #2 from Lewis Hyatt <lhyatt at gcc dot gnu.org> ---
When a PCH file is restored, the global line_maps instance is replaced with the
one that was stored in the PCH file. Hence any locations that were generated
prior to restoring the PCH file need to be added back to it. Currently, the
only such locations that can arise will be from preprocessor directives, such
as #define. The save/restore process is handled by libcpp/pch.cc and, looking
over this now, it doesn't seem it makes any effort to assign valid locations
when re-adding the saved macros. If the just-restored line map is at depth=0,
as it is after processing the empty pch, then it produces the ICE noted here in
case the first line map added is of type LC_RENAME, which happens if an ad-hoc
location is needed due to the long line. However you can see the invalid
locations without an ICE as well, for instance in this simpler example:

$ echo -n > h.h
$ cat > a.cpp
#define HELLO
#include "h.h"

$ gcc -x c++-header -c h.h
$ gcc -x c++ -c a.cpp -Wunused-macros
h.h:2: warning: macro "HELLO" is not used [-Wunused-macros]

Note that the warning is emitted with the wrong filename and the wrong line
number. If the PCH is absent, it will be correct, but when the definition of
HELLO is processed the second time after PCH restore, it comes out wrong.
Fixing this issue will also fix the ICE reported here; I think it will be
needed to add some more infrastructure in libcpp/pch.cc so that it can add to
the line map with proper locations. I can look into it sometime.

This was always incorrect AFAIK, and did not regress with
r11-338-g2a0225e47868fbfc specifically; that commit did make the line number
change from 1 to 2, because it handles EOF differently, and I think that caused
Sergei's test case to ICE rather than be silently wrong.

BTW, the issue can also be papered over by moving these 2 lines:

====
diff --git a/gcc/c-family/c-pch.cc b/gcc/c-family/c-pch.cc
index 2f014fca210..9ee6f179002 100644
--- a/gcc/c-family/c-pch.cc
+++ b/gcc/c-family/c-pch.cc
@@ -342,6 +342,8 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
   gt_pch_restore (f);
   cpp_set_line_map (pfile, line_table);
   rebuild_location_adhoc_htab (line_table);
+  line_table->trace_includes = saved_trace_includes;
+  linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);

   timevar_push (TV_PCH_CPP_RESTORE);
   if (cpp_read_state (pfile, name, f, smd) != 0)
@@ -355,9 +357,6 @@ c_common_read_pch (cpp_reader *pfile, const char *name,

   fclose (f);

-  line_table->trace_includes = saved_trace_includes;
-  linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
-
   /* Give the front end a chance to take action after a PCH file has
      been loaded.  */
   if (lang_post_pch_load)
=====

With that change, you still get wrong locations for the restored macros, but
the line_map state is sufficiently improved that the ICE is avoided. (The
locations get assigned, as if all the macros were defined on the line following
the PCH include.)

  parent reply	other threads:[~2023-05-02 20:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-15  8:08 [Bug preprocessor/105608] New: [13 Regression] ICE: in linemap_add, at libcpp/line-map.cc:502 on ovito-3.7.1 slyfox at gcc dot gnu.org
2022-05-16  7:12 ` [Bug preprocessor/105608] [11/12/13 Regression] ICE: in linemap_add, at libcpp/line-map.cc:502 on ovito-3.7.1 since r11-338-g2a0225e47868fbfc marxin at gcc dot gnu.org
2022-05-16  7:30 ` pinskia at gcc dot gnu.org
2022-05-16  8:12 ` rguenth at gcc dot gnu.org
2023-05-02 20:56 ` lhyatt at gcc dot gnu.org [this message]
2023-05-29 10:07 ` [Bug preprocessor/105608] [11/12/13/14 Regression] ICE: in linemap_add with a really long defined macro on the command line r11-338-g2a0225e47868fbfc jakub at gcc dot gnu.org
2023-12-15 21:30 ` lhyatt at gcc dot gnu.org
2024-01-27  4:30 ` cvs-commit at gcc dot gnu.org
2024-01-27 12:56 ` cvs-commit at gcc dot gnu.org
2024-01-27 17:08 ` cvs-commit at gcc dot gnu.org
2024-01-27 21:51 ` cvs-commit at gcc dot gnu.org
2024-01-27 21:53 ` lhyatt at gcc dot gnu.org
2024-01-30 13:57 ` ro at gcc dot gnu.org
2024-01-30 14:38 ` lhyatt at gcc dot gnu.org
2024-01-30 14:54 ` ro at CeBiTec dot Uni-Bielefeld.DE
2024-01-30 22:05 ` lhyatt at gcc dot gnu.org
2024-01-31 14:49 ` lhyatt at gcc dot gnu.org
2024-02-01 14:17 ` cvs-commit at gcc dot gnu.org
2024-02-22 14:45 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105608-4-yOctMHq0HY@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).