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
printf-cstr.patch
Description: Binary data
_______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
