Skip to content

Commit 3415f50

Browse files
authored
Fix enums using assignment operators (#175)
Previously the enumerator parsing logic utilized the same logic as the macro parsing logic. These are actually disparate logic. Now the enumerator logic uses the common base logic, with dedicated handling for the format_name() to only return the enumerator's name.
1 parent af6ab9e commit 3415f50

File tree

3 files changed

+92
-50
lines changed

3 files changed

+92
-50
lines changed

src/sphinx_c_autodoc/loader.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,21 @@ def get_parsed_declaration(self) -> str:
348348
return f"{self.name}({', '.join(tokens)})"
349349

350350

351-
class DocumentedEnumerator(DocumentedMacro):
351+
class DocumentedEnumerator(DocumentedObject):
352352
"""
353353
An enumerator, the constant values in an enum
354354
"""
355355

356356
type_ = "enumerator"
357357

358+
def format_name(self) -> str:
359+
"""
360+
The name of the object.
361+
362+
For things like functions and others this will include the return type.
363+
"""
364+
return self.name
365+
358366

359367
class DocumentedMember(DocumentedObject):
360368
"""

tests/assets/c_source/issue_174.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test case example of the issue that was reported in
2+
// https://github.com/speedyleion/sphinx-c-autodoc/issues/174
3+
4+
/**
5+
* My Define
6+
*/
7+
#define MY_BITMASK 0x8000U
8+
9+
/**
10+
* My Define mask
11+
*/
12+
#define MY_MASKED( base_enum ) ((base_enum) | MY_BITMASK )
13+
14+
/**
15+
* My enum
16+
*/
17+
typedef enum {
18+
VALUE_0, /**< my value 0*/
19+
VALUE_1, /**< my value 1*/
20+
VALUE_2 = MY_MASKED( VALUE_0 ), /**< my value 2*/
21+
VALUE_3 = MY_MASKED( VALUE_1 ), /**< my value 3*/
22+
} my_value_e;

tests/directives/test_autocenum.py

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,68 @@
77

88
from sphinx.ext.autodoc.directive import AutodocDirective
99

10+
some_enum = """\
11+
enum some_enum
12+
If you want to document the enumerators with napoleon
13+
then you use the section title Enumerators:.
1014
11-
class TestAutoCEnum:
15+
enumerator THE_FIRST_ENUM
16+
Used for the first item
17+
Documentation in a comment for THE_FIRST_ITEM. Note this is trailing, for some reason clang will apply leading comments to all the enumerators
18+
19+
enumerator THE_SECOND_ENUM
20+
Second verse same as the first.
21+
22+
enumerator THE_THIRD_ENUM
23+
Not once, note twice, but thrice.
24+
25+
enumerator THE_LAST_ENUM
26+
Just to be sure."""
27+
28+
my_value_e = """\
29+
enum my_value_e
30+
My enum
31+
32+
enumerator VALUE_0
33+
my value 0
34+
35+
enumerator VALUE_1
36+
my value 1
37+
38+
enumerator VALUE_2
39+
my value 2
40+
41+
enumerator VALUE_3
42+
my value 3"""
43+
44+
doc_data = [
45+
("example.c::some_enum", some_enum),
46+
("issue_174.c::my_value_e", my_value_e),
47+
]
48+
49+
50+
@pytest.mark.parametrize("enum, expected_doc", doc_data)
51+
def test_doc(enum, expected_doc, sphinx_state):
1252
"""
13-
Testing class for the autocenum directive for use in enums
53+
Tests the restructured text output returned by the directive.
1454
"""
55+
directive = AutodocDirective(
56+
"autocenum",
57+
[enum],
58+
{"members": None},
59+
None,
60+
None,
61+
None,
62+
None,
63+
sphinx_state,
64+
None,
65+
)
66+
output = directive.run()
67+
68+
# First item is the index entry
69+
assert 2 == len(output)
70+
body = output[1]
1571

16-
some_enum = """\
17-
enum some_enum
18-
If you want to document the enumerators with napoleon
19-
then you use the section title Enumerators:.
20-
21-
enumerator THE_FIRST_ENUM
22-
Used for the first item
23-
Documentation in a comment for THE_FIRST_ITEM. Note this is trailing, for some reason clang will apply leading comments to all the enumerators
24-
25-
enumerator THE_SECOND_ENUM
26-
Second verse same as the first.
27-
28-
enumerator THE_THIRD_ENUM
29-
Not once, note twice, but thrice.
30-
31-
enumerator THE_LAST_ENUM
32-
Just to be sure."""
33-
34-
doc_data = [
35-
("example.c::some_enum", some_enum),
36-
]
37-
38-
@pytest.mark.parametrize("enum, expected_doc", doc_data)
39-
def test_doc(self, enum, expected_doc, sphinx_state):
40-
"""
41-
Tests the restructured text output returned by the directive.
42-
"""
43-
directive = AutodocDirective(
44-
"autocenum",
45-
[enum],
46-
{"members": None},
47-
None,
48-
None,
49-
None,
50-
None,
51-
sphinx_state,
52-
None,
53-
)
54-
output = directive.run()
55-
56-
# First item is the index entry
57-
assert 2 == len(output)
58-
body = output[1]
59-
60-
# For whatever reason the as text comes back with double spacing, so we
61-
# knock it down to single spacing to make the expected string smaller.
62-
assert body.astext().replace("\n\n", "\n") == dedent(expected_doc)
72+
# For whatever reason the as text comes back with double spacing, so we
73+
# knock it down to single spacing to make the expected string smaller.
74+
assert body.astext().replace("\n\n", "\n") == dedent(expected_doc)

0 commit comments

Comments
 (0)