On 08/12/2016 01:27 PM, David Malcolm wrote:
In r239260 I attempted to add fix-it hints for -Wformat type
warnings.
Unfortunately, my implementation was too simplistic, and only
worked correctly for the most simple format strings: the fix-it
hint would suggest replacement of an entire conversion specification,
but the replacement would only include the length modifier (if
any) and the conversion character, thus effectively discarding
any user-supplied flags, width, or precision.
Additionally, the replacement failed to take into account the
user-specified conversion character, so that e.g. an "x" could
be replaced by a "d".
The following patch fixes these issues by:
(a) making the suggestion retain any user-supplied part of the
conversion specification before the length modifier, and
(b) attempt to preserve the user-specified conversion character, by
first considering replacements that only change the length
modifier.
It also expands the function comments in c-format.c, showing how
the functions work through a non-trivial example.
This patch is a followup to:
"[PATCH] Fix caret locations in format_type_warning (PR c/72857)"
https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00867.html
Successfully bootstrapped®rtested on x86_64-pc-linux-gnu (on top
of the other patch).
A couple of things I noticed when getting the -Wformat-length code
to use the new infrastructure from c-format.
Hints that suggest alternate directives that don't accept all
the same flags as those used in the original should filter out
those flags. For example, when passing a pointer to a "%#lx"
directive, the hint should suggest "%s" for character pointers
and "%p" for others rather than "%#s" or "%#p". Similarly,
when passing a string to a "%+i" or "% i" directive, the '+'
and space flags should be removed.
Also, with -Wformat-signedness (but I'd say without it as well),
the hints should respect the signedness of the arguments. For
example, the hint for "%u" with a long int argument should be
"%lu" and not "%li".
Martin