Source-to-source translation: modifying functions

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:

  1. 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.
  2. 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.
  3. 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.

Is it the right way to accomplish what I need, or am I totally wrong?
Any help will be appreciated.
Many thanks,
Ilya

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:

  1. 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.

  1. 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.

  1. 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