Skip to content

Commit 73a81d3

Browse files
committed
Retain doxygen comments for using declarations and type aliases
1 parent f1708bf commit 73a81d3

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

cxxheaderparser/parser.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,9 @@ def _parse_using_directive(self, state: NonClassBlockState) -> None:
992992

993993
self.visitor.on_using_namespace(state, names)
994994

995-
def _parse_using_declaration(self, tok: LexToken) -> None:
995+
def _parse_using_declaration(
996+
self, tok: LexToken, doxygen: typing.Optional[str]
997+
) -> None:
996998
"""
997999
using_declaration: "using" ["typename"] ["::"] nested_name_specifier unqualified_id ";"
9981000
| "using" "::" unqualified_id ";"
@@ -1004,12 +1006,15 @@ def _parse_using_declaration(self, tok: LexToken) -> None:
10041006
typename, _ = self._parse_pqname(
10051007
tok, fn_ok=True, compound_ok=True, fund_ok=True
10061008
)
1007-
decl = UsingDecl(typename, self._current_access)
1009+
decl = UsingDecl(typename, self._current_access, doxygen)
10081010

10091011
self.visitor.on_using_declaration(self.state, decl)
10101012

10111013
def _parse_using_typealias(
1012-
self, id_tok: LexToken, template: typing.Optional[TemplateDecl]
1014+
self,
1015+
id_tok: LexToken,
1016+
template: typing.Optional[TemplateDecl],
1017+
doxygen: typing.Optional[str],
10131018
) -> None:
10141019
"""
10151020
alias_declaration: "using" IDENTIFIER "=" type_id ";"
@@ -1023,7 +1028,7 @@ def _parse_using_typealias(
10231028

10241029
dtype = self._parse_cv_ptr(parsed_type)
10251030

1026-
alias = UsingAlias(id_tok.value, dtype, template, self._current_access)
1031+
alias = UsingAlias(id_tok.value, dtype, template, self._current_access, doxygen)
10271032

10281033
self.visitor.on_using_alias(self.state, alias)
10291034

@@ -1052,9 +1057,9 @@ def _parse_using(
10521057
raise CxxParseError(
10531058
"unexpected using-declaration when parsing alias-declaration", tok
10541059
)
1055-
self._parse_using_declaration(tok)
1060+
self._parse_using_declaration(tok, doxygen)
10561061
else:
1057-
self._parse_using_typealias(tok, template)
1062+
self._parse_using_typealias(tok, template, doxygen)
10581063

10591064
# All using things end with a semicolon
10601065
self._next_token_must_be(";")

cxxheaderparser/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,9 @@ class UsingDecl:
866866
#: If within a class, the access level for this decl
867867
access: typing.Optional[str] = None
868868

869+
#: Documentation if present
870+
doxygen: typing.Optional[str] = None
871+
869872

870873
@dataclass
871874
class UsingAlias:
@@ -886,3 +889,6 @@ class UsingAlias:
886889

887890
#: If within a class, the access level for this decl
888891
access: typing.Optional[str] = None
892+
893+
#: Documentation if present
894+
doxygen: typing.Optional[str] = None

tests/test_doxygen.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Type,
2727
Typedef,
2828
UsingDecl,
29+
UsingAlias,
2930
Value,
3031
Variable,
3132
)
@@ -436,3 +437,53 @@ def test_doxygen_attribute() -> None:
436437
]
437438
)
438439
)
440+
441+
442+
def test_doxygen_using_decl() -> None:
443+
content = """
444+
// clang-format off
445+
446+
/// Comment
447+
using ns::ClassName;
448+
"""
449+
data = parse_string(content, cleandoc=True)
450+
451+
assert data == ParsedData(
452+
namespace=NamespaceScope(
453+
using=[
454+
UsingDecl(
455+
typename=PQName(
456+
segments=[
457+
NameSpecifier(name="ns"),
458+
NameSpecifier(name="ClassName"),
459+
]
460+
),
461+
doxygen="/// Comment",
462+
)
463+
]
464+
)
465+
)
466+
467+
468+
def test_doxygen_using_alias() -> None:
469+
content = """
470+
// clang-format off
471+
472+
/// Comment
473+
using alias = sometype;
474+
"""
475+
data = parse_string(content, cleandoc=True)
476+
477+
assert data == ParsedData(
478+
namespace=NamespaceScope(
479+
using_alias=[
480+
UsingAlias(
481+
alias="alias",
482+
type=Type(
483+
typename=PQName(segments=[NameSpecifier(name="sometype")])
484+
),
485+
doxygen="/// Comment",
486+
)
487+
]
488+
)
489+
)

0 commit comments

Comments
 (0)