diff options
author | ldore <laurent.dore@gmail.com> | 2022-11-10 14:30:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 05:30:04 -0800 |
commit | f5ca0284e2c1c9ac2a5be45735735f2f287073cc (patch) | |
tree | 5dc4a50b7b426b720109542545290fec113934c3 | |
parent | b13a06d22d05d47f115f3ed141f4fa727c654c6d (diff) | |
download | pycparser-f5ca0284e2c1c9ac2a5be45735735f2f287073cc.tar.gz |
Fix #479 (multi-pragma/single statement blocks) (#480)
-rw-r--r-- | pycparser/c_parser.py | 12 | ||||
-rw-r--r-- | tests/test_c_generator.py | 9 | ||||
-rwxr-xr-x | tests/test_c_parser.py | 4 |
3 files changed, 20 insertions, 5 deletions
diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index 640a759..c9782e0 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -580,6 +580,12 @@ class CParser(PLYParser): 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): @@ -671,12 +677,12 @@ class CParser(PLYParser): # 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] diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py index d166d03..c9d5961 100644 --- a/tests/test_c_generator.py +++ b/tests/test_c_generator.py @@ -339,10 +339,17 @@ class TestCtoC(unittest.TestCase): void f() { #pragma bar i = (a, b, c); + if (d) + #pragma qux + j = e; + if (d) + #pragma qux + #pragma quux + j = e; } typedef struct s { #pragma baz - } s; + } s; ''') def test_compound_literal(self): diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index 4a5c90b..9b53622 100755 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -1776,6 +1776,7 @@ class TestCParser_fundamentals(TestCParser_base): if (sum > 10) #pragma bar + #pragma baz sum = 10; switch (sum) @@ -1800,7 +1801,8 @@ class TestCParser_fundamentals(TestCParser_base): 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], |