Pragma information in AST processing

Hi, this is my first post to the list.

For what I'm currently working on I'd like to use some information extracted from pragmas in an AST visiting/transformation pass. Since (as far as I can see) pragma information is only available in the preprocessing pass I currently see 2 ways of doing this, both using PragmaHandlers in the preprocessor:
(1) collect a list of the pragmas, their information and the associated source locations during preprocessing, then in the AST visitor check if we are at a location on a line directly after some pragma and if so process based on the information collected previously
(2) rewrite the pragmas to some dummy function call, which will be visible in the AST and can be used for further processing during AST traversal

Neither of these options seems particularly clean to me so I would appreciate if someone who is more experienced with clang can think of a better solution. Otherwise I'll go ahead with the first option.

Cheers,
  Peter

Hi Peter,

Hi, this is my first post to the list.

For what I'm currently working on I'd like to use some information
extracted from pragmas in an AST visiting/transformation pass. Since (as
far as I can see) pragma information is only available in the
preprocessing pass I currently see 2 ways of doing this, both using
PragmaHandlers in the preprocessor:

What kind of information do you want to extract from pragmas? Are you
planning on adding specific pragmas for your problem, or you want to
do something with existing pragmas which clang handles?

(1) collect a list of the pragmas, their information and the associated
source locations during preprocessing, then in the AST visitor check if
we are at a location on a line directly after some pragma and if so
process based on the information collected previously
(2) rewrite the pragmas to some dummy function call, which will be
visible in the AST and can be used for further processing during AST
traversal

Neither of these options seems particularly clean to me so I would
appreciate if someone who is more experienced with clang can think of a
better solution. Otherwise I'll go ahead with the first option.

The way we currently handle pragmas is generally to attach attributes
to other AST nodes in the pragma handler. This is relatively clean,
but does have the downside that there is no easy way to determine that
the attribute came from a pragma instead of being specified manually.

Eventually I can imagine that we will want to have an explicit
representation in the AST of the pragma (for example, if we want to be
able to print the parse AST back out in exactly the form the user
specified it). However, if your problem can be fit into the existing
model that will probably be much less work. Can you tell us more about
what you are trying to do?

- Daniel

Hi Peter,
What kind of information do you want to extract from pragmas? Are you
planning on adding specific pragmas for your problem, or you want to
do something with existing pragmas which clang handles?

They are pragmas that are not yet handled by clang.

The way we currently handle pragmas is generally to attach attributes
to other AST nodes in the pragma handler. This is relatively clean,
but does have the downside that there is no easy way to determine that
the attribute came from a pragma instead of being specified manually.
  

This sounds like it would work for my case. I digged around in the source tree and I think this is done in SemaAttr.cpp right now (in ActOnPragmaUnused). So I would derive subclasses from clang::Attr and attach them to the statements to carry the pragma information.
What I don't know how to do at this point is accessing the AST node for the statement that follows the pragma in question (which is what I would want to attach my attributes to). I'm confused because it seems like the nodes would not be available at the point where the pragma handler is executed (during the Preprocessor run).

Eventually I can imagine that we will want to have an explicit
representation in the AST of the pragma (for example, if we want to be
able to print the parse AST back out in exactly the form the user
specified it). However, if your problem can be fit into the existing
model that will probably be much less work. Can you tell us more about
what you are trying to do?
  

As I said above my problem should more or less fit into the attribute model (I don't care about the specific pragma string used) -- at least if I can attach these attributes to any statement following one of the pragmas. What I'm actually working on is a source to source OpenMP 3 implementation. Once that is in a usable state it should serve as a research testbed for checking the usefulness of some additions to OMP 3. Obviously this is a rather long-term project :wink:

Thanks,
  Peter