From patchwork Tue Feb 20 04:37:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 86016 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA76D3858C54 for ; Tue, 20 Feb 2024 04:39:15 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by sourceware.org (Postfix) with ESMTPS id 465C83858D1E for ; Tue, 20 Feb 2024 04:38:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 465C83858D1E Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 465C83858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::22e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708403932; cv=none; b=b8I4Gm9sUYnYSNgEq4dJkwH4KTZCX2bs3chqm7E75xFwPq+udhCRjMSr1GVTb6kYBXRdW+8bCnmsv7GBRZybKWLlir34pf7oiyj6k2MKXh8g3VAM2rzgO7KcwrT4yQKwwyDKIy8T1H/iywAOvGiy7LqAK82AVNazxl8Krnjjy/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708403932; c=relaxed/simple; bh=3bAjKjVq1ahzTtTy/qkmZrOtmCtpeYU1P5cE3PqpkRQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZN1/cZJmlR/68pcNBDjMDsqZs8hTT+Gx5LrIPmlN6CogGU5z6iw2Iso5okWdpHdRzToNVhZkNv1CK1xkfeTyoFB1LBjtMjPXjFlFIo8Eyp96KBo2gSuJk2eC3DjlGjcdegCbzFaEfAw3dUBsYlL4rfKKRYO4gIbj1rXgTIVlQY4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3bbbc6b4ed1so3830103b6e.2 for ; Mon, 19 Feb 2024 20:38:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1708403929; x=1709008729; darn=gcc.gnu.org; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=wJo0dRB7svp6I90ZeTCOUK2LfbOhrPgqFhkz1KByqIw=; b=GHLl4sbT9BRTKcRruXjWDG4O6uAhXUkkItiudm2JscpJwK5rsR4DOdLJf/MqjuQZAF DB8jaOvMhFXDuW4mIV+pZbEpJWDJrvLYk0tYuqWTW96lpFLChDrqAFBSl1ouHfdmKo8b 7Ee8o/SDdUhhsgu9uIohz5jYn3+h+QMxAt7/m7VZM1OUVPaWrYyn+BRv4aKRMSGOtF87 6iHhy2TXCWmF/+XLLmHJiqQII48fZeAR7r6LD5JeMEF9CmZP6//z4LO0CZ9BQYiVmsEZ 6ciwzXUZoFhiZs8QvUxPl/LKWngoekvqoEsdbczo/SoQ/XYQcS1pvEpkJIMrWz6kw2Cf jPzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708403929; x=1709008729; h=mime-version:user-agent:message-id:date:organization:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wJo0dRB7svp6I90ZeTCOUK2LfbOhrPgqFhkz1KByqIw=; b=LDdPduwY4oeB4izooAYyvrouU/7GMKhYqzTxiRFvz6fEXA4m7YISEpJZCsVOmaeU9J hPh52Q8hdPCUflgAqA+E4GesXV9DS+13eJTbf8Z4oAfNTuoP3BjDgfi5MWioKGdWwYr7 5f/qL694Poclv6A+NSEXI6o5qRgHbYEJR4Kaa+RKNgbOjU1KHtgvOjubcAtYbDE7wWki 6qF/8gio3KeDCsBJPaD7Ifg0VflbDOTAnw6B48eqyye1bKi1LajK8+0peqds9zS8YVAs oUsGqDYXYSJ3BL/lwLewD/qQA9ZFw2H1ocwTmBxnxEuVfmMeABlVxm/vR9m2mdaDSh8K YycQ== X-Gm-Message-State: AOJu0YwYB+kBl/abHeDu3YAa//vdXpQGTfKdWaAwg23IQoDiTqOCaJwZ QeQhKX5jfslrsEU4w1CAHM81VAKwLUwHZKbMoPfL7iCQI+aNnxsaxzmUtSP+VWm4CefWdxuB0sB UqQ== X-Google-Smtp-Source: AGHT+IG7n43OUTUAovhR79267RvQA6NdC2ioTOOeMrgqPq+Usabn7abdj6TRx8HnNtta+yLHsv3ryg== X-Received: by 2002:a05:6808:2127:b0:3c1:5fd4:314a with SMTP id r39-20020a056808212700b003c15fd4314amr3945685oiw.24.1708403928726; Mon, 19 Feb 2024 20:38:48 -0800 (PST) Received: from free.home ([2804:7f1:218a:c88b:e868:4eaf:8258:c30b]) by smtp.gmail.com with ESMTPSA id w184-20020a6262c1000000b006e47ab9b2e5sm422814pfb.215.2024.02.19.20.38.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 20:38:48 -0800 (PST) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 41K4blwZ006310 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 20 Feb 2024 01:37:47 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Cc: Per Bothner , David Malcolm , Lewis Hyatt Subject: [PATCH] libcpp: Stabilize the location for macros restored after PCH load [PR105608] Organization: Free thinker, does not speak for AdaCore Date: Tue, 20 Feb 2024 01:37:47 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, WEIRD_QUOTING 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+patchwork=sourceware.org@gcc.gnu.org This backport for gcc-13 is the second of two required for the g++.dg/pch/line-map-3.C test to stop hitting a variant of the known problem mentioned in that testcase: on riscv64-elf and riscv32-elf, after restoring the PCH, the location of the macros is mentioned as if they were on line 3 rather than 2, so even the existing xfails fail. I think this might be too much to backport, and I'm ready to use an xfail instead, but since this would bring more predictability, I thought I'd ask whether you'd find this backport acceptable. Regstrapped on x86_64-linux-gnu, along with other backports, and tested manually on riscv64-elf. Ok to install? From: Lewis Hyatt libcpp currently lacks the infrastructure to assign correct locations to macros that were defined prior to loading a PCH and then restored afterwards. While I plan to address that fully for GCC 15, this patch improves things by using at least a valid location, even if it's not the best one. Without this change, libcpp uses pfile->directive_line as the location for the restored macros, but this location_t applies to the old line map, not the one that was just restored from the PCH, so the resulting location is unpredictable and depends on what was stored in the line maps before. With this change, all restored macros get assigned locations at the line of the #include that triggered the PCH restore. A future patch will store the actual file name and line number of each definition and then synthesize locations in the new line map pointing to the right place. gcc/c-family/ChangeLog: PR preprocessor/105608 * c-pch.cc (c_common_read_pch): Adjust line map so that libcpp assigns a location to restored macros which is the same location that triggered the PCH include. libcpp/ChangeLog: PR preprocessor/105608 * pch.cc (cpp_read_state): Set a valid location for restored macros. (cherry picked from commit 019dc63819befb2b82077fb2d76b5dd670946f36) --- gcc/c-family/c-pch.cc | 23 +++++++++++++++-------- libcpp/pch.cc | 9 ++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/gcc/c-family/c-pch.cc b/gcc/c-family/c-pch.cc index 9ee6f1790023c..d60972ba93084 100644 --- a/gcc/c-family/c-pch.cc +++ b/gcc/c-family/c-pch.cc @@ -318,6 +318,7 @@ c_common_read_pch (cpp_reader *pfile, const char *name, struct save_macro_data *smd; expanded_location saved_loc; bool saved_trace_includes; + int cpp_result; timevar_push (TV_PCH_RESTORE); @@ -343,20 +344,26 @@ c_common_read_pch (cpp_reader *pfile, const char *name, 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); + + /* Set the current location to the line containing the #include (or the + #pragma GCC pch_preprocess) for the purpose of assigning locations to any + macros that are about to be restored. */ + linemap_add (line_table, LC_ENTER, 0, saved_loc.file, + saved_loc.line > 1 ? saved_loc.line - 1 : saved_loc.line); timevar_push (TV_PCH_CPP_RESTORE); - if (cpp_read_state (pfile, name, f, smd) != 0) - { - fclose (f); - timevar_pop (TV_PCH_CPP_RESTORE); - goto end; - } - timevar_pop (TV_PCH_CPP_RESTORE); + cpp_result = cpp_read_state (pfile, name, f, smd); + /* Set the current location to the line following the #include, where we + were prior to processing the PCH. */ + linemap_line_start (line_table, saved_loc.line, 0); + timevar_pop (TV_PCH_CPP_RESTORE); fclose (f); + if (cpp_result != 0) + goto end; + /* Give the front end a chance to take action after a PCH file has been loaded. */ if (lang_post_pch_load) diff --git a/libcpp/pch.cc b/libcpp/pch.cc index a9f4ff19bf1e1..17e423f44b801 100644 --- a/libcpp/pch.cc +++ b/libcpp/pch.cc @@ -838,7 +838,14 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f, != NULL) { _cpp_clean_line (r); - if (!_cpp_create_definition (r, h, 0)) + + /* ??? Using r->line_table->highest_line is not ideal here, but we + do need to use some location that is relative to the new line + map just loaded, not the old one that was in effect when these + macros were lexed. The proper fix is to remember the file name + and line number where each macro was defined, and then add + these locations into the new line map. See PR105608. */ + if (!_cpp_create_definition (r, h, r->line_table->highest_line)) abort (); _cpp_pop_buffer (r); }