From 2b94490cc76192fbd5d3721a83d2ae61876a98fb Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 25 Oct 2026 08:33:16 -0500 Subject: [PATCH] Fix for pycparser 3.12 Assisted-by: Zed:gpt-6.1-sol --- .github/workflows/ci.yml | 2 +- pycparserext/ext_c_generator.py | 10 +++++-- pycparserext/ext_c_lexer.py | 15 ++++++++-- pycparserext/ext_c_parser.py | 29 +++++++++++++++---- pyproject.toml | 4 +-- test/test_pycparserext.py | 49 +++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3e2df5..f35627b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.10', '3.12', '3.x'] + python-version: ['3.11', '3.13', '3.x'] steps: - uses: actions/checkout@v7 - diff --git a/pycparserext/ext_c_generator.py b/pycparserext/ext_c_generator.py index 427ccf0..5fee613 100644 --- a/pycparserext/ext_c_generator.py +++ b/pycparserext/ext_c_generator.py @@ -75,7 +75,8 @@ def _generate_type(self, n, modifiers=None, emit_declname=True): nstr += "[" + dim_quals + self.visit(modifier.dim) + "]" - elif isinstance(modifier, c_ast.FuncDecl): + elif (isinstance(modifier, c_ast.FuncDecl) + and not isinstance(modifier, FuncDeclExt)): if i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl): nstr = "(" + nstr + ")" nstr += "(" + self.visit(modifier.args) + ")" @@ -104,8 +105,11 @@ def _generate_type(self, n, modifiers=None, emit_declname=True): if hasattr(n, "asm") and n.asm: nstr += self.visit(n.asm) - if hasattr(n, "attributes") and n.attributes.exprs: - nstr += " __attribute__((" + self.visit(n.attributes) + "))" + if isinstance(n, TypeDeclExt): + if n.attributes.exprs: + nstr += " __attribute__((" + self.visit(n.attributes) + "))" + elif getattr(n, "attributes", None): + nstr += " " + self._generate_attributes(n.attributes) if nstr: s += " " + nstr diff --git a/pycparserext/ext_c_lexer.py b/pycparserext/ext_c_lexer.py index dc8d942..7117a7a 100644 --- a/pycparserext/ext_c_lexer.py +++ b/pycparserext/ext_c_lexer.py @@ -46,7 +46,18 @@ } -class GnuCLexer(CLexerBase): +class _AsmLexer(CLexerBase): + def _match_token(self): + # Keep adjacent asm operand separators separate, even with lexers + # that recognize C23's :: attribute namespace separator. + if self._lexdata[self._pos:self._pos + 2] == "::": + tok = self._make_token("COLON", ":", self._pos) + self._pos += 1 + return tok + return super()._match_token() + + +class GnuCLexer(_AsmLexer): """GNU C lexer that recognizes GNU-specific keywords.""" _extra_keywords = {**_COMMON_EXTRA_KEYWORDS, **_GNU_EXTRA_KEYWORDS} # ruff:ignore[mutable-class-default] @@ -68,7 +79,7 @@ def __init__(self, *args, **kwargs): GnuCLexer.__init__(self, *args, **kwargs) -class OpenCLCLexer(CLexerBase): +class OpenCLCLexer(_AsmLexer): """OpenCL C lexer that recognizes OpenCL-specific keywords and line comments.""" diff --git a/pycparserext/ext_c_parser.py b/pycparserext/ext_c_parser.py index 0a16d7a..91d486a 100644 --- a/pycparserext/ext_c_parser.py +++ b/pycparserext/ext_c_parser.py @@ -257,7 +257,7 @@ def to_decl_ext(d): raise TypeError("unexpected decl type: %s" % type(d).__name__) -class FuncDeclExt(c_ast.Node): +class FuncDeclExt(c_ast.FuncDecl): def __init__(self, args, type, attributes, asm, coord=None): self.args = args self.type = type @@ -317,6 +317,25 @@ def _parse_translation_unit_or_empty(self): del self._pending_initial_type_symbols return super()._parse_translation_unit_or_empty() + def _add_declaration_specifier(self, declspec, newspec, kind, append=True): + # Unlike pycparser, our function specifiers can be AttributeSpecifier + # nodes. Keep this extension's layout independent of upstream's helper, + # which no longer accepts append or non-string function specifiers. + if declspec is None: + declspec = { + "qual": [], + "storage": [], + "type": [], + "function": [], + "alignment": [], + "type_attributes": [], + } + if append: + declspec[kind].append(newspec) + else: + declspec[kind].insert(0, newspec) + return declspec + def _parse_attribute_list(self): exprs = [self._parse_attribute()] while self._accept("COMMA"): @@ -528,8 +547,7 @@ def _parse_declarator_kind(self, kind, allow_paren): decl_ext = to_decl_ext(innermost_decl) if asm_label: decl_ext.asm = asm_label - if attrs.exprs: - decl_ext.attributes = attrs + decl_ext.attributes = attrs if innermost_decl is direct: direct = decl_ext @@ -1092,9 +1110,10 @@ def _parse_designator(self): expr = self._parse_constant_expression() if self._accept("ELLIPSIS"): last = self._parse_constant_expression() - self._expect("RBRACKET") - return RangeExpression(expr, last, coord=self._tok_coord(lbrack_tok)) + expr = RangeExpression(expr, last, coord=self._tok_coord(lbrack_tok)) self._expect("RBRACKET") + if hasattr(c_ast, "ArrayDesignator"): + return c_ast.ArrayDesignator(expr, self._tok_coord(lbrack_tok)) return expr if self._accept("PERIOD"): return self._parse_identifier_or_typeid() diff --git a/pyproject.toml b/pyproject.toml index c7779db..5772c52 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ version = "2026.1" description = "Extensions for pycparser" readme = "README.rst" license = "MIT" -requires-python = "~=3.10" +requires-python = "~=3.11" authors = [ { name = "Andreas Kloeckner", email = "inform@tiker.net" }, ] @@ -23,7 +23,7 @@ classifiers = [ "Topic :: Utilities", ] dependencies = [ - "pycparser>=3.0", + "pycparser>=3.12", ] [project.urls] diff --git a/test/test_pycparserext.py b/test/test_pycparserext.py index 5e4811e..483f34d 100644 --- a/test/test_pycparserext.py +++ b/test/test_pycparserext.py @@ -762,6 +762,55 @@ def test_attribute_in_struct(): assert _round_trip_matches(src) +@pytest.mark.parametrize("dialect", ["GnuC", "OpenCLC"]) +@pytest.mark.parametrize("suffix", [";", " {}"]) +def test_function_declaration_type(dialect, suffix): + from pycparser import c_ast + + from pycparserext import ext_c_generator, ext_c_parser + + parser = getattr(ext_c_parser, dialect + "Parser")() + generator = getattr(ext_c_generator, dialect + "Generator")() + ast = parser.parse("unsigned long f(void)" + suffix) + decl = ast.ext[0] + if isinstance(decl, c_ast.FuncDef): + decl = decl.decl + + assert decl.name == "f" + assert isinstance(decl.type, c_ast.FuncDecl) + assert decl.type.type.type.names == ["unsigned", "long"] + generated = generator.visit(ast) + assert "unsigned long f(void)" in generated + assert _compare_asts(ast, parser.parse(generated)) + + +@pytest.mark.parametrize("dialect", ["GnuC", "OpenCLC"]) +def test_adjacent_asm_separators(dialect): + from pycparserext import ext_c_generator, ext_c_parser + + parser = getattr(ext_c_parser, dialect + "Parser")() + generator = getattr(ext_c_generator, dialect + "Generator")() + ast = parser.parse('void f(void) { asm("" ::: "memory"); }') + asm = ast.ext[0].body.block_items[0] + assert asm.output_operands is None + assert asm.input_operands is None + assert asm.clobbered_regs.exprs[0].value == '"memory"' + assert _compare_asts(ast, parser.parse(generator.visit(ast))) + + +def test_array_designator_generation(): + src = "int a[] = { [0 ... 9] = 1, [10] = 2 };" + from pycparserext.ext_c_generator import GnuCGenerator + from pycparserext.ext_c_parser import GnuCParser + + parser = GnuCParser() + ast = parser.parse(src) + generated = GnuCGenerator().visit(ast) + assert "[0 ... 9] = 1" in generated + assert "[10] = 2" in generated + assert _compare_asts(ast, parser.parse(generated)) + + if __name__ == "__main__": import sys if len(sys.argv) > 1: