Rewriter: How to rewrite an empty function argument list?

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

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

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;

Thanks Alex, works!

Marcel