]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree: add build_string_literal overloads
authorJason Merrill <jason@redhat.com>
Thu, 20 Oct 2022 17:51:37 +0000 (13:51 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 24 Oct 2022 20:28:03 +0000 (16:28 -0400)
Simplify several calls to build_string_literal by not requiring redundant
strlen or IDENTIFIER_* in the caller.

I also corrected a wrong comment on IDENTIFIER_LENGTH.

gcc/ChangeLog:

* tree.h (build_string_literal): New one-argument overloads that
take tree (identifier) and const char *.
* builtins.cc (fold_builtin_FILE)
(fold_builtin_FUNCTION)
* gimplify.cc (gimple_add_init_for_auto_var)
* vtable-verify.cc (verify_bb_vtables): Simplify calls.

gcc/cp/ChangeLog:

* cp-gimplify.cc (fold_builtin_source_location)
* vtable-class-hierarchy.cc (register_all_pairs): Simplify calls to
build_string_literal.
(build_string_from_id): Remove.

gcc/builtins.cc
gcc/cp/cp-gimplify.cc
gcc/cp/vtable-class-hierarchy.cc
gcc/gimplify.cc
gcc/tree.h
gcc/vtable-verify.cc

index 5f319b2803027f64e9fb699a969958cee35013cd..26898d7e27ea4798631d7cd1cb25a77d28595f79 100644 (file)
@@ -9521,10 +9521,10 @@ fold_builtin_FILE (location_t loc)
         __FILE__ macro so it appears appropriate to use the same file prefix
         mappings.  */
       fname = remap_macro_filename (fname);
-    return build_string_literal (strlen (fname) + 1, fname);
+      return build_string_literal (fname);
     }
 
-  return build_string_literal (1, "");
+  return build_string_literal ("");
 }
 
 /* Fold a call to __builtin_FUNCTION to a constant string.  */
@@ -9537,7 +9537,7 @@ fold_builtin_FUNCTION ()
   if (current_function_decl)
     name = lang_hooks.decl_printable_name (current_function_decl, 0);
 
-  return build_string_literal (strlen (name) + 1, name);
+  return build_string_literal (name);
 }
 
 /* Fold a call to __builtin_LINE to an integer constant.  */
index 28c339869b83d1d401d661351e18e694c1de8f4d..cc8bfada5afc533054a29f1290106d2043b612d9 100644 (file)
@@ -3378,10 +3378,10 @@ fold_builtin_source_location (location_t loc)
              if (const char *fname = LOCATION_FILE (loc))
                {
                  fname = remap_macro_filename (fname);
-                 val = build_string_literal (strlen (fname) + 1, fname);
+                 val = build_string_literal (fname);
                }
              else
-               val = build_string_literal (1, "");
+               val = build_string_literal ("");
            }
          else if (strcmp (n, "_M_function_name") == 0)
            {
@@ -3390,7 +3390,7 @@ fold_builtin_source_location (location_t loc)
              if (current_function_decl)
                name = cxx_printable_name (current_function_decl, 2);
 
-             val = build_string_literal (strlen (name) + 1, name);
+             val = build_string_literal (name);
            }
          else if (strcmp (n, "_M_line") == 0)
            val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc));
index cc1df1ebdb23f00f6caacf55b03fb36ebd830c33..1e180ea61dca06a0e753ca22261906450961c917 100644 (file)
@@ -467,19 +467,6 @@ check_and_record_registered_pairs (tree vtable_decl, tree vptr_address,
   return !inserted_something;
 }
 
-/* Given an IDENTIFIER_NODE, build and return a string literal based on it.  */
-
-static tree
-build_string_from_id (tree identifier)
-{
-  int len;
-
-  gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
-
-  len = IDENTIFIER_LENGTH (identifier);
-  return build_string_literal (len + 1, IDENTIFIER_POINTER (identifier));
-}
-
 /* A class may contain secondary vtables in it, for various reasons.
    This function goes through the decl chain of a class record looking
    for any fields that point to secondary vtables, and adding calls to
@@ -920,7 +907,7 @@ register_all_pairs (tree body)
 
 
       if (flag_vtv_debug)
-        str1 = build_string_from_id (DECL_NAME (base_ptr_var_decl));
+       str1 = build_string_literal (DECL_NAME (base_ptr_var_decl));
 
       new_type = build_pointer_type (TREE_TYPE (base_ptr_var_decl));
       arg1 = build1 (ADDR_EXPR, new_type, base_ptr_var_decl);
@@ -953,7 +940,7 @@ register_all_pairs (tree body)
                 if (vtable_decl)
                   {
                     vtable_should_be_output = TREE_ASM_WRITTEN (vtable_decl);
-                    str2 = build_string_from_id (DECL_NAME (vtable_decl));
+                   str2 = build_string_literal (DECL_NAME (vtable_decl));
                   }
 
                 if (vtable_decl && vtable_should_be_output)
@@ -1009,8 +996,7 @@ register_all_pairs (tree body)
       arg2 = build_key_buffer_arg (base_ptr_var_decl);
 
       if (str2 == NULL_TREE)
-        str2 = build_string_literal (strlen ("unknown") + 1,
-                                     "unknown");
+       str2 = build_string_literal ("unknown");
 
       if (flag_vtv_debug)
         output_set_info (current->class_info->class_type,
index 42a996dfeb911002692a983715ea0f6d268452bb..69bad340d2edea470647654a74d48d1aa730d694 100644 (file)
@@ -1771,14 +1771,12 @@ gimple_add_init_for_auto_var (tree decl,
   tree decl_name = NULL_TREE;
   if (DECL_NAME (decl))
 
-    decl_name = build_string_literal (IDENTIFIER_LENGTH (DECL_NAME (decl)) + 1,
-                                     IDENTIFIER_POINTER (DECL_NAME (decl)));
+    decl_name = build_string_literal (DECL_NAME (decl));
 
   else
     {
       char *decl_name_anonymous = xasprintf ("D.%u", DECL_UID (decl));
-      decl_name = build_string_literal (strlen (decl_name_anonymous) + 1,
-                                       decl_name_anonymous);
+      decl_name = build_string_literal (decl_name_anonymous);
       free (decl_name_anonymous);
     }
 
index 9af971cf401f40a80eef524ee9d5675f5bb6654d..a50f7b2be9d024a3a0915d1515c6481cddd51968 100644 (file)
@@ -1135,7 +1135,7 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int,
 
 /* Define fields and accessors for some special-purpose tree nodes.  */
 
-/* As with STRING_CST, in C terms this is sizeof, not strlen.  */
+/* Unlike STRING_CST, in C terms this is strlen, not sizeof.  */
 #define IDENTIFIER_LENGTH(NODE) \
   (IDENTIFIER_NODE_CHECK (NODE)->identifier.id.len)
 #define IDENTIFIER_POINTER(NODE) \
@@ -4706,6 +4706,13 @@ extern tree build_alloca_call_expr (tree, unsigned int, HOST_WIDE_INT);
 extern tree build_string_literal (unsigned, const char * = NULL,
                                  tree = char_type_node,
                                  unsigned HOST_WIDE_INT = HOST_WIDE_INT_M1U);
+inline tree build_string_literal (const char *p)
+{ return build_string_literal (strlen (p) + 1, p); }
+inline tree build_string_literal (tree t)
+{
+  return build_string_literal (IDENTIFIER_LENGTH (t) + 1,
+                              IDENTIFIER_POINTER (t));
+}
 
 /* Construct various nodes representing data types.  */
 
index 24894e7f10825c8e6a029b4f4f672da0f90ec68b..f01058e541218339dd420d60e1b2530626bb10bb 100644 (file)
@@ -725,10 +725,6 @@ verify_bb_vtables (basic_block bb)
                      trace information to debug problems.  */
                   if (flag_vtv_debug)
                     {
-                      int len1 = IDENTIFIER_LENGTH
-                                                 (DECL_NAME (vtbl_var_decl));
-                      int len2 = strlen (vtable_name);
-
                       call_stmt = gimple_build_call
                                      (verify_vtbl_ptr_fndecl, 4,
                                       build1 (ADDR_EXPR,
@@ -737,12 +733,8 @@ verify_bb_vtables (basic_block bb)
                                               vtbl_var_decl),
                                       lhs,
                                       build_string_literal
-                                                  (len1 + 1,
-                                                   IDENTIFIER_POINTER
-                                                       (DECL_NAME
-                                                            (vtbl_var_decl))),
-                                      build_string_literal (len2 + 1,
-                                                            vtable_name));
+                                                 (DECL_NAME (vtbl_var_decl)),
+                                     build_string_literal (vtable_name));
                     }
                   else
                     call_stmt = gimple_build_call