Skip to content

Commit 8fe580a

Browse files
added __alignof__ gnu extension support (#84)
* added __alignof__ gnu extension support * fix for linter * feat: Add __alignof__ support to generator and round-trip test This commit adds support for the `__alignof__` GNU extension to the C code generator. It also includes a round-trip test to verify that the generator correctly handles the `__alignof__` keyword. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 10ded02 commit 8fe580a

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

pycparserext/ext_c_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def visit_UnaryOp(self, n):
1616
# Always parenthesize the argument of sizeof since it can be
1717
# a name.
1818
return "sizeof(%s)" % self.visit(n.expr)
19+
elif n.op == "__alignof__":
20+
return "__alignof__(%s)" % self.visit(n.expr)
1921
else:
2022
# avoid merging of "- - x" or "__real__varname"
2123
return "%s %s" % (n.op, operand)

pycparserext/ext_c_lexer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def add_lexer_keywords(cls, keywords):
7474
"__restrict__", "__restrict",
7575
"__inline__", "__inline",
7676
"__extension__",
77-
"__volatile", "__volatile__"]
77+
"__volatile", "__volatile__",
78+
"__alignof__"]
7879

7980
add_lexer_keywords(GnuCLexer, _COMMON_KEYWORDS + _GNU_KEYWORDS)
8081

pycparserext/ext_c_parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,14 @@ def p_gnu_primary_expression_6(self, p):
549549
""" primary_expression : gnu_statement_expression """
550550
p[0] = p[1]
551551

552+
def p_gnu_unary_expression(self, p):
553+
""" unary_expression : __ALIGNOF__ LPAREN type_name RPAREN
554+
"""
555+
p[0] = c_ast.UnaryOp(
556+
p[1],
557+
p[2] if len(p) == 3 else p[3],
558+
self._token_coord(p, 1))
559+
552560
def p_statement(self, p):
553561
""" statement : labeled_statement
554562
| expression_statement

test/test_pycparserext.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ def visit_FuncDeclExt(self, node):
571571
assert visit_num[0] == visit_num[1], assert_msg
572572

573573

574+
def test_alignof():
575+
src = "int x = __alignof__(int);"
576+
assert _round_trip_matches(src)
577+
578+
574579
def test_typeof_reproduction():
575580
src = """
576581
int func(int a, int b) {

0 commit comments

Comments
 (0)