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

            Bug ID: 77336
           Summary: -Wsuggest-attribute=format warning overly simplistic
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

In my work on the -Wformat-length pass (PR 49905) after introducing a call to
the diagnostic_set_info() function decorated with ATTRIBUTE_GCC_DIAG(2,0) I
started seeing -Wsuggest-attribute=format warnings similar to those below:

/src/gcc/49905/gcc/gimple-ssa-sprintf.c: In function ‘void
{anonymous}::format_directive(const
{anonymous}::pass_sprintf_length::call_info&, {anonymous}::format_result*,
const char*, size_t, const {anonymous}::conversion_spec&, tree)’:
/src/gcc/49905/gcc/gimple-ssa-sprintf.c:1836:15: warning: function might be
possible candidate for ‘gcc_tdiag’ format attribute
[-Wsuggest-attribute=format]
         navail);
               ^

Since the warning isn't appropriate in my case I looked into the logic that
triggers it to see how it could be suppressed.  The following is an example
that demonstrates a case where the warning is unhelpful and where it shouldn't
be issued.  The check_function_format() function in c-format.c that's
responsible for issuing the warning simply looks for any char* argument to the
current function and if one exists and the current function calls a function
with attribute format, it issues a -Wsuggest-attribute=format warning
regardless of whether or not the format string is related to the function
argument.  It seems to me that the warning should not be issued if the format
is literal as in the case below since a function that hardcodes the format
string is unlikely to be a candidate for the attribute.

$ cat t.c && /build/gcc-49905/gcc/xgcc -B /build/gcc-49905/gcc -S -Wformat
-Wformat-signedness -Wsuggest-attribute=format t.c
#include <stdio.h>

void f (const char *fname, __builtin_va_list va)
{
  FILE *f = fopen (fname, "w");
  vfprintf (f, "%i", va);
  fclose (f);
}
t.c: In function ‘f’:
t.c:6:3: warning: function might be possible candidate for ‘gnu_printf’ format
attribute [-Wsuggest-attribute=format]
   vfprintf (f, "%i", va);
   ^~~~~~~~

Reply via email to