Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser for FuncDecl incorrectly sets declname attribute on return type #99

Closed
overdamped opened this issue Sep 25, 2015 · 3 comments
Closed

Comments

@overdamped
Copy link

Steps to reproduce in the examples folder running python interpreter.

>>> from pycparser import parse_file, c_generator
>>> from pycparser.c_ast import *
>>> ast = parse_file("c_files/memmgr.h", use_cpp=True)
>>> cg = c_generator.CGenerator()
>>> cg.visit(Cast(ast.ext[-3].type.type, FuncCall(ID('test_fun'), ExprList([]))))
'(void *memmgr_alloc) test_fun()'
>>> #workaround
... ast.ext[-3].type.type.type.declname = ''
>>> cg.visit(Cast(ast.ext[-3].type.type, FuncCall(ID('test_fun'), ExprList([]))))
'(void *) test_fun()'

@overdamped overdamped changed the title FuncDecl return TypeDecl incorrectly sets declname attribute parser for FuncDecl incorrectly sets declname attribute on return type Sep 25, 2015
@eliben eliben added the bug label Sep 26, 2015
@eliben
Copy link
Owner

eliben commented Sep 26, 2015

Thanks for the report. Would you like to try your hand at fixing this?

@overdamped
Copy link
Author

I'll try and dig at it a bit this week, but no promises. Thanks for the quick response.

@Juzley
Copy link
Contributor

Juzley commented Apr 21, 2017

Is this definitely a bug? Generally TypeDecls associated with declarators have the identifier's name associated with them in the declname, and the CGenerator uses that. Casts use abstract declarators, which is why taking the return type off the function and putting it straight into a cast doesn't work.

I think the correct way to achieve what you're trying to do is to create a new TypeDecl representing an abstract declarator (i.e. doesn't have a declname) for your cast, something like:

func_type = ast.ext[-3].type.type
cast_type = PtrDecl(func_type.quals, TypeDecl(None, func_type.type.quals, func_type.type.type))
cg.visit(Cast(cast_type, FuncCall(ID('test_fun'), ExprList([]))))

This gives the expected output: (void *) test_fun()

saullocarvalho added a commit to saullocarvalho/pycparser that referenced this issue Jun 7, 2019
eliben pushed a commit that referenced this issue Jun 27, 2019
@eliben eliben closed this as completed Jun 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants