This warning is supposed to detect cases like void bar (char *d, unsigned n, const char *f, va_list va) { vsnprintf (d, n, f, va); }
where the enclosing function is a candidate for the "format" attribute. The code assumed that current_function_decl is never null -- a reasonable assumption since you can't have file-scope function calls in C, but as shown below, this assumption can be fooled using typeof. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-01-29 Marek Polacek <pola...@redhat.com> PR c/83966 * c-format.c (check_function_format): Check current_function_decl. * gcc.dg/format/Wsuggest-attribute-1.c: New test. diff --git gcc/c-family/c-format.c gcc/c-family/c-format.c index 7a69d5a295c..3f4f83af219 100644 --- gcc/c-family/c-format.c +++ gcc/c-family/c-format.c @@ -1110,7 +1110,9 @@ check_function_format (tree attrs, int nargs, tree *argarray, from the format attribute if the called function is decorated with it. Avoid using calls with string literal formats for guidance since those are unlikely to be viable candidates. */ - if (warn_suggest_attribute_format && info.first_arg_num == 0 + if (warn_suggest_attribute_format + && current_function_decl != NULL_TREE + && info.first_arg_num == 0 && (format_types[info.format_type].flags & (int) FMT_FLAG_ARG_CONVERT) /* c_strlen will fail for a function parameter but succeed diff --git gcc/testsuite/gcc.dg/format/Wsuggest-attribute-1.c gcc/testsuite/gcc.dg/format/Wsuggest-attribute-1.c index e69de29bb2d..5b6fb051e72 100644 --- gcc/testsuite/gcc.dg/format/Wsuggest-attribute-1.c +++ gcc/testsuite/gcc.dg/format/Wsuggest-attribute-1.c @@ -0,0 +1,9 @@ +/* PR c/83966 */ +/* { dg-do compile } */ +/* { dg-options "-Wsuggest-attribute=format" } */ + +#include "format.h" + +va_list va; +const char *f; +__typeof (vsnprintf ("foo", 0U, f, va)) T; Marek