pycparser icon indicating copy to clipboard operation
pycparser copied to clipboard

parser for FuncDecl incorrectly sets declname attribute on return type

Open overdamped opened this issue 11 years ago • 3 comments

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 avatar Sep 25 '15 20:09 overdamped

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

eliben avatar Sep 26 '15 13:09 eliben

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

overdamped avatar Sep 28 '15 15:09 overdamped

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

Juzley avatar Apr 21 '17 10:04 Juzley