[cfe-dev] String Interpolation for ObjC

Jacob Carlborg via cfe-dev cfe-dev at lists.llvm.org
Wed Jul 26 23:56:25 PDT 2017


On 2017-07-26 19:43, Uli Kusterer via cfe-dev wrote:
> Hi,
> 
>   I have an idea on how one could add string interpolation to Swift. I wrote a tiny preprocessor as a proof-of-concept: https://github.com/uliwitness/ocpre
> 
>   Since that doesn't actually parse the code, it can only handle object types so far, so I'm thinking I should try to add it to the Clang ObjC compiler proper, instead. But I've never done this, never even built clang. Is there a tutorial somewhere on how to do this, and pointers on where I would start hooking into string escape sequence parsing, and which other parts I'd have to touch?
> 
>   My approach is basically a simple code transformation. I'd turn strings containing escape sequences and subexpressions:
> 
> 	NSLog( "This is \(NSFullUserName()), who is \([myDatabase ageForUser: NSUserName()]) years old." );
> 
> into
> 
> 	NSLog( "This is %1$@, who is %1$d years old.", (NSFullUserName()), ([myDatabase ageForUser: NSUserName()]) );
> 
> and that's it. So this *should* be a comparatively simple change, and, moreover should need no changes to any existing libraries, client source code etc. It would just work with existing variadic functions.

Highly unlikely, but in theory, someone might have code containing 
string literals with the exact syntax that is suggested for string 
interpolation. That code would break.

Do you propose to add string interpolation to C string literals or 
Objective-C string literals? Because the above are C string literals, 
unless some software involved in sending the message removed the at 
signs (@).

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list