Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 277
finnsson Avatar asked Dec 26 '12 16:12

finnsson


People also ask

Does Clang support GCC extensions?

In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions.

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.

Is Clang better than GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

What is LibClang?

LibClang is a stable high level C interface to clang. When in doubt LibClang is probably the interface you want to use. Consider the other interfaces only when you have a good reason not to use LibClang. Canonical examples of when to use LibClang: Xcode.


1 Answers

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.

like image 91
Eli Bendersky Avatar answered Oct 03 '22 21:10

Eli Bendersky