From patchwork Wed Dec 6 01:52:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 174270 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3827711vqy; Tue, 5 Dec 2023 17:52:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IFI4Ok1tR2upS9to4OJwTbTx0uC/XaLuCvkbBa4UV5T4ml2DAMhNvDkcWWPOlTLxEPEyA8I X-Received: by 2002:a05:620a:4622:b0:77e:fba3:9ceb with SMTP id br34-20020a05620a462200b0077efba39cebmr218928qkb.79.1701827568672; Tue, 05 Dec 2023 17:52:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701827568; cv=pass; d=google.com; s=arc-20160816; b=pqIcq8ICymNmc+5+1YQjmRoA72321fOurGUUenKQOWvytcRIYYuU86w8gCmtaYtZqe S3QQJIouqi651mB5CnK/8brqnMrRTw0BJjgTWjsh6QPjSLnDo3OuiT1pEUMD3bJI9BU2 ro4njcR2UO36rEDT8K6gfnhyvANnkru9nxsBdydVJox8rFAkaqkJ+ZdMPK4bv7Kbk/zq jAQQxn+sW+H2FrsCaAIq4ZBHnMTCdFpoHYBw5VMA0rgTytEkoKVrz0/CEm+4SgJPrlXf c0VNgCxoMBFsGMF+hhMJ6cwwkY38ciE79bLD27ACEDit5qJGDSt5EDPDjpB2LS9OI2EQ C4lQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=TPRGy8xEkFgYRyd2pYFfPCTCUQmqHj+Ou0hC5qwQ6r8=; fh=nfBkqenZlXK8N3U+CwE1Ju+aGndKKEqCeWWaqIiAkr0=; b=CAbaFo4x3HheAePxMfpCvjoRmUg7DIpzRK74CKwmfSNkQKXzFyZEw7i8VkANENf7GG pZS6elo89vMpQCU/jnyJcdWMNgGgcJ2K2jyKOfQVwtfKaSYRdoTpDEmnkLa8RptXZGB3 0TMgADtxcehAEJHAL+LsBCUjOzlMgedKPKQ8ZnXemuQ9fRzT1XRJcjFn6mlIOLt5rkjr lgCdy5JvvKKZLq3xDsK2JmML05q7OuttJ/YEtlLMyR+LsCjRSQn/adE4k1y9suA92EWg C0JzPelW5ZNUhtnM212QlYqLAc9iKNcYjRNReO5m28Sfpe/JBzOLK2P4dmI42snaTzru toPQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jCJqVXrS; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id de42-20020a05620a372a00b0076f0cc82e44si13678049qkb.718.2023.12.05.17.52.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 17:52:48 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jCJqVXrS; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 54F1D385840B for ; Wed, 6 Dec 2023 01:52:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by sourceware.org (Postfix) with ESMTPS id F23DD385840B for ; Wed, 6 Dec 2023 01:52:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F23DD385840B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F23DD385840B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::834 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701827544; cv=none; b=E5UZFzRYrlG7puEhP/51zzthblm2QQih4Uzno/uOrD70wCB07x0A6Rrnq5pzpo9hwNQl8kPURpWx3IpyfYONuyAt1mflPZ9BiX5VGaj/Zw4gKm7xjIMXrFA7IAqbo0D29QzcPdOXh1THdU99mZZH0vw9GYP0cE2vhB0cYxslaKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701827544; c=relaxed/simple; bh=zKHhw1wr+UhcgYpY98xzADXtVd76vMTyy1epvhcGZ1k=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=M0VfDWfb6SDd/VVHesKWw57AHgTr9ffMgKxY9/SsFpRhNPoiaCW0bKbxI2P0OSv7MWQFzOCGkDaosW7gmz0vi14/FX8T3s9/yg4hlU2ft8rWP+tN60yxGhvoINHOvWM95x6iAFJyLYLCbREUOfUdKIPhwL8BAcgjr4xUxe35uQk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qt1-x834.google.com with SMTP id d75a77b69052e-4254adc1f1cso2042901cf.1 for ; Tue, 05 Dec 2023 17:52:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701827541; x=1702432341; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TPRGy8xEkFgYRyd2pYFfPCTCUQmqHj+Ou0hC5qwQ6r8=; b=jCJqVXrS/n+29TaT80enM90Em3V3aQ7qxwoQGAxTQ6XjgY6yGNYYAF5sr6WdzjYhto D3XjzDl6joCKMx8C/AgckJEPBcOvCFuq09ixuTHGszp+exXzKllVJ4YDncrnuze0TDdZ Us6rKSX7u8wjsseOxZ6XGanW3ryvYGdz53N9UOzq4VMHgcMvWjQRCNhpoOGYK58roO39 +FLGBjwXSAnFg8jesUskGyHiSh4A8rgBO3jEJ83nCn+OeiDATxzoYXxB0KcNhqJWfIXi +cyYdhdq9eSpeNGIrJxmoc5NztgNXryU+MkibdFRYLgpVn8kniXh1JkxzK7MhvFnBXdj U/9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701827541; x=1702432341; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TPRGy8xEkFgYRyd2pYFfPCTCUQmqHj+Ou0hC5qwQ6r8=; b=S2nNgNiWjNX59JPHPOg+72e99zFiUeJX6xGnZ7GjOLesRXjHOKYwa8nAeKOt1nbkZp YSl/fdQSXvM7GNv9+N7jbmGifz00hzXoaik6+w+EEyXDKY9fQYsN80a3LqZ4c/GRiIpd LUud+NcKpZIljfSvRL+jTlgbQI0ucHXhgH/2IfA9lPPzQW9+tfJp8O8KpMZlCs0yy2AR fj3WRZcQYVRFBss7RflSHRnKqkx3n1XH2ut4dzK7CzuC433Iq5qXVtnOR9eg7L21Z9F7 reKcX2hjQ4U3CsTC51K5pwsANqJP2D2xKFNtpZofD2k2lZPgNdkMw48HM26BbA6C7kjV +Fxw== X-Gm-Message-State: AOJu0YzRpwDQ5dWUlMEnyQrwzfDzXAKI4DxuXCM+I/ZP10ttovmoSJ2d Derrm/WsgNlLhK1TR3Ninogap46fKyU= X-Received: by 2002:a05:622a:64b:b0:425:4043:5f29 with SMTP id a11-20020a05622a064b00b0042540435f29mr229308qtb.103.1701827541062; Tue, 05 Dec 2023 17:52:21 -0800 (PST) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id cm17-20020a05622a251100b004255a974865sm1952206qtb.18.2023.12.05.17.52.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 17:52:20 -0800 (PST) From: Lewis Hyatt To: gcc-patches@gcc.gnu.org Cc: Lewis Hyatt Subject: [PATCH] c-family: Fix ICE with large column number after restoring a PCH [PR105608] Date: Tue, 5 Dec 2023 20:52:11 -0500 Message-Id: <20231206015211.682650-1-lhyatt@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3038.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784495544748437180 X-GMAIL-MSGID: 1784495544748437180 Hello- https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105608 There are two related issues here really, a regression since GCC 11 where we can ICE after restoring a PCH, and a deeper issue with bogus locations assigned to macros that were defined prior to restoring a PCH. This patch fixes the ICE regression with a simple change, and I think it's appropriate for GCC 14 as well as backport to 11, 12, 13. The bad locations (wrong, but not generally causing an ICE, and mostly affecting only the output of -Wunused-macros) are not as problematic, and will be harder to fix. I could take a stab at that for GCC 15. In the meantime the patch adds XFAILed tests for the wrong locations (as well as passing tests for the regression fix). Does it look OK please? Bootstrap + regtest all languages on x86-64 Linux. Thanks! -Lewis -- >8 -- Users are allowed to define macros prior to restoring a precompiled header file, as long as those macros are not defined (or are defined identically) in the PCH. However, the PCH restoration process destroys all the macro definitions, so libcpp has to record them before restoring the PCH and then redefine them afterward. This process does not currently assign great locations to the macros after redefining them. Some work is needed to also remember the original locations and get the line_maps instance in the right state (since, like all other data structures, the line_maps instance is also reset after restoring a PCH). The new testcase line-map-3.C contains XFAILed examples where the locations are wrong. This patch addresses a more pressing issue, which is that we ICE in some cases since GCC 11, hitting an assert in line-maps.cc. It happens if the first line encountered after the PCH restore requires an LC_RENAME map, such as will happen if the line is sufficiently long. This is much easier to fix, since we just need to call linemap_line_start before asking libcpp to redefine the stored macros, instead of afterward, to avoid the unexpected need for an LC_RENAME before an LC_ENTER has been seen. gcc/c-family/ChangeLog: PR preprocessor/105608 * c-pch.cc (c_common_read_pch): Start a new line map before asking libcpp to restore macros defined prior to reading the PCH, instead of afterward. gcc/testsuite/ChangeLog: PR preprocessor/105608 * g++.dg/pch/line-map-1.C: New test. * g++.dg/pch/line-map-1.Hs: New test. * g++.dg/pch/line-map-2.C: New test. * g++.dg/pch/line-map-2.Hs: New test. * g++.dg/pch/line-map-3.C: New test. * g++.dg/pch/line-map-3.Hs: New test. --- gcc/c-family/c-pch.cc | 5 ++--- gcc/testsuite/g++.dg/pch/line-map-1.C | 4 ++++ gcc/testsuite/g++.dg/pch/line-map-1.Hs | 1 + gcc/testsuite/g++.dg/pch/line-map-2.C | 6 ++++++ gcc/testsuite/g++.dg/pch/line-map-2.Hs | 1 + gcc/testsuite/g++.dg/pch/line-map-3.C | 23 +++++++++++++++++++++++ gcc/testsuite/g++.dg/pch/line-map-3.Hs | 1 + 7 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/pch/line-map-1.C create mode 100644 gcc/testsuite/g++.dg/pch/line-map-1.Hs create mode 100644 gcc/testsuite/g++.dg/pch/line-map-2.C create mode 100644 gcc/testsuite/g++.dg/pch/line-map-2.Hs create mode 100644 gcc/testsuite/g++.dg/pch/line-map-3.C create mode 100644 gcc/testsuite/g++.dg/pch/line-map-3.Hs 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) diff --git a/gcc/testsuite/g++.dg/pch/line-map-1.C b/gcc/testsuite/g++.dg/pch/line-map-1.C new file mode 100644 index 00000000000..9d1ac6d1683 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-1.C @@ -0,0 +1,4 @@ +/* PR preprocessor/105608 */ +/* { dg-do compile } */ +#define MACRO_ON_A_LONG_LINE "this line is long enough that it forces the line table to create an LC_RENAME map, which formerly triggered an ICE after PCH restore" +#include "line-map-1.H" diff --git a/gcc/testsuite/g++.dg/pch/line-map-1.Hs b/gcc/testsuite/g++.dg/pch/line-map-1.Hs new file mode 100644 index 00000000000..3b6178bfae0 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-1.Hs @@ -0,0 +1 @@ +/* This space intentionally left blank. */ diff --git a/gcc/testsuite/g++.dg/pch/line-map-2.C b/gcc/testsuite/g++.dg/pch/line-map-2.C new file mode 100644 index 00000000000..0be035781c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-2.C @@ -0,0 +1,6 @@ +/* PR preprocessor/105608 */ +/* { dg-do compile } */ +/* { dg-additional-options "-save-temps" } */ +#define MACRO_ON_A_LONG_LINE "this line is long enough that it forces the line table to create an LC_RENAME map, which formerly triggered an ICE after PCH restore" +#include "line-map-2.H" +#error "suppress PCH assembly comparison, which does not work with -save-temps" /* { dg-error "." } */ diff --git a/gcc/testsuite/g++.dg/pch/line-map-2.Hs b/gcc/testsuite/g++.dg/pch/line-map-2.Hs new file mode 100644 index 00000000000..3b6178bfae0 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-2.Hs @@ -0,0 +1 @@ +/* This space intentionally left blank. */ diff --git a/gcc/testsuite/g++.dg/pch/line-map-3.C b/gcc/testsuite/g++.dg/pch/line-map-3.C new file mode 100644 index 00000000000..3390d7adba2 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-3.C @@ -0,0 +1,23 @@ +#define UNUSED_MACRO /* { dg-error "UNUSED_MACRO" "" { xfail *-*-* } } */ +#include "line-map-3.H" /* { dg-bogus "-:UNUSED_MACRO" "" { xfail *-*-* } } */ + +/* { dg-do compile } */ +/* { dg-additional-options "-Werror=unused-macros" } */ + +/* PR preprocessor/105608 */ +/* This test case is currently xfailed and requires work in libcpp/pch.cc to + resolve. Currently, the macro location is incorrectly assigned to line 2 + of the header file when read via PCH, because libcpp doesn't try to + assign locations relative to the newly loaded line map after restoring + the PCH. */ + +/* In PCH mode we also complain incorrectly about the command line macro -Dwith_PCH + added by dejagnu; that warning would get suppressed if the macro location were + correctly restored by libcpp to reflect that it was a command line macro. */ +/* { dg-bogus "-:with_PCH" "" { xfail *-*-* } 2 } */ + +/* The reason we used -Werror was to prevent pch.exp from rerunning without PCH; + in that case we would get unnecessary XPASS outputs since the test does work + fine without PCH. Once the bug is fixed, remove the -Werror and switch to + dg-warning. */ +/* { dg-regexp {[^[:space:]]*: some warnings being treated as errors} } */ diff --git a/gcc/testsuite/g++.dg/pch/line-map-3.Hs b/gcc/testsuite/g++.dg/pch/line-map-3.Hs new file mode 100644 index 00000000000..3b6178bfae0 --- /dev/null +++ b/gcc/testsuite/g++.dg/pch/line-map-3.Hs @@ -0,0 +1 @@ +/* This space intentionally left blank. */