Skip to content

Commit a0a5039

Browse files
committed
clang-format: Make it possible to turn off comment reflowing.
llvm-svn: 254414
1 parent 81f3f15 commit a0a5039

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

clang/include/clang/Format/Format.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,12 @@ struct FormatStyle {
466466
PAS_Middle
467467
};
468468

469-
/// Pointer and reference alignment style.
469+
/// \brief Pointer and reference alignment style.
470470
PointerAlignmentStyle PointerAlignment;
471471

472+
/// \brief If true, clang-format will attempt to re-flow comments.
473+
bool ReflowComments;
474+
472475
/// \brief If true, clang-format will sort #includes.
473476
bool SortIncludes;
474477

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,15 +1076,17 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
10761076
return 0;
10771077
}
10781078
} else if (Current.is(TT_BlockComment) && Current.isTrailingComment()) {
1079-
if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
1079+
if (!Style.ReflowComments ||
1080+
CommentPragmasRegex.match(Current.TokenText.substr(2)))
10801081
return 0;
10811082
Token.reset(new BreakableBlockComment(
10821083
Current, State.Line->Level, StartColumn, Current.OriginalColumn,
10831084
!Current.Previous, State.Line->InPPDirective, Encoding, Style));
10841085
} else if (Current.is(TT_LineComment) &&
10851086
(Current.Previous == nullptr ||
10861087
Current.Previous->isNot(TT_ImplicitStringLiteral))) {
1087-
if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
1088+
if (!Style.ReflowComments ||
1089+
CommentPragmasRegex.match(Current.TokenText.substr(2)))
10881090
return 0;
10891091
Token.reset(new BreakableLineComment(Current, State.Line->Level,
10901092
StartColumn, /*InPPDirective=*/false,

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ template <> struct MappingTraits<FormatStyle> {
284284
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
285285
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
286286
Style.PenaltyReturnTypeOnItsOwnLine);
287-
IO.mapOptional("SortIncludes", Style.SortIncludes);
288287
IO.mapOptional("PointerAlignment", Style.PointerAlignment);
288+
IO.mapOptional("ReflowComments", Style.ReflowComments);
289+
IO.mapOptional("SortIncludes", Style.SortIncludes);
289290
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
290291
IO.mapOptional("SpaceBeforeAssignmentOperators",
291292
Style.SpaceBeforeAssignmentOperators);
@@ -490,6 +491,7 @@ FormatStyle getLLVMStyle() {
490491
LLVMStyle.SpacesBeforeTrailingComments = 1;
491492
LLVMStyle.Standard = FormatStyle::LS_Cpp11;
492493
LLVMStyle.UseTab = FormatStyle::UT_Never;
494+
LLVMStyle.ReflowComments = true;
493495
LLVMStyle.SpacesInParentheses = false;
494496
LLVMStyle.SpacesInSquareBrackets = false;
495497
LLVMStyle.SpaceInEmptyParentheses = false;

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,13 @@ TEST_F(FormatTest, AlignsBlockComments) {
11971197
"comment */"));
11981198
}
11991199

1200+
TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1201+
FormatStyle Style = getLLVMStyleWithColumns(20);
1202+
Style.ReflowComments = false;
1203+
verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1204+
verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1205+
}
1206+
12001207
TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
12011208
EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12021209
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
@@ -9639,6 +9646,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
96399646
CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
96409647
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
96419648
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9649+
CHECK_PARSE_BOOL(ReflowComments);
96429650
CHECK_PARSE_BOOL(SortIncludes);
96439651
CHECK_PARSE_BOOL(SpacesInParentheses);
96449652
CHECK_PARSE_BOOL(SpacesInSquareBrackets);

0 commit comments

Comments
 (0)