Skip to content

Commit

Permalink
Allow abstract declarator "static" (GH issue #539) (#545)
Browse files Browse the repository at this point in the history
This is similar to:
    allow "static" in array parameters (GH issue #21)
    aac7b27
which was revised shortly after in:
    Fuller support for qualifiers in array dimensions.
    8aad318

The grammar is as defined in C99 6.7.6 Type names, or
A.2.2 Declarations (6.7.6).

Fixes #539
  • Loading branch information
gperciva authored Jun 23, 2024
1 parent 7847544 commit ab00af8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,20 @@ def p_direct_abstract_declarator_7(self, p):
type=c_ast.TypeDecl(None, None, None, None),
coord=self._token_coord(p, 1))

def p_direct_abstract_declarator_8(self, p):
""" direct_abstract_declarator : LBRACKET STATIC type_qualifier_list_opt assignment_expression RBRACKET
| LBRACKET type_qualifier_list STATIC assignment_expression RBRACKET
"""
listed_quals = [item if isinstance(item, list) else [item]
for item in [p[2],p[3]]]
quals = [qual for sublist in listed_quals for qual in sublist
if qual is not None]
p[0] = c_ast.ArrayDecl(
type=c_ast.TypeDecl(None, None, None, None),
dim=p[4],
dim_quals=quals,
coord=self._token_coord(p, 1))

# declaration is a list, statement isn't. To make it consistent, block_item
# will always be a list
#
Expand Down
15 changes: 15 additions & 0 deletions tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,21 @@ def test_func_decls_with_array_dim_qualifiers(self):
[['Decl', 'p', ['ArrayDecl', '10', ['static'],
['TypeDecl', ['IdentifierType', ['int']]]]]],
['TypeDecl', ['IdentifierType', ['int']]]]])
# anonymous function parameter
self.assertEqual(self.get_decl('int zz(int [static 10]);'),
['Decl', 'zz',
['FuncDecl',
[['Typename',
['ArrayDecl', '10', ['static'],
['TypeDecl', ['IdentifierType', ['int']]]]]],
['TypeDecl', ['IdentifierType', ['int']]]]])
self.assertEqual(self.get_decl('int zz(int [static const restrict 10]);'),
['Decl', 'zz',
['FuncDecl',
[['Typename',
['ArrayDecl', '10', ['static', 'const', 'restrict'],
['TypeDecl', ['IdentifierType', ['int']]]]]],
['TypeDecl', ['IdentifierType', ['int']]]]])

self.assertEqual(self.get_decl('int zz(int p[const 10]);'),
['Decl', 'zz',
Expand Down

0 comments on commit ab00af8

Please sign in to comment.