From 1a1bbcaa563dfaf397407f89885b13a5793b9c08 Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 11 Dec 2023 11:08:43 +0100 Subject: [PATCH 1/3] Rename TypeDocstring to TypedefDocstring Signed-off-by: Stephan Lachnit --- src/hawkmoth/__init__.py | 4 ++-- src/hawkmoth/docstring.py | 2 +- src/hawkmoth/parser.py | 2 +- test/test_parser.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hawkmoth/__init__.py b/src/hawkmoth/__init__.py index 7c6c2220..b8b862e1 100644 --- a/src/hawkmoth/__init__.py +++ b/src/hawkmoth/__init__.py @@ -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' @@ -281,7 +281,7 @@ class CppAutoVarDirective(_AutoSymbolDirective): class CppAutoTypeDirective(_AutoSymbolDirective): _domain = 'cpp' - _docstring_types = [docstring.TypeDocstring] + _docstring_types = [docstring.TypedefDocstring] class CppAutoMacroDirective(_AutoSymbolDirective): _domain = 'cpp' diff --git a/src/hawkmoth/docstring.py b/src/hawkmoth/docstring.py index 4d1c379b..eff5061e 100644 --- a/src/hawkmoth/docstring.py +++ b/src/hawkmoth/docstring.py @@ -232,7 +232,7 @@ def _get_header_lines(self): return header.splitlines() -class TypeDocstring(Docstring): +class TypedefDocstring(Docstring): _indent = 1 _fmt = '.. {domain}:type:: {name}' diff --git a/src/hawkmoth/parser.py b/src/hawkmoth/parser.py index 111a507d..2f12ac8a 100644 --- a/src/hawkmoth/parser.py +++ b/src/hawkmoth/parser.py @@ -217,7 +217,7 @@ 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] diff --git a/test/test_parser.py b/test/test_parser.py index eec1b479..8c8ee549 100755 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -27,7 +27,7 @@ def _filter_types(directive): 'autodoc': None, 'autosection': [docstring.TextDocstring], 'autovar': [docstring.VarDocstring], - 'autotype': [docstring.TypeDocstring], + 'autotype': [docstring.TypedefDocstring], 'autostruct': [docstring.StructDocstring], 'autounion': [docstring.UnionDocstring], 'autoenum': [docstring.EnumDocstring], From 44214a4ca568cfa37a9f6fed22903274cddfe179 Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 11 Dec 2023 11:12:08 +0100 Subject: [PATCH 2/3] Add support for C++ alias type definitions C++ native type definitions (aliases) use the `using` keyword and support template arguments. This commit adds support for them. Signed-off-by: Stephan Lachnit --- src/hawkmoth/__init__.py | 2 +- src/hawkmoth/doccursor.py | 17 +++++++++++++++-- src/hawkmoth/docstring.py | 16 ++++++++++++++++ src/hawkmoth/parser.py | 6 ++++++ test/test_parser.py | 2 +- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/hawkmoth/__init__.py b/src/hawkmoth/__init__.py index b8b862e1..ba07d0b5 100644 --- a/src/hawkmoth/__init__.py +++ b/src/hawkmoth/__init__.py @@ -281,7 +281,7 @@ class CppAutoVarDirective(_AutoSymbolDirective): class CppAutoTypeDirective(_AutoSymbolDirective): _domain = 'cpp' - _docstring_types = [docstring.TypedefDocstring] + _docstring_types = [docstring.TypedefDocstring, docstring.TypeAliasDocstring] class CppAutoMacroDirective(_AutoSymbolDirective): _domain = 'cpp' diff --git a/src/hawkmoth/doccursor.py b/src/hawkmoth/doccursor.py index 476dc109..4da92d77 100644 --- a/src/hawkmoth/doccursor.py +++ b/src/hawkmoth/doccursor.py @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/hawkmoth/docstring.py b/src/hawkmoth/docstring.py index eff5061e..f0eddfd3 100644 --- a/src/hawkmoth/docstring.py +++ b/src/hawkmoth/docstring.py @@ -236,6 +236,22 @@ class TypedefDocstring(Docstring): _indent = 1 _fmt = '.. {domain}:type:: {name}' +class TypeAliasDocstring(Docstring): + _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. diff --git a/src/hawkmoth/parser.py b/src/hawkmoth/parser.py index 2f12ac8a..e455158b 100644 --- a/src/hawkmoth/parser.py +++ b/src/hawkmoth/parser.py @@ -221,6 +221,12 @@ def _recursive_parse(errors, cursor, 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] + elif cursor.kind in [CursorKind.STRUCT_DECL, CursorKind.UNION_DECL, CursorKind.ENUM_DECL, diff --git a/test/test_parser.py b/test/test_parser.py index 8c8ee549..da630ea8 100755 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -27,7 +27,7 @@ def _filter_types(directive): 'autodoc': None, 'autosection': [docstring.TextDocstring], 'autovar': [docstring.VarDocstring], - 'autotype': [docstring.TypedefDocstring], + 'autotype': [docstring.TypedefDocstring, docstring.TypeAliasDocstring], 'autostruct': [docstring.StructDocstring], 'autounion': [docstring.UnionDocstring], 'autoenum': [docstring.EnumDocstring], From db250e28a40e504ad77a0030923148600003f40b Mon Sep 17 00:00:00 2001 From: Stephan Lachnit Date: Mon, 11 Dec 2023 11:40:45 +0100 Subject: [PATCH 3/3] Add tests for TypeAliasDocstring Signed-off-by: Stephan Lachnit --- test/cpp/autotype.rst | 5 +++++ test/cpp/autotype.yaml | 8 ++++++++ test/cpp/using-alias.cpp | 17 +++++++++++++++++ test/cpp/using-alias.rst | 25 +++++++++++++++++++++++++ test/cpp/using-alias.yaml | 6 ++++++ 5 files changed, 61 insertions(+) create mode 100644 test/cpp/autotype.rst create mode 100644 test/cpp/autotype.yaml create mode 100644 test/cpp/using-alias.cpp create mode 100644 test/cpp/using-alias.rst create mode 100644 test/cpp/using-alias.yaml diff --git a/test/cpp/autotype.rst b/test/cpp/autotype.rst new file mode 100644 index 00000000..9232ca57 --- /dev/null +++ b/test/cpp/autotype.rst @@ -0,0 +1,5 @@ + +.. cpp:type:: template foonestalias = foovaralias, Args...> + + Nested template alias + diff --git a/test/cpp/autotype.yaml b/test/cpp/autotype.yaml new file mode 100644 index 00000000..82e721b8 --- /dev/null +++ b/test/cpp/autotype.yaml @@ -0,0 +1,8 @@ +directives: +- domain: cpp + directive: autotype + arguments: + - foonestalias + options: + file: using-alias.cpp +expected: autotype.rst diff --git a/test/cpp/using-alias.cpp b/test/cpp/using-alias.cpp new file mode 100644 index 00000000..b2021551 --- /dev/null +++ b/test/cpp/using-alias.cpp @@ -0,0 +1,17 @@ +/** Type alias */ +using footypealias = int; + +/** Function alias */ +using foofctalias = void(int, int); + +/** Template alias */ +template +using footmplalias = T*; + +/** Variadic template alias */ +template +using foovaralias = void(footypealias, Args...); + +/** Nested template alias */ +template +using foonestalias = foovaralias, Args...>; diff --git a/test/cpp/using-alias.rst b/test/cpp/using-alias.rst new file mode 100644 index 00000000..19f4e165 --- /dev/null +++ b/test/cpp/using-alias.rst @@ -0,0 +1,25 @@ + +.. cpp:type:: footypealias = int + + Type alias + + +.. cpp:type:: foofctalias = void (int, int) + + Function alias + + +.. cpp:type:: template footmplalias = T * + + Template alias + + +.. cpp:type:: template foovaralias = void (footypealias, Args...) + + Variadic template alias + + +.. cpp:type:: template foonestalias = foovaralias, Args...> + + Nested template alias + diff --git a/test/cpp/using-alias.yaml b/test/cpp/using-alias.yaml new file mode 100644 index 00000000..67921269 --- /dev/null +++ b/test/cpp/using-alias.yaml @@ -0,0 +1,6 @@ +directives: +- domain: cpp + directive: autodoc + arguments: + - using-alias.cpp +expected: using-alias.rst