From c830da88193cff670180b57eee95712fe1f01b07 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sun, 10 May 2015 07:56:41 -0700 Subject: Adding support for empty initializer lists. The idea comes from #79 but the implementation is somewhat different. --- CHANGES | 8 ++++++-- pycparser/c_parser.py | 8 ++++++-- tests/test_c_parser.py | 3 +++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 71f4ecf..aa12591 100644 --- a/CHANGES +++ b/CHANGES @@ -1,7 +1,11 @@ + Version 2.13 (??) - - Adding support for offsetof() - - Adding faked va_* macros (these are expected to come from stdarg.h) + - Added support for offsetof() the way gcc implements it (special builtin + that takes a type as an argument). + - Added faked va_* macros (these are expected to come from stdarg.h) + - Added a bunch more fake headers and typedefs to support parsing C projects + like Git and SQLite without modifications to pycparser. + - Added support for empty initializer lists (#79). + Version 2.12 (21.04.2015) diff --git a/pycparser/c_parser.py b/pycparser/c_parser.py index e4fa503..11405ed 100644 --- a/pycparser/c_parser.py +++ b/pycparser/c_parser.py @@ -85,6 +85,7 @@ class CParser(PLYParser): 'expression', 'identifier_list', 'init_declarator_list', + 'initializer_list', 'parameter_type_list', 'specifier_qualifier_list', 'block_item_list', @@ -1166,10 +1167,13 @@ class CParser(PLYParser): p[0] = p[1] def p_initializer_2(self, p): - """ initializer : brace_open initializer_list brace_close + """ initializer : brace_open initializer_list_opt brace_close | brace_open initializer_list COMMA brace_close """ - p[0] = p[2] + if p[2] is None: + p[0] = c_ast.InitList([], self._coord(p.lineno(1))) + else: + p[0] = p[2] def p_initializer_list(self, p): """ initializer_list : designation_opt initializer diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py index ea624d9..c1bf5be 100755 --- a/tests/test_c_parser.py +++ b/tests/test_c_parser.py @@ -1096,6 +1096,9 @@ class TestCParser_fundamentals(TestCParser_base): ['Constant', 'int', '8'], ['Constant', 'int', '9']]) + d21 = 'long ar[4] = {};' + self.assertEqual(self.get_decl_init(d21), []) + d3 = 'char p = j;' self.assertEqual(self.get_decl(d3), ['Decl', 'p', ['TypeDecl', ['IdentifierType', ['char']]]]) -- cgit v1.2.1