inducer / pycparserext

Extensions for Eli Bendersky's pycparser
http://pypi.python.org/pypi/pycparserext
Other
83 stars 28 forks source link

Support __attribute__ within struct declaration #75

Open nxmaintainer opened 1 year ago

nxmaintainer commented 1 year ago
    typedef struct {
        __attribute__((__deprecated__)) int test1;
    } test2;

fails with plyparser.ParseError: :3:9: before: __attribute__

PLY: PARSE DEBUG START
State  : 0
Stack  : . LexToken(TYPEDEF,'typedef',2,5)
Action : Shift and goto state 65
State  : 65
Stack  : TYPEDEF . LexToken(STRUCT,'struct',2,13)
Action : Reduce rule [storage_class_specifier -> TYPEDEF] with ['typedef'] and goto state 22
Result : <str @ 0x7f027d5ccab0> ('typedef')
State  : 22
Stack  : storage_class_specifier . LexToken(STRUCT,'struct',2,13)
Action : Reduce rule [empty -> <empty>] with [] and goto state 121
Result : <NoneType @ 0x7f02812c6280> (None)
State  : 121
Stack  : storage_class_specifier empty . LexToken(STRUCT,'struct',2,13)
Action : Reduce rule [declaration_specifiers_no_type_opt -> empty] with [None] and goto state 124
Result : <NoneType @ 0x7f02812c6280> (None)
State  : 124
Stack  : storage_class_specifier declaration_specifiers_no_type_opt . LexToken(STRUCT,'struct',2,13)
Action : Reduce rule [declaration_specifiers_no_type -> storage_class_specifier declaration_specifiers_no_type_opt] with ['typedef',None] and goto state 26
Result : <dict @ 0x7f027d837500> ({'qual': [], 'storage': ['typedef'], 'ty ...)
State  : 26
Stack  : declaration_specifiers_no_type . LexToken(STRUCT,'struct',2,13)
Action : Shift and goto state 73
State  : 73
Stack  : declaration_specifiers_no_type STRUCT . LexToken(LBRACE,'{',2,20)
Action : Reduce rule [struct_or_union -> STRUCT] with ['struct'] and goto state 39
Result : <str @ 0x7f027d5cd7d0> ('struct')
State  : 39
Stack  : declaration_specifiers_no_type struct_or_union . LexToken(LBRACE,'{',2,20)
Action : Shift and goto state 145
State  : 145
Stack  : declaration_specifiers_no_type struct_or_union LBRACE . LexToken(__ATTRIBUTE__,'__attribute__',3,30)
Action : Reduce rule [brace_open -> LBRACE] with ['{'] and goto state 148
Result : <str @ 0x7f02813fc070> ('{')
State  : 148
Stack  : declaration_specifiers_no_type struct_or_union brace_open . LexToken(__ATTRIBUTE__,'__attribute__',3,30)
ERROR: Error  : declaration_specifiers_no_type struct_or_union brace_open . LexToken(__ATTRIBUTE__,'__attribute__',3,30)

I'm trying to fix, but don't understand why function_specifier doesn't hit here, @inducer if you'd have a moment to take a quick look and point me into a right direction, I'd provide a PR asap.

nxmaintainer commented 1 year ago

Ok, I don't think the problem is related to struct at all, my bad.

typedef struct 
{
  int test1 __attribute__((__deprecated__));
} test2;

works correctly (both CParser and CGenerator), which means the problem related only to prefixing with __attribute__ and it seems reasonable due to xxx_declarator : direct_xxx_declarator asm_label_opt attributes_opt rule, where attributes_opt can be defined only after. Ok, I approximately understand how to fix, will try.