Skip to content

Commit

Permalink
Fix multi-pragma/single statement blocks (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldore committed Nov 8, 2022
1 parent b13a06d commit 723cb68
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
12 changes: 9 additions & 3 deletions pycparser/c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ def p_pppragma_directive(self, p):
else:
p[0] = c_ast.Pragma("", self._token_coord(p, 1))

def p_pppragma_directive_list(self, p):
""" pppragma_directive_list : pppragma_directive
| pppragma_directive_list pppragma_directive
"""
p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]

# In function definitions, the declarator can be followed by
# a declaration list, for old "K&R style" function definitios.
def p_function_definition_1(self, p):
Expand Down Expand Up @@ -671,12 +677,12 @@ def p_statement(self, p):
# sum += 1;
# }
def p_pragmacomp_or_statement(self, p):
""" pragmacomp_or_statement : pppragma_directive statement
""" pragmacomp_or_statement : pppragma_directive_list statement
| statement
"""
if isinstance(p[1], c_ast.Pragma) and len(p) == 3:
if len(p) == 3:
p[0] = c_ast.Compound(
block_items=[p[1], p[2]],
block_items=p[1]+[p[2]],
coord=self._token_coord(p, 1))
else:
p[0] = p[1]
Expand Down
6 changes: 5 additions & 1 deletion tests/test_c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,14 @@ def test_pragma(self):
void f() {
#pragma bar
i = (a, b, c);
if (d)
#pragma qux
#pragma quux
j = e;
}
typedef struct s {
#pragma baz
} s;
} s;
''')

def test_compound_literal(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ def test_pragmacomp_or_statement(self):
if (sum > 10)
#pragma bar
#pragma baz
sum = 10;
switch (sum)
Expand All @@ -1800,7 +1801,8 @@ def test_pragmacomp_or_statement(self):
self.assertIsInstance(s1_ast.ext[0].body.block_items[4], If)
self.assertIsInstance(s1_ast.ext[0].body.block_items[4].iftrue, Compound)
self.assertIsInstance(s1_ast.ext[0].body.block_items[4].iftrue.block_items[0], Pragma)
self.assertIsInstance(s1_ast.ext[0].body.block_items[4].iftrue.block_items[1], Assignment)
self.assertIsInstance(s1_ast.ext[0].body.block_items[4].iftrue.block_items[1], Pragma)
self.assertIsInstance(s1_ast.ext[0].body.block_items[4].iftrue.block_items[2], Assignment)
self.assertIsInstance(s1_ast.ext[0].body.block_items[5], Switch)
self.assertIsInstance(s1_ast.ext[0].body.block_items[5].stmt.stmts[0], Compound)
self.assertIsInstance(s1_ast.ext[0].body.block_items[5].stmt.stmts[0].block_items[0],
Expand Down

0 comments on commit 723cb68

Please sign in to comment.