From 01e7fcf7b2c1e31556ee3e7d4207a3a17b741390 Mon Sep 17 00:00:00 2001 From: "Kyle D. Kavanagh" Date: Tue, 24 Jun 2025 18:38:04 -0500 Subject: [PATCH] Fix trailing return parsing with pure virtual functions. Fixes #114 --- cxxheaderparser/parser.py | 2 +- tests/test_fn.py | 53 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/cxxheaderparser/parser.py b/cxxheaderparser/parser.py index 45f7fb6..7cea0ea 100644 --- a/cxxheaderparser/parser.py +++ b/cxxheaderparser/parser.py @@ -1987,7 +1987,7 @@ def _parse_method_end(self, method: Method) -> None: if self.lex.token_if("{"): self._discard_contents("{", "}") method.has_body = True - break + break elif tok_value == "throw": tok = self._next_token_must_be("(") method.throw = self._create_value(self._consume_balanced_tokens(tok)) diff --git a/tests/test_fn.py b/tests/test_fn.py index 34734d6..d261032 100644 --- a/tests/test_fn.py +++ b/tests/test_fn.py @@ -1196,6 +1196,59 @@ class C { ) +def test_pure_virtual_trailing_return() -> None: + content = """ + class C { + public: + int x; + virtual auto GetSelected() -> decltype(x) = 0; + }; + """ + data = parse_string(content, cleandoc=True) + + assert data == ParsedData( + namespace=NamespaceScope( + classes=[ + ClassScope( + class_decl=ClassDecl( + typename=PQName( + segments=[NameSpecifier(name="C")], classkey="class" + ) + ), + fields=[ + Field( + access="public", + type=Type( + typename=PQName( + segments=[FundamentalSpecifier(name="int")] + ) + ), + name="x", + ) + ], + methods=[ + Method( + return_type=Type( + typename=PQName( + segments=[ + DecltypeSpecifier(tokens=[Token(value="x")]) + ] + ) + ), + name=PQName(segments=[NameSpecifier(name="GetSelected")]), + parameters=[], + has_trailing_return=True, + pure_virtual=True, + virtual=True, + access="public", + ) + ], + ) + ] + ) + ) + + def test_fn_trailing_return_with_body() -> None: content = """ auto test() -> void