From patchwork Sun Jul 7 02:13:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1957648 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=P9puaoPD; 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 4WGrZc0bthz1xpP for ; Sun, 7 Jul 2024 12:20:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 26BDD385DDEA for ; Sun, 7 Jul 2024 02:20:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) by sourceware.org (Postfix) with ESMTPS id DDE8A385DDE8 for ; Sun, 7 Jul 2024 02:13:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDE8A385DDE8 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 DDE8A385DDE8 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c2f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720318450; cv=none; b=XnfEwI3RjipzdCrwj66w10v9a2aYFeVQXqaUSY1jiWOaG1EwBVNTkTSwrguOrT3bbADMwm2TkY6UpQ07m3mSfXTuhv+/MRUlavfn6QklIoNqEXFWyQeIWJdchNvvArD9BLp3xCPPL6yX5UVpfSIqrAnNcib+cc/RWzNyhkeYwd0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720318450; c=relaxed/simple; bh=XKsA49c7qSgcgNEG4MUMPvE/K1/WV4dFqeim2y9Sqrk=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=w4NtfBEw5g8+6qoshc+XNVof220Ayr91ngFgQBuc1g4ri7qtenvNFtOHSR173KYD72tm4Av5RV/lDLUk8ttkW8ee4NsO5RcnYtYgt0YRZHSaiF8QOfuFNVOIUXHePuOi2PfHmkW5NwoYcPLX7Q0IHautTZ8fUuVzEWmtOpZjYI8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-5c66de0c24bso320559eaf.1 for ; Sat, 06 Jul 2024 19:13:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720318435; x=1720923235; 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=nNTcbj8Spnitu7zdasS8EnuTyc7JRMBsS9VWXOOs/Us=; b=P9puaoPDb00gPMGdZa+wwQej2qtBP1pmov2WAFyb9IB3N/Aa2kX+RGxd8VIwFVo015 Rhi47W1TEGIYncUHgn3WosmLSf+eAQv9D1jYgVc4K5WaqJidzxBP+9sgxVJ5aef/8HJY rmjtY2yee5gYoI0GRbsh5iXzQNpLTSwOb943VtdxZzkKHp4cvTwdb17PuMk4wjdNRokX dj9W9iBxlgyhm2F8xIYUHR1mkNPBwl7+jIdWLGZT6pr8c9knS23aFp85FsZQaBikHCzV YrLdL4QMbZWrSXm6472YBfUwRsl0z0+U4yKn9F2UV1ZclnXUlMEIfx13bK653GkCy++E B9nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720318435; x=1720923235; 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=nNTcbj8Spnitu7zdasS8EnuTyc7JRMBsS9VWXOOs/Us=; b=Kux/WoMDM3y4RODRag1NbgawOrZJfChAAb3LXk3f/tDErvXYourCpr1qnoRgAn28Kc nBitCqeStBfkV/IffBFXWF136UDfbaubaBgsk5dPUGZiwUqr9pYluHRMLc6DBEZYwsc6 ZRsD9I+WZ7U94do+dJt8K8/6iPFGHAakDLdQj8szOzknNTyEu7xbZX2XhlowJaSEHG6y HgOWxP05t7mw9ZrcwjwMQxCN/t2P7A5E5IHBcUAHnobTuvia2u4s0DvW9be5o7a8WcCX WFlbNdjjHQm6ev/3Ht7UiYIvTf6hFC4Wosk9wFZpWO3Nx2a/hz4afJUTZHUggk0gCHCd J+Rw== X-Gm-Message-State: AOJu0Yyp3R7dOKQvfGs2pV7YNNz5hBws201F6AS6fIQrUch5FQ/bY+nH XLO8f3xgCbMjOrtqfClIInxYnjPbW7QMBdLKSkX6Sb2JNI4QpLCaflV8lA== X-Google-Smtp-Source: AGHT+IHU0iNOMZINPCJxJAZHpaSrrmRTmdLkSGF2+1Gg3YrI+faEu2yPE+kgS0RfmJ9tsbt59vomBg== X-Received: by 2002:a05:6870:a54e:b0:254:c95f:cdb6 with SMTP id 586e51a60fabf-25e2bf8cb62mr6698092fac.52.1720318434939; Sat, 06 Jul 2024 19:13:54 -0700 (PDT) Received: from Thaum. (125-209-140-42.tpgi.com.au. [125.209.140.42]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70b1e0dbf2dsm1829512b3a.67.2024.07.06.19.13.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Jul 2024 19:13:54 -0700 (PDT) Message-ID: <6689f9e2.050a0220.e5708.3f2d@mx.google.com> X-Google-Original-Message-ID: Date: Sun, 7 Jul 2024 12:13:50 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH 3/3] c++: Add locations to using_p OVERLOADs MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.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 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? I have also been working on a patch that uses the locations of using-decls in 'diagnose_name_conflict' and 'duplicate_decls' calls, but that will need a fair bit more work that I'll probaby put off for the meantime. Otherwise, as far as I can tell there's no circumstance when modules-imported namespace-scope USING_DECLs have their locations printed (hence the lack of tests), so I'm happy to defer this patch until I've made something I can actually test. An alternative approach here (rather than giving locations to OVERLOADs) would be to rewrite the modules handling to not using OVERLOADs internally for non-function usings, that way we can attach the location to those USING_DECLs. I went this way because I felt having locations in OVL_USING_P overloads would be useful anyway, but happy to try that route instead if you're uncomfortable increasing the size of most OVERLOADs for something largely unused. -- >8 -- This is used by module streaming to track locations of USING_DECLs (that are internally wrapped and unwrapped as OVERLOADs for consistency with function usings). No testcases with this patch as there aren't any easy ways to actually cause a diagnostic that uses this information yet; that'll come in a later patch. gcc/cp/ChangeLog: * cp-tree.h (OVL_SOURCE_LOCATION): New. (struct tree_overload): Add loc field. (class ovl_iterator): New member function. * module.cc (depset::hash::add_binding_entity): Add parameter. Track locations of usings. (depset::hash::find_dependencies): Note locations of usings. (module_state::write_cluster): Write locations of usings. (module_state::read_cluster): Read locations of usings. * name-lookup.cc (ovl_iterator::source_location): New. (walk_module_binding): Add parameter to callback. Provide locations of usings. * name-lookup.h (walk_module_binding): Add parameter to callback. * ptree.cc (cxx_print_xnode): Write using location. * tree.cc (ovl_insert): Initialise source location for usings. (lookup_maybe_add): Propagate source location for usings. Signed-off-by: Nathaniel Shead --- gcc/cp/cp-tree.h | 6 ++++++ gcc/cp/module.cc | 29 +++++++++++++++++++---------- gcc/cp/name-lookup.cc | 32 ++++++++++++++++++++++++++------ gcc/cp/name-lookup.h | 3 ++- gcc/cp/ptree.cc | 6 ++++++ gcc/cp/tree.cc | 2 ++ 6 files changed, 61 insertions(+), 17 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 8732b7dc71b..cc7c1947f9e 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -824,6 +824,10 @@ typedef struct ptrmem_cst * ptrmem_cst_t; /* The name of the overload set. */ #define OVL_NAME(NODE) DECL_NAME (OVL_FIRST (NODE)) +/* The source location of this OVL_USING_P overload. */ +#define OVL_SOURCE_LOCATION(NODE) \ + (((struct tree_overload*)OVERLOAD_CHECK (NODE))->loc) + /* Whether this is a set of overloaded functions. TEMPLATE_DECLS are always wrapped in an OVERLOAD, so we don't need to check them here. */ @@ -838,6 +842,7 @@ typedef struct ptrmem_cst * ptrmem_cst_t; struct GTY(()) tree_overload { struct tree_common common; tree function; + location_t loc; }; /* Iterator for a 1 dimensional overload. Permits iterating over the @@ -895,6 +900,7 @@ class ovl_iterator { } bool purview_p () const; bool exporting_p () const; + location_t source_location () const; public: tree remove_node (tree head) diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index f5966fc8c1c..5bb3e824acb 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -2581,7 +2581,7 @@ public: void add_namespace_context (depset *, tree ns); private: - static bool add_binding_entity (tree, WMB_Flags, void *); + static bool add_binding_entity (tree, WMB_Flags, void *, location_t); public: bool add_namespace_entities (tree ns, bitmap partitions); @@ -13122,7 +13122,8 @@ struct add_binding_data /* Return true if we are, or contain something that is exported. */ bool -depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_) +depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_, + location_t loc) { auto data = static_cast (data_); decl = strip_using_decl (decl); @@ -13185,7 +13186,6 @@ depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_) { if (!(flags & WMB_Hidden)) d->clear_hidden_binding (); - OVL_PURVIEW_P (d->get_entity ()) = true; if (flags & WMB_Export) OVL_EXPORT_P (d->get_entity ()) = true; return bool (flags & WMB_Export); @@ -13229,6 +13229,7 @@ depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_) OVL_PURVIEW_P (decl) = true; if (flags & WMB_Export) OVL_EXPORT_P (decl) = true; + OVL_SOURCE_LOCATION (decl) = loc; } depset *dep = data->hash->make_dependency @@ -13616,7 +13617,10 @@ depset::hash::find_dependencies (module_state *module) dump.indent (); walker.begin (); if (current->get_entity_kind () == EK_USING) - walker.tree_node (OVL_FUNCTION (decl)); + { + module->note_location (OVL_SOURCE_LOCATION (decl)); + walker.tree_node (OVL_FUNCTION (decl)); + } else if (TREE_VISITED (decl)) /* A global tree. */; else if (item->get_entity_kind () == EK_NAMESPACE) @@ -15114,6 +15118,7 @@ module_state::write_cluster (elf_out *to, depset *scc[], unsigned size, depset *dep = b->deps[jx]; tree bound = dep->get_entity (); unsigned flags = 0; + location_t loc = UNKNOWN_LOCATION; if (dep->get_entity_kind () == depset::EK_USING) { tree ovl = bound; @@ -15126,6 +15131,7 @@ module_state::write_cluster (elf_out *to, depset *scc[], unsigned size, flags |= cbf_using; if (OVL_EXPORT_P (ovl)) flags |= cbf_export; + loc = OVL_SOURCE_LOCATION (ovl); } else { @@ -15140,6 +15146,8 @@ module_state::write_cluster (elf_out *to, depset *scc[], unsigned size, gcc_checking_assert (DECL_P (bound)); sec.i (flags); + if (flags & cbf_using) + write_location (sec, loc); sec.tree_node (bound); } @@ -15281,6 +15289,10 @@ module_state::read_cluster (unsigned snum) && (flags & (cbf_using | cbf_export))) sec.set_overrun (); + location_t loc = UNKNOWN_LOCATION; + if (flags & cbf_using) + loc = read_location (sec); + tree decl = sec.tree_node (); if (sec.get_overrun ()) break; @@ -15291,8 +15303,7 @@ module_state::read_cluster (unsigned snum) if (type || !DECL_IMPLICIT_TYPEDEF_P (decl)) sec.set_overrun (); - type = build_lang_decl_loc (UNKNOWN_LOCATION, - USING_DECL, + type = build_lang_decl_loc (loc, USING_DECL, DECL_NAME (decl), NULL_TREE); USING_DECL_DECLS (type) = decl; @@ -15313,10 +15324,7 @@ module_state::read_cluster (unsigned snum) if (decls) sec.set_overrun (); - /* FIXME: Propagate the location of the using-decl - for use in diagnostics. */ - decls = build_lang_decl_loc (UNKNOWN_LOCATION, - USING_DECL, + decls = build_lang_decl_loc (loc, USING_DECL, DECL_NAME (decl), NULL_TREE); USING_DECL_DECLS (decls) = decl; @@ -15339,6 +15347,7 @@ module_state::read_cluster (unsigned snum) OVL_PURVIEW_P (decls) = true; if (flags & cbf_export) OVL_EXPORT_P (decls) = true; + OVL_SOURCE_LOCATION (decls) = loc; } if (flags & cbf_hidden) diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 96d7f938162..52c07e46d4f 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -4229,6 +4229,17 @@ ovl_iterator::exporting_p () const return OVL_EXPORT_P (ovl); } +/* The declared source location of this using. */ + +location_t +ovl_iterator::source_location () const +{ + gcc_checking_assert (using_p ()); + if (TREE_CODE (ovl) == USING_DECL) + return DECL_SOURCE_LOCATION (ovl); + return OVL_SOURCE_LOCATION (ovl); +} + /* Given a namespace-level binding BINDING, walk it, calling CALLBACK for all decls of the current module. When partitions are involved, decls might be mentioned more than once. Return the accumulation of @@ -4236,7 +4247,8 @@ ovl_iterator::exporting_p () const unsigned walk_module_binding (tree binding, bitmap partitions, - bool (*callback) (tree decl, WMB_Flags, void *data), + bool (*callback) (tree decl, WMB_Flags, void *data, + location_t loc), void *data) { tree current = binding; @@ -4249,6 +4261,7 @@ walk_module_binding (tree binding, bitmap partitions, if (tree type = MAYBE_STAT_TYPE (current)) { WMB_Flags flags = WMB_None; + location_t loc = UNKNOWN_LOCATION; if (STAT_TYPE_HIDDEN_P (current)) flags = WMB_Flags (flags | WMB_Hidden); if (TREE_CODE (type) == USING_DECL) @@ -4258,8 +4271,9 @@ walk_module_binding (tree binding, bitmap partitions, flags = WMB_Flags (flags | WMB_Purview); if (DECL_MODULE_EXPORT_P (type)) flags = WMB_Flags (flags | WMB_Export); + loc = DECL_SOURCE_LOCATION (type); } - count += callback (type, flags, data); + count += callback (type, flags, data, loc); decl_hidden = STAT_DECL_HIDDEN_P (current); } @@ -4270,6 +4284,7 @@ walk_module_binding (tree binding, bitmap partitions, if (!(decl_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter))) { WMB_Flags flags = WMB_None; + location_t loc = UNKNOWN_LOCATION; if (decl_hidden) flags = WMB_Flags (flags | WMB_Hidden); if (iter.using_p ()) @@ -4279,8 +4294,9 @@ walk_module_binding (tree binding, bitmap partitions, flags = WMB_Flags (flags | WMB_Purview); if (iter.exporting_p ()) flags = WMB_Flags (flags | WMB_Export); + loc = iter.source_location (); } - count += callback (*iter, flags, data); + count += callback (*iter, flags, data, loc); } decl_hidden = false; } @@ -4319,13 +4335,14 @@ walk_module_binding (tree binding, bitmap partitions, WMB_Flags flags = WMB_None; if (maybe_dups) flags = WMB_Flags (flags | WMB_Dups); - count += callback (bind, flags, data); + count += callback (bind, flags, data, UNKNOWN_LOCATION); } else if (STAT_HACK_P (bind) && MODULE_BINDING_PARTITION_P (bind)) { if (tree btype = STAT_TYPE (bind)) { WMB_Flags flags = WMB_None; + location_t loc = UNKNOWN_LOCATION; if (maybe_dups) flags = WMB_Flags (flags | WMB_Dups); if (STAT_TYPE_HIDDEN_P (bind)) @@ -4337,8 +4354,9 @@ walk_module_binding (tree binding, bitmap partitions, flags = WMB_Flags (flags | WMB_Purview); if (DECL_MODULE_EXPORT_P (btype)) flags = WMB_Flags (flags | WMB_Export); + loc = DECL_SOURCE_LOCATION (btype); } - count += callback (btype, flags, data); + count += callback (btype, flags, data, loc); } bool part_hidden = STAT_DECL_HIDDEN_P (bind); for (ovl_iterator iter (MAYBE_STAT_DECL (STAT_DECL (bind))); @@ -4350,6 +4368,7 @@ walk_module_binding (tree binding, bitmap partitions, (!(part_hidden && DECL_IS_UNDECLARED_BUILTIN (*iter))); WMB_Flags flags = WMB_None; + location_t loc = UNKNOWN_LOCATION; if (maybe_dups) flags = WMB_Flags (flags | WMB_Dups); if (part_hidden) @@ -4361,8 +4380,9 @@ walk_module_binding (tree binding, bitmap partitions, flags = WMB_Flags (flags | WMB_Purview); if (iter.exporting_p ()) flags = WMB_Flags (flags | WMB_Export); + loc = iter.source_location (); } - count += callback (*iter, flags, data); + count += callback (*iter, flags, data, loc); part_hidden = false; } } diff --git a/gcc/cp/name-lookup.h b/gcc/cp/name-lookup.h index 5cf6ae6374a..8183cd50159 100644 --- a/gcc/cp/name-lookup.h +++ b/gcc/cp/name-lookup.h @@ -499,7 +499,8 @@ enum WMB_Flags }; extern unsigned walk_module_binding (tree binding, bitmap partitions, - bool (*)(tree decl, WMB_Flags, void *data), + bool (*)(tree decl, WMB_Flags, void *data, + location_t loc), void *data); extern tree add_imported_namespace (tree ctx, tree name, location_t, unsigned module, diff --git a/gcc/cp/ptree.cc b/gcc/cp/ptree.cc index 15e46752d01..4f7fc96cc74 100644 --- a/gcc/cp/ptree.cc +++ b/gcc/cp/ptree.cc @@ -299,6 +299,12 @@ cxx_print_xnode (FILE *file, tree node, int indent) print_node (file, "optype", BASELINK_OPTYPE (node), indent + 4); break; case OVERLOAD: + if (location_t loc = OVL_SOURCE_LOCATION (node)) + { + expanded_location xloc = expand_location (loc); + indent_to (file, indent + 4); + fprintf (file, "%s:%d:%d", xloc.file, xloc.line, xloc.column); + } print_node (file, "function", OVL_FUNCTION (node), indent + 4); print_node (file, "next", OVL_CHAIN (node), indent + 4); break; diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index dfd4a3a948b..21d8e0de329 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -2387,6 +2387,7 @@ ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden) OVL_PURVIEW_P (maybe_ovl) = true; if (using_or_hidden > 2) OVL_EXPORT_P (maybe_ovl) = true; + OVL_SOURCE_LOCATION (maybe_ovl) = input_location; } } else @@ -2532,6 +2533,7 @@ lookup_maybe_add (tree fns, tree lookup, bool deduping) { lookup = ovl_make (OVL_FUNCTION (fns), lookup); OVL_USING_P (lookup) = true; + OVL_SOURCE_LOCATION (lookup) = OVL_SOURCE_LOCATION (fns); } else lookup = lookup_add (OVL_FUNCTION (fns), lookup);