]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: use label_text for logical_location strings
authorDavid Malcolm <dmalcolm@redhat.com>
Sat, 28 Feb 2026 04:35:44 +0000 (23:35 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Sat, 28 Feb 2026 04:35:44 +0000 (23:35 -0500)
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>
gcc/diagnostics/html-sink.cc
gcc/diagnostics/logical-locations.h
gcc/diagnostics/sarif-sink.cc
gcc/diagnostics/selftest-logical-locations.cc
gcc/diagnostics/selftest-logical-locations.h
gcc/diagnostics/state-graphs-to-dot.cc
gcc/libgdiagnostics.cc
gcc/tree-logical-location.cc
gcc/tree-logical-location.h

index f2d5917e2ed923807a594fa9189bb676ca335d1a..58442e9a62bcfd32ec2daff166c0db374f3d8f02 100644 (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;
          }
 
index 847843bc800d74c1358256a9f550b17d3d47c2d0..b91f72825251ed09bf496ded736a9ca5d39215c4 100644 (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;
index 2cd64d65112e9afd485b1b03f8098cbec8248118..1e25b459c645f500c3a59e821414487c19043a78 100644 (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;
 }
index f2176e331331bff9f53373cc5ef8535360b02cf0..3a0ee7f20ddf1fe071c54dc6cd71521b74eb5058 100644 (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
index bc01894df82b9a300f21d9ae8640349ff1edfcf1..2d00871fc2124f844282b1eab62997b7ae567618 100644 (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
index fe5a07fd02dcfa8acb9b16eb53f71b81c9c96e1f..a0ccec71ed29a5ea95ad5499dc98ba641846af40 100644 (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:
        {
index 98e15bfec11e7c417a3317992526451a92d5d794..eb2d0ac4cac1ab00c939164bdbb97d9b2d619198 100644 (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
index 71294875c99c12f9e1601d3ac5ca2b8bea930cd2..b741b4e8af36117664fad2d1eae8a79566a76fe0 100644 (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
index 037496010505f9817e0bbd850fc2985d63ca5c5d..7a37480bee026702f644be129a300d7eeb465e8f 100644 (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;