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

Add support for C++ using type definitions #226

Merged
merged 3 commits into from
Dec 17, 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
4 changes: 2 additions & 2 deletions src/hawkmoth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class CAutoVarDirective(_AutoSymbolDirective):

class CAutoTypeDirective(_AutoSymbolDirective):
_domain = 'c'
_docstring_types = [docstring.TypeDocstring]
_docstring_types = [docstring.TypedefDocstring]

class CAutoMacroDirective(_AutoSymbolDirective):
_domain = 'c'
Expand Down Expand Up @@ -281,7 +281,7 @@ class CppAutoVarDirective(_AutoSymbolDirective):

class CppAutoTypeDirective(_AutoSymbolDirective):
_domain = 'cpp'
_docstring_types = [docstring.TypeDocstring]
_docstring_types = [docstring.TypedefDocstring, docstring.TypeAliasDocstring]

class CppAutoMacroDirective(_AutoSymbolDirective):
_domain = 'cpp'
Expand Down
17 changes: 15 additions & 2 deletions src/hawkmoth/doccursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def decl_name(self):
CursorKind.UNION_DECL,
CursorKind.ENUM_DECL,
CursorKind.CLASS_DECL,
CursorKind.CLASS_TEMPLATE]:
CursorKind.CLASS_TEMPLATE,
CursorKind.TYPE_ALIAS_TEMPLATE_DECL]:
return self._type_definition_fixup()
else:
# self.name would recurse back here if self._cc.spelling is None
Expand Down Expand Up @@ -136,6 +137,8 @@ def value(self):
return self._cc.enum_value
else:
return None
if self._cc.kind in [CursorKind.TYPE_ALIAS_DECL, CursorKind.TYPE_ALIAS_TEMPLATE_DECL]:
return self._get_underlying_type()
else:
return None

Expand Down Expand Up @@ -458,7 +461,8 @@ def _get_template_line(self):

if self._cc.kind not in [CursorKind.CLASS_TEMPLATE,
CursorKind.FUNCTION_TEMPLATE,
CursorKind.TEMPLATE_TEMPLATE_PARAMETER]:
CursorKind.TEMPLATE_TEMPLATE_PARAMETER,
CursorKind.TYPE_ALIAS_TEMPLATE_DECL]:
return None

# The type of type parameters can be 'typename' and 'class'. These are
Expand Down Expand Up @@ -601,3 +605,12 @@ def pad(s): return s if s.endswith('*') or s.endswith('&') else s + ' '

ttype = ' '.join(type_elem)
return ttype, name

def _get_underlying_type(self):
if self._cc.kind == CursorKind.TYPE_ALIAS_DECL:
return self._cc.underlying_typedef_type
elif self._cc.kind == CursorKind.TYPE_ALIAS_TEMPLATE_DECL:
for child in self._cc.get_children():
if child.kind == CursorKind.TYPE_ALIAS_DECL:
return child.underlying_typedef_type
return None
18 changes: 17 additions & 1 deletion src/hawkmoth/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,26 @@ def _get_header_lines(self):

return header.splitlines()

class TypeDocstring(Docstring):
class TypedefDocstring(Docstring):
_indent = 1
_fmt = '.. {domain}:type:: {name}'

class TypeAliasDocstring(Docstring):
stephanlachnit marked this conversation as resolved.
Show resolved Hide resolved
_indent = 1
_fmt = '.. cpp:type:: {name} = {underlying_type}'

def __init__(self, cursor, nest):
self._underlying_type = cursor.value
super().__init__(cursor=cursor, nest=nest)

def _get_header_lines(self):
name = self._get_decl_name()
underlying_type = self._underlying_type.spelling

header = self._fmt.format(name=name, underlying_type=underlying_type)

return header.splitlines()

class _CompoundDocstring(Docstring):
def _get_decl_name(self):
# If decl_name is empty, it means this is an anonymous declaration.
Expand Down
8 changes: 7 additions & 1 deletion src/hawkmoth/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ def _recursive_parse(errors, cursor, nest):

elif cursor.kind == CursorKind.TYPEDEF_DECL:

ds = docstring.TypeDocstring(cursor=cursor, nest=nest)
ds = docstring.TypedefDocstring(cursor=cursor, nest=nest)

return [ds]

elif cursor.kind in [CursorKind.TYPE_ALIAS_DECL, CursorKind.TYPE_ALIAS_TEMPLATE_DECL]:

ds = docstring.TypeAliasDocstring(cursor=cursor, nest=nest)

return [ds]

Expand Down
5 changes: 5 additions & 0 deletions test/cpp/autotype.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.. cpp:type:: template<typename T, typename... Args> foonestalias = foovaralias<footmplalias<T>, Args...>

Nested template alias

8 changes: 8 additions & 0 deletions test/cpp/autotype.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
directives:
- domain: cpp
directive: autotype
arguments:
- foonestalias
options:
file: using-alias.cpp
expected: autotype.rst
17 changes: 17 additions & 0 deletions test/cpp/using-alias.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** Type alias */
stephanlachnit marked this conversation as resolved.
Show resolved Hide resolved
using footypealias = int;

/** Function alias */
using foofctalias = void(int, int);

/** Template alias */
template<typename T>
using footmplalias = T*;

/** Variadic template alias */
template<typename... Args>
using foovaralias = void(footypealias, Args...);

/** Nested template alias */
template<typename T, typename... Args>
using foonestalias = foovaralias<footmplalias<T>, Args...>;
25 changes: 25 additions & 0 deletions test/cpp/using-alias.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

.. cpp:type:: footypealias = int

Type alias


.. cpp:type:: foofctalias = void (int, int)

Function alias


.. cpp:type:: template<typename T> footmplalias = T *

Template alias


.. cpp:type:: template<typename... Args> foovaralias = void (footypealias, Args...)

Variadic template alias


.. cpp:type:: template<typename T, typename... Args> foonestalias = foovaralias<footmplalias<T>, Args...>

Nested template alias

6 changes: 6 additions & 0 deletions test/cpp/using-alias.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
directives:
- domain: cpp
directive: autodoc
arguments:
- using-alias.cpp
expected: using-alias.rst
2 changes: 1 addition & 1 deletion test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _filter_types(directive):
'autodoc': None,
'autosection': [docstring.TextDocstring],
'autovar': [docstring.VarDocstring],
'autotype': [docstring.TypeDocstring],
'autotype': [docstring.TypedefDocstring, docstring.TypeAliasDocstring],
'autostruct': [docstring.StructDocstring],
'autounion': [docstring.UnionDocstring],
'autoenum': [docstring.EnumDocstring],
Expand Down
Loading