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 <dmalcolm@redhat.com>
This commit is contained in:
David Malcolm
2026-02-27 23:35:44 -05:00
parent 9a100f0525
commit d88eb2e961
9 changed files with 75 additions and 58 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -3063,16 +3063,20 @@ ensure_sarif_logical_location_for (logical_locations::key k)
auto sarif_logical_loc = std::make_unique<sarif_logical_location> ();
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;
}

View File

@@ -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

View File

@@ -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

View File

@@ -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:
{

View File

@@ -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

View File

@@ -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

View File

@@ -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;