Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use clang::TreeTransform?

i'm trying to figure out the integration point for the clang::TreeTransform.

The goal is to transform the AST before the Code Generation.

Thanks in advance.

like image 340
Gaetano Avatar asked Jul 01 '16 13:07

Gaetano


1 Answers

One way to do this is to use ASTFrontEndAction, then redefine the method CreateASTConsumer in order to create a SemaConsumer (instead of an ASTConsumer).

Then in handleTopLevelDecl method create a RecursiveASTVisitor by handing it the Sema, which will be necessary for TreeTransform.

Then the Visit method of the RecursiveASTVisitor can create an instance of your TreeTransform, and then call the appropriate transforming method.

PS: Sometimes when tranforming compund statements, function scope might by empty. You can do

SemaRef.PushFunctionScope();
SemaRef.PushCompoundScope();

before you call your tranforming function.

like image 106
Nishant Sharma Avatar answered Oct 02 '22 14:10

Nishant Sharma