Skip to content

Commit

Permalink
Merge pull request #88 from robotpy/more-using
Browse files Browse the repository at this point in the history
Retain doxygen comments for using declarations and type aliases
  • Loading branch information
virtuald committed Dec 2, 2023
2 parents f1708bf + 73a81d3 commit 04ba4bf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
17 changes: 11 additions & 6 deletions cxxheaderparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,9 @@ def _parse_using_directive(self, state: NonClassBlockState) -> None:

self.visitor.on_using_namespace(state, names)

def _parse_using_declaration(self, tok: LexToken) -> None:
def _parse_using_declaration(
self, tok: LexToken, doxygen: typing.Optional[str]
) -> None:
"""
using_declaration: "using" ["typename"] ["::"] nested_name_specifier unqualified_id ";"
| "using" "::" unqualified_id ";"
Expand All @@ -1004,12 +1006,15 @@ def _parse_using_declaration(self, tok: LexToken) -> None:
typename, _ = self._parse_pqname(
tok, fn_ok=True, compound_ok=True, fund_ok=True
)
decl = UsingDecl(typename, self._current_access)
decl = UsingDecl(typename, self._current_access, doxygen)

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

def _parse_using_typealias(
self, id_tok: LexToken, template: typing.Optional[TemplateDecl]
self,
id_tok: LexToken,
template: typing.Optional[TemplateDecl],
doxygen: typing.Optional[str],
) -> None:
"""
alias_declaration: "using" IDENTIFIER "=" type_id ";"
Expand All @@ -1023,7 +1028,7 @@ def _parse_using_typealias(

dtype = self._parse_cv_ptr(parsed_type)

alias = UsingAlias(id_tok.value, dtype, template, self._current_access)
alias = UsingAlias(id_tok.value, dtype, template, self._current_access, doxygen)

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

Expand Down Expand Up @@ -1052,9 +1057,9 @@ def _parse_using(
raise CxxParseError(
"unexpected using-declaration when parsing alias-declaration", tok
)
self._parse_using_declaration(tok)
self._parse_using_declaration(tok, doxygen)
else:
self._parse_using_typealias(tok, template)
self._parse_using_typealias(tok, template, doxygen)

# All using things end with a semicolon
self._next_token_must_be(";")
Expand Down
6 changes: 6 additions & 0 deletions cxxheaderparser/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,9 @@ class UsingDecl:
#: If within a class, the access level for this decl
access: typing.Optional[str] = None

#: Documentation if present
doxygen: typing.Optional[str] = None


@dataclass
class UsingAlias:
Expand All @@ -886,3 +889,6 @@ class UsingAlias:

#: If within a class, the access level for this decl
access: typing.Optional[str] = None

#: Documentation if present
doxygen: typing.Optional[str] = None
51 changes: 51 additions & 0 deletions tests/test_doxygen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Type,
Typedef,
UsingDecl,
UsingAlias,
Value,
Variable,
)
Expand Down Expand Up @@ -436,3 +437,53 @@ def test_doxygen_attribute() -> None:
]
)
)


def test_doxygen_using_decl() -> None:
content = """
// clang-format off
/// Comment
using ns::ClassName;
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
using=[
UsingDecl(
typename=PQName(
segments=[
NameSpecifier(name="ns"),
NameSpecifier(name="ClassName"),
]
),
doxygen="/// Comment",
)
]
)
)


def test_doxygen_using_alias() -> None:
content = """
// clang-format off
/// Comment
using alias = sometype;
"""
data = parse_string(content, cleandoc=True)

assert data == ParsedData(
namespace=NamespaceScope(
using_alias=[
UsingAlias(
alias="alias",
type=Type(
typename=PQName(segments=[NameSpecifier(name="sometype")])
),
doxygen="/// Comment",
)
]
)
)

0 comments on commit 04ba4bf

Please sign in to comment.