diff options
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | pycparser/c_parser.py | 3 | ||||
-rw-r--r-- | tests/test_c_parser.py | 13 | ||||
-rw-r--r-- | z.py | 11 |
4 files changed, 21 insertions, 7 deletions
@@ -3,6 +3,7 @@ - Add support for C99 6.5.3.7 p7 - qualifiers within array dimensions in function declarations. Started with issue #21 (reported with initial patch by Robin Martinjak). + - Issue #28: fix coord reporting for 'for' loops. + Version 2.10 (03.08.2013) diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index 8ec3416..46551c3 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -1339,7 +1339,8 @@ class CParser(PLYParser): def p_iteration_statement_4(self, p): """ iteration_statement : FOR LPAREN declaration expression_opt SEMI expression_opt RPAREN statement """ - p[0] = c_ast.For(c_ast.DeclList(p[3]), p[4], p[6], p[8], self._coord(p.lineno(1))) + p[0] = c_ast.For(c_ast.DeclList(p[3], self._coord(p.lineno(1))), + p[4], p[6], p[8], self._coord(p.lineno(1))) def p_jump_statement_1(self, p): """ jump_statement : GOTO ID SEMI """ diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index f6bf0bd..d617ddd 100644 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -210,6 +210,19 @@ class TestCParser_fundamentals(TestCParser_base): f6 = self.parse(t6, filename='z.c') self.assert_coord(self.parse(t6).ext[0].decl.type.args.params[1], 3) + def test_forloop_coord(self): + t = '''\ + void foo() { + for(int z=0; z<4; + z++){} + } + ''' + s = self.parse(t, filename='f.c') + forloop = s.ext[0].body.block_items[0] + self.assert_coord(forloop.init, 2, 'f.c') + self.assert_coord(forloop.cond, 2, 'f.c') + self.assert_coord(forloop.next, 3, 'f.c') + def test_simple_decls(self): self.assertEqual(self.get_decl('int a;'), ['Decl', 'a', ['TypeDecl', ['IdentifierType', ['int']]]]) @@ -75,13 +75,12 @@ class NodeVisitor(object): if __name__ == "__main__": - source_code = r''' - -void f(int x[10]); + source_code = r'''void foo() { +for(int z=0; z<4; z++){ +} +} ''' parser = CParser() ast = parser.parse(source_code, filename='zz') - ast.show(showcoord=False, attrnames=True, nodenames=True) - - + ast.show(showcoord=True, attrnames=True, nodenames=True) |