Skip to content

Commit 3b84e9e

Browse files
authored
Support __restrict keyword in parser (#143)
* Support __restrict keyword in parser * Format code with black
1 parent 5334b31 commit 3b84e9e

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

cxxheaderparser/lexer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class PlyLexer:
125125
"reinterpret_cast",
126126
"requires",
127127
"__restrict__",
128+
"__restrict",
128129
"restrict",
129130
"return",
130131
"short",

cxxheaderparser/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ def _parse_cv_ptr_or_fn(
24322432

24332433
while True:
24342434
tok = self.lex.token_if(
2435-
"*", "const", "volatile", "__restrict__", "restrict", "("
2435+
"*", "const", "volatile", "__restrict__", "__restrict", "restrict", "("
24362436
)
24372437
if not tok:
24382438
break
@@ -2449,7 +2449,7 @@ def _parse_cv_ptr_or_fn(
24492449
if not isinstance(dtype, (Pointer, Type)):
24502450
raise self._parse_error(tok)
24512451
dtype.volatile = True
2452-
elif tok.type in ("__restrict__", "restrict"):
2452+
elif tok.type in ("__restrict__", "__restrict", "restrict"):
24532453
if not isinstance(dtype, (Pointer, Reference)):
24542454
raise self._parse_error(tok)
24552455
dtype.restrict = True
@@ -2524,7 +2524,7 @@ def _parse_cv_ptr_or_fn(
25242524

25252525
# peek at the next token and see if it's a paren. If so, it might
25262526
# be a nasty function pointer
2527-
if self.lex.token_peek_if("(", "__restrict__", "restrict"):
2527+
if self.lex.token_peek_if("(", "__restrict__", "__restrict", "restrict"):
25282528
dtype = self._parse_cv_ptr_or_fn(dtype, nonptr_fn)
25292529

25302530
return dtype

tests/test_fn.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def test_fn_pointer_params() -> None:
141141
int fn3(int(*p));
142142
int fn4(int* __restrict__ p);
143143
int fn5(int& __restrict__ p);
144+
int fn6(int* __restrict p);
144145
"""
145146
data = parse_string(content, cleandoc=True)
146147

@@ -238,6 +239,25 @@ def test_fn_pointer_params() -> None:
238239
)
239240
],
240241
),
242+
Function(
243+
return_type=Type(
244+
typename=PQName(segments=[FundamentalSpecifier(name="int")])
245+
),
246+
name=PQName(segments=[NameSpecifier(name="fn6")]),
247+
parameters=[
248+
Parameter(
249+
name="p",
250+
type=Pointer(
251+
ptr_to=Type(
252+
typename=PQName(
253+
segments=[FundamentalSpecifier(name="int")]
254+
)
255+
),
256+
restrict=True,
257+
),
258+
)
259+
],
260+
),
241261
]
242262
)
243263
)

0 commit comments

Comments
 (0)