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

Cleanup C# doc comment generation #3366

Merged
merged 3 commits into from
Jan 16, 2025
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
39 changes: 36 additions & 3 deletions cpp/src/Slice/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,8 @@ namespace
}
}

StringList splitComment(string comment, function<string(string, string)> linkFormatter, bool stripMarkup)
StringList
splitComment(string comment, function<string(string, string)> linkFormatter, bool stripMarkup, bool xmlEscape)
{
string::size_type pos = 0;

Expand All @@ -634,6 +635,37 @@ namespace
comment.erase(pos, endpos - pos + 1);
}
} while (pos != string::npos);

// Escape XML entities.
if (xmlEscape)
{
const string amp = "&amp;";
const string lt = "&lt;";
const string gt = "&gt;";

pos = 0;
while ((pos = comment.find_first_of("&<>", pos)) != string::npos)
{
switch (comment[pos])
{
case '&':
comment.replace(pos, 1, amp);
pos += amp.size();
break;
case '<':
comment.replace(pos, 1, lt);
pos += lt.size();
break;
case '>':
comment.replace(pos, 1, gt);
pos += gt.size();
break;
default:
assert(false);
break;
}
}
}
}

// Fix any link tags using the provided link formatter.
Expand Down Expand Up @@ -729,14 +761,15 @@ namespace
}

DocCommentPtr
Slice::Contained::parseDocComment(function<string(string, string)> linkFormatter, bool stripMarkup) const
Slice::Contained::parseDocComment(function<string(string, string)> linkFormatter, bool stripMarkup, bool xmlEscape)
const
{
// Some tags are only valid if they're applied to an operation.
// If they aren't, we want to ignore the tag and issue a warning.
bool isOperation = dynamic_cast<const Operation*>(this);

// Split the comment's raw text up into lines.
StringList lines = splitComment(_docComment, std::move(linkFormatter), stripMarkup);
StringList lines = splitComment(_docComment, std::move(linkFormatter), stripMarkup, xmlEscape);
if (lines.empty())
{
return nullptr;
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/Slice/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,18 @@ namespace Slice
[[nodiscard]] int line() const;

[[nodiscard]] std::string docComment() const;

/// Parses the documentation comment into a structured format.
///
/// @param linkFormatter A function used to format links according to the expected documentation format.
/// @param stripMarkup If true, removes all HTML markup from the parsed comment. Defaults to false.
/// @param xmlEscape If true, escapes all XML special characters in the parsed comment. Defaults to false.
/// @return The parsed documentation comment or nullptr if the element does not contain a documentation
/// comment.
[[nodiscard]] DocCommentPtr parseDocComment(
std::function<std::string(std::string, std::string)> linkFormatter,
bool stripMarkup = false) const;
bool stripMarkup = false,
bool xmlEscape = false) const;

[[nodiscard]] int includeLevel() const;

Expand Down
3 changes: 2 additions & 1 deletion cpp/src/slice2cs/CsUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace Slice
const std::string& prefix = "",
const std::string& suffix = "");

static std::string fixId(const std::string&, unsigned int = 0, bool = false);

protected:
//
// Returns the namespace prefix of a Contained entity.
Expand All @@ -47,7 +49,6 @@ namespace Slice
static std::string resultStructName(const std::string&, const std::string&, bool = false);
static std::string resultType(const OperationPtr&, const std::string&, bool = false);
static std::string taskResultType(const OperationPtr&, const std::string&, bool = false);
static std::string fixId(const std::string&, unsigned int = 0, bool = false);
static std::string getOptionalFormat(const TypePtr&);
static std::string getStaticId(const TypePtr&);
static std::string typeToString(const TypePtr&, const std::string&, bool = false);
Expand Down
Loading
Loading