paul-j-lucas / cdecl

Composing and deciphering C (or C++) declarations or casts, aka ‘‘gibberish.’’
GNU General Public License v3.0
90 stars 11 forks source link

cdecl fails to round-trip "pointer to function returning pointer to array" in function parameter #14

Closed blipvert closed 3 years ago

blipvert commented 3 years ago

Consider the following:

cdecl> declare foo as pointer to function returning pointer to array of char
char (*(*foo)())[];
cdecl> explain char (*(*foo)())[]
declare foo as pointer to function returning pointer to array of char

So far, so good. Now as function parameter:

cdecl> declare foo as function (pointer to function returning pointer to array of char) returning int
int foo(char (*(*)())[]);

Also good; but when fed back to cdecl an error occurs:

cdecl> explain int foo(char (*(*)())[])
                                    ^
30: error: function returning array; did you mean function returning pointer?
paul-j-lucas commented 3 years ago

The bug is either in the array_cast_c_astp grammar rule (or a rule reduced as a parameter to that rule) -- OR -- in the c_ast_add_array() function. I haven't figured out which yet.

One difference is that when it's a stand-alone declaration (not a function argument), the array_decl_c_astp rule has a non-NULL target_ast; but in the array_cast_c_astp case, target_ast is NULL. Maybe that's it.

paul-j-lucas commented 3 years ago

It turns out that the grammar rules pointer_cast_c_ast, pointer_to_member_cast_c_ast, and reference_cast_c_ast were too cavalier about not copying target_ast across to the result. Said rules are now *_astp (AST pair) rules that copy the target_ast across.

Anyway, I pushed a fix. Let me know.

By the way, I appreciate that you're finding these bugs, but how is it that you are trying out such complicated declarations? Do you have existing code that has such declarations? Or are you intentionally torture testing cdecl? Just curious.

paul-j-lucas commented 3 years ago

I've not heard back and, AFAICT, it's fixed, so closing.