[cfe-dev] Source-to-source translation: modifying functions

Douglas Gregor dgregor at apple.com
Mon Aug 23 07:16:15 PDT 2010


On Aug 22, 2010, at 3:54 AM, Ilya Mirsky wrote:

> Hi all,
> 
> I'm working on a Source-to-source translator, which should introduce various modifications to the original source code.
> I'm doing the modifications from an ASTConsumer, calling clang/Rewrite/Rewriter methods, and using the different visitors (clang/AST/*Visitor) to access all Decl/Stmt.
> The problems I'm having:
> 
> Changing function names: if I change the name with FunctionDecl::SetDeclName it doesn't reflect in RewriteBuffer. If I want to use the Rewriter::ReplaceText method I don't know how to determine the exact SourceLocation of the function's identifier.

Decl::getLocation() points at the name of the function. It's not recommended to call SetDeclName at all, since you'll be breaking semantic invariants in the AST.
> Retrieving params value in calls: CallExpr::getArg(i) gives me the Arg Expr, and I can easily get its type, but how can I get it's value? I don't need to evaluate the Expr, just get the corresponding string code.
You can getSourceRange() on the expression and then grab the string representation of the expression from the source buffer.
> Modifying signatures(params list): I'm having difficulties with FucntionDecl::setParams (Kind of a custom defined type), and Rewriter::ReplaceText requires the exact source location of the params list, which I'm unable to determine.

It's not recommended to call setParams, since you'll be breaking semantic invariants in the AST. You should be able to use getSourceRange() on the parameters in the list (possibly also relying on the range information in the TypeSourceInfo for the parameters, directly) to compute the source range covering all of the parameters, if you want to rewrite them.

	- Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100823/67080f82/attachment.html>


More information about the cfe-dev mailing list