Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down
10 changes: 7 additions & 3 deletions pycparserext/ext_c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions pycparserext/ext_c_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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."""

Expand Down
29 changes: 24 additions & 5 deletions pycparserext/ext_c_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
]
Expand All @@ -23,7 +23,7 @@ classifiers = [
"Topic :: Utilities",
]
dependencies = [
"pycparser>=3.0",
"pycparser>=3.12",
]

[project.urls]
Expand Down
49 changes: 49 additions & 0 deletions test/test_pycparserext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading