Skip to content

Commit 42f1cb6

Browse files
committed
Change to use bool options for each control statement
1 parent dd3af42 commit 42f1cb6

File tree

6 files changed

+121
-346
lines changed

6 files changed

+121
-346
lines changed

clang/include/clang/Format/Format.h

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

65-
/// Different styles for breaking the parenthesis after ``if/else if``.
65+
/// Force break after the left parenthesis of an if control statement
66+
/// when the expression exceeds the column limit.
67+
/// \code
68+
/// true: false:
69+
/// if constexpr ( vs. if constexpr (a ||
70+
/// a || b)
71+
/// b)
72+
/// \endcode
6673
/// \version 21
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;
74+
bool BreakAfterOpenBracketIf;
9175

92-
/// Different styles for breaking the parenthesis after loops ``(for/while)``.
76+
/// Force break after the left parenthesis of a loop control statement
77+
/// when the expression exceeds the column limit.
78+
/// \code
79+
/// true: false:
80+
/// while ( vs. while (a &&
81+
/// a && b) {
82+
/// b) {
83+
/// \endcode
9384
/// \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-
};
85+
bool BreakAfterOpenBracketLoop;
11686

117-
BreakAfterOpenBracketLoopStyle BreakAfterOpenBracketLoop;
118-
119-
/// Different styles for breaking the parenthesis after ``switch``.
87+
/// Force break after the left parenthesis of a switch control statement
88+
/// when the expression exceeds the column limit.
89+
/// \code
90+
/// true: false:
91+
/// switch ( vs. switch (a &&
92+
/// a && b) {
93+
/// b) {
94+
/// \endcode
12095
/// \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,
142-
};
143-
144-
BreakAfterOpenBracketSwitchStyle BreakAfterOpenBracketSwitch;
96+
bool BreakAfterOpenBracketSwitch;
14597

14698
/// Different styles for aligning after open brackets.
14799
enum BracketAlignmentStyle : int8_t {
@@ -2296,87 +2248,38 @@ struct FormatStyle {
22962248
/// \version 3.7
22972249
BraceBreakingStyle BreakBeforeBraces;
22982250

2299-
/// Different styles for breaking before ``if/else if`` closing parenthesis.
2251+
/// Force break before the right parenthesis of an if control statement
2252+
/// when the expression exceeds the column limit.
2253+
/// \code
2254+
/// true: false:
2255+
/// if constexpr (a || vs. if constexpr (a ||
2256+
/// b b)
2257+
/// )
2258+
/// \endcode
23002259
/// \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;
2260+
bool BreakBeforeCloseBracketIf;
23252261

2326-
/// Different styles for breaking before loop ``(for/while)`` closing
2327-
/// parenthesis.
2262+
/// Force break before the right parenthesis of a loop control statement
2263+
/// when the expression exceeds the column limit.
2264+
/// \code
2265+
/// true: false:
2266+
/// while (a && vs. while (a &&
2267+
/// b b) {
2268+
/// ) {
2269+
/// \endcode
23282270
/// \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-
};
2271+
bool BreakBeforeCloseBracketLoop;
23512272

2352-
BreakBeforeCloseBracketLoopStyle BreakBeforeCloseBracketLoop;
2353-
2354-
/// Different styles for breaking before ``switch`` closing parenthesis.
2273+
/// Force break before the right parenthesis of a switch control statement
2274+
/// when the expression exceeds the column limit.
2275+
/// \code
2276+
/// true: false:
2277+
/// switch (a && vs. switch (a &&
2278+
/// b b) {
2279+
/// ) {
2280+
/// \endcode
23552281
/// \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;
2282+
bool BreakBeforeCloseBracketSwitch;
23802283

23812284
/// Different ways to break before concept declarations.
23822285
enum BreakBeforeConceptDeclarationsStyle : int8_t {

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
358358

359359
// Allow breaking before the right parens with block indentation if there was
360360
// a break after the left parens, which is tracked by BreakBeforeClosingParen.
361-
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
362-
Current.is(tok::r_paren)) {
361+
bool might_break_before =
362+
Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
363+
Style.BreakBeforeCloseBracketSwitch ||
364+
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
365+
366+
if (might_break_before && Current.is(tok::r_paren))
363367
return CurrentState.BreakBeforeClosingParen;
364-
}
365368

366369
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser))
367370
return CurrentState.BreakBeforeClosingAngle;
@@ -830,21 +833,12 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
830833
}
831834
if (!Tok.Previous)
832835
return true;
833-
if (Tok.Previous->isIf()) {
834-
/* For backward compatibility, use AlignAfterOpenBracket
835-
* in case AlignAfterControlStatement is not initialized */
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;
847-
}
836+
if (Tok.Previous->isIf())
837+
return Style.BreakAfterOpenBracketIf;
838+
if (IsLoopConditional(*Tok.Previous))
839+
return Style.BreakAfterOpenBracketLoop;
840+
if (Tok.Previous->is(tok::kw_switch))
841+
return Style.BreakAfterOpenBracketSwitch;
848842
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
849843
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
850844
return !Tok.Previous->is(TT_CastRParen) &&
@@ -1282,23 +1276,18 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
12821276
};
12831277

12841278
if (Previous->isIf()) {
1285-
CurrentState.BreakBeforeClosingParen =
1286-
Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
1287-
Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always;
1279+
CurrentState.BreakBeforeClosingParen = Style.BreakBeforeCloseBracketIf;
12881280
} else if (IsLoopConditional(*Previous)) {
12891281
CurrentState.BreakBeforeClosingParen =
1290-
Style.BreakBeforeCloseBracketLoop ==
1291-
FormatStyle::BBCBLS_MultiLine ||
1292-
Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always;
1282+
Style.BreakBeforeCloseBracketLoop;
12931283
} else if (Previous->is(tok::kw_switch)) {
12941284
CurrentState.BreakBeforeClosingParen =
1295-
Style.BreakBeforeCloseBracketSwitch ==
1296-
FormatStyle::BBCBSS_MultiLine ||
1297-
Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always;
1285+
Style.BreakBeforeCloseBracketSwitch;
1286+
} else {
1287+
CurrentState.BreakBeforeClosingParen =
1288+
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
12981289
}
12991290
}
1300-
CurrentState.BreakBeforeClosingParen =
1301-
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
13021291
}
13031292

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

0 commit comments

Comments
 (0)