Mingw32 targets use ms_printf format for printf, but mingw-w64 when
configured for UCRT uses gnu_format (via stdio.h).  GCC then checks both
formats, which means that one cannot print a 64-bit integer without a warning.
All these lines issue a warning:

  printf("Hello %"PRIu64"\n", x); // 1
  printf("Hello %I64u\n", x);     // 2
  printf("Hello %llu\n", x);      // 3

because each of them violates one of the formats.  This causes trouble
particularly for systems that turn warnings into errors or otherwise require
no warnings (leading to the use of -Wno-format or of various printf
replacements).

Also, one gets a warning twice if the format string violates both formats.

These issues have been reported as PR 95130 and PR 92292:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95130
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92292

This patch fixes these issues following the suggestion of Joseph Myers, it
disables the built in format in case there are additional ones. It applies
to GCC 12, 11, 10 and fixes the example above as tested on cross
compilers built on Linux.  I've also verified that R built using a 10.3
native compiler with the patch applied builds and passes its tests.  I've
updated the patch based on advice and comments from Martin Liska and
Martin Storsjo.

Could this or a variant of please be accepted to 12/11/10?

Thanks
Tomas

gcc/c-family/ChangeLog:

        * c-common.c (check_function_arguments): Pass also function
          declaration to check_function_format.

        * c-common.h (check_function_format): Extra argument - function
          declaration.

        * c-format.c (check_function_format): For builtin functions with a
          built in format and at least one more, do not check the first one.

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 13341fa315e..be4d8400447 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -6057,7 +6057,7 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
   /* Check for errors in format strings.  */

   if (warn_format || warn_suggest_attribute_format)
-    check_function_format (fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray, +    check_function_format (fndecl, fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray,
                arglocs);

   if (warn_format)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 8b7bf35e888..ee370eafbbc 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -856,7 +856,7 @@ extern void check_function_arguments_recurse (void (*)
                           unsigned HOST_WIDE_INT);
 extern bool check_builtin_function_arguments (location_t, vec<location_t>,
                           tree, tree, int, tree *);
-extern void check_function_format (const_tree, tree, int, tree *,
+extern void check_function_format (const_tree, const_tree, tree, int, tree *,
                    vec<location_t> *);
 extern bool attribute_fallthrough_p (tree);
 extern tree handle_format_attribute (tree *, tree, tree, int, bool *);
diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index afa77810a5c..8155ee8c6f2 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -1160,12 +1160,13 @@ decode_format_type (const char *s, bool *is_raw /* = NULL */)
    attribute themselves.  */

 void
-check_function_format (const_tree fntype, tree attrs, int nargs,
+check_function_format (const_tree fndecl, const_tree fntype, tree attrs, int nargs,
                tree *argarray, vec<location_t> *arglocs)
 {
-  tree a;
+  tree a, aa;

   tree atname = get_identifier ("format");
+  int skipped_default_format = 0;

   /* See if this function has any format attributes.  */
   for (a = attrs; a; a = TREE_CHAIN (a))
@@ -1176,6 +1177,58 @@ check_function_format (const_tree fntype, tree attrs, int nargs,
       function_format_info info;
       decode_format_attr (fntype, atname, TREE_VALUE (a), &info,
                   /*validated=*/true);
+
+      /* Mingw32 targets have traditionally used ms_printf format for the
+         printf function, and this format is built in GCC. But nowadays,
+         if mingw-w64 is configured to target UCRT, the printf function
+         uses the gnu_printf format (specified in the stdio.h header). This
+         causes GCC to check both formats, which means that there is no way
+         to e.g. print a long long unsigned without a warning (ms_printf
+         warns for %llu and gnu_printf warns for %I64u). Also, GCC would warn
+         twice about the same issue when both formats are violated, e.g.
+         for %lu used to print long long unsigned.
+
+         Hence, if there are multiple format specifiers, we skip the first
+         one. See PR 95130, PR 92292.  */
+
+      if (!skipped_default_format && fndecl)
+        {
+          for(aa = TREE_CHAIN (a); aa; aa = TREE_CHAIN(aa))
+        {
+          if (is_attribute_p ("format", get_attribute_name (aa)) &&
+              fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
+            {
+              switch (DECL_FUNCTION_CODE (fndecl))
+            {
+            case BUILT_IN_FSCANF:
+            case BUILT_IN_PRINTF:
+            case BUILT_IN_SCANF:
+            case BUILT_IN_SNPRINTF:
+            case BUILT_IN_SSCANF:
+            case BUILT_IN_VFSCANF:
+            case BUILT_IN_VPRINTF:
+            case BUILT_IN_VSCANF:
+            case BUILT_IN_VSNPRINTF:
+            case BUILT_IN_VSSCANF:
+            case BUILT_IN_DCGETTEXT:
+            case BUILT_IN_DGETTEXT:
+            case BUILT_IN_GETTEXT:
+            case BUILT_IN_STRFMON:
+            case BUILT_IN_STRFTIME:
+            case BUILT_IN_SNPRINTF_CHK:
+            case BUILT_IN_VSNPRINTF_CHK:
+            case BUILT_IN_PRINTF_CHK:
+            case BUILT_IN_VPRINTF_CHK:
+              skipped_default_format = 1;
+              break;
+            default:
+              break;
+            }
+            }
+        }
+          if (skipped_default_format) continue;
+        }
+
       if (warn_format)
         {
           /* FIXME: Rewrite all the internal functions in this file

Reply via email to