From patchwork Fri Sep 27 05:58: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: 98036 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 E060A385841D for ; Fri, 27 Sep 2024 06:00:36 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by sourceware.org (Postfix) with ESMTPS id B27F43858D28 for ; Fri, 27 Sep 2024 05:58:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B27F43858D28 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 B27F43858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1030 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727416737; cv=none; b=g1oV86skLgiR1Yox9xaQ5Sp+OouoIp2NHIiglErNzAfSJVW7kSzSh3Y2dvGRLG/mJsWRzDt007JZsteYWTG8RDFl//kaCZZti8sRZzuXaQ6XDEMRqm/WicDO+QMxw2VJkVd/i5c4V/UsZL3Gjz/GH0NHBsBEXY4QgBsryH2lP7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727416737; c=relaxed/simple; bh=f6bx7ZXdYPgbOxMdFcwUQ/XDyrBbV2wEwHHFJFs42L0=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=PbVw0jkS6qTCB/RN/0ZUhjeNqFNJ/mpWXVr4gNZSckGbm+notASMHmxzAfJBkMCtaL07mGWhqS0COPDiaM4JqwSA9BbwKVW0KZBGqc8+k+3TDdQKFC7+ADhdzlnqx/JYuszN3HyrU1TWrbz5z5lCVqKhakGJYnbowPS33+afHQE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-2da516e6940so354793a91.3 for ; Thu, 26 Sep 2024 22:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727416733; x=1728021533; 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=NDYzWxxQmAC8vfJhDWj5ZMqUvBLM4QxCrBzOL2LMRYE=; b=KgenHs45LN72pAao1yiQRLZKSz5jTruDXbS2M5YYDPcF8qXHmuyczL+BhzVGHPp2BT GB7HjNh07MMjrv0U+MvILim5u7ILXiqes9zaT8i7rTBxLcNnA7IelUNG5nNld2w07vmN i0M61no8BWeKsO7OphbyCzGTuKoN27Plq1IpoAbAU1waUilw1SxPCm72fSz/QIVrAVSs 1HVG9ldMP2rk6TcacN8JsjPzav4payvNeOmMZqDtW/XYzO5GQ02eKh+jtkHofHFKP+Bm Ikvepvll9LQBdkqGCGjCX6+E9Aa4dqw1UFoEPOi52PQwm11+wmuSUQct9WwqWYW43Izu iJEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727416733; x=1728021533; 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=NDYzWxxQmAC8vfJhDWj5ZMqUvBLM4QxCrBzOL2LMRYE=; b=jURwjXLY9cbiwUIYwihg+HbTyOzFn3z9LxMmTlFgMeBHCiYZt5WRHqcs0PeRYad1Tm BpR49tDubc+g1HhxldaP6LKeFP7nxTVf133lCSpGLxZSmyQNh20V24g3RoU53l1ccGIH EdRZ/7A3sytio/K6pFhErtLgzzJXpY5RFp0NFTi7YwBPxVBuguqkAFDLgSM7qd7/QWbr bgj8QeJ8Zbjy96Ye+Ew1v98AJD3LfV4E0lV8iggElSdhU8bqU9bG9Dn6LEcJEGs+uoEw l93IFbKZyBVNQuLq0h4vdPqv9JUJ2n7zK4VKksAHhbJBk93an0IpsRZbN7qGKwAwwbnA Lyrw== X-Gm-Message-State: AOJu0YyN5mXdYoc2tsdn1cA8H+HCsApU4j4YIJr2HzYvKCWWDwrVYwmQ vAT80GPVyLkHBGw/AbEDn3In1Vn830hPyZwMP4E7HcbaUI9lON318Y5yqA== X-Google-Smtp-Source: AGHT+IFpP8gOJ3oAnh1EiSepM03cNWrVuUBlmY8vO1O7qX0OwKSuSPZC7vIP+h00NB5UACMZuCGCYQ== X-Received: by 2002:a17:90a:d802:b0:2da:71f8:7ff with SMTP id 98e67ed59e1d1-2e0b8e841bfmr1073362a91.5.1727416732550; Thu, 26 Sep 2024 22:58:52 -0700 (PDT) Received: from Thaum. (163-47-68-2.ipv4.originbroadband.com.au. [163.47.68.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e0b6c74071sm1104240a91.18.2024.09.26.22.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Sep 2024 22:58:52 -0700 (PDT) Message-ID: <66f6499c.170a0220.1abf25.5cf3@mx.google.com> X-Google-Original-Message-ID: Date: Fri, 27 Sep 2024 15:58:48 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH v2 3/6] c++/modules: Support anonymous namespaces in header units References: <66f64939.050a0220.261230.4b66@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <66f64939.050a0220.261230.4b66@mx.google.com> X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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~patchwork=sourceware.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu and aarch64-unknown-linux-gnu, OK for trunk? -- >8 -- A header unit may contain anonymous 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 anonymous 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 anonymous 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 anonymous 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-8_a.H: New test. * g++.dg/modules/internal-8_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 7 +++-- gcc/cp/name-lookup.cc | 8 +++--- gcc/testsuite/g++.dg/modules/internal-8_a.H | 28 ++++++++++++++++++++ gcc/testsuite/g++.dg/modules/internal-8_b.C | 29 +++++++++++++++++++++ 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/internal-8_a.H create mode 100644 gcc/testsuite/g++.dg/modules/internal-8_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 5d7f4941b2a..df407c1fd55 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 eb365b259d9..fe2eb2e0917 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -9098,6 +9098,9 @@ 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); + + 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, @@ -9234,11 +9237,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-8_a.H b/gcc/testsuite/g++.dg/modules/internal-8_a.H new file mode 100644 index 00000000000..57fe60bb3c0 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-8_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-8_b.C b/gcc/testsuite/g++.dg/modules/internal-8_b.C new file mode 100644 index 00000000000..a2d74a87473 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/internal-8_b.C @@ -0,0 +1,29 @@ +// { dg-additional-options "-fmodules-ts" } + +import "internal-8_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; +}