https://sourceware.org/bugzilla/show_bug.cgi?id=31863

            Bug ID: 31863
           Summary: Warnings about %z format when compiling ctf-open.c
                    with MinGW
           Product: binutils
           Version: 2.43 (HEAD)
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libctf
          Assignee: unassigned at sourceware dot org
          Reporter: eliz at gnu dot org
  Target Milestone: ---

When compiling ctf-open.c with mingw.org's MinGW tools (as part of building the
current pretest 15.0.91 of GDB), the compiler emits warnings about the use of
%z format specifier, like this:

       CC       libctf_la-ctf-open.lo
     ctf-open.c: In function 'init_static_types':
     ctf-open.c:979:18: warning: unknown conversion type character 'z' in
format [-Wformat=]
       979 |   ctf_dprintf ("%zu enum names hashed\n",
           |                  ^
     ctf-open.c:979:16: warning: too many arguments for format
[-Wformat-extra-args]
       979 |   ctf_dprintf ("%zu enum names hashed\n",
           |                ^~~~~~~~~~~~~~~~~~~~~~~~~

The reason is that ctf-impl.h defines the _libctf_printflike attribute like
this:

#define _libctf_printflike_(string_index,first_to_check) \
    __attribute__ ((__format__ (__printf__, (string_index), (first_to_check))))

and then uses this attribute to declare the type of ctf_dprintf function:

_libctf_printflike_ (1, 2)
void ctf_dprintf (const char *format, ...)
{

The GCC manual says about the __format__ function attribute:

  'format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)'
       The 'format' attribute specifies that a function takes 'printf',
       'scanf', 'strftime' or 'strfmon' style arguments that should be
       type-checked against a format string.
       [...]
       The parameter ARCHETYPE determines how the format string is
       interpreted, and should be 'printf', 'scanf', 'strftime',
       'gnu_printf', 'gnu_scanf', 'gnu_strftime' or 'strfmon'.  (You can
       also use '__printf__', '__scanf__', '__strftime__' or
       '__strfmon__'.)  On MinGW targets, 'ms_printf', 'ms_scanf', and
       'ms_strftime' are also present.  ARCHETYPE values such as 'printf'
       refer to the formats accepted by the system's C runtime library,
       while values prefixed with 'gnu_' always refer to the formats
       accepted by the GNU C Library.

Note the last sentence above: it means that using __printf__ invokes the
platform's default formats, and those don't include %z on MS-Windows.  The
solution is to use __gnu_printf__ instead:

--- libctf/ctf-impl.h.~1~       2024-05-26 18:55:53.000000000 +0300
+++ libctf/ctf-impl.h   2024-06-07 14:41:48.357697100 +0300
@@ -68,7 +68,7 @@ extern "C"
    __attribute_blah__.  */

 #define _libctf_printflike_(string_index,first_to_check) \
-    __attribute__ ((__format__ (__printf__, (string_index),
(first_to_check))))
+    __attribute__ ((__format__ (__gnu_printf__, (string_index),
(first_to_check))))
 #define _libctf_unlikely_(x) __builtin_expect ((x), 0)
 #define _libctf_unused_ __attribute__ ((__unused__))
 #define _libctf_malloc_ __attribute__((__malloc__))

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to