[cfe-dev] Generating printf format specifiers
rjmccall at apple.com
Fri Jan 22 15:41:46 CST 2010
On Jan 22, 2010, at 12:23 PM, Dave Keck wrote:
> Thanks for the reply!
>> It depends on what you mean by "substitute". __typeof__ is not a macro; it doesn't substitute tokens or anything, it actually gets parsed and interpreted by the semantic layer as a special kind of type-sugaring. It looks like you want __fmt__ to work much more like a macro, since string-literal concatenation is still happening. That's somewhat trickier and raises some interesting questions about behavior.
> Ah yes - please excuse my loose language.
Well, I'm not really asking about language, I'm asking about semantics. It looks like you want this to work in all cases as if the user had typed a string literal instead of __fmt__(foo). That's going to complicate the implementation, because the parser has to be prepared to accept __fmt__ everywhere it would accept a string literal. That might be very straightforward, it might not be.
>> Also, we try to be very cautious about accepting new language enhancements, even in Objective C. We'll really need to talk about whether we want this before we can accept it.
> I see; I just wanted to inquire to see if I could get any hints of how
> I might do it myself, more as an exercise than anything else. Official
> support would be great, though!
If you just want it in private code, of course that's fine. You'll need to recognize __fmt__ as a new type of token, parse the following tokens appropriately (presumably as an arbitrary expression), and add an API call to Sema to turn the expression into a string literal (by looking at its type). If you do this in ParseStringLiteralExpression and fake up a string-literal token appropriately, most everything else should fall out.
More information about the cfe-dev