Clang/GCC plugin to interpret custom c++11 attribu

2019-05-02 15:45发布

问题:

I'm trying to write a plugin to clang or gcc to interpret custom [[cxx11::attributes]] and generate some code based on that.

Since version 4.5, gcc supports plugins that can be hooked at almost every compilation steps. However, I think gcc is not very clear about how modify its AST and navigate through it (at least I'm having a bad time trying...).

Then I remembered that clang was designed to be extended and used as a library and I gave it a shot. After digging around I found some topics saying clang does not support custom attributes. I cried.

My main goal is to generate code based on any kind of annotations that user can use at code. I'd like to use c++11 attributes because they are very clear. Pragmas are also an option but they have some limitations.

Here are the questions:

1) Is it really (currently) impossible to have custom attributes in clang? 2) What is the best way to learn gcc internals? (I read lots of pages of documentation but still they didn't say what I want) 3) Does gcc have some dump function to print its AST like clang does? It would help a lot to explore its tree.

Thank you! Any information/tip would be appreciated!

回答1:

GCC plugins are specific to GCC, and even (in principle) to a particular version of GCC (there is no guarantee that a plugin coded for GCC 4.8 would work on GCC 4.9).

You might consider extending GCC with MELT, which is a lispy domain specific language to extend GCC, implemented as a GCC (meta-) plugin.

However, you need to understand the internal representations of GCC (Gimple, Trees, the pass manager, ...). Read first my slides on GCC plugins thru the MELT example (Linux Collaboration Summit, march 2014)

You can easily add GCC attributes with plugins and with MELT, and probably also C++11 attributes.

And yes, GCC has a lot of possible dumps (try using -fdump-tree-all).

You'll better use recent versions of GCC (e.g. 4.9.1) and of MELT (e.g. 1.1.2 or later; I'll probably release MELT 1.1.3 within a week or two)