Skip to content

Commit

Permalink
Fix typedef keyword being passed to sphinx
Browse files Browse the repository at this point in the history
For the c type directive sphinx will throw a warning if the `typedef`
keyword is provided as part of the type's signature.  This also results
in losing the ability to link to the type in html documentation.  This
has now been fixed.
  • Loading branch information
speedyleion committed Oct 24, 2020
1 parent 4211a01 commit d482df5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Added
Fixes
-----

* No longer passing `typedef` to autodocumentation of types.
Previously the `typedef` keyword was being provided to sphinx as part of the
signature. This resulted in losing the ability to link to the type in html
documentation.

* Display of function arguments with unknown array types.
Previously when a function had an argument that was an array and an unknown type, it
would result in being empty in the output documentation. Now the full function
Expand Down
1 change: 1 addition & 0 deletions docs/example_c_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Begin Autodocumented C File

.. autocmodule:: example.c
:members:
:private-members:
2 changes: 1 addition & 1 deletion docs/napoleon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Using this extension will take:

.. literalinclude:: ../tests/assets/c_source/example.c
:language: c
:lines: 70-92
:lines: 73-95

and convert it into

Expand Down
11 changes: 11 additions & 0 deletions src/sphinx_c_autodoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ def consolidate_members(self) -> StringList:

return self.directive.result

def format_name(self) -> str:
"""Format the name of *self.object*.
Sphinx doesn't like the typedef keyword being in typedef signatures so strip
them off here.
"""
raw_name = self.object.format_name()

cleaned_name = raw_name.replace("typedef ", "")
return cleaned_name


class CStructDocumenter(CTypeDocumenter):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/assets/c_source/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
#define MY_COOL_MACRO(_a, _b) \
some_func((_a))

/**
* A plain old typedef
*/
typedef int a_typedef_type;

/**
* Structures can be documented.
*
Expand Down
5 changes: 0 additions & 5 deletions tests/directives/test_autocstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ class TestAutoCStruct:
my_struct_type foo
"""

typedefed_struct = """\
typedef intermediate_type typedefed_struct
A typedef of a struct after the fact."""

documented_members = """\
struct documented_members
A struct with documented members
Expand Down Expand Up @@ -75,7 +71,6 @@ class TestAutoCStruct:
doc_data = [
("types.c::my_struct_type", my_struct_type),
("types.c::some_struct", some_struct),
("types.c::typedefed_struct", typedefed_struct),
("types.c::documented_members", documented_members),
("types.c::nested_struct", nested_struct),
]
Expand Down
9 changes: 7 additions & 2 deletions tests/directives/test_autoctype.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ class TestAutoCType:
# Similar to the unknown members of the union clang just falls back to
# int's
function_pointer_type = """\
typedef int (int *what)
int (int *what)
A function pointer type with unknown return type"""

wrapped_function_pointer = """\
typedef int (*wrapped_function_pointer)(const int *, const float)
typedef int (*wrapped_function_pointer)(const int*, const float)
A function pointer wrapped on multiple lines."""

char_array = """\
typedef char char_array[10]
A char array typedef"""

typedefed_struct = """\
typedef intermediate_type typedefed_struct
A typedef of a struct after the fact."""

doc_data = [
("types.c::my_int", my_int),
("types.c::undocumented", undocumented),
("types.c::unknown_return_type", function_type),
("types.c::what", function_pointer_type),
("types.c::wrapped_function_pointer", wrapped_function_pointer),
("types.c::char_array", char_array),
("types.c::typedefed_struct", typedefed_struct),
]

@pytest.mark.parametrize("type_, expected_doc", doc_data)
Expand Down

0 comments on commit d482df5

Please sign in to comment.