Skip to content

Commit 1b2c790

Browse files
committed
Addressing @owenca's feedback.
1 parent e11767e commit 1b2c790

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

clang/include/clang/Format/Format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,7 +3814,7 @@ struct FormatStyle {
38143814
ReferenceAlignmentStyle ReferenceAlignment;
38153815

38163816
// clang-format off
3817-
/// \brief Types of comment re-flow style.
3817+
/// \brief Types of comment reflow style.
38183818
enum ReflowCommentsStyle : int8_t {
38193819
/// Leave comments untouched.
38203820
/// \code
@@ -3833,7 +3833,7 @@ struct FormatStyle {
38333833
/// * and a misaligned second line */
38343834
/// \endcode
38353835
RCS_IndentOnly,
3836-
/// Apply indentation rules and re-flow long comments into new lines, trying
3836+
/// Apply indentation rules and reflow long comments into new lines, trying
38373837
/// to obey the ``ColumnLimit``.
38383838
/// \code
38393839
/// // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of

clang/lib/Format/BreakableToken.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,8 @@ BreakableComment::getSplit(unsigned LineIndex, unsigned TailOffset,
420420
unsigned ColumnLimit, unsigned ContentStartColumn,
421421
const llvm::Regex &CommentPragmasRegex) const {
422422
// Don't break lines matching the comment pragmas regex.
423-
if (Style.ReflowComments != FormatStyle::RCS_Always ||
424-
CommentPragmasRegex.match(Content[LineIndex])) {
423+
if (!AlwaysReflow || CommentPragmasRegex.match(Content[LineIndex]))
425424
return Split(StringRef::npos, 0);
426-
}
427425
return getCommentSplit(Content[LineIndex].substr(TailOffset),
428426
ContentStartColumn, ColumnLimit, Style.TabWidth,
429427
Encoding, Style);
@@ -610,10 +608,8 @@ BreakableToken::Split BreakableBlockComment::getSplit(
610608
unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
611609
unsigned ContentStartColumn, const llvm::Regex &CommentPragmasRegex) const {
612610
// Don't break lines matching the comment pragmas regex.
613-
if (Style.ReflowComments != FormatStyle::RCS_Always ||
614-
CommentPragmasRegex.match(Content[LineIndex])) {
611+
if (!AlwaysReflow || CommentPragmasRegex.match(Content[LineIndex]))
615612
return Split(StringRef::npos, 0);
616-
}
617613
return getCommentSplit(Content[LineIndex].substr(TailOffset),
618614
ContentStartColumn, ColumnLimit, Style.TabWidth,
619615
Encoding, Style, Decoration.ends_with("*"));
@@ -859,7 +855,7 @@ bool BreakableBlockComment::mayReflow(
859855
StringRef IndentContent = Content[LineIndex];
860856
if (Lines[LineIndex].ltrim(Blanks).starts_with("*"))
861857
IndentContent = Lines[LineIndex].ltrim(Blanks).substr(1);
862-
return LineIndex > 0 && Style.ReflowComments == FormatStyle::RCS_Always &&
858+
return LineIndex > 0 && AlwaysReflow &&
863859
!CommentPragmasRegex.match(IndentContent) &&
864860
mayReflowContent(Content[LineIndex]) && !Tok.Finalized &&
865861
!switchesFormatting(tokenAt(LineIndex));
@@ -1165,7 +1161,7 @@ bool BreakableLineCommentSection::mayReflow(
11651161
// // text that protrudes
11661162
// // into text with different indent
11671163
// We do reflow in that case in block comments.
1168-
return LineIndex > 0 && Style.ReflowComments == FormatStyle::RCS_Always &&
1164+
return LineIndex > 0 && AlwaysReflow &&
11691165
!CommentPragmasRegex.match(IndentContent) &&
11701166
mayReflowContent(Content[LineIndex]) && !Tok.Finalized &&
11711167
!switchesFormatting(tokenAt(LineIndex)) &&

clang/lib/Format/BreakableToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ class BreakableComment : public BreakableToken {
384384
// The intended start column of the first line of text from this section.
385385
unsigned StartColumn;
386386

387+
const bool AlwaysReflow = Style.ReflowComments == FormatStyle::RCS_Always;
388+
387389
// The prefix to use in front a line that has been reflown up.
388390
// For example, when reflowing the second line after the first here:
389391
// // comment 1

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ReflowCommentsStyle> {
520520
IO.enumCase(Value, "Never", FormatStyle::RCS_Never);
521521
IO.enumCase(Value, "IndentOnly", FormatStyle::RCS_IndentOnly);
522522
IO.enumCase(Value, "Always", FormatStyle::RCS_Always);
523+
// For backward compatibility:
523524
IO.enumCase(Value, "false", FormatStyle::RCS_Never);
524525
IO.enumCase(Value, "true", FormatStyle::RCS_Always);
525526
}

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4605,11 +4605,11 @@ bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
46054605

46064606
// Checks if \p FormatTok is a line comment that continues the line comment
46074607
// section on \p Line.
4608-
static bool continuesLineCommentSection(
4609-
const FormatToken &FormatTok, const UnwrappedLine &Line,
4610-
const FormatStyle::ReflowCommentsStyle ReflowCommentsStyle,
4611-
const llvm::Regex &CommentPragmasRegex) {
4612-
if (Line.Tokens.empty() || ReflowCommentsStyle != FormatStyle::RCS_Always)
4608+
static bool
4609+
continuesLineCommentSection(const FormatToken &FormatTok,
4610+
const UnwrappedLine &Line, const FormatStyle &Style,
4611+
const llvm::Regex &CommentPragmasRegex) {
4612+
if (Line.Tokens.empty() || Style.ReflowComments != FormatStyle::RCS_Always)
46134613
return false;
46144614

46154615
StringRef IndentContent = FormatTok.TokenText;
@@ -4721,8 +4721,8 @@ void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
47214721
//
47224722
// FIXME: Consider putting separate line comment sections as children to the
47234723
// unwrapped line instead.
4724-
Tok->ContinuesLineCommentSection = continuesLineCommentSection(
4725-
*Tok, *Line, Style.ReflowComments, CommentPragmasRegex);
4724+
Tok->ContinuesLineCommentSection =
4725+
continuesLineCommentSection(*Tok, *Line, Style, CommentPragmasRegex);
47264726
if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
47274727
addUnwrappedLine();
47284728
pushToken(Tok);
@@ -4796,7 +4796,7 @@ void UnwrappedLineParser::distributeComments(
47964796
FormatTok->ContinuesLineCommentSection = false;
47974797
} else {
47984798
FormatTok->ContinuesLineCommentSection = continuesLineCommentSection(
4799-
*FormatTok, *Line, Style.ReflowComments, CommentPragmasRegex);
4799+
*FormatTok, *Line, Style, CommentPragmasRegex);
48004800
}
48014801
if (!FormatTok->ContinuesLineCommentSection &&
48024802
(isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {

0 commit comments

Comments
 (0)