finnsson
finnsson

Reputation: 4067

How to extend Clang with an additional parser?

How can I extend Clang with an additional parser for files with a special file ending, i.e. can I develop a FrontendAction that says "Hey! I'll take care of all files with the file ending '.lorem' and return an abstract syntax tree (clang::ASTContext ?)"?

I've read about clang::FrontendAction, clang::Parser and clang::driver::Driver but I haven't been able to figure out where and how I should extend Clang to be able to extend the compiler with an additional parser (not extending the current parser).

Upvotes: 12

Views: 3157

Answers (1)

Eli Bendersky
Eli Bendersky

Reputation: 273366

Here are some pointers:

in tools/clang/lib/Driver/Types.cpp you have lookupTypeForExtension, that determines the "type" of the compiled code based on the extension. For example, for the .m extension it returns TY_ObjC. You also have the isObjC predicate that determines whether the given type belongs to Objective C.

As for how the parser knows which language it's parsing... It knows it via the Preprocessor, which has a LangOptions member. The latter has many options defined in include/clang/Basic/LangOptions.def. For instance, ObjC1 and ObjC2. These are set in CompilerInvocation::setLangDefaults, which eventually gets invoked from CompilerInvocation::CreateFromArgs.

Remember that the clang driver will invoke the clang frontend as a "subprocess", passing it additional command-line arguments. The driver is gcc-compatible, and the frontend can be seen as clang itself.

Also, IMHO it would be tons of trouble adding an extra parser to clang. While everything is modular, a lot of work needs to be done to create and tie everything together. If your language extends ObjC, just use the existing parser. If you language is something completely different, then clang may not be a good option for you.

Upvotes: 1

Related Questions