IRILL - Research and Innovation on Free Software

An experimental framework for Pragma Handling in Clang


"An experimental framework for Pragma Handling in Clang"
by Simone Pellegrini - U Innsbruck,
on 2013-04-29 00:00:00
Download Webm format
Download MP4 format

Clang is one of the fully featured C/C++ frontend which managed to bring compiler research into the mainstream. Its clean interfaces and structure enabled several new research ideas to be applied to real codes in a scale that was never possible in the past. However, one of the main sin of researchers is the need to extend the language. Since C allows language extensions through the #pragma preprocessor directive, many have used this medium for feeding meta-information to the compiler analysis module. A very famous example is the OpenMP standard. This is an extension to the semantics of C/C++ which grants to the compiler the possibility of parallelizing a portion of the input code. However Clang's support for pragmas is lacking and primitive at most. Clang allows pragma handlers to be registered for a particular pragma but the user is left with the burden of parsing the tokens returned by the lexer. This is not a problem for many extensions which rely on simple keywords and integer identifiers, however it can become a parsing nightmare if a C expression is allowed in the pragma (as it is the case for OpenMP). In those cases, the user has to provide its own expression parser which basically means rewriting Clang's parser. My solution relies on a simple idea, i.e. exposing the full Clang parser to pragma handlers. Together with a framework which allows pragma definition to be specified in EBNF form, new language extensions can be easily defined in a single line of C++ code.