Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consume function body if present after trailing return type #82

Merged
merged 1 commit into from
Nov 14, 2023
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
8 changes: 6 additions & 2 deletions cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,11 +1914,12 @@ def _parse_fn_end(self, fn: Function) -> None:
fn_template = fn_template[0]
fn_template.raw_requires_post = self._parse_requires(rtok)

if self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)

if self.lex.token_if("{"):
self._discard_contents("{", "}")
fn.has_body = True
elif self.lex.token_if("ARROW"):
self._parse_trailing_return_type(fn)

def _parse_method_end(self, method: Method) -> None:
"""
Expand Down Expand Up @@ -1963,6 +1964,9 @@ def _parse_method_end(self, method: Method) -> None:
method.ref_qualifier = tok_value
elif tok_value == "->":
self._parse_trailing_return_type(method)
if self.lex.token_if("{"):
self._discard_contents("{", "}")
method.has_body = True
break
elif tok_value == "throw":
tok = self._next_token_must_be("(")
Expand Down
64 changes: 64 additions & 0 deletions tests/test_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,3 +1194,67 @@ class C {
]
)
)


def test_fn_trailing_return_with_body() -> None:
content = """
auto test() -> void
{
}
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
functions=[
Function(
return_type=Type(
typename=PQName(segments=[FundamentalSpecifier(name="void")])
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
)
]
)
)


def test_method_trailing_return_with_body() -> None:
content = """
struct X {
auto test() -> void
{
}
};
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
classes=[
ClassScope(
class_decl=ClassDecl(
typename=PQName(
segments=[NameSpecifier(name="X")], classkey="struct"
)
),
methods=[
Method(
return_type=Type(
typename=PQName(
segments=[FundamentalSpecifier(name="void")]
)
),
name=PQName(segments=[NameSpecifier(name="test")]),
parameters=[],
has_body=True,
has_trailing_return=True,
access="public",
)
],
)
]
)
)