From d88eb2e961e5eebcffed3b9f6646219ea6d4a8ca Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 27 Feb 2026 23:35:44 -0500 Subject: [PATCH] diagnostics: use label_text for logical_location strings Doing so makes it possible for logical_locations::manager subclasses to return copies of temporary buffers, rather than requiring the buffer to outlive the call. This is useful for generating JSON pointer strings, for logical locations within JSON files. gcc/ChangeLog: * diagnostics/html-sink.cc (html_builder::make_element_for_diagnostic): Update for logical location strings being returned as label_text rather than const char *. * diagnostics/logical-locations.h (logical_locations::manager::get_short_name): Return label_text rather than const char *. (logical_locations::manager::get_name_with_scope): Likewise. (logical_locations::manager::get_internal_name): Likewise. * diagnostics/sarif-sink.cc (sarif_builder::ensure_sarif_logical_location_for): Update for logical location strings being returned as label_text rather than const char *. (sarif_builder::make_minimal_sarif_logical_location): Likewise. * diagnostics/selftest-logical-locations.cc (test_manager::get_short_name): Likewise. (test_manager::get_name_with_scope): Likewise. (test_manager::get_internal_name): Likewise. (selftest_logical_locations_cc_tests): Likewise. * diagnostics/selftest-logical-locations.h (test_manager::get_short_name): Likewise. (test_manager::get_name_with_scope): Likewise. (test_manager::get_internal_name): Likewise. * diagnostics/state-graphs-to-dot.cc (state_diagram::on_node_in_table): Likewise. * libgdiagnostics.cc (impl_logical_location_manager::get_short_name): Likewise. (impl_logical_location_manager::get_name_with_scope): Likewise. (impl_logical_location_manager::get_internal_name): Likewise. * tree-logical-location.cc (tree_logical_location_manager::get_short_name): Likewise. (tree_logical_location_manager::get_name_with_scope): Likewise. (tree_logical_location_manager::get_internal_name): Likewise. * tree-logical-location.h (tree_logical_location_manager::get_short_name): Likewise. (tree_logical_location_manager::get_name_with_scope): Likewise. (tree_logical_location_manager::get_internal_name): Likewise. Signed-off-by: David Malcolm --- gcc/diagnostics/html-sink.cc | 11 +++++--- gcc/diagnostics/logical-locations.h | 6 ++--- gcc/diagnostics/sarif-sink.cc | 24 ++++++++++------- gcc/diagnostics/selftest-logical-locations.cc | 18 ++++++------- gcc/diagnostics/selftest-logical-locations.h | 6 ++--- gcc/diagnostics/state-graphs-to-dot.cc | 15 ++++++----- gcc/libgdiagnostics.cc | 21 ++++++++------- gcc/tree-logical-location.cc | 26 ++++++++++--------- gcc/tree-logical-location.h | 6 ++--- 9 files changed, 75 insertions(+), 58 deletions(-) diff --git a/gcc/diagnostics/html-sink.cc b/gcc/diagnostics/html-sink.cc index f2d5917e2ed..58442e9a62b 100644 --- a/gcc/diagnostics/html-sink.cc +++ b/gcc/diagnostics/html-sink.cc @@ -1093,10 +1093,13 @@ html_builder::make_element_for_diagnostic (const diagnostic_info &diagnostic, enum logical_locations::kind kind = logical_loc_mgr->get_kind (logical_loc);; if (const char *label = get_label_for_logical_location_kind (kind)) - if (const char *name_with_scope - = logical_loc_mgr->get_name_with_scope (logical_loc)) - add_labelled_value (xp, "logical-location", - label, name_with_scope, true); + { + label_text name_with_scope + = logical_loc_mgr->get_name_with_scope (logical_loc); + if (name_with_scope.get ()) + add_labelled_value (xp, "logical-location", + label, name_with_scope.get (), true); + } m_last_logical_location = logical_loc; } diff --git a/gcc/diagnostics/logical-locations.h b/gcc/diagnostics/logical-locations.h index 847843bc800..b91f7282525 100644 --- a/gcc/diagnostics/logical-locations.h +++ b/gcc/diagnostics/logical-locations.h @@ -155,15 +155,15 @@ public: /* Get a string (or NULL) for K suitable for use by the SARIF logicalLocation "name" property (SARIF v2.1.0 section 3.33.4). */ - virtual const char *get_short_name (key k) const = 0; + virtual label_text get_short_name (key k) const = 0; /* Get a string (or NULL) for K suitable for use by the SARIF logicalLocation "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */ - virtual const char *get_name_with_scope (key k) const = 0; + virtual label_text get_name_with_scope (key k) const = 0; /* Get a string (or NULL) for K suitable for use by the SARIF logicalLocation "decoratedName" property (SARIF v2.1.0 section 3.33.6). */ - virtual const char *get_internal_name (key k) const = 0; + virtual label_text get_internal_name (key k) const = 0; /* Get what kind of SARIF logicalLocation K is (if any). */ virtual enum kind get_kind (key k) const = 0; diff --git a/gcc/diagnostics/sarif-sink.cc b/gcc/diagnostics/sarif-sink.cc index 2cd64d65112..1e25b459c64 100644 --- a/gcc/diagnostics/sarif-sink.cc +++ b/gcc/diagnostics/sarif-sink.cc @@ -3063,16 +3063,20 @@ ensure_sarif_logical_location_for (logical_locations::key k) auto sarif_logical_loc = std::make_unique (); - if (const char *short_name = logical_loc_mgr->get_short_name (k)) - sarif_logical_loc->set_string ("name", short_name); + label_text short_name = logical_loc_mgr->get_short_name (k); + if (short_name.get ()) + sarif_logical_loc->set_string ("name", short_name.get ()); /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */ - if (const char *name_with_scope = logical_loc_mgr->get_name_with_scope (k)) - sarif_logical_loc->set_string ("fullyQualifiedName", name_with_scope); + label_text name_with_scope = logical_loc_mgr->get_name_with_scope (k); + if (name_with_scope.get ()) + sarif_logical_loc->set_string ("fullyQualifiedName", + name_with_scope.get ()); /* "decoratedName" property (SARIF v2.1.0 section 3.33.6). */ - if (const char *internal_name = logical_loc_mgr->get_internal_name (k)) - sarif_logical_loc->set_string ("decoratedName", internal_name); + label_text internal_name = logical_loc_mgr->get_internal_name (k); + if (internal_name.get ()) + sarif_logical_loc->set_string ("decoratedName", internal_name.get ()); /* "kind" property (SARIF v2.1.0 section 3.33.7). */ enum logical_locations::kind kind = logical_loc_mgr->get_kind (k); @@ -3118,9 +3122,11 @@ make_minimal_sarif_logical_location (logical_locations::key logical_loc) sarif_logical_loc->set_integer ("index", index); /* "fullyQualifiedName" property (SARIF v2.1.0 section 3.33.5). */ - if (const char *name_with_scope - = logical_loc_mgr->get_name_with_scope (logical_loc)) - sarif_logical_loc->set_string ("fullyQualifiedName", name_with_scope); + label_text name_with_scope + = logical_loc_mgr->get_name_with_scope (logical_loc); + if (name_with_scope.get ()) + sarif_logical_loc->set_string ("fullyQualifiedName", + name_with_scope.get ()); return sarif_logical_loc; } diff --git a/gcc/diagnostics/selftest-logical-locations.cc b/gcc/diagnostics/selftest-logical-locations.cc index f2176e33133..3a0ee7f20dd 100644 --- a/gcc/diagnostics/selftest-logical-locations.cc +++ b/gcc/diagnostics/selftest-logical-locations.cc @@ -45,27 +45,27 @@ test_manager::dump (FILE *outfile, int indent) const dumping::emit_heading (outfile, indent, "test_manager"); } -const char * +label_text test_manager::get_short_name (key k) const { auto item = item_from_key (k); if (!item) - return nullptr; - return item->m_name; + return label_text (); + return label_text::borrow (item->m_name); } -const char * +label_text test_manager::get_name_with_scope (key k) const { auto item = item_from_key (k); - return item->m_name; + return label_text::borrow (item->m_name); } -const char * +label_text test_manager::get_internal_name (key k) const { auto item = item_from_key (k); - return item->m_name; + return label_text::borrow (item->m_name); } enum diagnostics::logical_locations::kind @@ -118,8 +118,8 @@ selftest_logical_locations_cc_tests () ASSERT_NE (loc_foo, loc_bar); - ASSERT_STREQ (mgr.get_short_name (loc_foo), "foo"); - ASSERT_STREQ (mgr.get_short_name (loc_bar), "bar"); + ASSERT_STREQ (mgr.get_short_name (loc_foo).get (), "foo"); + ASSERT_STREQ (mgr.get_short_name (loc_bar).get (), "bar"); } } // namespace diagnostics::logical_locations::selftest diff --git a/gcc/diagnostics/selftest-logical-locations.h b/gcc/diagnostics/selftest-logical-locations.h index bc01894df82..2d00871fc21 100644 --- a/gcc/diagnostics/selftest-logical-locations.h +++ b/gcc/diagnostics/selftest-logical-locations.h @@ -41,9 +41,9 @@ public: void dump (FILE *out, int indent) const final override; - const char *get_short_name (key) const final override; - const char *get_name_with_scope (key) const final override; - const char *get_internal_name (key) const final override; + label_text get_short_name (key) const final override; + label_text get_name_with_scope (key) const final override; + label_text get_internal_name (key) const final override; kind get_kind (key) const final override; label_text get_name_for_path_output (key) const final override; key get_parent (key) const final override diff --git a/gcc/diagnostics/state-graphs-to-dot.cc b/gcc/diagnostics/state-graphs-to-dot.cc index fe5a07fd02d..a0ccec71ed2 100644 --- a/gcc/diagnostics/state-graphs-to-dot.cc +++ b/gcc/diagnostics/state-graphs-to-dot.cc @@ -324,12 +324,15 @@ private: break; case state_node_properties::kind_t::stack_frame: if (auto logical_loc = state_node.get_logical_loc ()) - if (const char *function - = m_logical_loc_mgr.get_short_name (logical_loc)) - add_title_tr (id_of_dot_node, xp, num_columns, state_node, - std::string ("Frame: ") + function, - style::h2, - state_node_properties::dynalloc_state_t::unknown); + { + label_text function + = m_logical_loc_mgr.get_short_name (logical_loc); + if (function.get ()) + add_title_tr (id_of_dot_node, xp, num_columns, state_node, + std::string ("Frame: ") + function.get (), + style::h2, + state_node_properties::dynalloc_state_t::unknown); + } break; case state_node_properties::kind_t::dynalloc_buffer: { diff --git a/gcc/libgdiagnostics.cc b/gcc/libgdiagnostics.cc index 98e15bfec11..eb2d0ac4cac 100644 --- a/gcc/libgdiagnostics.cc +++ b/gcc/libgdiagnostics.cc @@ -485,28 +485,31 @@ public: (outfile, indent, "impl_logical_location_manager"); } - const char *get_short_name (key k) const final override + label_text + get_short_name (key k) const final override { if (auto loc = ptr_from_key (k)) - return loc->m_short_name.get_str (); + return label_text::borrow (loc->m_short_name.get_str ()); else - return nullptr; + return label_text (); } - const char *get_name_with_scope (key k) const final override + label_text + get_name_with_scope (key k) const final override { if (auto loc = ptr_from_key (k)) - return loc->m_fully_qualified_name.get_str (); + return label_text::borrow (loc->m_fully_qualified_name.get_str ()); else - return nullptr; + return label_text (); } - const char *get_internal_name (key k) const final override + label_text + get_internal_name (key k) const final override { if (auto loc = ptr_from_key (k)) - return loc->m_decorated_name.get_str (); + return label_text::borrow (loc->m_decorated_name.get_str ()); else - return nullptr; + return label_text (); } kind get_kind (key k) const final override diff --git a/gcc/tree-logical-location.cc b/gcc/tree-logical-location.cc index 71294875c99..b741b4e8af3 100644 --- a/gcc/tree-logical-location.cc +++ b/gcc/tree-logical-location.cc @@ -48,33 +48,35 @@ tree_logical_location_manager::dump (FILE *outfile, int indent) const "tree_logical_location_manager"); } -const char * +label_text tree_logical_location_manager::get_short_name (key k) const { tree node = tree_from_key (k); assert_valid_tree (node); if (DECL_P (node)) - return identifier_to_locale (lang_hooks.decl_printable_name (node, 0)); + return label_text::borrow + (identifier_to_locale (lang_hooks.decl_printable_name (node, 0))); if (TYPE_P (node)) - return IDENTIFIER_POINTER (TYPE_IDENTIFIER (node)); - return nullptr; + return label_text::borrow (IDENTIFIER_POINTER (TYPE_IDENTIFIER (node))); + return label_text (); } -const char * +label_text tree_logical_location_manager::get_name_with_scope (key k) const { tree node = tree_from_key (k); assert_valid_tree (node); if (DECL_P (node)) - return identifier_to_locale (lang_hooks.decl_printable_name (node, 1)); + return label_text::borrow + (identifier_to_locale (lang_hooks.decl_printable_name (node, 1))); if (TYPE_P (node)) - return nullptr; - return nullptr; + return label_text (); + return label_text (); } -const char * +label_text tree_logical_location_manager::get_internal_name (key k) const { tree node = tree_from_key (k); @@ -85,11 +87,11 @@ tree_logical_location_manager::get_internal_name (key k) const if (HAS_DECL_ASSEMBLER_NAME_P (node) && TREE_CODE (node) != NAMESPACE_DECL) // FIXME if (tree id = DECL_ASSEMBLER_NAME (node)) - return IDENTIFIER_POINTER (id); + return label_text::borrow (IDENTIFIER_POINTER (id)); } else if (TYPE_P (node)) - return nullptr; - return NULL; + return label_text (); + return label_text (); } enum kind diff --git a/gcc/tree-logical-location.h b/gcc/tree-logical-location.h index 03749601050..7a37480bee0 100644 --- a/gcc/tree-logical-location.h +++ b/gcc/tree-logical-location.h @@ -36,9 +36,9 @@ public: void dump (FILE *out, int indent) const final override; - const char *get_short_name (key) const final override; - const char *get_name_with_scope (key) const final override; - const char *get_internal_name (key) const final override; + label_text get_short_name (key) const final override; + label_text get_name_with_scope (key) const final override; + label_text get_internal_name (key) const final override; kind get_kind (key) const final override; label_text get_name_for_path_output (key) const final override; key get_parent (key) const final override;