Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add keyword to Objective-C using Clang

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

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

I ask this question as Clang is very modular and I'm not sure which .td (see tablegen) files, .h files and AST visitor passes I would need to modify to achieve my goal.

like image 629
Sedate Alien Avatar asked Feb 03 '12 07:02

Sedate Alien


People also ask

Does Clang define __ GNUC __?

(GNU C is a language, GCC is a compiler for that language.Clang defines __GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ according to the version of gcc that it claims full compatibility with.

How do you compile with Clang?

To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

What is option in Clang?

clang supports the -std option, which changes what language mode clang uses. The supported modes for C are c89, gnu89, c94, c99, gnu99 and various aliases for those modes. If no -std option is specified, clang defaults to gnu99 mode.

What is Clang LLVM?

Clang: a C language family frontend for LLVM. The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript) for the LLVM project.


1 Answers

If I understand the clang's code correctly (I'm still learning, so take caution), I think the starting point for this type of addition would be in Parser::ParseObjCAtExpression within clang/lib/Parse/ParseObjc.cpp.

One thing to note is that the Parser class is implemented in several files (seemingly separated by input language), but is declared entirely in clang/include/Parser.h.

Parser has many methods following the pattern of ParseObjCAt, e.g., ParseObjCAtExpression ParseObjCAtStatement ParseObjCAtDirectives etc..

Specifically, line 1779 of ParseObjc.cpp appears to be where the parser detects an Objective-C string literal in the form of @"foo". However, it also calls ParsePostfixExpressionSuffix which I don't fully understand yet. I haven't figured out how it knows to parse a string literal (vs. an @synchronize, for example).

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

If you haven't yet, visit clang's "Getting Started" page to get started with compiling.

like image 126
Josh Rosen Avatar answered Oct 19 '22 22:10

Josh Rosen