Skip to content

Commit dd3af42

Browse files
committed
updates
1 parent eafb176 commit dd3af42

File tree

6 files changed

+386
-65
lines changed

6 files changed

+386
-65
lines changed

clang/include/clang/Format/Format.h

Lines changed: 165 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,86 @@ struct FormatStyle {
6262
/// \version 3.3
6363
int AccessModifierOffset;
6464

65-
/// Different styles for breaking the parenthesis after a control statement
66-
/// (``if/switch/while/for ...``).
65+
/// Different styles for breaking the parenthesis after ``if/else if``.
6766
/// \version 21
68-
enum BreakAfterControlStatementStyle : int8_t {
69-
/// Use the default behavior.
70-
BACSS_Default,
71-
/// Force break after the left parenthesis of a control statement only
72-
/// when the expression exceeds the column limit, and align on the
73-
/// ``ContinuationIndentWidth``.
74-
BACSS_MultiLine,
75-
/// Do not force a break after the control statment.
76-
BACSS_No,
67+
enum BreakAfterOpenBracketIfStyle : int8_t {
68+
/// Always break the opening parenthesis of an if statement, e.g.:
69+
/// \code
70+
/// if constexpr (
71+
/// a)
72+
/// \endcode
73+
BAOBIS_Always,
74+
/// Force break after the left parenthesis of an if statement only
75+
/// when the expression exceeds the column limit, e.g..:
76+
/// \code
77+
/// if constexpr (
78+
/// a ||
79+
/// b)
80+
/// \endcode
81+
BAOBIS_MultiLine,
82+
/// Do not force a break after the control statement.
83+
/// \code
84+
/// if constexpr (a ||
85+
/// b
86+
/// \endcode
87+
BAOBIS_No,
88+
};
89+
90+
BreakAfterOpenBracketIfStyle BreakAfterOpenBracketIf;
91+
92+
/// Different styles for breaking the parenthesis after loops ``(for/while)``.
93+
/// \version 21
94+
enum BreakAfterOpenBracketLoopStyle : int8_t {
95+
/// Always break the opening parenthesis of a loop statement, e.g.:
96+
/// \code
97+
/// while (
98+
/// a) {
99+
/// \endcode
100+
BAOBLS_Always,
101+
/// Force break after the left parenthesis of a loop only
102+
/// when the expression exceeds the column limit, e.g..:
103+
/// \code
104+
/// while (
105+
/// a &&
106+
/// b) {
107+
/// \endcode
108+
BAOBLS_MultiLine,
109+
/// Do not force a break after the control statement.
110+
/// \code
111+
/// while (a &&
112+
/// b) {
113+
/// \endcode
114+
BAOBLS_No,
115+
};
116+
117+
BreakAfterOpenBracketLoopStyle BreakAfterOpenBracketLoop;
118+
119+
/// Different styles for breaking the parenthesis after ``switch``.
120+
/// \version 21
121+
enum BreakAfterOpenBracketSwitchStyle : int8_t {
122+
/// Always break the opening parenthesis of a switch statement, e.g.:
123+
/// \code
124+
/// switch (
125+
/// a) {
126+
/// \endcode
127+
BAOBSS_Always,
128+
/// Force break after the left parenthesis of a switch only
129+
/// when the expression exceeds the column limit, e.g..:
130+
/// \code
131+
/// switch (
132+
/// a &&
133+
/// b) {
134+
/// \endcode
135+
BAOBSS_MultiLine,
136+
/// Do not force a break after the control statement.
137+
/// \code
138+
/// switch (a &&
139+
/// b) {
140+
/// \endcode
141+
BAOBSS_No,
77142
};
78143

79-
BreakAfterControlStatementStyle AlignAfterControlStatement;
144+
BreakAfterOpenBracketSwitchStyle BreakAfterOpenBracketSwitch;
80145

81146
/// Different styles for aligning after open brackets.
82147
enum BracketAlignmentStyle : int8_t {
@@ -2231,6 +2296,88 @@ struct FormatStyle {
22312296
/// \version 3.7
22322297
BraceBreakingStyle BreakBeforeBraces;
22332298

2299+
/// Different styles for breaking before ``if/else if`` closing parenthesis.
2300+
/// \version 21
2301+
enum BreakBeforeCloseBracketIfStyle : int8_t {
2302+
/// Always break the closing parenthesis of an if statement, e.g.:
2303+
/// \code
2304+
/// if constexpr (a
2305+
/// )
2306+
/// \endcode
2307+
BBCBIS_Always,
2308+
/// Force break before the closing parenthesis of an if statement only
2309+
/// when the expression exceeds the column limit, e.g..:
2310+
/// \code
2311+
/// if constexpr (a ||
2312+
/// b
2313+
/// )
2314+
/// \endcode
2315+
BBCBIS_MultiLine,
2316+
/// Do not force a break before closing the if control statement.
2317+
/// \code
2318+
/// if constexpr (a ||
2319+
/// b)
2320+
/// \endcode
2321+
BBCBIS_No,
2322+
};
2323+
2324+
BreakBeforeCloseBracketIfStyle BreakBeforeCloseBracketIf;
2325+
2326+
/// Different styles for breaking before loop ``(for/while)`` closing
2327+
/// parenthesis.
2328+
/// \version 21
2329+
enum BreakBeforeCloseBracketLoopStyle : int8_t {
2330+
/// Always break the closing parenthesis of a loop statement, e.g.:
2331+
/// \code
2332+
/// while (a
2333+
/// ) {
2334+
/// \endcode
2335+
BBCBLS_Always,
2336+
/// Force break before the closing parenthesis of a loop only
2337+
/// when the expression exceeds the column limit, e.g..:
2338+
/// \code
2339+
/// while (a &&
2340+
/// b
2341+
/// ) {
2342+
/// \endcode
2343+
BBCBLS_MultiLine,
2344+
/// Do not force a break before closing the loop control statement.
2345+
/// \code
2346+
/// while (a &&
2347+
/// b) {
2348+
/// \endcode
2349+
BBCBLS_No,
2350+
};
2351+
2352+
BreakBeforeCloseBracketLoopStyle BreakBeforeCloseBracketLoop;
2353+
2354+
/// Different styles for breaking before ``switch`` closing parenthesis.
2355+
/// \version 21
2356+
enum BreakBeforeCloseBracketSwitchStyle : int8_t {
2357+
/// Always break before the closing parenthesis of a switch statement, e.g.:
2358+
/// \code
2359+
/// switch (a
2360+
/// ) {
2361+
/// \endcode
2362+
BBCBSS_Always,
2363+
/// Force break before the closing parenthesis of a switch only
2364+
/// when the expression exceeds the column limit, e.g..:
2365+
/// \code
2366+
/// switch (a &&
2367+
/// b
2368+
/// ) {
2369+
/// \endcode
2370+
BBCBSS_MultiLine,
2371+
/// Do not force a break before closing the switch control statement.
2372+
/// \code
2373+
/// switch (a &&
2374+
/// b) {
2375+
/// \endcode
2376+
BBCBSS_No,
2377+
};
2378+
2379+
BreakBeforeCloseBracketSwitchStyle BreakBeforeCloseBracketSwitch;
2380+
22342381
/// Different ways to break before concept declarations.
22352382
enum BreakBeforeConceptDeclarationsStyle : int8_t {
22362383
/// Keep the template declaration line together with ``concept``.
@@ -5321,7 +5468,6 @@ struct FormatStyle {
53215468

53225469
bool operator==(const FormatStyle &R) const {
53235470
return AccessModifierOffset == R.AccessModifierOffset &&
5324-
AlignAfterControlStatement == R.AlignAfterControlStatement &&
53255471
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
53265472
AlignArrayOfStructures == R.AlignArrayOfStructures &&
53275473
AlignConsecutiveAssignments == R.AlignConsecutiveAssignments &&
@@ -5371,10 +5517,16 @@ struct FormatStyle {
53715517
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
53725518
BreakAfterAttributes == R.BreakAfterAttributes &&
53735519
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
5520+
BreakAfterOpenBracketIf == R.BreakAfterOpenBracketIf &&
5521+
BreakAfterOpenBracketLoop == R.BreakAfterOpenBracketLoop &&
5522+
BreakAfterOpenBracketSwitch == R.BreakAfterOpenBracketSwitch &&
53745523
BreakAfterReturnType == R.BreakAfterReturnType &&
53755524
BreakArrays == R.BreakArrays &&
53765525
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
53775526
BreakBeforeBraces == R.BreakBeforeBraces &&
5527+
BreakBeforeCloseBracketIf == R.BreakBeforeCloseBracketIf &&
5528+
BreakBeforeCloseBracketLoop == R.BreakBeforeCloseBracketLoop &&
5529+
BreakBeforeCloseBracketSwitch == R.BreakBeforeCloseBracketSwitch &&
53785530
BreakBeforeConceptDeclarations == R.BreakBeforeConceptDeclarations &&
53795531
BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
53805532
BreakBeforeTemplateCloser == R.BreakBeforeTemplateCloser &&

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
814814
// parenthesis by disallowing any further line breaks if there is no line
815815
// break after the opening parenthesis. Don't break if it doesn't conserve
816816
// columns.
817-
auto IsOtherConditional = [&](const FormatToken &Tok) {
818-
return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) ||
817+
auto IsLoopConditional = [&](const FormatToken &Tok) {
818+
return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
819819
(Style.isJavaScript() && Tok.is(Keywords.kw_await) && Tok.Previous &&
820820
Tok.Previous->is(tok::kw_for));
821821
};
@@ -833,12 +833,18 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
833833
if (Tok.Previous->isIf()) {
834834
/* For backward compatibility, use AlignAfterOpenBracket
835835
* in case AlignAfterControlStatement is not initialized */
836-
return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine ||
837-
(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default &&
838-
Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak);
836+
return Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_MultiLine ||
837+
Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_Always;
838+
}
839+
if (IsLoopConditional(*Tok.Previous)) {
840+
return Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_MultiLine ||
841+
Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_Always;
842+
}
843+
if (Tok.Previous->is(tok::kw_switch)) {
844+
return Style.BreakAfterOpenBracketSwitch ==
845+
FormatStyle::BAOBSS_MultiLine ||
846+
Style.BreakAfterOpenBracketSwitch == FormatStyle::BAOBSS_Always;
839847
}
840-
if (IsOtherConditional(*Tok.Previous))
841-
return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
842848
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
843849
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
844850
return !Tok.Previous->is(TT_CastRParen) &&
@@ -883,8 +889,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
883889
}
884890
const auto *Previous = TokAfterLParen.Previous;
885891
assert(Previous); // IsOpeningBracket(Previous)
886-
if (Previous->Previous && (Previous->Previous->isIf() ||
887-
IsOtherConditional(*Previous->Previous))) {
892+
if (Previous->Previous &&
893+
(Previous->Previous->isIf() || IsLoopConditional(*Previous->Previous) ||
894+
Previous->Previous->is(tok::kw_switch))) {
888895
return false;
889896
}
890897
if (!Previous->isOneOf(TT_FunctionDeclarationLParen,
@@ -1266,16 +1273,32 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
12661273

12671274
if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
12681275
auto Previous = PreviousNonComment->Previous;
1269-
if (Previous &&
1270-
(Previous->isIf() ||
1271-
Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch))) {
1272-
CurrentState.BreakBeforeClosingParen =
1273-
Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine &&
1274-
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1275-
} else {
1276-
CurrentState.BreakBeforeClosingParen =
1277-
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1276+
if (Previous) {
1277+
1278+
auto IsLoopConditional = [&](const FormatToken &Tok) {
1279+
return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
1280+
(Style.isJavaScript() && Tok.is(Keywords.kw_await) &&
1281+
Tok.Previous && Tok.Previous->is(tok::kw_for));
1282+
};
1283+
1284+
if (Previous->isIf()) {
1285+
CurrentState.BreakBeforeClosingParen =
1286+
Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
1287+
Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always;
1288+
} else if (IsLoopConditional(*Previous)) {
1289+
CurrentState.BreakBeforeClosingParen =
1290+
Style.BreakBeforeCloseBracketLoop ==
1291+
FormatStyle::BBCBLS_MultiLine ||
1292+
Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always;
1293+
} else if (Previous->is(tok::kw_switch)) {
1294+
CurrentState.BreakBeforeClosingParen =
1295+
Style.BreakBeforeCloseBracketSwitch ==
1296+
FormatStyle::BBCBSS_MultiLine ||
1297+
Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always;
1298+
}
12781299
}
1300+
CurrentState.BreakBeforeClosingParen =
1301+
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
12791302
}
12801303

12811304
if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))

0 commit comments

Comments
 (0)