FunctionDecl question

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

FunctionDecl question

Sumner, Brian via cfe-dev
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
_______________________________________________
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: FunctionDecl question

Sumner, Brian via cfe-dev
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() .

On 18 July 2017 at 10:33, Frank Redeker via cfe-dev <[hidden email]> wrote:
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
_______________________________________________
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: FunctionDecl question

Sumner, Brian via cfe-dev
> On 18 July 2017 at 10:33, Frank Redeker via cfe-dev
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     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.
>

> Am 18.07.2017 um 11:41 schrieb Alex L:
> 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
_______________________________________________
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: FunctionDecl question

Sumner, Brian via cfe-dev
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.

--
Mats

On 18 July 2017 at 13:19, Frank Redeker via cfe-dev <[hidden email]> wrote:
> On 18 July 2017 at 10:33, Frank Redeker via cfe-dev
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     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.
>

> Am 18.07.2017 um 11:41 schrieb Alex L:
> 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
_______________________________________________
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
Loading...