parser for FuncDecl incorrectly sets declname attribute on return type
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()'
Thanks for the report. Would you like to try your hand at fixing this?
I'll try and dig at it a bit this week, but no promises. Thanks for the quick response.
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()