Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C++ apigen page naming for variable template specializations #396

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sphinx_immaterial/apidoc/cpp/api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _make_replacement_pattern(
)

SPECIAL_GROUP_COMMAND_PATTERN = re.compile(
r"^(?:\\|@)(ingroup|relates|membergroup|id)\s+(.+[^\s])\s*$", re.MULTILINE
r"^(?:\\|@)(ingroup|relates|membergroup|id)\s+(.*[^\s])\s*$", re.MULTILINE
)


Expand Down Expand Up @@ -2206,7 +2206,9 @@ def _format_template_arguments(entity: CppApiEntity) -> str:

def _get_entity_base_page_name_component(entity: CppApiEntity) -> str:
base_name = entity["name"]
if entity["kind"] == "class" and entity.get("specializes"):
if (entity["kind"] == "class" or entity["kind"] == "var") and entity.get(
"specializes"
):
# Strip any template arguments
base_name = re.sub("([^<]*).*", r"\1", base_name)
elif entity["kind"] == "conversion_function":
Expand Down
43 changes: 41 additions & 2 deletions tests/cpp_api_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_basic():
config = api_parser.Config(
input_path="a.cpp",
input_content=b"""
input_content=rb"""

/// This is the doc.
int foo(bool x, int y);
Expand All @@ -17,6 +17,7 @@ def test_basic():
)

output = api_parser.generate_output(config)
assert not output.get("errors")
entities = list(output["entities"].values())
assert len(entities) == 1

Expand Down Expand Up @@ -45,6 +46,7 @@ class DimensionIdentifier {
)

output = api_parser.generate_output(config)
assert not output.get("errors")
print(output)
entities = list(output["entities"].values())
assert len(entities) == 2
Expand All @@ -62,6 +64,8 @@ def test_enable_if_transform():
}

/// This is the doc.
///
/// \ingroup X
template <typename U, typename T, typename = std::enable_if_t<std::is_convertible_v<U(*)[], T(*)[]>>>
int foo(T x);

Expand All @@ -76,7 +80,8 @@ def test_enable_if_transform():
)

output = api_parser.generate_output(config)

assert not output.get("errors")
assert not output.get("warnings")
entities = list(output["entities"].values())
assert len(entities) == 1
requires = entities[0].get("requires")
Expand Down Expand Up @@ -170,6 +175,7 @@ def test_comment_styles(doc_str: bytes, expected: str):
input_content=doc_str,
)
output = api_parser.generate_output(config)
assert not output.get("errors")
doc_strings = [
cast(api_parser.JsonDocComment, v["doc"])["text"]
for v in output.get("entities", {}).values()
Expand Down Expand Up @@ -198,6 +204,7 @@ def test_function_fields():
)

output = api_parser.generate_output(config)
assert not output.get("errors")
entities = output.get("entities", {})
doc_str = ""
for entity in entities.values():
Expand Down Expand Up @@ -238,9 +245,41 @@ def test_unnamed_template_parameter():
)

output = api_parser.generate_output(config)
assert not output.get("errors")
assert not output.get("warnings")
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"] == ""


def test_variable_template_specialization():
config = api_parser.Config(
input_path="a.cpp",
compiler_flags=["-std=c++17", "-x", "c++"],
input_content=rb"""
/// Check if it has A.
///
/// \ingroup Array
template <typename T>
constexpr inline bool HasA = false;

/// Specializes HasA for int.
/// \ingroup Array
/// \id int
template <>
constexpr inline bool HasA<int> = true;
""",
)

output = api_parser.generate_output(config)
assert not output.get("errors")
assert not output.get("warnings")
entities = output.get("entities", {})
assert len(entities) == 2
assert sorted([entity["page_name"] for entity in entities.values()]) == [
"HasA",
"HasA-int",
]