diagnostics: Use an inline function rather than hardcoding <built-in> string
authorLewis Hyatt <lhyatt@gmail.com>
Tue, 1 Nov 2022 22:43:10 +0000 (18:43 -0400)
committerLewis Hyatt <lhyatt@gmail.com>
Tue, 15 Nov 2022 15:50:23 +0000 (10:50 -0500)
The string "<built-in>" is hard-coded in several places throughout the
diagnostics code, and in some of those places, it is used incorrectly with
respect to internationalization. (Comparing a translated string to an
untranslated string.) The error is not currently observable in any output GCC
actually produces, hence no testcase added here, but it's worth fixing, and
also, I am shortly going to add a new such string and want to avoid hardcoding
that one in similar places.

gcc/c-family/ChangeLog:

* c-opts.cc (c_finish_options): Use special_fname_builtin () rather
than a hard-coded string.

gcc/ChangeLog:

* diagnostic.cc (diagnostic_get_location_text): Use
special_fname_builtin () rather than a hardcoded string (which was
also incorrectly left untranslated previously.)
* input.cc (special_fname_builtin): New function.
(expand_location_1): Use special_fname_builtin () rather than a
hard-coded string.
(test_builtins): Likewise.
* input.h (special_fname_builtin): Declare.

gcc/fortran/ChangeLog:

* cpp.cc (gfc_cpp_init): Use special_fname_builtin () rather than a
hardcoded string (which was also incorrectly left untranslated
previously.)
* error.cc (gfc_diagnostic_build_locus_prefix): Likewise.
* f95-lang.cc (gfc_init): Likewise.

gcc/c-family/c-opts.cc
gcc/diagnostic.cc
gcc/fortran/cpp.cc
gcc/fortran/error.cc
gcc/fortran/f95-lang.cc
gcc/input.cc
gcc/input.h

index 9e0494b..bc1f85e 100644 (file)
@@ -1473,7 +1473,7 @@ c_finish_options (void)
     {
       const line_map_ordinary *bltin_map
        = linemap_check_ordinary (linemap_add (line_table, LC_RENAME, 0,
-                                              _("<built-in>"), 0));
+                                              special_fname_builtin (), 0));
       cb_file_change (parse_in, bltin_map);
       linemap_line_start (line_table, 0, 1);
 
index 22f7b0b..7c7ee6d 100644 (file)
@@ -470,7 +470,7 @@ diagnostic_get_location_text (diagnostic_context *context,
   const char *file = s.file ? s.file : progname;
   int line = 0;
   int col = -1;
-  if (strcmp (file, N_("<built-in>")))
+  if (strcmp (file, special_fname_builtin ()))
     {
       line = s.line;
       if (context->show_column)
index 364bd0d..0b5755e 100644 (file)
@@ -605,7 +605,7 @@ gfc_cpp_init (void)
   if (gfc_option.flag_preprocessed)
     return;
 
-  cpp_change_file (cpp_in, LC_RENAME, _("<built-in>"));
+  cpp_change_file (cpp_in, LC_RENAME, special_fname_builtin ());
   if (!gfc_cpp_option.no_predefined)
     {
       /* Make sure all of the builtins about to be declared have
index c9d6edb..214fb78 100644 (file)
@@ -1147,7 +1147,7 @@ gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
   const char *locus_ce = colorize_stop (pp_show_color (pp));
   return (s.file == NULL
          ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
-         : !strcmp (s.file, N_("<built-in>"))
+         : !strcmp (s.file, special_fname_builtin ())
          ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
          : context->show_column
          ? build_message_string ("%s%s:%d:%d:%s", locus_cs, s.file, s.line,
@@ -1167,7 +1167,7 @@ gfc_diagnostic_build_locus_prefix (diagnostic_context *context,
 
   return (s.file == NULL
          ? build_message_string ("%s%s:%s", locus_cs, progname, locus_ce )
-         : !strcmp (s.file, N_("<built-in>"))
+         : !strcmp (s.file, special_fname_builtin ())
          ? build_message_string ("%s%s:%s", locus_cs, s.file, locus_ce)
          : context->show_column
          ? build_message_string ("%s%s:%d:%d-%d:%s", locus_cs, s.file, s.line,
index a6750be..0d83f3f 100644 (file)
@@ -259,7 +259,7 @@ gfc_init (void)
   if (!gfc_cpp_enabled ())
     {
       linemap_add (line_table, LC_ENTER, false, gfc_source_file, 1);
-      linemap_add (line_table, LC_RENAME, false, "<built-in>", 0);
+      linemap_add (line_table, LC_RENAME, false, special_fname_builtin (), 0);
     }
   else
     gfc_cpp_init_0 ();
index c185bd7..18777a8 100644 (file)
@@ -29,6 +29,12 @@ along with GCC; see the file COPYING3.  If not see
 #define HAVE_ICONV 0
 #endif
 
+const char *
+special_fname_builtin ()
+{
+  return _("<built-in>");
+}
+
 /* Input charset configuration.  */
 static const char *default_charset_callback (const char *)
 {
@@ -275,7 +281,7 @@ expand_location_1 (location_t loc,
 
   xloc.data = block;
   if (loc <= BUILTINS_LOCATION)
-    xloc.file = loc == UNKNOWN_LOCATION ? NULL : _("<built-in>");
+    xloc.file = loc == UNKNOWN_LOCATION ? NULL : special_fname_builtin ();
 
   return xloc;
 }
@@ -2194,7 +2200,7 @@ test_unknown_location ()
 static void
 test_builtins ()
 {
-  assert_loceq (_("<built-in>"), 0, 0, BUILTINS_LOCATION);
+  assert_loceq (special_fname_builtin (), 0, 0, BUILTINS_LOCATION);
   ASSERT_PRED1 (is_location_from_builtin_token, BUILTINS_LOCATION);
 }
 
index f187699..2173a39 100644 (file)
@@ -32,6 +32,9 @@ extern GTY(()) class line_maps *saved_line_table;
 /* The location for declarations in "<built-in>" */
 #define BUILTINS_LOCATION ((location_t) 1)
 
+/* Returns the translated string referring to the special location.  */
+const char *special_fname_builtin ();
+
 /* line-map.cc reserves RESERVED_LOCATION_COUNT to the user.  Ensure
    both UNKNOWN_LOCATION and BUILTINS_LOCATION fit into that.  */
 STATIC_ASSERT (BUILTINS_LOCATION < RESERVED_LOCATION_COUNT);