On 10/26/2017 12:23 PM, Nathan Sidwell wrote:
On 10/26/2017 02:12 PM, Eric Gallager wrote:
On 10/26/17, Nathan Sidwell <nat...@acm.org> wrote:
On 10/26/2017 10:34 AM, David Malcolm wrote:
Possibly a silly question, but is it OK to have a formatted string
call in which some of the arguments aren't consumed? (here "col" is
only
consumed for the true case, which consumes 2 arguments; it's not
consumed
for the false case).
Yes.
I think I remember clang disagreeing; I remember it printing warnings
from -Wformat-extra-args in a similar situation in gnulib's
error_at_line module
C++ 21.10.1 defers to C. C-99 7.15.1 has no words saying va_arg must be
applied to exactly all arguments captured by va_list object. (and I'm
pretty sure scanf can bail early)
Now, it might be sensible to warn about:
printf ("", 5);
because printf's semantics are known. But that's not ill-formed, just
inefficient. And in this case we're doing the equivalent of:
printf (not-compile-time-constant, 5);
C says excess arguments are ignored:
If the format is exhausted while arguments remain, the excess
arguments are evaluated (as always) but are otherwise ignored.
-Wformat normally warns on this case when the format string is
constant. It doesn't when the string is non-constant but that's
likely just a consequence of the warning running very early. If
it ran later on it would warn. It would be quite useful to have
-Wformat run later to catch mistakes in conditional format strings
(like the growing number of GCC's own uses of conditionals in
warning_at type of calls). If/when -Wformat is ever moved to run
later, these cases will either have to be fixed or the warning
relaxed to allow them. There have been requests to add
a portability level to -Wformat so this case could be moved into
some pedantic level if one were ever added.
Martin