Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array attributes #78

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pycparserext/ext_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,11 @@ def p_xxx_declarator_1(self, p):
if p[2] or p[3].exprs:
if isinstance(p[1], (c_ast.ArrayDecl, c_ast.FuncDecl)):
decl_ext = to_decl_ext(p[1].type)
p[1].type = decl_ext

elif isinstance(p[1], c_ast.TypeDecl):
decl_ext = to_decl_ext(p[1])
p[1] = decl_ext

else:
raise NotImplementedError(
Expand All @@ -407,8 +409,6 @@ def p_xxx_declarator_1(self, p):
if p[3].exprs:
decl_ext.attributes = p[3]

p[1] = decl_ext

p[0] = p[1]

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
Expand All @@ -430,9 +430,11 @@ def p_xxx_declarator_2(self, p):
if asm_label or attr_decl.exprs:
if isinstance(decl, (c_ast.ArrayDecl, c_ast.FuncDecl)):
decl_ext = to_decl_ext(decl.type)
decl.type = decl_ext

elif isinstance(decl, c_ast.TypeDecl):
decl_ext = to_decl_ext(decl)
decl = decl_ext

else:
raise NotImplementedError(
Expand All @@ -445,8 +447,6 @@ def p_xxx_declarator_2(self, p):
if attr_decl.exprs:
decl_ext.attributes = attr_decl

p[1] = decl_ext

p[0] = self._type_modify_decl(decl, p[1])

@parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
Expand Down
17 changes: 17 additions & 0 deletions test/test_pycparserext.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ def test_array_ptr_decl_attribute():
print(GnuCGenerator().visit(ast))


def test_array_attribute():
src = """
int arr[2] __attribute__((deprecated));
"""

from pycparser.c_ast import ArrayDecl
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()

assert isinstance(ast.ext[0].type, ArrayDecl)

from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be reasonable to assert that the round trip works?



def test_gnu_statement_expression():
src = """
int func(int a) {
Expand Down