================
@@ -27,6 +27,113 @@ namespace format {
static constexpr StringRef Blanks(" \t\v\f\r");
+static bool lineContainsContent(StringRef Line) {
+ return Line.find_first_not_of(Blanks) != StringRef::npos;
+}
+
+static StringRef stripTrailingCarriageReturn(StringRef Line) {
+ if (!Line.empty() && Line.back() == '\r')
+ Line = Line.drop_back();
+ return Line;
+}
+
+static FormatStyle::CommentSpaceMode
+resolveCommentSpaceMode(CommentKind Kind,
+ FormatStyle::CommentSpaceMode GeneralMode,
+ FormatStyle::CommentSpaceMode ParamMode) {
+ switch (Kind) {
+ case CommentKind::Plain:
+ return GeneralMode;
+ case CommentKind::Parameter:
+ return ParamMode;
+
+ case CommentKind::DocString:
+ return FormatStyle::CommentSpaceMode::Leave;
+ }
+ llvm_unreachable("Unhandled CommentKind");
+}
+
+static void applySpaceInBlockComment(const FormatToken &Tok,
+ const FormatStyle &Style,
+ WhitespaceManager &Whitespaces,
+ bool InPPDirective) {
+ StringRef Text = Tok.TokenText;
+ if (!Text.starts_with("/*") || !Text.ends_with("*/") || Text.size() < 4)
+ return;
+
+ const CommentKind Kind = Tok.getBlockCommentKind();
+ const auto OpeningMode =
+ resolveCommentSpaceMode(Kind, Style.SpaceInComments.AfterOpeningComment,
+ Style.SpaceInComments.AfterOpeningParamComment);
+ const auto ClosingMode =
+ resolveCommentSpaceMode(Kind, Style.SpaceInComments.BeforeClosingComment,
+ Style.SpaceInComments.BeforeClosingParamComment);
+
+ StringRef Body = Text.substr(2, Text.size() - 4);
+ if (Body.empty())
+ return;
+
+ const auto applyReplacement = [&](unsigned Offset, unsigned Length,
+ StringRef Replacement) {
+ Whitespaces.replaceWhitespaceInToken(Tok, Offset, Length, "", Replacement,
+ InPPDirective,
+ /*Newlines=*/0, /*Spaces=*/0);
+ };
+
+ if (Body.find_first_not_of(Blanks) == StringRef::npos) {
+ if (OpeningMode == FormatStyle::CommentSpaceMode::Never ||
+ ClosingMode == FormatStyle::CommentSpaceMode::Never) {
+ applyReplacement(2, Body.size(), "");
+ return;
+ }
+ if (OpeningMode == FormatStyle::CommentSpaceMode::Always ||
+ ClosingMode == FormatStyle::CommentSpaceMode::Always) {
+ applyReplacement(2, Body.size(), " ");
+ return;
+ }
+ }
+
+ StringRef FirstLine = Body;
+ if (size_t NL = Body.find_first_of('\n'); NL != StringRef::npos)
+ FirstLine = Body.substr(0, NL);
+ FirstLine = stripTrailingCarriageReturn(FirstLine);
+ if (lineContainsContent(FirstLine)) {
+ const size_t LeadingWhitespace = FirstLine.find_first_not_of(Blanks);
+ switch (OpeningMode) {
+ case FormatStyle::CommentSpaceMode::Leave:
+ break;
+ case FormatStyle::CommentSpaceMode::Always:
+ applyReplacement(2, LeadingWhitespace, " ");
+ break;
+ case FormatStyle::CommentSpaceMode::Never:
+ if (LeadingWhitespace > 0)
+ applyReplacement(2, LeadingWhitespace, "");
+ break;
+ }
+ }
+
+ StringRef LastLine = Body;
+ if (size_t NL = Body.find_last_of('\n'); NL != StringRef::npos)
+ LastLine = Body.substr(NL + 1);
+ LastLine = stripTrailingCarriageReturn(LastLine);
+ if (lineContainsContent(LastLine)) {
+ const size_t LastNonWhitespace = LastLine.find_last_not_of(Blanks);
+ const size_t ReplaceChars = LastLine.size() - (LastNonWhitespace + 1);
+ const size_t ReplaceOffset = 2 + Body.size() - ReplaceChars;
+ switch (ClosingMode) {
+ case FormatStyle::CommentSpaceMode::Leave:
+ break;
----------------
HazardyKnusperkeks wrote:
Can we move the check for leave way up, so we don't compute a lot of
unnecessary stuff?
https://github.com/llvm/llvm-project/pull/162105
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits