Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
11 changes: 10 additions & 1 deletion sphinx_immaterial/apidoc/cpp/api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,20 +940,28 @@ def get_extent_spelling(translation_unit: TranslationUnit, extent: SourceRange)
because this is intended to be parsed by the Sphinx cpp domain anyway.
"""

add_space_between = (TokenKind.KEYWORD, TokenKind.IDENTIFIER, TokenKind.LITERAL)
def get_spellings():
prev_token = None
COMMENT = TokenKind.COMMENT
for token in translation_unit.get_tokens(extent=extent):
if prev_token is not None:
yield prev_token.spelling
if (
prev_token.kind in add_space_between
and token.kind in add_space_between
):
yield " "
prev_token = None
if token.kind == COMMENT:
yield " "
continue
prev_token = token
# We need to handle the last token specially, because clang sometimes parses
# ">>" as a single token but the extent may cover only the first of the two
# angle brackets.
if prev_token is not None:
yield " "
spelling = prev_token.spelling
token_end = cast(SourceLocation, prev_token.extent.end)
offset_diff = token_end.offset - cast(SourceLocation, extent.end).offset
Expand All @@ -962,7 +970,7 @@ def get_spellings():
else:
yield spelling

return " ".join(get_spellings())
return "".join(get_spellings())


def get_related_comments(decl: Cursor):
Expand Down Expand Up @@ -2344,6 +2352,7 @@ def _normalize_entity_requires(entity: CppApiEntity):
if _is_function(entity):
func_entity = cast(FunctionEntity, entity)
declaration = func_entity["declaration"]
declaration = _substitute_internal_type_names(config, declaration)
if replacements:
declaration = _apply_identifier_replacements(declaration, replacements)
if (
Expand Down
42 changes: 42 additions & 0 deletions tests/cpp_api_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,45 @@ def test_variable_template_specialization():
"HasA",
"HasA-int",
]


def test_type_replacements():
config = api_parser.Config(
input_path="a.cpp",
input_content=rb"""
struct source_location {};

namespace foo {
struct SourceLocation {};
}

/// Default method
foo::SourceLocation Default();

/// Logging method
void LogSourceLocation(foo::SourceLocation loc = Default());

/// Class
class ClassWithSourceLocation {
public:
/// Constructor.
ClassWithSourceLocation(foo::SourceLocation loc = Default())
: loc_(loc) {}

foo::SourceLocation loc_;
};
""",
type_replacements={
"foo::SourceLocation": "source_location",
},
)

output = api_parser.generate_output(config)
assert not output.get("errors")
print(output)

assert len(output.get("entities", {}).values()) == 4
for x in output["entities"].values():
d = x.get("declaration")
if d:
assert cast(str, d).find("source_location") != -1
Loading