From patchwork Fri Oct 11 13:31:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1996133 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=gHv26Ihw; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XQ6xl64fwz1xtv for ; Sat, 12 Oct 2024 00:32:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13F993857358 for ; Fri, 11 Oct 2024 13:32:42 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 810D9385AC1F for ; Fri, 11 Oct 2024 13:31:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 810D9385AC1F 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 810D9385AC1F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728653483; cv=none; b=jkh2y+F/V1XS81tAKrTllLBxEbDlO/TDUcG5AVr+k/QMRlv33RpxYc/BuL6FK/Wgz/zoKD+FDCPvJqB/M6WG9EDLhGPtZAsEQgnJ5RVXE2zdm4J/HyOFq6NfqXv76inlBzOohuykbd3ObSIOq/O7LvfMD/9tGT+QZd8St2cuMs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728653483; c=relaxed/simple; bh=NaeDkqYFod3iuQOpotYgrNghGVg7pNbWGmgh9b2mnuE=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=wnu2nep+Fy2DhaoMPEifBblGuZgT7qusdGHWCgfnLRczAJS/8SsjobiIfx28ivlUGXio+7Mt1W96ZpLIAGGvKRIjEIIPePROlY38e09O7mmga1tKoLgw3uWljjpCcmjHmx+rS9HciBq/p6C5gl8Ezd/EnyC/MrWaKwnR3YEwZ3o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-20c804409b0so805485ad.2 for ; Fri, 11 Oct 2024 06:31:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728653479; x=1729258279; darn=gcc.gnu.org; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=l4af7uBo+8Ddh8NeCTGyJb25AdiNBiJDI1IGZFwHN5w=; b=gHv26Ihwb/Ff9KNTW24wi7Akm5AXxlWz1COiqfjJ8VyUdUu9J5q8EbqnNaRBmYApBM QlqkYIe/ZlnuoJex1yUsZgPmQoBazEv0RpDvkE2zi/pNZ1d3bYe6jO2fwB+NUIfCPLnb 6v2gaMKlQFjuQ5Gaqt2g0gEvl6xUECMxnP2IwKmZXtACHC08KaiM+bDAQTqO6CAoD7nH 5W5cSH6oQMT13LJT4+ZQ7zr6z0QzJHuj+G0etD19UXiXO34yWNqKDOJ52ZVbtZZQ3AOk Ca1h1LC7IW/btL74zzP9Afoi/oJyruoUvsYJ57aCdzoluAk1DiY25xlPtFsoHiYITGKU LYeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728653479; x=1729258279; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=l4af7uBo+8Ddh8NeCTGyJb25AdiNBiJDI1IGZFwHN5w=; b=rspeyrskvOw8lrgEB6jZlcj8nwAo9AC2D9poEWNXkqZ57vhvnaAyb5SZySRPhrnDoA l+AofD/z9ZYNmF9Wsx8WnpIXDDr1xgdqNqcxemA4FvC7RJ2qnt3QB8D/ps7TawrqIPYJ 2q6IBAMatVLavuIOX9lbUzYQX2ZmpedEhYO8p8JKmuL4XCVhk+swDyWcMr56TumrIxCH bxaHjuR0ki0rY6k3xHB3LYSZ5BkaFgBsWgx32oZpRW1Q71/SCtUDGLePTPwnNTbIB3uN evSgPVZIpbxpkQinRHiFTG14BWkq2L1AMN3Xg2xs66zr8lJRPwsDIsSAZ/2aMT3afz3/ Uh9g== X-Gm-Message-State: AOJu0YwwxuBCbAwUxWlIDgG/kooV/Z8QQHvfoId81vHaeNV1weuI2hwI MD7Lwxbli5752E5PCI2tlJ108tjJcl8myx1AXEAuXkWn5og8TMzl/HsbU0wD X-Google-Smtp-Source: AGHT+IGnNY4b1UiV+5GQxQdcdi0P2OKLj4cRiSWJ/FXiDba2PjTzx6X1mggiCkdC7OC2YC8aayA0tg== X-Received: by 2002:a17:902:d48f:b0:20c:5bf8:bd70 with SMTP id d9443c01a7336-20ca1478a7amr16050535ad.6.1728653479277; Fri, 11 Oct 2024 06:31:19 -0700 (PDT) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20c8bc136c5sm23471895ad.83.2024.10.11.06.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Oct 2024 06:31:18 -0700 (PDT) Message-ID: <670928a6.170a0220.2beec7.848e@mx.google.com> X-Google-Original-Message-ID: Date: Sat, 12 Oct 2024 00:31:14 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH v3 3/5] c++/modules: Support unnamed namespaces in header units References: <6709282a.a70a0220.1dd501.9a14@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <6709282a.a70a0220.1dd501.9a14@mx.google.com> X-Spam-Status: No, score=-12.0 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 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 Added the rationale comment for the 'make_namespace_finish' changes, changed commit message to mention 'unnamed' rather than 'anonymous' namespaces. -- >8 -- A header unit may contain unnamed namespaces, and those declarations are exported (as with any declaration in a header unit). This patch ensures that such declarations are correctly handled. The change to 'make_namespace_finish' is required so that if an unnamed namespace is first seen by an import it is correctly handled within 'add_imported_namespace'. I don't see any particular reason why handling of unnamed namespaces here had to be handled separately outside that function since these are the only two callers. gcc/cp/ChangeLog: * module.cc (depset::hash::add_binding_entity): Also walk unnamed namespaces. (module_state::write_namespaces): Adjust assertion. * name-lookup.cc (push_namespace): Move anon using-directive handling to... (make_namespace_finish): ...here. gcc/testsuite/ChangeLog: * g++.dg/modules/internal-9_a.H: New test. * g++.dg/modules/internal-9_b.C: New test. Signed-off-by: Nathaniel Shead Reviewed-by: Jason Merrill --- gcc/cp/module.cc | 7 +++-- gcc/cp/name-lookup.cc | 10 +++---- gcc/testsuite/g++.dg/modules/internal-9_a.H | 28 ++++++++++++++++++++ gcc/testsuite/g++.dg/modules/internal-9_b.C | 29 +++++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/internal-9_a.H create mode 100644 gcc/testsuite/g++.dg/modules/internal-9_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 4b26dc5d367..f75e211e0e1 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -13723,15 +13723,15 @@ depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_) return (flags & WMB_Using ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl)); } - else if (DECL_NAME (decl) && !data->met_namespace) + else if (!data->met_namespace) { /* Namespace, walk exactly once. */ - gcc_checking_assert (TREE_PUBLIC (decl)); data->met_namespace = true; if (data->hash->add_namespace_entities (decl, data->partitions)) { /* It contains an exported thing, so it is exported. */ gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl)); + gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ()); DECL_MODULE_EXPORT_P (decl) = true; } @@ -16126,8 +16126,7 @@ module_state::write_namespaces (elf_out *to, vec spaces, tree ns = b->get_entity (); gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL); - /* P1815 may have something to say about this. */ - gcc_checking_assert (TREE_PUBLIC (ns)); + gcc_checking_assert (TREE_PUBLIC (ns) || header_module_p ()); unsigned flags = 0; if (TREE_PUBLIC (ns)) diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 609bd6e8c9b..22391110fd7 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -9102,6 +9102,11 @@ make_namespace_finish (tree ns, tree *slot, bool from_import = false) if (DECL_NAMESPACE_INLINE_P (ns) || !DECL_NAME (ns)) emit_debug_info_using_namespace (ctx, ns, true); + + /* An unnamed namespace implicitly has a using-directive inserted so + that its contents are usable in the surrounding context. */ + if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns)) + add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns); } /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, @@ -9238,11 +9243,6 @@ push_namespace (tree name, bool make_inline) gcc_checking_assert (slot); } make_namespace_finish (ns, slot); - - /* Add the anon using-directive here, we don't do it in - make_namespace_finish. */ - if (!DECL_NAMESPACE_INLINE_P (ns) && !name) - add_using_namespace (current_binding_level->using_directives, ns); } } diff --git a/gcc/testsuite/g++.dg/modules/internal-9_a.H b/gcc/testsuite/g++.dg/modules/internal-9_a.H new file mode 100644 index 00000000000..57fe60bb3c0 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-9_a.H @@ -0,0 +1,28 @@ +// { dg-additional-options "-fmodule-header" } +// { dg-module-cmi {} } + +static int x = 123; +static void f() {} +template static void t() {} + +namespace { + int y = 456; + void g() {}; + template void u() {} + + namespace ns { int in_ns = 456; } + + struct A {}; + template struct B {}; + + enum E { X }; + enum class F { Y }; + + template using U = int; + +#if __cplusplus >= 202002L + template concept C = true; +#endif +} + +namespace ns2 = ns; diff --git a/gcc/testsuite/g++.dg/modules/internal-9_b.C b/gcc/testsuite/g++.dg/modules/internal-9_b.C new file mode 100644 index 00000000000..5f8eeac2340 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-9_b.C @@ -0,0 +1,29 @@ +// { dg-additional-options "-fmodules-ts" } + +import "internal-9_a.H"; + +int main() { + auto x2 = x; + f(); + t(); + + auto y2 = y; + g(); + u(); + + int val1 = ns::in_ns; + + A a; + B b; + + E e = X; + F f = F::Y; + + U temp; + +#if __cplusplus >= 202002L + static_assert(C); +#endif + + int val2 = ns2::in_ns; +}