Skip to content

Commit

Permalink
Add support for C++ namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Lachnit <[email protected]>
  • Loading branch information
stephanlachnit committed Feb 12, 2024
1 parent 067a5cf commit 73668d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/hawkmoth/doccursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
)


def _get_semantic_parent_namespace(cursor, namespace):
semantic_parent = cursor.semantic_parent
if not semantic_parent:
return namespace

if semantic_parent.kind == CursorKind.NAMESPACE:
# parent is a namespace => add namespace in front
if namespace is not None:
namespace = f'{semantic_parent.spelling}::{namespace}'
else:
namespace = semantic_parent.spelling
# check again for nested namespaces
return _get_semantic_parent_namespace(semantic_parent, namespace)

return namespace


class DocCursor:
"""Documentation centric wrapper for Clang's own ``Cursor``.
Expand Down Expand Up @@ -66,7 +83,7 @@ def kind(self):

@property
def name(self):
return self._cc.spelling if self._cc.spelling else self.decl_name
return self.namespace_prefix + self._cc.spelling if self._cc.spelling else self.decl_name

@property
def decl_name(self):
Expand All @@ -81,7 +98,14 @@ def decl_name(self):
return self._type_definition_fixup()
else:
# self.name would recurse back here if self._cc.spelling is None
return self._cc.spelling
return self.namespace_prefix + self._cc.spelling if self._cc.spelling else None

@property
def namespace_prefix(self):
if self.domain != 'cpp':
return ''
namespace = _get_semantic_parent_namespace(self._cc, None)
return f'{namespace}::' if namespace else ''

@property
def type(self):
Expand Down Expand Up @@ -270,7 +294,7 @@ def _type_definition_fixup(self):
template = self._get_template_line()
template = template + ' ' if template else ''

return f'{template}{self._cc.spelling}{colon_suffix}'
return f'{template}{self.namespace_prefix}{self._cc.spelling}{colon_suffix}'

def _get_macro_args(self):
"""Get macro arguments.
Expand Down
11 changes: 11 additions & 0 deletions src/hawkmoth/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,17 @@ def _parse_undocumented_block(errors, cursor, nest):
if c.comment:
ret.extend(_recursive_parse(errors, c, nest))

elif cursor.kind == CursorKind.NAMESPACE:
# ignore internal STL namespaces
if cursor.name in ['std', '__gnu_cxx', '__cxxabiv1', '__gnu_debug']:
return ret
# iterate over namespace
for c in cursor.get_children():
if c.comment:
ret.extend(_recursive_parse(errors, c, nest))
else:
ret.extend(_parse_undocumented_block(errors, c, nest))

return ret

def _language_option(filename, domain):
Expand Down

0 comments on commit 73668d0

Please sign in to comment.