This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Implement a warning for bogus sizeof(pointer) / sizeof(pointer[0])


On 04/28/2017 08:12 AM, Bernd Edlinger wrote:
On 04/28/17 00:52, Martin Sebor wrote:
I suggest avoiding "element size" because the pointed-to argument
need not be an array.  Mentioning the types should help users better
understand the problem (especially in C++ where types are often
obscured by layers of templates).  It might also be a nice touch
to add a note pointing to the declaration of the first sizeof
operand (if it's an object).


Yes, many thanks for your suggestions.

Do the new warning and info messages look right?

Sorry for not replying sooner.  The new warning looks good to
me, with just one minor nit.  For proper highlighting within
the message, the expression should be quoted in a pair of %<
and %> directives, like so:

  warning_at (stack[sp].loc, OPT_Wsizeof_pointer_div,
              "division %<sizeof (%T) / sizeof (%T)%> does "
              "not compute the number of array elements",
              type0, type1))

This way the whole quoted expression will be highlighted in
the style appropriate for quoted text.

Similarly, in the inform call, sizeof should be quoted in
the same pair of %< and %> directives.


Do you want me to change the %qT format strings to %T ?

Yes, with the surrounding %< and %> the nested directives should
use the unquoted forms, otherwise the printer would end up quoting
both the whole expression and the type operand.

FWIW, to help avoid this mistake, I think this might be something
for GCC -Wformat to warn on and the pretty-printer to detect (and
ICE on).

Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]