Clang / GCC plugin for interpreting C ++ 11 user attributes

I am trying to write a plugin for clang or gcc to interpret custom [[cxx11 :: attributes]] and generate some code based on this.

Starting with version 4.5, gcc supports plugins that can be connected at almost every stage of compilation. However, I think gcc doesn't really know how to modify its AST and navigate it (at least I'm having a bad time trying ...).

Then I remembered that clang was designed to be expanded and used as a library, and I gave it a chance. After digging, I found several topics saying that clang does not support user attributes. I cried.

My main goal is to generate code based on any annotations that the user can use in the code. I would 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 (currently) impossible to have custom attributes in clang? 2) What is the best way to learn gcc internals? (I read a lot of documentation pages, but still they didn’t say what I want) 3) Does gcc have some kind of dump function for printing its AST, how does clang do it? This will help a lot to study its tree.

Thanks! Any info / tip would be appreciated!

+5
source share
1 answer

GCC plugins are specific to GCC and even (in principle) to a specific version of GCC (there is no guarantee that the plugin encoded for GCC 4.8 will work on GCC 4.9).

You can consider expanding GCC with MELT , which is a lysing domain language for the GCC extension, implemented as a GCC (meta-) plugin.

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

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

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

It is better to use the latest versions of GCC (for example, 4.9.1) and MELT (for example, 1.1.2 or later, I may release MELT 1.1.3 in a week or two)

+3
source

Source: https://habr.com/ru/post/1204513/


All Articles