[cfe-commits] r160886 - in /cfe/trunk: include/clang/Analysis/Analyses/FormatString.h lib/Analysis/FormatString.cpp lib/Analysis/PrintfFormatString.cpp lib/Analysis/ScanfFormatString.cpp test/Sema/format-strings-fixit.c

Hans Wennborg hans at chromium.org
Sat Jul 28 11:39:52 PDT 2012


On Sat, Jul 28, 2012 at 1:51 PM, Matthieu Monrocq
<matthieu.monrocq at gmail.com> wrote:
> On Fri, Jul 27, 2012 at 9:17 PM, Hans Wennborg <hans at hanshq.net> wrote:
>> Author: hans
>> Date: Fri Jul 27 14:17:46 2012
>> New Revision: 160886
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=160886&view=rev
>> Log:
>> Make -Wformat walk the typedef chain when looking for size_t, etc.
>>
>> Clang's -Wformat fix-its currently suggest using "%zu" for values of
>> type size_t (in C99 or C++11 mode). However, for a type such as
>> std::vector<T>::size_type, it does not notice that type is actually
>> typedeffed to size_t, and instead suggests a format for the underlying
>> type, such as "%lu" or "%u".
>>
>> This commit makes the format string fix mechanism walk the typedef chain
>> so that it notices if the type is size_t, even if that isn't "at the
>> top".
>>
> Hi Hans,
>
> I wonder why it would be necessary to "know" that the
> std::vector<...>::size_type is a size_t instead of being a long unsigned
> int. The very purpose of the typedef to `size_type` in the first place is to
> abstract the underlying type, and different STL implementations are free to
> alias it differently as far as I know, so...
>
> ... could you explain why it is important for you ?

It's not necessary, but I think it allows us to provide a better
fix-it for the user that is trying to figure out how to print such a
value.

The point is that when std::vector<T>::size_type is size_t (which
AFAIK is pretty much always the case), printf provides a portable way
to print it via "%zu". Thus, if Clang can detect this and suggest
"%zu" as a fix-it instead of "%lu" or "%u", that's better since it is
more portable.

 - Hans



More information about the cfe-commits mailing list