How to retrieve entire argument name with operators

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to retrieve entire argument name with operators

Louis Dionne via cfe-dev

Hi,

 

I am writing a replacement to move arguments from one function to another.

 

For instance,

 

foo(arg1) -> foo().bar(arg1)

foo(*arg2) -> foo().bar(*arg2)

foo(…) -> foo().bar(…)

 

where arg1, arg2, … are resolved to the same type.

 

I am able to handle the first case doing something like this:

 

matcher = callExpr(callee(functionDecl(hasName(“foo”)))).bind(call)

 

replacements.emplace(sourceManager,
                                          call->getArg(0)->getExprLoc(),
                                          Lexer::getLocForEndOfToken(call->getArg(0)->getExprLoc(),
                                                                                                         0,

sourceManager,
context.getLangOpts())),

             “”);

 

Which is able to change “foo(arg1)” to foo().bar(arg1). However, for cases with UnaryOperators inbetween, the outcome is:

“foo(*arg2)” -> “foo(arg2).bar(*arg2). It only considers the unary operator and does not remove the “arg2” portion of the argument.

 

I cannot figure out a clean way to do this. What is the suggested way to get that argument regardless of what operators that could be in front of it?

Note: I am trying to remove that argument completely.

 

 

Thanks,

David Lai


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: How to retrieve entire argument name with operators

Louis Dionne via cfe-dev
Hi David,

Typically, when you get a SourceLocation from a AST object, it points at the start of token.  Since you are using the same SourceLocation for the start and end, the replacement only removes the * token.  Also, the function getExprLoc() returns the best location for a diagnostic, and may not be useful elsewhere.

To get the start and end SourceLocation's, use the functions getLocStart() and getLocEnd().  These are from the class Stmt, which is the parent of Expr.

Hope that helps.

On Sun, Apr 8, 2018 at 4:21 AM David Lai via cfe-dev <[hidden email]> wrote:

Hi,

 

I am writing a replacement to move arguments from one function to another.

 

For instance,

 

foo(arg1) -> foo().bar(arg1)

foo(*arg2) -> foo().bar(*arg2)

foo(…) -> foo().bar(…)

 

where arg1, arg2, … are resolved to the same type.

 

I am able to handle the first case doing something like this:

 

matcher = callExpr(callee(functionDecl(hasName(“foo”)))).bind(call)

 

replacements.emplace(sourceManager,
                                          call->getArg(0)->getExprLoc(),
                                          Lexer::getLocForEndOfToken(call->getArg(0)->getExprLoc(),
                                                                                                         0,

sourceManager,
context.getLangOpts())),

             “”);

 

Which is able to change “foo(arg1)” to foo().bar(arg1). However, for cases with UnaryOperators inbetween, the outcome is:

“foo(*arg2)” -> “foo(arg2).bar(*arg2). It only considers the unary operator and does not remove the “arg2” portion of the argument.

 

I cannot figure out a clean way to do this. What is the suggested way to get that argument regardless of what operators that could be in front of it?

Note: I am trying to remove that argument completely.

 

 

Thanks,

David Lai

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev