FunctionDecl question

Hello list,

is there is an easy way to distinguish if a function is declared as

void foo (void);
or
void foo ();

The reason I'm asking is that I use an ASTVisitor to generate stub
functions. But the compiler that I use reports an error when the
prototype is declared with (void) but the implementation not.

Frank

You can look at it’s type and check if it has a function prototype (i.e. it has parameters or (void), see the FunctionProtoType class), e.g. FD->getType()->isFunctionProtoType() .

Hallo Alex,

thanks for the hint. But the FD->getType()->isFunctionProtoType()
returns true in both cases and FD->getType()->isFunctionNoProtoType()
returns false in both cases.

So is there any other way to distinguish the declarations?

Frank

Is this C++ or C?

In C++, both declarations represent the same thing (a function with no arguments), so there shouldn’t be any meaningful difference in the AST.
In C the foo() means “unspecified arguments”, foo(void) means “no arguments”.

For C++, I guess you could try to find the location in the source of the parenthesis [the AST has “sourcelocation”, I think - yes, it does: https://stackoverflow.com/questions/11083066/getting-the-source-behind-clangs-ast ], and see if there is a “void” between there - of course, that could get interesting if someone does:

#define V void

void foo(V) { … }

or

#define NO_ARGS (void)

void foo NO_ARGS { … }

[and many other variations, that one could come up with, I’m sure]

but for the simple case where there are no macros involved, it should work.