Add keyword in Objective-C using Clang

How can I add a relatively trivial keyword in Objective-C using the Clang compiler? For example, adding the @yes literal, which maps to [NSNumber numberWithBool:YES] .

I looked at the (excellent) source code for Clang and believe that most of the work I will need to do is lib/Rewrite/RewriteObjC.cpp . There is a method RewriteObjC::RewriteObjCStringLiteral (see the previous link) that performs a similar task for literal NSString * instances.

I ask this question because Klang is very modular, and I'm not sure if the .td (see tablegen ) files, .h files and AST guest passes I will need to change to achieve my goal.

+7
source share
1 answer

If I understand the clang code correctly (I'm still learning, so be careful), I think that the starting point for this type of add would be in Parser :: ParseObjCAtExpression inside clang / lib / Parse / ParseObjc.cpp.

It should be noted that the Parser class is implemented in several files (apparently separated by the input language), but is fully declared in clang / include / Parser.h.

The parser has many methods following the ParseObjCAt pattern, for example, ParseObjCAtExpression ParseObjCAtStatement ParseObjCAtDirectives, etc ..

In particular, the string 1779 ParseObjc.cpp looks like the parser detects the Objective-C string literal in the form @ "foo". However, it also calls ParsePostfixExpressionSuffix, which I still don't quite understand. I did not understand how it understands a string literal (e.g. @synchronize).

 ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { ... return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc)); ... } 

If you haven’t already done so, visit the clang Getting Started page to get started with compilation.

+1
source

All Articles