Skip to content

Commit

Permalink
cpp/apigen: Fix handling of unnamed template parameters
Browse files Browse the repository at this point in the history
Previously, unnamed template parameters were incorrectly assigned a name
of `"None"`.
  • Loading branch information
jbms committed Aug 3, 2024
1 parent ddb753c commit cbbf71f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sphinx_immaterial/apidoc/cpp/api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,10 +1006,11 @@ def get_nonitpick_directives(decl: Cursor) -> List[str]:
def _clang_template_parameter_to_json(config: Config, decl: Cursor):
param_decl_str = get_extent_spelling(decl.translation_unit, decl.extent)
param = _parse_template_parameter(param_decl_str)
spelling = decl.spelling
if param is None:
return {
"declaration": param_decl_str,
"name": decl.spelling,
"name": spelling,
"kind": TEMPLATE_PARAMETER_KIND_TO_JSON_KIND[decl.kind],
# Heuristic to determine if it is a pack.
"pack": "..." in param_decl_str,
Expand Down Expand Up @@ -1508,9 +1509,11 @@ def _sphinx_ast_template_parameter_to_json(
else:
kind = "non_type"

identifier = param.get_identifier()

return {
"declaration": _substitute_internal_type_names(config, str(param)),
"name": str(param.get_identifier()),
"name": str(identifier) if identifier else "",
"kind": cast(TemplateParameterKind, kind),
"pack": param.isPack, # type: ignore[attr-defined]
}
Expand Down
22 changes: 22 additions & 0 deletions tests/cpp_api_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,25 @@ def test_function_fields():
]
)
assert expected == doc_str


def test_unnamed_template_parameter():
config = api_parser.Config(
input_path="a.cpp",
compiler_flags=["-std=c++17", "-x", "c++"],
input_content=rb"""
/// Tests something.
///
/// \ingroup Array
template <typename = void>
constexpr inline bool IsArray = false;
""",
)

output = api_parser.generate_output(config)
entities = output.get("entities", {})
assert len(entities) == 1
entity = list(entities.values())[0]
tparams = entity["template_parameters"]
assert tparams is not None
assert tparams[0]["name"] == ""
38 changes: 38 additions & 0 deletions tests/cpp_apigen_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Tests cpp/apigen."""


def test_unnamed_template_parameter(immaterial_make_app):
app = immaterial_make_app(
extra_conf="""
extensions.append("sphinx_immaterial.apidoc.cpp.apigen")
""",
confoverrides=dict(
nitpicky=True,
cpp_apigen_configs=[
dict(
document_prefix="cpp_apigen_generated/",
api_parser_config=dict(
input_content=r"""
/// Tests if something is an array.
///
/// \ingroup Array
template <typename T, typename = void>
constexpr inline bool IsArray = false;
""",
compiler_flags=["-std=c++17", "-x", "c++"],
verbose=True,
),
),
],
),
files={
"index.rst": """
.. cpp-apigen-group:: Array
"""
},
)

app.build()

assert not app._warning.getvalue()

0 comments on commit cbbf71f

Please sign in to comment.