Skip to content

Commit 1f66113

Browse files
committed
Fix comments in function declarations showing up in documentation.
When comments were placed inbetween parameter types and the parameter names, and the type was unknown to clang, the fallback parsing would take the declaration character for character (consolidating whitespace). This resulted in comments being pulled in to the documentation verbatim. Now comments will explicitly be skipped over when the fall back parsing for function declarations happens.
1 parent 1e7dc6d commit 1f66113

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

CHANGELOG.rst

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ This project adheres to `Semantic Versioning <https://semver.org/>`_.
99
`v0.3.0-dev`_ (unreleased)
1010
==========================
1111

12+
Fixes
13+
-----
14+
15+
* Fix comments in function declarations showing up in documentation. When
16+
comments were placed inbetween parameter types and the parameter names, and
17+
the type was unknown to clang, the fallback parsing would take the
18+
declaration character for character (consolidating whitespace). This
19+
resulted in comments being pulled in to the documentation verbatim. Now
20+
comments will explicitly be skipped over when the fall back parsing for
21+
function declarations happens.
22+
1223
`v0.2.0`_ (2020-04-04)
1324
==========================
1425

@@ -17,7 +28,7 @@ Added
1728

1829
* Viewcode functionality which allows for listing the source C files and
1930
providing links between the documentation and the C source listings.
20-
* :private-members: and :no-private-members: option for the autocmodule
31+
* `:private-members:` and `:no-private-members:` option for the autocmodule
2132
directive. This option set allows for controlling the documentation of
2233
constructs based on what is visible outside of the module. For header
2334
files this means everything will still be documented. For standard source
@@ -28,20 +39,20 @@ Added
2839
Fixes
2940
-----
3041

31-
* Anonymous enumerations which were contained in a typedef were being documented twice.
32-
Once as the typedef and once as anonymous. Now they are only documnted as
33-
part of the typedef.
42+
* Anonymous enumerations which were contained in a typedef were being documented twice.
43+
Once as the typedef and once as anonymous. Now they are only documnted as
44+
part of the typedef.
3445

3546
`v0.1.1`_ (2020-03-15)
3647
======================
3748

3849
Fixes
3950
-----
4051

41-
* C module is not resolved relative to the document root,
42-
`#1 <https://github.com/speedyleion/sphinx-c-autodoc/issues/1>`_.
43-
* C module can not be specified in a sub directory,
44-
`#2 <https://github.com/speedyleion/sphinx-c-autodoc/issues/2>`_.
52+
* C module is not resolved relative to the document root,
53+
`#1 <https://github.com/speedyleion/sphinx-c-autodoc/issues/1>`_.
54+
* C module can not be specified in a sub directory,
55+
`#2 <https://github.com/speedyleion/sphinx-c-autodoc/issues/2>`_.
4556

4657
`v0.1.0`_ (2020-03-07)
4758
======================

src/sphinx_c_autodoc/loader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ def get_parsed_declaration(self) -> str:
459459
func = self.node
460460
args = []
461461
for arg in func.get_arguments():
462-
args.append(" ".join(t.spelling for t in arg.get_tokens()))
462+
non_comment_tokens = (
463+
t for t in arg.get_tokens() if t.kind != cindex.TokenKind.COMMENT
464+
)
465+
args.append(" ".join(t.spelling for t in non_comment_tokens))
463466

464467
tu = func.tu
465468

tests/assets/c_source_2/nested/functions.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,15 @@ void doxy_documented_parameters_no_returns(int water, int air)
7878
* first_param: A parameter to document
7979
* second_param: Why not
8080
*/
81-
void * custom_napoleon_section(char first_param, int second_param);
81+
void * custom_napoleon_section(char first_param, int second_param);
82+
83+
/**
84+
* A function with comment inside of parameter declaration.
85+
*
86+
* Parameters:
87+
* my_char_ptr: Pointer to my character, probably actually an array
88+
* or string like representation.
89+
*/
90+
void * function_with_comment_in_parameter(
91+
const unknown_type * /* A comment in the middle of parameter line */
92+
my_char_ptr );

tests/directives/test_autocfunction.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ class TestAutoCFunction:
7171
FUNCTION_LIKE_MACRO_x, _y
7272
A function like macro with 2 parameters"""
7373

74+
function_with_comment_in_parameter = """\
75+
void * function_with_comment_in_parameterconst unknown_type *\xa0my_char_ptr
76+
A function with comment inside of parameter declaration.
77+
Parameters
78+
my_char_ptr -- Pointer to my character, probably actually an array
79+
or string like representation."""
80+
7481
doc_data = [
7582
("functions.c::single_line_function_comment", single_line_comment),
7683
("functions.c::return_value_function", return_value_function),
@@ -83,6 +90,10 @@ class TestAutoCFunction:
8390
),
8491
("functions.c::undocumented_function", undocumented_function),
8592
("macros.c::FUNCTION_LIKE_MACRO", function_like_macro),
93+
(
94+
"functions.c::function_with_comment_in_parameter",
95+
function_with_comment_in_parameter,
96+
),
8697
]
8798

8899
@pytest.mark.parametrize("function, expected_doc", doc_data)

0 commit comments

Comments
 (0)