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

            Bug ID: 107802
           Summary: -Wsuggest-attribute=format ignores
                    [[gnu::format(...)]]
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adam.f.badura at gmail dot com
  Target Milestone: ---

I checked the issue with GCC 10.2 and trunk as on Compiler Explorer using
arguments

 -std=c++17 -O3 -Wall -Wextra -Wpedantic -Wsuggest-attribute=format

The following code (https://godbolt.org/z/qP91h5Knv):

 #include <cstdarg>
 #include <cstdio>
 #include <cstdlib>

 __attribute__((format(printf, 4, 5))) [[noreturn]] void raise(
     [[maybe_unused]] const char* const file,
     [[maybe_unused]] const unsigned line,
     [[maybe_unused]] const char* const condition,
     const char* assertionMessage,
     ...)
 {
     std::fprintf(stderr, "*** Assertion message: ");
     va_list args;
     va_start(args, assertionMessage);
     std::vfprintf(stderr, assertionMessage, args);
     va_end(args);
     std::fprintf(stderr, "\n");

     std::abort();
 }

passes fine.

However, if we replace

 __attribute__((format(printf, 4, 5)))

with

 [[gnu::format(printf, 4, 5)]]

a warning shows up (https://godbolt.org/z/oecjj4Tzv):

 <source>: In function 'void raise(const char*, unsigned int, const char*,
const char*, ...)':
 <source>:15:49: warning: function 'void raise(const char*, unsigned int, const
char*, const char*, ...)' might be a candidate for 
 'gnu_printf' format attribute [-Wsuggest-attribute=format]
    15 |     std::vfprintf(stderr, assertionMessage, args);
       |                                                 ^

My overall experience is that the [[gnu::...]] syntax for attributes doesn't
behave the same as the __attribute__((...)) syntax. Not only it seems to be
more limited to where we can place it (this is up to C++, not GCC) but also it
seems attributes provided this way are more likely to be more or less ignored.
However, this is the first time I caught the issue so clearly.

Reply via email to