[cfe-dev] Modifying an AST and Sema's dependency on an AST consumer

Lukasz Janyst ljanyst at cern.ch
Wed Dec 3 06:26:07 PST 2008


Hi there,

   I'm trying to do a few weird things with clang. The general scheme
is that I want to generate ASTs from my C/C++ code and keep them
around to modify or extract some information later. Then, at some
point, I want to feed the ASTs to CodeGen, play with the modules and
then link them together and run. I have encountered something that I
do not completely understand: Sema needs an ASTConsumer because when
it's done with the class definition processing it calls
ASTConsumer::HandleTagDeclDefinition which is implemented in CodeGen
and seems to do some important work. Shouldn't CodeGen be able to
figure out by itself if it has got all the nodes constituting a class
definition? Otherwise the user has to emulate the Sema's behavior.

   The second thing I'm trying to do is an insertion of some function
declaration nodes to the TranslationUnit that is being created
(implicitly declare some functions that are defined in in another
file/TU/module and will be linked to the one being processed). For
example I have:

test01.c:

double doSth( double param ) {
   return 2.0*param+3.0;
}

test02.c:

#include <stdio.h>

int main( int argv, char** argv ) {
   printf( "%lf\n", doSth( 17.0 ) );
   return 0;
}

   I want to parse test01.c and get all the function definitions,
"convert" them to function declarations, and insert to the translation
unit/ast context of the test02.c. I can extract the definitions and
create the declaration nodes using FunctionDecl::Create, I enter new
function types to the ASTContext and I think that I update all the
tables in it. Yet still I seem to experience some strange memory
issues when the modified TU gets processed by the CodeGen. I think I
have missed something essential. Can somebody please explain me the
steps necessary to do something like this?

Cheers,
   Lukasz



More information about the cfe-dev mailing list