Skip to content

Commit

Permalink
Fix declname for _Atomic specifiers, and add c-to-c tests
Browse files Browse the repository at this point in the history
Updates eliben#430
  • Loading branch information
eliben committed Sep 14, 2021
1 parent bdcc186 commit f6db111
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pycparser/ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ def fix_atomic_specifiers(decl):
if not found:
break

# Make sure to add an _Atomic qual on the topmost decl if needed.
# Make sure to add an _Atomic qual on the topmost decl if needed. Also
# restore the declname on the innermost TypeDecl (it gets placed in the
# wrong place during construction).
typ = decl
while not isinstance(typ, c_ast.TypeDecl):
try:
Expand All @@ -129,6 +131,8 @@ def fix_atomic_specifiers(decl):
return decl
if '_Atomic' in typ.quals and '_Atomic' not in decl.quals:
decl.quals.append('_Atomic')
if typ.declname is None:
typ.declname = decl.name

return decl

Expand Down Expand Up @@ -158,4 +162,3 @@ def _fix_atomic_specifiers_once(decl):
if '_Atomic' not in node.type.quals:
node.type.quals.append('_Atomic')
return decl, True

25 changes: 25 additions & 0 deletions tests/test_c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,31 @@ def test_ptr_decl(self):
self.assertEqual(generator.visit(ast.ext[0].type.type.type),
'const int')

def test_atomic_qual(self):
#s = '_Atomic(_Atomic(int)*) x;'
#ast = parse_to_ast(s)
#print(c_generator.CGenerator().visit(ast))

self._assert_ctoc_correct('_Atomic int x;')
self._assert_ctoc_correct('_Atomic int* x;')
self._assert_ctoc_correct('int* _Atomic x;')

# _Atomic specifier gets turned into qualifier.
s1 = '_Atomic(int) x;'
c1 = self._run_c_to_c(s1)
self.assertEqual(c1, '_Atomic int x;\n')
self._assert_ctoc_correct(s1)

s2 = '_Atomic(int*) x;'
c2 = self._run_c_to_c(s2)
self.assertEqual(c2, 'int * _Atomic x;\n')
self._assert_ctoc_correct(s2)

s3 = '_Atomic(_Atomic(int)*) x;'
c3 = self._run_c_to_c(s3)
self.assertEqual(c3, '_Atomic int * _Atomic x;\n')
self._assert_ctoc_correct(s3)

def test_nested_sizeof(self):
src = '1'
for _ in range(30):
Expand Down

0 comments on commit f6db111

Please sign in to comment.