[cfe-dev] Generating printf format specifiers
davekeck at gmail.com
Sun Jan 24 05:21:15 PST 2010
> If you're happy with adding incompatible language extensions, maybe you should consider, as an alternative, adding a new type specifier that's understood in format strings to functions marked with the printf attribute. That way, you'd do something like this:
> NSLog(@"Hello, %!! This is your user id: %!", username, userID);
Wow - I like that a lot more; it's much more concise and less
confusing to the eye.
> The printf format string checker in clang already has code to see what the format string for the specified type should be, so you can just have it replace the ! with the correct value in the string.
> Note that this has some problems, however. If the argument is an int, for example, would you insert %x or %d? This applies even with the original __fmt__() pseudo-macro idea. There is not an injective mapping from types to format strings. This is especially true if you consider modifiers. With __fmt__(), I'm not sure how you would specify whether to show the sign, the precision, or the width, for example.
I was thinking of keeping it simple to start:
integer types => %d, %u (and variants depending on width of course)
char, char * => %s
all other pointers => %p
float, double => %f
which would cover the vast majority of my uses, and perhaps adding
more options once the groundwork is laid.
More information about the cfe-dev