From patchwork Fri Mar 1 01:08:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1906501 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=V9DzmNzR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tm93q6Vmjz1yXC for ; Fri, 1 Mar 2024 12:09:41 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 994A43858C74 for ; Fri, 1 Mar 2024 01:09:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 512723858C74 for ; Fri, 1 Mar 2024 01:08:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 512723858C74 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 512723858C74 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::531 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709255337; cv=none; b=HM6laR9aWAEDCDcB2Kbvh0DKgYj8Z6mfFCq19x4xDCeQCp/YLR7g7FbW7SpVQNHin4ElscmRsvq7X3HwpQcJ5Slsal8wbVq7Lbn+X/DTjwm+D2hZqoDDBUtrHqTRK2oT/bhejKTbILxfJK0T0fxPK5miaIUodKaXgViGacQ+1KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709255337; c=relaxed/simple; bh=jj73KO6fUUNwYWI4KqCBt1BwV7ENnrek3a+iveaZ1A0=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=J/IzAvEFJ3d8unRxxjzBEUCMVZi20BqoMfIwL1+vQLhZLCf56fim4Le2LXCXuvER1Pu0neWI1ebptw1e3Tn3Bq531CNSH5IAHnQTM6G2FZk9i/bNthjqarUkg7CXHPSPdlCMW6RdS9QuK5a6+yIcrDUYiIL+MkOTIcvGxz1pOgk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so2127855a12.1 for ; Thu, 29 Feb 2024 17:08:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709255334; x=1709860134; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=I5LEOOUTzjX9OJ0JIj6GrP48+nNcEnQJqRwiqBx5gkQ=; b=V9DzmNzRAd1bmVMet7ZbDA54QMDNkI7qpFaKcnd4pc3EFud3vKNjdRHwbaJZGUFD3P /QtJGxS8wriBgiNyZhPpbmO6clxVur9jpMb0xZGKbzufItpAWv4OaDFMovBqPCTCAEYb CyrzMicieKXWVO8nKcLB1uS6NhmmVsPAMsHQc0gNqETQx70o8DSFKE3AQuExwcs+Bykc SSnQC3ip5a+sn01JJNNEfQfWa+8/s5iKzoJtulXReWbpbkFFoCUHwScLbiBA2VFgzIkR YA06ysylQ9Z2z/gUZ73+N4lau8N3tcT3RFHDmDptJPwBWyNWDUVtFyAWEJe5Fv5qIvpL 3qPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709255334; x=1709860134; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=I5LEOOUTzjX9OJ0JIj6GrP48+nNcEnQJqRwiqBx5gkQ=; b=c6tcV+nwAkpBcg+Ayi5IlG84dB2pPLHmwhzLlVNilzUugxmkasx3QH8HHuk4HniF56 Xvj43GmS19tRJvv1dcymfHGn0Mqh3pNWfs1I97+dXnDLlocKKerTJwkoDkFKyY+rsaMh b4wZKU+7g82c9mk5EIyksjuzJ/74iw8c+dOcLh6too5CiUIyg11lIvfjlG9lknbFWIs4 Fb94tlUimSwRcP7XgQKvTfpOzA7QaWXgnH2eufmXPNWUDT5U3AoY3RBmCuyFTNF/v2wE hraYmuadyg/dU+VZjj+iSq3DQpvqbWf4mI99mXSqRw9reXQ+6engoNsmWz2qQPo+paEU 6x0A== X-Gm-Message-State: AOJu0YyCC4Y3iNPNUJCWkPtUgglNR6SmpTrh3hTrH1B3uDMdFlI+yQqI kIqkczH2+3Ae4YUEaDLdfGKceJKYQ59uTlzVUNsL4XPGv4xjLfCCbgmJKarp X-Google-Smtp-Source: AGHT+IERSnFjTgj+jhXC6QT5IsD1yxj4GDUQHSMop5rSqNndsN9hjqNCUyZngP0Mfneb5LrJK7oMKw== X-Received: by 2002:a17:90a:d497:b0:299:1cce:f3c3 with SMTP id s23-20020a17090ad49700b002991ccef3c3mr323227pju.7.1709255334105; Thu, 29 Feb 2024 17:08:54 -0800 (PST) Received: from Thaum. (110-175-172-107.tpgi.com.au. [110.175.172.107]) by smtp.gmail.com with ESMTPSA id d15-20020a63fd0f000000b005dc87643cc3sm1939279pgh.27.2024.02.29.17.08.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 17:08:53 -0800 (PST) Message-ID: <65e12aa5.630a0220.6b431.a00b@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 1 Mar 2024 12:08:48 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Stream definitions for implicit instantiations [PR114170] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? -- >8 -- An implicit instantiation has an initializer depending on whether DECL_INITIALIZED_P is set (like normal VAR_DECLs) which needs to be written to ensure that consumers of header modules properly emit definitions for these instantiations. This patch ensures that we correctly fallback to checking this flag when DECL_INITIAL is not set for a template instantiation. As a drive-by fix, also ensures that the count of initializers matches the actual number of initializers written. This doesn't seem to be necessary for correctness in the current testsuite, but feels wrong and makes debugging harder when initializers aren't properly written for other reasons. PR c++/114170 gcc/cp/ChangeLog: * module.cc (has_definition): Fall back to DECL_INITIALIZED_P when DECL_INITIAL is not set on a template. (module_state::write_inits): Only increment count when initializers are actually written. gcc/testsuite/ChangeLog: * g++.dg/modules/var-tpl-2_a.H: New test. * g++.dg/modules/var-tpl-2_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 8 +++++--- gcc/testsuite/g++.dg/modules/var-tpl-2_a.H | 10 ++++++++++ gcc/testsuite/g++.dg/modules/var-tpl-2_b.C | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/var-tpl-2_a.H create mode 100644 gcc/testsuite/g++.dg/modules/var-tpl-2_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 1b2ba2e0fa8..09578de41ec 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -11586,8 +11586,9 @@ has_definition (tree decl) case VAR_DECL: if (DECL_LANG_SPECIFIC (decl) - && DECL_TEMPLATE_INFO (decl)) - return DECL_INITIAL (decl); + && DECL_TEMPLATE_INFO (decl) + && DECL_INITIAL (decl)) + return true; else { if (!DECL_INITIALIZED_P (decl)) @@ -17528,13 +17529,14 @@ module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr) tree list = static_aggregates; for (int passes = 0; passes != 2; passes++) { - for (tree init = list; init; init = TREE_CHAIN (init), count++) + for (tree init = list; init; init = TREE_CHAIN (init)) if (TREE_LANG_FLAG_0 (init)) { tree decl = TREE_VALUE (init); dump ("Initializer:%u for %N", count, decl); sec.tree_node (decl); + ++count; } list = tls_aggregates; diff --git a/gcc/testsuite/g++.dg/modules/var-tpl-2_a.H b/gcc/testsuite/g++.dg/modules/var-tpl-2_a.H new file mode 100644 index 00000000000..607fc0b808e --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/var-tpl-2_a.H @@ -0,0 +1,10 @@ +// PR c++/114170 +// { dg-additional-options "-fmodule-header" } +// { dg-module-cmi {} } + +inline int f() { return 42; } + +template +inline int v = f(); + +inline int g() { return v; } diff --git a/gcc/testsuite/g++.dg/modules/var-tpl-2_b.C b/gcc/testsuite/g++.dg/modules/var-tpl-2_b.C new file mode 100644 index 00000000000..6d2ef4004e6 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/var-tpl-2_b.C @@ -0,0 +1,10 @@ +// PR c++/114170 +// { dg-module-do run } +// { dg-additional-options "-fmodules-ts" } + +import "var-tpl-2_a.H"; + +int main() { + if (v != 42) + __builtin_abort(); +}