[cfe-dev] generate/create a function call dynamically in clang

Douglas Gregor dgregor at apple.com
Mon Nov 1 14:08:49 PDT 2010


On Oct 31, 2010, at 8:54 AM, Eli Bendersky wrote:

> Hello,
> 
> In some situations, I need to generate a function call on the fly in clang. For example, whenever I find a reference to some identifier 'myvar', I want to replace the reference with the call foo(myvar), where 'foo' is a known function name. 
> 
> I investigated the code of clang and I see CallExpr represents a function call, but it requires quite a lot to be created and I'm not sure where to get that information.just be created. What would be the best way to go about this? 


Let Sema help you build a proper call expression, by looking at the callbacks the parser makes when parsing "foo(myvar)", then emulating those with your own code. For the transformation you're talking about, build the DeclRefExpr for "myvar" (with Sema::ActOnIdExpression) and for "foo" (with the same routine), then pass those to ActOnCallExpr.

	- Doug



More information about the cfe-dev mailing list