Rewriter: How to rewrite an empty function argument list?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Rewriter: How to rewrite an empty function argument list?

Sumner, Brian via cfe-dev
Hi everyone,

I am trying to transform function arguments. For functions with a at
least one argument I can position the Rewriter on the paramdecl.

But if the function has no arguments there is no paramdecl. So how can I
rewrite the argument list of such a function?

Example:

int foo() { return 42; } transform into int foo(int p1) { return 42;);

Thanks

Marcel

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

Re: Rewriter: How to rewrite an empty function argument list?

Sumner, Brian via cfe-dev
Hi Marcel,

I believe that you can get the location of '('/')' and insert a new parameter after/before that location. The locations of braces should be in the FunctionTypeLoc which you can extract from the TypeLoc value returned by calling ((FunctionDecl *)YourFunction)->getTypeSourceInfo->getTypeLoc();

Alex

On 30 July 2017 at 16:44, Marcel Schaible via cfe-dev <[hidden email]> wrote:
Hi everyone,

I am trying to transform function arguments. For functions with a at least one argument I can position the Rewriter on the paramdecl.

But if the function has no arguments there is no paramdecl. So how can I rewrite the argument list of such a function?

Example:

int foo() { return 42; } transform into int foo(int p1) { return 42;);

Thanks

Marcel

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriter: How to rewrite an empty function argument list?

Sumner, Brian via cfe-dev

Alex thanks for your hint.

I was playing around with getTypeLoc, but I am didn't find a way to get the location of the opening parenthesis of a function argument list.

Maybe you have an idea?

Marcel

Am 30.07.2017 um 21:56 schrieb Alex L:
Hi Marcel,

I believe that you can get the location of '('/')' and insert a new parameter after/before that location. The locations of braces should be in the FunctionTypeLoc which you can extract from the TypeLoc value returned by calling ((FunctionDecl *)YourFunction)->getTypeSourceInfo->getTypeLoc();

Alex

On 30 July 2017 at 16:44, Marcel Schaible via cfe-dev <[hidden email]> wrote:
Hi everyone,

I am trying to transform function arguments. For functions with a at least one argument I can position the Rewriter on the paramdecl.

But if the function has no arguments there is no paramdecl. So how can I rewrite the argument list of such a function?

Example:

int foo() { return 42; } transform into int foo(int p1) { return 42;);

Thanks

Marcel

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriter: How to rewrite an empty function argument list?

Sumner, Brian via cfe-dev
Once you get the TypeLoc of the function, you can try casting it to function type loc using getAsAdjusted. Here's a snipped from SemaDecl that emits a warning using the location of the '(':

        TypeSourceInfo *TI = FD->getTypeSourceInfo();
        TypeLoc TL = TI->getTypeLoc();
        FunctionTypeLoc FTL = TL.getAsAdjusted<FunctionTypeLoc>();
        Diag(FTL.getLParenLoc(), diag::warn_strict_prototypes) << 2;

On 31 July 2017 at 08:45, Marcel Schaible <[hidden email]> wrote:

Alex thanks for your hint.

I was playing around with getTypeLoc, but I am didn't find a way to get the location of the opening parenthesis of a function argument list.

Maybe you have an idea?

Marcel

Am 30.07.2017 um 21:56 schrieb Alex L:
Hi Marcel,

I believe that you can get the location of '('/')' and insert a new parameter after/before that location. The locations of braces should be in the FunctionTypeLoc which you can extract from the TypeLoc value returned by calling ((FunctionDecl *)YourFunction)->getTypeSourceInfo->getTypeLoc();

Alex

On 30 July 2017 at 16:44, Marcel Schaible via cfe-dev <[hidden email]> wrote:
Hi everyone,

I am trying to transform function arguments. For functions with a at least one argument I can position the Rewriter on the paramdecl.

But if the function has no arguments there is no paramdecl. So how can I rewrite the argument list of such a function?

Example:

int foo() { return 42; } transform into int foo(int p1) { return 42;);

Thanks

Marcel

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriter: How to rewrite an empty function argument list?

Sumner, Brian via cfe-dev

Thanks Alex, works!

Marcel


Am 31.07.2017 um 09:53 schrieb Alex L:
        TypeSourceInfo *TI = FD->getTypeSourceInfo();
        TypeLoc TL = TI->getTypeLoc();
        FunctionTypeLoc FTL = TL.getAsAdjusted<FunctionTypeLoc>();
        Diag(FTL.getLParenLoc(), diag::warn_strict_prototypes) << 2;


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