Skip to content

Conversation

@retif
Copy link
Collaborator

@retif retif commented Nov 8, 2025

This commit fixes issue #75 where attribute specifiers within struct declarations were not parsed correctly, leading to a ParseError.

Example code that now parses correctly:

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

Changes:

  • Extended specifier_qualifier_list rule in GnuCParser to handle function_specifier nodes, allowing attribute within structs
  • Added _p_specifier_qualifier_list_left_recursion method to correctly handle AttributeSpecifier nodes in the funcspec list
  • Modified _generate_decl in GnuCGenerator to handle funcspec list with AttributeSpecifier nodes
  • Improved AttributeSpecifier.eq to use structural comparison of AST nodes instead of object identity
  • Added round-trip test to verify the fix

This commit fixes issue inducer#75 where __attribute__ specifiers within struct
declarations were not parsed correctly, leading to a ParseError.

Example code that now parses correctly:
    typedef struct {
        __attribute__((__deprecated__)) int test1;
    } test2;

Changes:
- Extended specifier_qualifier_list rule in GnuCParser to handle
  function_specifier nodes, allowing __attribute__ within structs
- Added _p_specifier_qualifier_list_left_recursion method to correctly
  handle AttributeSpecifier nodes in the funcspec list
- Modified _generate_decl in GnuCGenerator to handle funcspec list with
  AttributeSpecifier nodes
- Improved AttributeSpecifier.__eq__ to use structural comparison of
  AST nodes instead of object identity
- Added round-trip test to verify the fix

Fixes inducer#75
@inducer inducer force-pushed the issue-75-attribute-in-struct branch from d26ec9a to e1067e5 Compare November 10, 2025 18:28
@inducer inducer merged commit e38e2c8 into inducer:main Nov 10, 2025
4 checks passed
@inducer
Copy link
Owner

inducer commented Nov 10, 2025

Thanks!

@inducer
Copy link
Owner

inducer commented Nov 10, 2025

@retif Want to be co-maintainer? (Sent you an invite)

@retif
Copy link
Collaborator Author

retif commented Nov 10, 2025

@retif Want to be co-maintainer? (Sent you an invite)

Sure, thanks, accepted the invite

@retif retif deleted the issue-75-attribute-in-struct branch November 10, 2025 20:08
@simonlindholm
Copy link

simonlindholm commented Nov 16, 2025

DeclExt seems to be unused?

Edit: also confused about why AttributeSpecifier should have a __eq__ when all other AST nodes are compared by object identity.

@retif
Copy link
Collaborator Author

retif commented Nov 18, 2025

@simonlindholm thank you, removed the unused function.

Answering your question, its because:

from pycparserext.ext_c_parser import GnuCParser

parser = GnuCParser()
src = '__attribute__((unused)) int foo;'

decl_1 = parser.parse(src).ext[0]
decl_2 = parser.parse(src).ext[0]

print(decl_1.funcspec)             # [AttributeSpecifier(...)]
print(decl_1.funcspec[0] == decl_2.funcspec[0])  # False without the custom __eq__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants