[cfe-commits] [patch] Added a suggestion when a std::string is passed to printf()
Sam Panzer
panzer at google.com
Fri Jun 1 14:59:42 PDT 2012
Hi,
We had a request for more user-friendly printf() diagnostics, which asked
for a note suggesting a call to .c_str() when passing a c++ string to
printf when a char* is expected, and I thought it would make a good first
submission:
Currently code such as
>
> string foo;
>
> printf("Oh dear %s", foo);
>
> produces a reasonable (if somewhat technical) diagnostic:
>
> error: cannot pass object of non-trivial type 'string' (aka
>> 'basic_string<char>') through variadic function; call will abort at runtime
>> [-Wnon-pod-varargs]
>
> printf("Oh dear %s", foo);
>
>
>> That confuses some people (such as people who happened to be sitting near
>> me today). It would be easy for them to understand if this could
>> special-case the situation where the call is to a *printf-like function and
>> the non-POD in question is a string (or std::string), to suggest that the
>> solution might be to add the missing ".c_str()".
>
>
>> When format string checking is active for the function/argument in
>> question it would also be possible to report on the expected argument type.
>
>
>> (Of course it's also not ideal to report that the "call will abort at
>> runtime" when generating an error rather than a warning, but that might not
>> be worth fixing.)
>
>
I attached a patch which emits this note when an object defining a .c_str()
member function is passed instead of a char*, along with a test file.
Searching for c_str() was intended to allow the note to fire for
std::string replacements.
-Sam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120601/c67447b1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: printf-cstr.patch
Type: application/octet-stream
Size: 4310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120601/c67447b1/attachment.obj>
More information about the cfe-commits
mailing list