[cfe-dev] relating preprocessing information to syntax trees

Dietmar Ebner ebner at complang.tuwien.ac.at
Fri Apr 10 08:56:25 PDT 2009


Hi,

On 4/10/09 6:47 AM, Chris Lattner wrote:
> On Apr 9, 2009, at 11:42 AM, Dietmar Ebner wrote:
>> I'm thus looking for a proper way to relate preprocessing information
>> such as define or include directives and pragmas to nodes in the syntax
>> tree. A quick hack would be to use the annotated source locations, but I
>> don't particularly like this solution. Any advice would be greatly
>> appreciated. Sorry if this is a trivial question, I'm fairly new to
>> clang.
> I'm not sure exactly what you are asking for. Instead of a general
> question, can you ask about a specific feature?
Sure. My pass is implemented as an ASTConsumer by processing 
declarations passed to HandleTopLevelDecl(). I'm looking for a way to 
annotate declarations with preprocessor directives that are located 
immediately before them in the original source code, e.g., for
     #include "foo.h"
     void foo() {}
I would like to create a term that looks roughly like this:
     function_declaration(
         function_parameter_list([], <...>),
         function_definition(basic_block([]), <...>),
         function_declaration_annotation(
             function_type(type_void, <...>),
             foo,
             declaration_modifier(<...>),
   -->       preprocessing_info(
   -->           [cpreprocessorIncludeDeclaration(
   -->                '#include "foo.h"\n',
   -->                 before,
   -->                 file_info('foo.c',1,1)
                  )])))

I'm at a point where the AST is available, i.e., the lexer and the 
parser already did their job. I had a quick look at some examples such 
as the HTMLRewriter and they seem to instantiate their own lexer and 
process the token stream. I was wondering if, given an AST, there is a 
way to query the preprocessor directives already processed so far, e.g., 
can I find out that there was the include directive for foo.h when 
processing the declaration for foo()?

Thanks!

-
Dietmar



More information about the cfe-dev mailing list