https://github.com/gedare updated
https://github.com/llvm/llvm-project/pull/108332
>From 34c27d6148111d0dfac4ab1c71cc34adf1c0299b Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Thu, 20 Jun 2024 17:35:39 -0600
Subject: [PATCH 01/11] Format: add AlignAfterControlStatement
Introduce new style option to allow overriding the breaking after the
opening parenthesis for control statements (if/for/while/switch).
Fixes #67738.
Fixes #79176.
Fixes #80123.
---
clang/include/clang/Format/Format.h | 17 ++
clang/lib/Format/ContinuationIndenter.cpp | 69 +++--
clang/lib/Format/Format.cpp | 13 +
clang/lib/Format/TokenAnnotator.cpp | 8 +-
clang/unittests/Format/ConfigParseTest.cpp | 8 +
clang/unittests/Format/FormatTest.cpp | 298 +++++++++++++++++++++
6 files changed, 391 insertions(+), 22 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 5dfdb23594610..b41fb191754d4 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,6 +62,22 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
+ /// Different styles for breaking the parenthesis after a control statement
+ /// (``if/switch/while/for ...``).
+ /// \version 21
+ enum BreakAfterControlStatementStyle : int8_t {
+ /// Use the default behavior.
+ BACSS_Default,
+ /// Force break after the left parenthesis of a control statement only
+ /// when the expression exceeds the column limit, and align on the
+ /// ``ContinuationIndentWidth``.
+ BACSS_MultiLine,
+ /// Do not force a break after the control statment.
+ BACSS_No,
+ };
+
+ BreakAfterControlStatementStyle AlignAfterControlStatement;
+
/// Different styles for aligning after open brackets.
enum BracketAlignmentStyle : int8_t {
/// Align parameters on the open bracket, e.g.:
@@ -5357,6 +5373,7 @@ struct FormatStyle {
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
+ AlignAfterControlStatement == R.AlignAfterControlStatement &&
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
AlignArrayOfStructures == R.AlignArrayOfStructures &&
AlignConsecutiveAssignments == R.AlignConsecutiveAssignments &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index 888d0faf80931..be741ae4d6cde 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -828,6 +828,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State, bool DryRun,
// parenthesis by disallowing any further line breaks if there is no line
// break after the opening parenthesis. Don't break if it doesn't conserve
// columns.
+ auto IsOtherConditional = [&](const FormatToken &Tok) {
+ return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) ||
+ (Style.isJavaScript() && Tok.is(Keywords.kw_await) && Tok.Previous
&&
+ Tok.Previous->is(tok::kw_for));
+ };
auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
@@ -839,26 +844,36 @@ void
ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
}
if (!Tok.Previous)
return true;
- if (Tok.Previous->isIf())
- return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
- return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
- tok::kw_switch) &&
- !(Style.isJavaScript() && Tok.Previous->is(Keywords.kw_await));
+ if (Tok.Previous->isIf()) {
+ /* For backward compatibility, use AlignAfterOpenBracket
+ * in case AlignAfterControlStatement is not initialized */
+ return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine
||
+ (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default &&
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak);
+ }
+ if (IsOtherConditional(*Tok.Previous))
+ return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
+ return !Tok.Previous->is(TT_CastRParen) &&
+ !(Style.isJavaScript() && Tok.is(Keywords.kw_await));
+ }
+ return false;
};
auto IsFunctionCallParen = [](const FormatToken &Tok) {
return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
Tok.Previous->is(tok::identifier);
};
- auto IsInTemplateString = [this](const FormatToken &Tok) {
+ auto IsInTemplateString = [this](const FormatToken &Tok, bool NestBlocks) {
if (!Style.isJavaScript())
return false;
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
if (Prev->is(TT_TemplateString) && Prev->opensScope())
return true;
- if (Prev->opensScope() ||
- (Prev->is(TT_TemplateString) && Prev->closesScope())) {
- break;
- }
+ if (Prev->opensScope() && !NestBlocks)
+ return false;
+ if (Prev->is(TT_TemplateString) && Prev->closesScope())
+ return false;
}
return false;
};
@@ -880,21 +895,24 @@ void
ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) {
return true;
}
- const auto *Previous = Tok.Previous;
- if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
- TT_LambdaDefinitionLParen) &&
- !IsFunctionCallParen(*Previous))) {
+ const auto *Previous = TokAfterLParen.Previous;
+ assert(Previous); // IsOpeningBracket(Previous)
+ if (Previous->Previous && (Previous->Previous->isIf() ||
+ IsOtherConditional(*Previous->Previous))) {
+ return false;
+ }
+ if (!Previous->isOneOf(TT_FunctionDeclarationLParen,
+ TT_LambdaDefinitionLParen) &&
+ !IsFunctionCallParen(*Previous)) {
return true;
}
- if (IsOpeningBracket(Tok) || IsInTemplateString(Tok))
+ if (IsOpeningBracket(Tok) || IsInTemplateString(Tok, true))
return true;
const auto *Next = Tok.Next;
return !Next || Next->isMemberAccess() ||
Next->is(TT_FunctionDeclarationLParen) ||
IsFunctionCallParen(*Next);
};
- if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
- IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
+ if (IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
// Don't do this for simple (no expressions) one-argument function calls
// as that feels like needlessly wasting whitespace, e.g.:
//
@@ -924,7 +942,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State, bool DryRun,
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
- !IsInTemplateString(Current)) {
+ !IsInTemplateString(Current, false)) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
}
@@ -1261,8 +1279,17 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
}
if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
- CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ auto Previous = PreviousNonComment->Previous;
+ if (Previous &&
+ (Previous->isIf() ||
+ Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch))) {
+ CurrentState.BreakBeforeClosingParen =
+ Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine &&
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ } else {
+ CurrentState.BreakBeforeClosingParen =
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ }
}
if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index e3b22cdabaccd..42da37a331740 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,6 +203,16 @@ template <> struct
MappingTraits<FormatStyle::BraceWrappingFlags> {
}
};
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakAfterControlStatementStyle> {
+ static void enumeration(IO &IO,
+ FormatStyle::BreakAfterControlStatementStyle &Value)
{
+ IO.enumCase(Value, "Default", FormatStyle::BACSS_Default);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BACSS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BACSS_No);
+ }
+};
+
template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle>
{
static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
@@ -982,6 +992,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
+ IO.mapOptional("AlignAfterControlStatement",
+ Style.AlignAfterControlStatement);
IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures);
IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
@@ -1525,6 +1537,7 @@ static void expandPresetsSpacesInParens(FormatStyle
&Expanded) {
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2;
+ LLVMStyle.AlignAfterControlStatement = FormatStyle::BACSS_Default;
LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignConsecutiveAssignments = {};
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index a220de54f46bf..0e2ef8ec40cc2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6219,7 +6219,13 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine
&Line,
if (Next && Next->is(tok::l_paren))
return false;
const FormatToken *Previous = Right.MatchingParen->Previous;
- return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+ if (!Previous)
+ return true;
+ if (Previous->isIf() ||
+ Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) {
+ return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
+ }
+ return true;
}
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
diff --git a/clang/unittests/Format/ConfigParseTest.cpp
b/clang/unittests/Format/ConfigParseTest.cpp
index 7c993c0f8fd33..3ad48f91827cb 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -533,6 +533,14 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_Default;
+ CHECK_PARSE("AlignAfterControlStatement: MultiLine",
+ AlignAfterControlStatement, FormatStyle::BACSS_MultiLine);
+ CHECK_PARSE("AlignAfterControlStatement: No", AlignAfterControlStatement,
+ FormatStyle::BACSS_No);
+ CHECK_PARSE("AlignAfterControlStatement: Default",
AlignAfterControlStatement,
+ FormatStyle::BACSS_Default);
+
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 83c664c3b81f3..f227c14ab83df 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9728,6 +9728,304 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
Style);
}
+TEST_F(FormatTest, AlignAfterConditionalStatements) {
+ FormatStyle Style = getLLVMStyle();
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+
+ verifyFormat("void foo() {\n"
+ " if constexpr (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbb) == 0) {\n"
+ " return;\n"
+ " } else if (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbb) == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " for (\n"
+ " aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) == 0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next) {\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " while (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0) "
+ "{\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+
+ verifyFormat("void foo() {\n"
+ " if constexpr (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ ") == 0) {\n"
+ " return;\n"
+ " } else if (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ ") == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " for (\n"
+ " aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) ==
0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next)
{\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " while (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0)
"
+ "{\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+
+ verifyFormat("void foo() {\n"
+ " if constexpr (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ ") == 0) {\n"
+ " return;\n"
+ " } else if (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ ") == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " for (\n"
+ " aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) ==
0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next)
{\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " while (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0)
"
+ "{\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_No;
+
+ verifyFormat("void foo() {\n"
+ " if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbbbbb) ==\n"
+ " 0) {\n"
+ " return;\n"
+ " } else if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbb) == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " for (aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) == 0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next) {\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " while ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0) "
+ "{\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+
+ verifyFormat(
+ "void foo() {\n"
+ " if constexpr (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0\n"
+ " ) {\n"
+ " return;\n"
+ " } else if (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0\n"
+ " ) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | "
+ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
+ " ) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " for (\n"
+ " aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) ==
0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next\n"
+ " ) {\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " while (\n"
+ " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) ==
0\n"
+ " ) {\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.AlignAfterControlStatement = FormatStyle::BACSS_No;
+
+ verifyFormat("void foo() {\n"
+ " if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbbbbb) ==\n"
+ " 0) {\n"
+ " return;\n"
+ " } else if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbb) == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " switch (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n"
+ " default:\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " for (aaaaaaaaaaaaaaaaaaaaaa = 0;\n"
+ " (aaaaaaaaaaaaaaaaaaaaaa->bbbbbbbbbbbbbb |\n"
+ " aaaaaaaaaaaaaaaaaaaaaa->ccccccccccccccccccccccc) == 0;\n"
+ " aaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaa->next) {\n"
+ " ;\n"
+ " }\n"
+ "}",
+ Style);
+
+ verifyFormat(
+ "void foo() {\n"
+ " while ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) == 0) {\n"
+ " continue;\n"
+ " }\n"
+ "}",
+ Style);
+}
+
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
>From a2b9fd1c3f417be5e75a6d93f6b2b94fb3ea5df2 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 11 Sep 2024 22:48:12 -0600
Subject: [PATCH 02/11] Update release notes
---
clang/docs/ReleaseNotes.rst | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0d85b6f426995..b6f1fbc94b33c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,6 +381,9 @@ AST Matchers
clang-format
------------
- Add ``SpaceInEmptyBraces`` option and set it to ``Always`` for WebKit style.
+- Add ``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
+ ``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketIf``,
+ ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch`` options.
libclang
--------
>From 7bf6a065dc10a91d464ec9b0133c6d20617cb257 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 16:11:23 -0600
Subject: [PATCH 03/11] updates
---
clang/include/clang/Format/Format.h | 178 +++++++++++++++++++--
clang/lib/Format/ContinuationIndenter.cpp | 59 ++++---
clang/lib/Format/Format.cpp | 91 +++++++++--
clang/lib/Format/TokenAnnotator.cpp | 37 ++++-
clang/unittests/Format/ConfigParseTest.cpp | 56 ++++++-
clang/unittests/Format/FormatTest.cpp | 30 +++-
6 files changed, 386 insertions(+), 65 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index b41fb191754d4..3c408f8caee0d 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,21 +62,86 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /// Different styles for breaking the parenthesis after a control statement
- /// (``if/switch/while/for ...``).
+ /// Different styles for breaking the parenthesis after ``if/else if``.
/// \version 21
- enum BreakAfterControlStatementStyle : int8_t {
- /// Use the default behavior.
- BACSS_Default,
- /// Force break after the left parenthesis of a control statement only
- /// when the expression exceeds the column limit, and align on the
- /// ``ContinuationIndentWidth``.
- BACSS_MultiLine,
- /// Do not force a break after the control statment.
- BACSS_No,
+ enum BreakAfterOpenBracketIfStyle : int8_t {
+ /// Always break the opening parenthesis of an if statement, e.g.:
+ /// \code
+ /// if constexpr (
+ /// a)
+ /// \endcode
+ BAOBIS_Always,
+ /// Force break after the left parenthesis of an if statement only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// if constexpr (
+ /// a ||
+ /// b)
+ /// \endcode
+ BAOBIS_MultiLine,
+ /// Do not force a break after the control statement.
+ /// \code
+ /// if constexpr (a ||
+ /// b
+ /// \endcode
+ BAOBIS_No,
+ };
+
+ BreakAfterOpenBracketIfStyle BreakAfterOpenBracketIf;
+
+ /// Different styles for breaking the parenthesis after loops
``(for/while)``.
+ /// \version 21
+ enum BreakAfterOpenBracketLoopStyle : int8_t {
+ /// Always break the opening parenthesis of a loop statement, e.g.:
+ /// \code
+ /// while (
+ /// a) {
+ /// \endcode
+ BAOBLS_Always,
+ /// Force break after the left parenthesis of a loop only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// while (
+ /// a &&
+ /// b) {
+ /// \endcode
+ BAOBLS_MultiLine,
+ /// Do not force a break after the control statement.
+ /// \code
+ /// while (a &&
+ /// b) {
+ /// \endcode
+ BAOBLS_No,
+ };
+
+ BreakAfterOpenBracketLoopStyle BreakAfterOpenBracketLoop;
+
+ /// Different styles for breaking the parenthesis after ``switch``.
+ /// \version 21
+ enum BreakAfterOpenBracketSwitchStyle : int8_t {
+ /// Always break the opening parenthesis of a switch statement, e.g.:
+ /// \code
+ /// switch (
+ /// a) {
+ /// \endcode
+ BAOBSS_Always,
+ /// Force break after the left parenthesis of a switch only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// switch (
+ /// a &&
+ /// b) {
+ /// \endcode
+ BAOBSS_MultiLine,
+ /// Do not force a break after the control statement.
+ /// \code
+ /// switch (a &&
+ /// b) {
+ /// \endcode
+ BAOBSS_No,
};
- BreakAfterControlStatementStyle AlignAfterControlStatement;
+ BreakAfterOpenBracketSwitchStyle BreakAfterOpenBracketSwitch;
/// Different styles for aligning after open brackets.
enum BracketAlignmentStyle : int8_t {
@@ -2231,6 +2296,88 @@ struct FormatStyle {
/// \version 3.7
BraceBreakingStyle BreakBeforeBraces;
+ /// Different styles for breaking before ``if/else if`` closing parenthesis.
+ /// \version 21
+ enum BreakBeforeCloseBracketIfStyle : int8_t {
+ /// Always break the closing parenthesis of an if statement, e.g.:
+ /// \code
+ /// if constexpr (a
+ /// )
+ /// \endcode
+ BBCBIS_Always,
+ /// Force break before the closing parenthesis of an if statement only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// if constexpr (a ||
+ /// b
+ /// )
+ /// \endcode
+ BBCBIS_MultiLine,
+ /// Do not force a break before closing the if control statement.
+ /// \code
+ /// if constexpr (a ||
+ /// b)
+ /// \endcode
+ BBCBIS_No,
+ };
+
+ BreakBeforeCloseBracketIfStyle BreakBeforeCloseBracketIf;
+
+ /// Different styles for breaking before loop ``(for/while)`` closing
+ /// parenthesis.
+ /// \version 21
+ enum BreakBeforeCloseBracketLoopStyle : int8_t {
+ /// Always break the closing parenthesis of a loop statement, e.g.:
+ /// \code
+ /// while (a
+ /// ) {
+ /// \endcode
+ BBCBLS_Always,
+ /// Force break before the closing parenthesis of a loop only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// while (a &&
+ /// b
+ /// ) {
+ /// \endcode
+ BBCBLS_MultiLine,
+ /// Do not force a break before closing the loop control statement.
+ /// \code
+ /// while (a &&
+ /// b) {
+ /// \endcode
+ BBCBLS_No,
+ };
+
+ BreakBeforeCloseBracketLoopStyle BreakBeforeCloseBracketLoop;
+
+ /// Different styles for breaking before ``switch`` closing parenthesis.
+ /// \version 21
+ enum BreakBeforeCloseBracketSwitchStyle : int8_t {
+ /// Always break before the closing parenthesis of a switch statement,
e.g.:
+ /// \code
+ /// switch (a
+ /// ) {
+ /// \endcode
+ BBCBSS_Always,
+ /// Force break before the closing parenthesis of a switch only
+ /// when the expression exceeds the column limit, e.g..:
+ /// \code
+ /// switch (a &&
+ /// b
+ /// ) {
+ /// \endcode
+ BBCBSS_MultiLine,
+ /// Do not force a break before closing the switch control statement.
+ /// \code
+ /// switch (a &&
+ /// b) {
+ /// \endcode
+ BBCBSS_No,
+ };
+
+ BreakBeforeCloseBracketSwitchStyle BreakBeforeCloseBracketSwitch;
+
/// Different ways to break before concept declarations.
enum BreakBeforeConceptDeclarationsStyle : int8_t {
/// Keep the template declaration line together with ``concept``.
@@ -5373,7 +5520,6 @@ struct FormatStyle {
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
- AlignAfterControlStatement == R.AlignAfterControlStatement &&
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
AlignArrayOfStructures == R.AlignArrayOfStructures &&
AlignConsecutiveAssignments == R.AlignConsecutiveAssignments &&
@@ -5423,10 +5569,16 @@ struct FormatStyle {
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
BreakAfterAttributes == R.BreakAfterAttributes &&
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations
&&
+ BreakAfterOpenBracketIf == R.BreakAfterOpenBracketIf &&
+ BreakAfterOpenBracketLoop == R.BreakAfterOpenBracketLoop &&
+ BreakAfterOpenBracketSwitch == R.BreakAfterOpenBracketSwitch &&
BreakAfterReturnType == R.BreakAfterReturnType &&
BreakArrays == R.BreakArrays &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
BreakBeforeBraces == R.BreakBeforeBraces &&
+ BreakBeforeCloseBracketIf == R.BreakBeforeCloseBracketIf &&
+ BreakBeforeCloseBracketLoop == R.BreakBeforeCloseBracketLoop &&
+ BreakBeforeCloseBracketSwitch == R.BreakBeforeCloseBracketSwitch &&
BreakBeforeConceptDeclarations == R.BreakBeforeConceptDeclarations
&&
BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
BreakBeforeTemplateCloser == R.BreakBeforeTemplateCloser &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index be741ae4d6cde..60289fe680965 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -828,8 +828,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State, bool DryRun,
// parenthesis by disallowing any further line breaks if there is no line
// break after the opening parenthesis. Don't break if it doesn't conserve
// columns.
- auto IsOtherConditional = [&](const FormatToken &Tok) {
- return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) ||
+ auto IsLoopConditional = [&](const FormatToken &Tok) {
+ return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
(Style.isJavaScript() && Tok.is(Keywords.kw_await) && Tok.Previous
&&
Tok.Previous->is(tok::kw_for));
};
@@ -847,12 +847,18 @@ void
ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (Tok.Previous->isIf()) {
/* For backward compatibility, use AlignAfterOpenBracket
* in case AlignAfterControlStatement is not initialized */
- return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine
||
- (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default &&
- Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak);
+ return Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_MultiLine ||
+ Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_Always;
+ }
+ if (IsLoopConditional(*Tok.Previous)) {
+ return Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_MultiLine
||
+ Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_Always;
+ }
+ if (Tok.Previous->is(tok::kw_switch)) {
+ return Style.BreakAfterOpenBracketSwitch ==
+ FormatStyle::BAOBSS_MultiLine ||
+ Style.BreakAfterOpenBracketSwitch == FormatStyle::BAOBSS_Always;
}
- if (IsOtherConditional(*Tok.Previous))
- return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
return !Tok.Previous->is(TT_CastRParen) &&
@@ -897,8 +903,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState
&State, bool DryRun,
}
const auto *Previous = TokAfterLParen.Previous;
assert(Previous); // IsOpeningBracket(Previous)
- if (Previous->Previous && (Previous->Previous->isIf() ||
- IsOtherConditional(*Previous->Previous))) {
+ if (Previous->Previous &&
+ (Previous->Previous->isIf() || IsLoopConditional(*Previous->Previous)
||
+ Previous->Previous->is(tok::kw_switch))) {
return false;
}
if (!Previous->isOneOf(TT_FunctionDeclarationLParen,
@@ -1280,16 +1287,32 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
auto Previous = PreviousNonComment->Previous;
- if (Previous &&
- (Previous->isIf() ||
- Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch))) {
- CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine &&
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
- } else {
- CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ if (Previous) {
+
+ auto IsLoopConditional = [&](const FormatToken &Tok) {
+ return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
+ (Style.isJavaScript() && Tok.is(Keywords.kw_await) &&
+ Tok.Previous && Tok.Previous->is(tok::kw_for));
+ };
+
+ if (Previous->isIf()) {
+ CurrentState.BreakBeforeClosingParen =
+ Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
+ Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always;
+ } else if (IsLoopConditional(*Previous)) {
+ CurrentState.BreakBeforeClosingParen =
+ Style.BreakBeforeCloseBracketLoop ==
+ FormatStyle::BBCBLS_MultiLine ||
+ Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always;
+ } else if (Previous->is(tok::kw_switch)) {
+ CurrentState.BreakBeforeClosingParen =
+ Style.BreakBeforeCloseBracketSwitch ==
+ FormatStyle::BBCBSS_MultiLine ||
+ Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always;
+ }
}
+ CurrentState.BreakBeforeClosingParen =
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 42da37a331740..964b7b39364c9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,16 +203,6 @@ template <> struct
MappingTraits<FormatStyle::BraceWrappingFlags> {
}
};
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakAfterControlStatementStyle> {
- static void enumeration(IO &IO,
- FormatStyle::BreakAfterControlStatementStyle &Value)
{
- IO.enumCase(Value, "Default", FormatStyle::BACSS_Default);
- IO.enumCase(Value, "MultiLine", FormatStyle::BACSS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BACSS_No);
- }
-};
-
template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle>
{
static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
@@ -242,6 +232,67 @@ struct ScalarEnumerationTraits<
}
};
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketIfStyle> {
+ static void enumeration(IO &IO,
+ FormatStyle::BreakAfterOpenBracketIfStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BAOBIS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BAOBIS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BAOBIS_No);
+ }
+};
+
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketLoopStyle> {
+ static void enumeration(IO &IO,
+ FormatStyle::BreakAfterOpenBracketLoopStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BAOBLS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BAOBLS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BAOBLS_No);
+ }
+};
+
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketSwitchStyle> {
+ static void
+ enumeration(IO &IO, FormatStyle::BreakAfterOpenBracketSwitchStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BAOBSS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BAOBSS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BAOBSS_No);
+ }
+};
+
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakBeforeCloseBracketIfStyle> {
+ static void enumeration(IO &IO,
+ FormatStyle::BreakBeforeCloseBracketIfStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BBCBIS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BBCBIS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BBCBIS_No);
+ }
+};
+
+template <>
+struct ScalarEnumerationTraits<FormatStyle::BreakBeforeCloseBracketLoopStyle> {
+ static void
+ enumeration(IO &IO, FormatStyle::BreakBeforeCloseBracketLoopStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BBCBLS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BBCBLS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BBCBLS_No);
+ }
+};
+
+template <>
+struct ScalarEnumerationTraits<
+ FormatStyle::BreakBeforeCloseBracketSwitchStyle> {
+ static void
+ enumeration(IO &IO, FormatStyle::BreakBeforeCloseBracketSwitchStyle &Value) {
+ IO.enumCase(Value, "Always", FormatStyle::BBCBSS_Always);
+ IO.enumCase(Value, "MultiLine", FormatStyle::BBCBSS_MultiLine);
+ IO.enumCase(Value, "No", FormatStyle::BBCBSS_No);
+ }
+};
+
template <>
struct ScalarEnumerationTraits<
FormatStyle::BreakBeforeConceptDeclarationsStyle> {
@@ -992,8 +1043,6 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
- IO.mapOptional("AlignAfterControlStatement",
- Style.AlignAfterControlStatement);
IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures);
IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
@@ -1056,10 +1105,21 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes);
IO.mapOptional("BreakAfterJavaFieldAnnotations",
Style.BreakAfterJavaFieldAnnotations);
+ IO.mapOptional("BreakAfterOpenBracketIf", Style.BreakAfterOpenBracketIf);
+ IO.mapOptional("BreakAfterOpenBracketLoop",
+ Style.BreakAfterOpenBracketLoop);
+ IO.mapOptional("BreakAfterOpenBracketSwitch",
+ Style.BreakAfterOpenBracketSwitch);
IO.mapOptional("BreakAfterReturnType", Style.BreakAfterReturnType);
IO.mapOptional("BreakArrays", Style.BreakArrays);
IO.mapOptional("BreakBeforeBinaryOperators",
Style.BreakBeforeBinaryOperators);
+ IO.mapOptional("BreakBeforeCloseBracketIf",
+ Style.BreakBeforeCloseBracketIf);
+ IO.mapOptional("BreakBeforeCloseBracketLoop",
+ Style.BreakBeforeCloseBracketLoop);
+ IO.mapOptional("BreakBeforeCloseBracketSwitch",
+ Style.BreakBeforeCloseBracketSwitch);
IO.mapOptional("BreakBeforeConceptDeclarations",
Style.BreakBeforeConceptDeclarations);
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
@@ -1537,7 +1597,6 @@ static void expandPresetsSpacesInParens(FormatStyle
&Expanded) {
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2;
- LLVMStyle.AlignAfterControlStatement = FormatStyle::BACSS_Default;
LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignConsecutiveAssignments = {};
@@ -1597,10 +1656,16 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind
Language) {
LLVMStyle.BreakAdjacentStringLiterals = true;
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
+ LLVMStyle.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
+ LLVMStyle.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
+ LLVMStyle.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
LLVMStyle.BreakAfterReturnType = FormatStyle::RTBS_None;
LLVMStyle.BreakArrays = true;
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ LLVMStyle.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
+ LLVMStyle.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
+ LLVMStyle.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
LLVMStyle.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Always;
LLVMStyle.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
LLVMStyle.BreakBeforeTemplateCloser = false;
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index 0e2ef8ec40cc2..551a63ea2d22f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6209,10 +6209,16 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine
&Line,
// We only break before r_paren if we're in a block indented context.
if (Right.is(tok::r_paren)) {
- if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent ||
- !Right.MatchingParen) {
+ bool might_break =
+ Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always ||
+ Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
+ Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always ||
+ Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_MultiLine ||
+ Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always ||
+ Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_MultiLine ||
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ if (!might_break || !Right.MatchingParen)
return false;
- }
auto Next = Right.Next;
if (Next && Next->is(tok::r_paren))
Next = Next->Next;
@@ -6221,11 +6227,28 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine
&Line,
const FormatToken *Previous = Right.MatchingParen->Previous;
if (!Previous)
return true;
- if (Previous->isIf() ||
- Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) {
- return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
+ if (Previous->isIf()) {
+ return Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always ||
+ Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine;
+ }
+ auto IsLoopConditional = [&](const FormatToken &Tok) {
+ return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
+ (Style.isJavaScript() && Tok.is(Keywords.kw_await) &&
+ Tok.Previous && Tok.Previous->is(tok::kw_for));
+ };
+
+ if (IsLoopConditional(*Previous)) {
+ return Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always ||
+ Style.BreakBeforeCloseBracketLoop ==
FormatStyle::BBCBLS_MultiLine;
}
- return true;
+
+ if (Previous->is(tok::kw_switch)) {
+ return Style.BreakBeforeCloseBracketSwitch ==
+ FormatStyle::BBCBSS_Always ||
+ Style.BreakBeforeCloseBracketSwitch ==
+ FormatStyle::BBCBSS_MultiLine;
+ }
+ return Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
diff --git a/clang/unittests/Format/ConfigParseTest.cpp
b/clang/unittests/Format/ConfigParseTest.cpp
index 3ad48f91827cb..5cc916093bdef 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -533,14 +533,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
- Style.AlignAfterControlStatement = FormatStyle::BACSS_Default;
- CHECK_PARSE("AlignAfterControlStatement: MultiLine",
- AlignAfterControlStatement, FormatStyle::BACSS_MultiLine);
- CHECK_PARSE("AlignAfterControlStatement: No", AlignAfterControlStatement,
- FormatStyle::BACSS_No);
- CHECK_PARSE("AlignAfterControlStatement: Default",
AlignAfterControlStatement,
- FormatStyle::BACSS_Default);
-
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
@@ -777,6 +769,30 @@ TEST(ConfigParseTest, ParsesConfiguration) {
" AfterControlStatement: false",
BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
+ CHECK_PARSE("BreakAfterOpenBracketIf: MultiLine", BreakAfterOpenBracketIf,
+ FormatStyle::BAOBIS_MultiLine);
+ CHECK_PARSE("BreakAfterOpenBracketIf: No", BreakAfterOpenBracketIf,
+ FormatStyle::BAOBIS_No);
+ CHECK_PARSE("BreakAfterOpenBracketIf: Always", BreakAfterOpenBracketIf,
+ FormatStyle::BAOBIS_Always);
+
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
+ CHECK_PARSE("BreakAfterOpenBracketLoop: MultiLine",
BreakAfterOpenBracketLoop,
+ FormatStyle::BAOBLS_MultiLine);
+ CHECK_PARSE("BreakAfterOpenBracketLoop: No", BreakAfterOpenBracketLoop,
+ FormatStyle::BAOBLS_No);
+ CHECK_PARSE("BreakAfterOpenBracketLoop: Always", BreakAfterOpenBracketLoop,
+ FormatStyle::BAOBLS_Always);
+
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
+ CHECK_PARSE("BreakAfterOpenBracketSwitch: MultiLine",
+ BreakAfterOpenBracketSwitch, FormatStyle::BAOBSS_MultiLine);
+ CHECK_PARSE("BreakAfterOpenBracketSwitch: No", BreakAfterOpenBracketSwitch,
+ FormatStyle::BAOBSS_No);
+ CHECK_PARSE("BreakAfterOpenBracketSwitch: Always",
+ BreakAfterOpenBracketSwitch, FormatStyle::BAOBSS_Always);
+
Style.BreakAfterReturnType = FormatStyle::RTBS_All;
CHECK_PARSE("BreakAfterReturnType: None", BreakAfterReturnType,
FormatStyle::RTBS_None);
@@ -1091,6 +1107,30 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
FormatStyle::RCPS_OwnLine);
+ Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
+ CHECK_PARSE("BreakBeforeCloseBracketIf: MultiLine",
BreakBeforeCloseBracketIf,
+ FormatStyle::BBCBIS_MultiLine);
+ CHECK_PARSE("BreakBeforeCloseBracketIf: No", BreakBeforeCloseBracketIf,
+ FormatStyle::BBCBIS_No);
+ CHECK_PARSE("BreakBeforeCloseBracketIf: Always", BreakBeforeCloseBracketIf,
+ FormatStyle::BBCBIS_Always);
+
+ Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
+ CHECK_PARSE("BreakBeforeCloseBracketLoop: MultiLine",
+ BreakBeforeCloseBracketLoop, FormatStyle::BBCBLS_MultiLine);
+ CHECK_PARSE("BreakBeforeCloseBracketLoop: No", BreakBeforeCloseBracketLoop,
+ FormatStyle::BBCBLS_No);
+ CHECK_PARSE("BreakBeforeCloseBracketLoop: Always",
+ BreakBeforeCloseBracketLoop, FormatStyle::BBCBLS_Always);
+
+ Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
+ CHECK_PARSE("BreakBeforeCloseBracketSwitch: MultiLine",
+ BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_MultiLine);
+ CHECK_PARSE("BreakBeforeCloseBracketSwitch: No",
+ BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_No);
+ CHECK_PARSE("BreakBeforeCloseBracketSwitch: Always",
+ BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_Always);
+
CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index f227c14ab83df..58809fa5da2b3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9732,7 +9732,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9783,7 +9785,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9832,7 +9836,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9881,7 +9887,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_No;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
verifyFormat("void foo() {\n"
" if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
@@ -9928,7 +9936,12 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
+ Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_MultiLine;
+ Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_MultiLine;
+ Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_MultiLine;
verifyFormat(
"void foo() {\n"
@@ -9980,7 +9993,12 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
- Style.AlignAfterControlStatement = FormatStyle::BACSS_No;
+ Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
+ Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
+ Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
+ Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
+ Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
+ Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
verifyFormat("void foo() {\n"
" if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
>From 65da9f87dcbad4371f9eeed5a3df8f653ba6dcd6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 16:44:48 -0600
Subject: [PATCH 04/11] Change to use bool options for each control statement
---
clang/include/clang/Format/Format.h | 205 ++++++---------------
clang/lib/Format/ContinuationIndenter.cpp | 47 ++---
clang/lib/Format/Format.cpp | 76 +-------
clang/lib/Format/TokenAnnotator.cpp | 37 +---
clang/unittests/Format/ConfigParseTest.cpp | 54 +-----
clang/unittests/Format/FormatTest.cpp | 48 ++---
6 files changed, 121 insertions(+), 346 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 3c408f8caee0d..5a8577a595273 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,86 +62,38 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /// Different styles for breaking the parenthesis after ``if/else if``.
+ /// Force break after the left parenthesis of an if control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// if constexpr ( vs. if constexpr (a ||
+ /// a || b)
+ /// b)
+ /// \endcode
/// \version 21
- enum BreakAfterOpenBracketIfStyle : int8_t {
- /// Always break the opening parenthesis of an if statement, e.g.:
- /// \code
- /// if constexpr (
- /// a)
- /// \endcode
- BAOBIS_Always,
- /// Force break after the left parenthesis of an if statement only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// if constexpr (
- /// a ||
- /// b)
- /// \endcode
- BAOBIS_MultiLine,
- /// Do not force a break after the control statement.
- /// \code
- /// if constexpr (a ||
- /// b
- /// \endcode
- BAOBIS_No,
- };
-
- BreakAfterOpenBracketIfStyle BreakAfterOpenBracketIf;
+ bool BreakAfterOpenBracketIf;
- /// Different styles for breaking the parenthesis after loops
``(for/while)``.
+ /// Force break after the left parenthesis of a loop control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// while ( vs. while (a &&
+ /// a && b) {
+ /// b) {
+ /// \endcode
/// \version 21
- enum BreakAfterOpenBracketLoopStyle : int8_t {
- /// Always break the opening parenthesis of a loop statement, e.g.:
- /// \code
- /// while (
- /// a) {
- /// \endcode
- BAOBLS_Always,
- /// Force break after the left parenthesis of a loop only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// while (
- /// a &&
- /// b) {
- /// \endcode
- BAOBLS_MultiLine,
- /// Do not force a break after the control statement.
- /// \code
- /// while (a &&
- /// b) {
- /// \endcode
- BAOBLS_No,
- };
+ bool BreakAfterOpenBracketLoop;
- BreakAfterOpenBracketLoopStyle BreakAfterOpenBracketLoop;
-
- /// Different styles for breaking the parenthesis after ``switch``.
+ /// Force break after the left parenthesis of a switch control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// switch ( vs. switch (a &&
+ /// a && b) {
+ /// b) {
+ /// \endcode
/// \version 21
- enum BreakAfterOpenBracketSwitchStyle : int8_t {
- /// Always break the opening parenthesis of a switch statement, e.g.:
- /// \code
- /// switch (
- /// a) {
- /// \endcode
- BAOBSS_Always,
- /// Force break after the left parenthesis of a switch only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// switch (
- /// a &&
- /// b) {
- /// \endcode
- BAOBSS_MultiLine,
- /// Do not force a break after the control statement.
- /// \code
- /// switch (a &&
- /// b) {
- /// \endcode
- BAOBSS_No,
- };
-
- BreakAfterOpenBracketSwitchStyle BreakAfterOpenBracketSwitch;
+ bool BreakAfterOpenBracketSwitch;
/// Different styles for aligning after open brackets.
enum BracketAlignmentStyle : int8_t {
@@ -2296,87 +2248,38 @@ struct FormatStyle {
/// \version 3.7
BraceBreakingStyle BreakBeforeBraces;
- /// Different styles for breaking before ``if/else if`` closing parenthesis.
+ /// Force break before the right parenthesis of an if control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// if constexpr (a || vs. if constexpr (a ||
+ /// b b)
+ /// )
+ /// \endcode
/// \version 21
- enum BreakBeforeCloseBracketIfStyle : int8_t {
- /// Always break the closing parenthesis of an if statement, e.g.:
- /// \code
- /// if constexpr (a
- /// )
- /// \endcode
- BBCBIS_Always,
- /// Force break before the closing parenthesis of an if statement only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// if constexpr (a ||
- /// b
- /// )
- /// \endcode
- BBCBIS_MultiLine,
- /// Do not force a break before closing the if control statement.
- /// \code
- /// if constexpr (a ||
- /// b)
- /// \endcode
- BBCBIS_No,
- };
-
- BreakBeforeCloseBracketIfStyle BreakBeforeCloseBracketIf;
+ bool BreakBeforeCloseBracketIf;
- /// Different styles for breaking before loop ``(for/while)`` closing
- /// parenthesis.
+ /// Force break before the right parenthesis of a loop control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// while (a && vs. while (a &&
+ /// b b) {
+ /// ) {
+ /// \endcode
/// \version 21
- enum BreakBeforeCloseBracketLoopStyle : int8_t {
- /// Always break the closing parenthesis of a loop statement, e.g.:
- /// \code
- /// while (a
- /// ) {
- /// \endcode
- BBCBLS_Always,
- /// Force break before the closing parenthesis of a loop only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// while (a &&
- /// b
- /// ) {
- /// \endcode
- BBCBLS_MultiLine,
- /// Do not force a break before closing the loop control statement.
- /// \code
- /// while (a &&
- /// b) {
- /// \endcode
- BBCBLS_No,
- };
+ bool BreakBeforeCloseBracketLoop;
- BreakBeforeCloseBracketLoopStyle BreakBeforeCloseBracketLoop;
-
- /// Different styles for breaking before ``switch`` closing parenthesis.
+ /// Force break before the right parenthesis of a switch control statement
+ /// when the expression exceeds the column limit.
+ /// \code
+ /// true: false:
+ /// switch (a && vs. switch (a &&
+ /// b b) {
+ /// ) {
+ /// \endcode
/// \version 21
- enum BreakBeforeCloseBracketSwitchStyle : int8_t {
- /// Always break before the closing parenthesis of a switch statement,
e.g.:
- /// \code
- /// switch (a
- /// ) {
- /// \endcode
- BBCBSS_Always,
- /// Force break before the closing parenthesis of a switch only
- /// when the expression exceeds the column limit, e.g..:
- /// \code
- /// switch (a &&
- /// b
- /// ) {
- /// \endcode
- BBCBSS_MultiLine,
- /// Do not force a break before closing the switch control statement.
- /// \code
- /// switch (a &&
- /// b) {
- /// \endcode
- BBCBSS_No,
- };
-
- BreakBeforeCloseBracketSwitchStyle BreakBeforeCloseBracketSwitch;
+ bool BreakBeforeCloseBracketSwitch;
/// Different ways to break before concept declarations.
enum BreakBeforeConceptDeclarationsStyle : int8_t {
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index 60289fe680965..bc0ef6ed7232e 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -358,10 +358,13 @@ bool ContinuationIndenter::canBreak(const LineState
&State) {
// Allow breaking before the right parens with block indentation if there was
// a break after the left parens, which is tracked by
BreakBeforeClosingParen.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
- Current.is(tok::r_paren)) {
+ bool might_break_before =
+ Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
+ Style.BreakBeforeCloseBracketSwitch ||
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+
+ if (might_break_before && Current.is(tok::r_paren))
return CurrentState.BreakBeforeClosingParen;
- }
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser))
return CurrentState.BreakBeforeClosingAngle;
@@ -844,21 +847,12 @@ void
ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
}
if (!Tok.Previous)
return true;
- if (Tok.Previous->isIf()) {
- /* For backward compatibility, use AlignAfterOpenBracket
- * in case AlignAfterControlStatement is not initialized */
- return Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_MultiLine ||
- Style.BreakAfterOpenBracketIf == FormatStyle::BAOBIS_Always;
- }
- if (IsLoopConditional(*Tok.Previous)) {
- return Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_MultiLine
||
- Style.BreakAfterOpenBracketLoop == FormatStyle::BAOBLS_Always;
- }
- if (Tok.Previous->is(tok::kw_switch)) {
- return Style.BreakAfterOpenBracketSwitch ==
- FormatStyle::BAOBSS_MultiLine ||
- Style.BreakAfterOpenBracketSwitch == FormatStyle::BAOBSS_Always;
- }
+ if (Tok.Previous->isIf())
+ return Style.BreakAfterOpenBracketIf;
+ if (IsLoopConditional(*Tok.Previous))
+ return Style.BreakAfterOpenBracketLoop;
+ if (Tok.Previous->is(tok::kw_switch))
+ return Style.BreakAfterOpenBracketSwitch;
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
return !Tok.Previous->is(TT_CastRParen) &&
@@ -1296,23 +1290,18 @@ unsigned
ContinuationIndenter::addTokenOnNewLine(LineState &State,
};
if (Previous->isIf()) {
- CurrentState.BreakBeforeClosingParen =
- Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
- Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always;
+ CurrentState.BreakBeforeClosingParen = Style.BreakBeforeCloseBracketIf;
} else if (IsLoopConditional(*Previous)) {
CurrentState.BreakBeforeClosingParen =
- Style.BreakBeforeCloseBracketLoop ==
- FormatStyle::BBCBLS_MultiLine ||
- Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always;
+ Style.BreakBeforeCloseBracketLoop;
} else if (Previous->is(tok::kw_switch)) {
CurrentState.BreakBeforeClosingParen =
- Style.BreakBeforeCloseBracketSwitch ==
- FormatStyle::BBCBSS_MultiLine ||
- Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always;
+ Style.BreakBeforeCloseBracketSwitch;
+ } else {
+ CurrentState.BreakBeforeClosingParen =
+ Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
}
- CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 964b7b39364c9..0ad56e30502ea 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -232,67 +232,6 @@ struct ScalarEnumerationTraits<
}
};
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketIfStyle> {
- static void enumeration(IO &IO,
- FormatStyle::BreakAfterOpenBracketIfStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BAOBIS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BAOBIS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BAOBIS_No);
- }
-};
-
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketLoopStyle> {
- static void enumeration(IO &IO,
- FormatStyle::BreakAfterOpenBracketLoopStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BAOBLS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BAOBLS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BAOBLS_No);
- }
-};
-
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakAfterOpenBracketSwitchStyle> {
- static void
- enumeration(IO &IO, FormatStyle::BreakAfterOpenBracketSwitchStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BAOBSS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BAOBSS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BAOBSS_No);
- }
-};
-
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakBeforeCloseBracketIfStyle> {
- static void enumeration(IO &IO,
- FormatStyle::BreakBeforeCloseBracketIfStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BBCBIS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BBCBIS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BBCBIS_No);
- }
-};
-
-template <>
-struct ScalarEnumerationTraits<FormatStyle::BreakBeforeCloseBracketLoopStyle> {
- static void
- enumeration(IO &IO, FormatStyle::BreakBeforeCloseBracketLoopStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BBCBLS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BBCBLS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BBCBLS_No);
- }
-};
-
-template <>
-struct ScalarEnumerationTraits<
- FormatStyle::BreakBeforeCloseBracketSwitchStyle> {
- static void
- enumeration(IO &IO, FormatStyle::BreakBeforeCloseBracketSwitchStyle &Value) {
- IO.enumCase(Value, "Always", FormatStyle::BBCBSS_Always);
- IO.enumCase(Value, "MultiLine", FormatStyle::BBCBSS_MultiLine);
- IO.enumCase(Value, "No", FormatStyle::BBCBSS_No);
- }
-};
-
template <>
struct ScalarEnumerationTraits<
FormatStyle::BreakBeforeConceptDeclarationsStyle> {
@@ -1656,16 +1595,16 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind
Language) {
LLVMStyle.BreakAdjacentStringLiterals = true;
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
- LLVMStyle.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
- LLVMStyle.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
- LLVMStyle.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
+ LLVMStyle.BreakAfterOpenBracketIf = false;
+ LLVMStyle.BreakAfterOpenBracketLoop = false;
+ LLVMStyle.BreakAfterOpenBracketSwitch = false;
LLVMStyle.BreakAfterReturnType = FormatStyle::RTBS_None;
LLVMStyle.BreakArrays = true;
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
- LLVMStyle.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
- LLVMStyle.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
- LLVMStyle.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
+ LLVMStyle.BreakBeforeCloseBracketIf = false;
+ LLVMStyle.BreakBeforeCloseBracketLoop = false;
+ LLVMStyle.BreakBeforeCloseBracketSwitch = false;
LLVMStyle.BreakBeforeConceptDeclarations = FormatStyle::BBCDS_Always;
LLVMStyle.BreakBeforeInlineASMColon = FormatStyle::BBIAS_OnlyMultiline;
LLVMStyle.BreakBeforeTemplateCloser = false;
@@ -1927,6 +1866,9 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind
Language) {
GoogleStyle.SpacesBeforeTrailingComments = 1;
} else if (Language == FormatStyle::LK_JavaScript) {
GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ GoogleStyle.BreakAfterOpenBracketIf = true;
+ GoogleStyle.BreakAfterOpenBracketLoop = false;
+ GoogleStyle.BreakAfterOpenBracketSwitch = false;
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign;
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
// TODO: still under discussion whether to switch to SLS_All.
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index 551a63ea2d22f..934d181f1c96e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6207,17 +6207,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine
&Line,
(Right.isBlockIndentedInitRBrace(Style)));
}
- // We only break before r_paren if we're in a block indented context.
+ // We can break before r_paren if we're in a block indented context or
+ // a control statement with an explicit style option.
if (Right.is(tok::r_paren)) {
- bool might_break =
- Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always ||
- Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine ||
- Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always ||
- Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_MultiLine ||
- Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_Always ||
- Style.BreakBeforeCloseBracketSwitch == FormatStyle::BBCBSS_MultiLine ||
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
- if (!might_break || !Right.MatchingParen)
+ if (!Right.MatchingParen)
return false;
auto Next = Right.Next;
if (Next && Next->is(tok::r_paren))
@@ -6226,28 +6219,18 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine
&Line,
return false;
const FormatToken *Previous = Right.MatchingParen->Previous;
if (!Previous)
- return true;
- if (Previous->isIf()) {
- return Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_Always ||
- Style.BreakBeforeCloseBracketIf == FormatStyle::BBCBIS_MultiLine;
- }
+ return false;
+ if (Previous->isIf())
+ return Style.BreakBeforeCloseBracketIf;
auto IsLoopConditional = [&](const FormatToken &Tok) {
return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
(Style.isJavaScript() && Tok.is(Keywords.kw_await) &&
Tok.Previous && Tok.Previous->is(tok::kw_for));
};
-
- if (IsLoopConditional(*Previous)) {
- return Style.BreakBeforeCloseBracketLoop == FormatStyle::BBCBLS_Always ||
- Style.BreakBeforeCloseBracketLoop ==
FormatStyle::BBCBLS_MultiLine;
- }
-
- if (Previous->is(tok::kw_switch)) {
- return Style.BreakBeforeCloseBracketSwitch ==
- FormatStyle::BBCBSS_Always ||
- Style.BreakBeforeCloseBracketSwitch ==
- FormatStyle::BBCBSS_MultiLine;
- }
+ if (IsLoopConditional(*Previous))
+ return Style.BreakBeforeCloseBracketLoop;
+ if (Previous->is(tok::kw_switch))
+ return Style.BreakBeforeCloseBracketSwitch;
return Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp
b/clang/unittests/Format/ConfigParseTest.cpp
index 5cc916093bdef..cefe6689ad15d 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -171,6 +171,12 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(BinPackLongBracedList);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
+ CHECK_PARSE_BOOL(BreakAfterOpenBracketIf);
+ CHECK_PARSE_BOOL(BreakAfterOpenBracketLoop);
+ CHECK_PARSE_BOOL(BreakAfterOpenBracketSwitch);
+ CHECK_PARSE_BOOL(BreakBeforeCloseBracketIf);
+ CHECK_PARSE_BOOL(BreakBeforeCloseBracketLoop);
+ CHECK_PARSE_BOOL(BreakBeforeCloseBracketSwitch);
CHECK_PARSE_BOOL(BreakBeforeTemplateCloser);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
CHECK_PARSE_BOOL(BreakStringLiterals);
@@ -769,30 +775,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
" AfterControlStatement: false",
BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
- CHECK_PARSE("BreakAfterOpenBracketIf: MultiLine", BreakAfterOpenBracketIf,
- FormatStyle::BAOBIS_MultiLine);
- CHECK_PARSE("BreakAfterOpenBracketIf: No", BreakAfterOpenBracketIf,
- FormatStyle::BAOBIS_No);
- CHECK_PARSE("BreakAfterOpenBracketIf: Always", BreakAfterOpenBracketIf,
- FormatStyle::BAOBIS_Always);
-
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
- CHECK_PARSE("BreakAfterOpenBracketLoop: MultiLine",
BreakAfterOpenBracketLoop,
- FormatStyle::BAOBLS_MultiLine);
- CHECK_PARSE("BreakAfterOpenBracketLoop: No", BreakAfterOpenBracketLoop,
- FormatStyle::BAOBLS_No);
- CHECK_PARSE("BreakAfterOpenBracketLoop: Always", BreakAfterOpenBracketLoop,
- FormatStyle::BAOBLS_Always);
-
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
- CHECK_PARSE("BreakAfterOpenBracketSwitch: MultiLine",
- BreakAfterOpenBracketSwitch, FormatStyle::BAOBSS_MultiLine);
- CHECK_PARSE("BreakAfterOpenBracketSwitch: No", BreakAfterOpenBracketSwitch,
- FormatStyle::BAOBSS_No);
- CHECK_PARSE("BreakAfterOpenBracketSwitch: Always",
- BreakAfterOpenBracketSwitch, FormatStyle::BAOBSS_Always);
-
Style.BreakAfterReturnType = FormatStyle::RTBS_All;
CHECK_PARSE("BreakAfterReturnType: None", BreakAfterReturnType,
FormatStyle::RTBS_None);
@@ -1107,30 +1089,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("RequiresClausePosition: OwnLine", RequiresClausePosition,
FormatStyle::RCPS_OwnLine);
- Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
- CHECK_PARSE("BreakBeforeCloseBracketIf: MultiLine",
BreakBeforeCloseBracketIf,
- FormatStyle::BBCBIS_MultiLine);
- CHECK_PARSE("BreakBeforeCloseBracketIf: No", BreakBeforeCloseBracketIf,
- FormatStyle::BBCBIS_No);
- CHECK_PARSE("BreakBeforeCloseBracketIf: Always", BreakBeforeCloseBracketIf,
- FormatStyle::BBCBIS_Always);
-
- Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
- CHECK_PARSE("BreakBeforeCloseBracketLoop: MultiLine",
- BreakBeforeCloseBracketLoop, FormatStyle::BBCBLS_MultiLine);
- CHECK_PARSE("BreakBeforeCloseBracketLoop: No", BreakBeforeCloseBracketLoop,
- FormatStyle::BBCBLS_No);
- CHECK_PARSE("BreakBeforeCloseBracketLoop: Always",
- BreakBeforeCloseBracketLoop, FormatStyle::BBCBLS_Always);
-
- Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
- CHECK_PARSE("BreakBeforeCloseBracketSwitch: MultiLine",
- BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_MultiLine);
- CHECK_PARSE("BreakBeforeCloseBracketSwitch: No",
- BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_No);
- CHECK_PARSE("BreakBeforeCloseBracketSwitch: Always",
- BreakBeforeCloseBracketSwitch, FormatStyle::BBCBSS_Always);
-
CHECK_PARSE("BreakBeforeConceptDeclarations: Never",
BreakBeforeConceptDeclarations, FormatStyle::BBCDS_Never);
CHECK_PARSE("BreakBeforeConceptDeclarations: Always",
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 58809fa5da2b3..a69897cb1b1a8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9732,9 +9732,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakAfterOpenBracketLoop = true;
+ Style.BreakAfterOpenBracketSwitch = true;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9785,9 +9785,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakAfterOpenBracketLoop = true;
+ Style.BreakAfterOpenBracketSwitch = true;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9836,9 +9836,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakAfterOpenBracketLoop = true;
+ Style.BreakAfterOpenBracketSwitch = true;
verifyFormat("void foo() {\n"
" if constexpr (\n"
@@ -9887,9 +9887,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
+ Style.BreakAfterOpenBracketIf = false;
+ Style.BreakAfterOpenBracketLoop = false;
+ Style.BreakAfterOpenBracketSwitch = false;
verifyFormat("void foo() {\n"
" if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
@@ -9936,12 +9936,12 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_MultiLine;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_MultiLine;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_MultiLine;
- Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_MultiLine;
- Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_MultiLine;
- Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_MultiLine;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakAfterOpenBracketLoop = true;
+ Style.BreakAfterOpenBracketSwitch = true;
+ Style.BreakBeforeCloseBracketIf = true;
+ Style.BreakBeforeCloseBracketLoop = true;
+ Style.BreakBeforeCloseBracketSwitch = true;
verifyFormat(
"void foo() {\n"
@@ -9993,12 +9993,12 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
- Style.BreakAfterOpenBracketIf = FormatStyle::BAOBIS_No;
- Style.BreakAfterOpenBracketLoop = FormatStyle::BAOBLS_No;
- Style.BreakAfterOpenBracketSwitch = FormatStyle::BAOBSS_No;
- Style.BreakBeforeCloseBracketIf = FormatStyle::BBCBIS_No;
- Style.BreakBeforeCloseBracketLoop = FormatStyle::BBCBLS_No;
- Style.BreakBeforeCloseBracketSwitch = FormatStyle::BBCBSS_No;
+ Style.BreakAfterOpenBracketIf = false;
+ Style.BreakAfterOpenBracketLoop = false;
+ Style.BreakAfterOpenBracketSwitch = false;
+ Style.BreakBeforeCloseBracketIf = false;
+ Style.BreakBeforeCloseBracketLoop = false;
+ Style.BreakBeforeCloseBracketSwitch = false;
verifyFormat("void foo() {\n"
" if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
>From b1e9285eeb698c8b0f13b1baea2e9feef29f624d Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 17:39:58 -0600
Subject: [PATCH 05/11] update clang-format-style
---
clang/docs/ClangFormatStyleOptions.rst | 78 ++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/clang/docs/ClangFormatStyleOptions.rst
b/clang/docs/ClangFormatStyleOptions.rst
index 3ac9e3795cae7..f0d2ceebab888 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2739,6 +2739,45 @@ the configuration (without a prefix: ``Auto``).
@Mock
DataLoad loader;
+.. _BreakAfterOpenBracketIf:
+
+**BreakAfterOpenBracketIf** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakAfterOpenBracketIf>`
+ Force break after the left parenthesis of an if control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ if constexpr ( vs. if constexpr (a ||
+ a || b)
+ b)
+
+.. _BreakAfterOpenBracketLoop:
+
+**BreakAfterOpenBracketLoop** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakAfterOpenBracketLoop>`
+ Force break after the left parenthesis of a loop control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ while ( vs. while (a &&
+ a && b) {
+ b) {
+
+.. _BreakAfterOpenBracketSwitch:
+
+**BreakAfterOpenBracketSwitch** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakAfterOpenBracketSwitch>`
+ Force break after the left parenthesis of a switch control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ switch ( vs. switch (a &&
+ a && b) {
+ b) {
+
.. _BreakAfterReturnType:
**BreakAfterReturnType** (``ReturnTypeBreakingStyle``)
:versionbadge:`clang-format 19` :ref:`¶ <BreakAfterReturnType>`
@@ -3376,6 +3415,45 @@ the configuration (without a prefix: ``Auto``).
+.. _BreakBeforeCloseBracketIf:
+
+**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakBeforeCloseBracketIf>`
+ Force break before the right parenthesis of an if control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ if constexpr (a || vs. if constexpr (a ||
+ b b)
+ )
+
+.. _BreakBeforeCloseBracketLoop:
+
+**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakBeforeCloseBracketLoop>`
+ Force break before the right parenthesis of a loop control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ while (a && vs. while (a &&
+ b b) {
+ ) {
+
+.. _BreakBeforeCloseBracketSwitch:
+
+**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format
21` :ref:`¶ <BreakBeforeCloseBracketSwitch>`
+ Force break before the right parenthesis of a switch control statement
+ when the expression exceeds the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ switch (a && vs. switch (a &&
+ b b) {
+ ) {
+
.. _BreakBeforeConceptDeclarations:
**BreakBeforeConceptDeclarations** (``BreakBeforeConceptDeclarationsStyle``)
:versionbadge:`clang-format 12` :ref:`¶ <BreakBeforeConceptDeclarations>`
>From c6bd432a5d52c04f36fb42cf1cdbe1f11f85899b Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 17:47:09 -0600
Subject: [PATCH 06/11] update tests
---
clang/unittests/Format/FormatTest.cpp | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index a69897cb1b1a8..605e7bd6767d9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9728,7 +9728,7 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
Style);
}
-TEST_F(FormatTest, AlignAfterConditionalStatements) {
+TEST_F(FormatTest, AlignAndBreakControlStatements) {
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
@@ -9803,6 +9803,20 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
" }\n"
"}",
Style);
+ Style.BreakAfterOpenBracketIf = false;
+ verifyFormat("void foo() {\n"
+ " if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbbbbb) ==\n"
+ " 0) {\n"
+ " return;\n"
+ " } else if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
+ "bbbbbbb) == 0) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
verifyFormat("void foo() {\n"
" switch (\n"
>From b4a31e9ea81d6d1c59e9baeec815e1aadf50b401 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 20:59:10 -0600
Subject: [PATCH 07/11] updates
---
clang/include/clang/Format/Format.h | 39 ++++++++++++-----------
clang/lib/Format/ContinuationIndenter.cpp | 5 +++
clang/unittests/Format/FormatTest.cpp | 3 ++
3 files changed, 29 insertions(+), 18 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 5a8577a595273..5abd952f5bbfe 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -67,8 +67,7 @@ struct FormatStyle {
/// \code
/// true: false:
/// if constexpr ( vs. if constexpr (a ||
- /// a || b)
- /// b)
+ /// a || b) b)
/// \endcode
/// \version 21
bool BreakAfterOpenBracketIf;
@@ -78,8 +77,7 @@ struct FormatStyle {
/// \code
/// true: false:
/// while ( vs. while (a &&
- /// a && b) {
- /// b) {
+ /// a && b) { b) {
/// \endcode
/// \version 21
bool BreakAfterOpenBracketLoop;
@@ -89,8 +87,7 @@ struct FormatStyle {
/// \code
/// true: false:
/// switch ( vs. switch (a &&
- /// a && b) {
- /// b) {
+ /// a && b) { b) {
/// \endcode
/// \version 21
bool BreakAfterOpenBracketSwitch;
@@ -2249,34 +2246,40 @@ struct FormatStyle {
BraceBreakingStyle BreakBeforeBraces;
/// Force break before the right parenthesis of an if control statement
- /// when the expression exceeds the column limit.
+ /// when the expression exceeds the column limit. The break before the
+ /// closing parenthesis is only made if there is a break after the opening
+ /// parenthesis.
/// \code
/// true: false:
- /// if constexpr (a || vs. if constexpr (a ||
- /// b b)
- /// )
+ /// if constexpr ( vs. if constexpr (a ||
+ /// a || b b)
+ /// )
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketIf;
/// Force break before the right parenthesis of a loop control statement
- /// when the expression exceeds the column limit.
+ /// when the expression exceeds the column limit. The break before the
+ /// closing parenthesis is only made if there is a break after the opening
+ /// parenthesis.
/// \code
/// true: false:
- /// while (a && vs. while (a &&
- /// b b) {
- /// ) {
+ /// while ( vs. while (a &&
+ /// a && b b) {
+ /// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketLoop;
/// Force break before the right parenthesis of a switch control statement
- /// when the expression exceeds the column limit.
+ /// when the expression exceeds the column limit. The break before the
+ /// closing parenthesis is only made if there is a break after the opening
+ /// parenthesis.
/// \code
/// true: false:
- /// switch (a && vs. switch (a &&
- /// b b) {
- /// ) {
+ /// switch ( vs. switch (a &&
+ /// a && b b) {
+ /// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketSwitch;
diff --git a/clang/lib/Format/ContinuationIndenter.cpp
b/clang/lib/Format/ContinuationIndenter.cpp
index bc0ef6ed7232e..b50aa3674bebd 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1451,6 +1451,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const
LineState &State) {
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
+ if ((Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
+ Style.BreakBeforeCloseBracketSwitch) &&
+ Current.is(tok::r_paren) && State.Stack.size() > 1) {
+ return State.Stack[State.Stack.size() - 2].LastSpace;
+ }
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser) &&
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 605e7bd6767d9..19a36c2b474bb 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9788,6 +9788,9 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
+ Style.BreakBeforeCloseBracketIf = false;
+ Style.BreakBeforeCloseBracketLoop = false;
+ Style.BreakBeforeCloseBracketSwitch = false;
verifyFormat("void foo() {\n"
" if constexpr (\n"
>From 7c4ee65f94e1147b61295279a1c8deeb5c2ff63d Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Wed, 21 May 2025 20:59:31 -0600
Subject: [PATCH 08/11] dump format style
---
clang/docs/ClangFormatStyleOptions.rst | 39 ++++++++++++++------------
1 file changed, 21 insertions(+), 18 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst
b/clang/docs/ClangFormatStyleOptions.rst
index f0d2ceebab888..0823b687cef29 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2749,8 +2749,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
if constexpr ( vs. if constexpr (a ||
- a || b)
- b)
+ a || b) b)
.. _BreakAfterOpenBracketLoop:
@@ -2762,8 +2761,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
while ( vs. while (a &&
- a && b) {
- b) {
+ a && b) { b) {
.. _BreakAfterOpenBracketSwitch:
@@ -2775,8 +2773,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
switch ( vs. switch (a &&
- a && b) {
- b) {
+ a && b) { b) {
.. _BreakAfterReturnType:
@@ -3419,40 +3416,46 @@ the configuration (without a prefix: ``Auto``).
**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakBeforeCloseBracketIf>`
Force break before the right parenthesis of an if control statement
- when the expression exceeds the column limit.
+ when the expression exceeds the column limit. The break before the
+ closing parenthesis is only made if there is a break after the opening
+ parenthesis.
.. code-block:: c++
true: false:
- if constexpr (a || vs. if constexpr (a ||
- b b)
- )
+ if constexpr ( vs. if constexpr (a ||
+ a || b b)
+ )
.. _BreakBeforeCloseBracketLoop:
**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21`
:ref:`¶ <BreakBeforeCloseBracketLoop>`
Force break before the right parenthesis of a loop control statement
- when the expression exceeds the column limit.
+ when the expression exceeds the column limit. The break before the
+ closing parenthesis is only made if there is a break after the opening
+ parenthesis.
.. code-block:: c++
true: false:
- while (a && vs. while (a &&
- b b) {
- ) {
+ while ( vs. while (a &&
+ a && b b) {
+ ) {
.. _BreakBeforeCloseBracketSwitch:
**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format
21` :ref:`¶ <BreakBeforeCloseBracketSwitch>`
Force break before the right parenthesis of a switch control statement
- when the expression exceeds the column limit.
+ when the expression exceeds the column limit. The break before the
+ closing parenthesis is only made if there is a break after the opening
+ parenthesis.
.. code-block:: c++
true: false:
- switch (a && vs. switch (a &&
- b b) {
- ) {
+ switch ( vs. switch (a &&
+ a && b b) {
+ ) {
.. _BreakBeforeConceptDeclarations:
>From 60c53d2463299c686e799d5939636512ee6d06da Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Thu, 22 May 2025 07:57:03 -0600
Subject: [PATCH 09/11] fix format
---
clang/include/clang/Format/Format.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 5abd952f5bbfe..2e3fbb5710a4e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2253,7 +2253,7 @@ struct FormatStyle {
/// true: false:
/// if constexpr ( vs. if constexpr (a ||
/// a || b b)
- /// )
+ /// )
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketIf;
@@ -2266,7 +2266,7 @@ struct FormatStyle {
/// true: false:
/// while ( vs. while (a &&
/// a && b b) {
- /// ) {
+ /// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketLoop;
@@ -2279,7 +2279,7 @@ struct FormatStyle {
/// true: false:
/// switch ( vs. switch (a &&
/// a && b b) {
- /// ) {
+ /// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketSwitch;
>From 17176874fb6c123af1ef40f0122aaa38403f067e Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Thu, 22 May 2025 08:07:59 -0600
Subject: [PATCH 10/11] Fix false case examples
---
clang/include/clang/Format/Format.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Format/Format.h
b/clang/include/clang/Format/Format.h
index 2e3fbb5710a4e..2d7a933cc7223 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2251,8 +2251,8 @@ struct FormatStyle {
/// parenthesis.
/// \code
/// true: false:
- /// if constexpr ( vs. if constexpr (a ||
- /// a || b b)
+ /// if constexpr ( vs. if constexpr (
+ /// a || b a || b )
/// )
/// \endcode
/// \version 21
@@ -2264,8 +2264,8 @@ struct FormatStyle {
/// parenthesis.
/// \code
/// true: false:
- /// while ( vs. while (a &&
- /// a && b b) {
+ /// while ( vs. while (
+ /// a && b a && b) {
/// ) {
/// \endcode
/// \version 21
@@ -2277,8 +2277,8 @@ struct FormatStyle {
/// parenthesis.
/// \code
/// true: false:
- /// switch ( vs. switch (a &&
- /// a && b b) {
+ /// switch ( vs. switch (
+ /// a && b a && b) {
/// ) {
/// \endcode
/// \version 21
>From 00e4968afd1e2b1e6876148f1ddaafc942b73f89 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <ged...@rtems.org>
Date: Thu, 22 May 2025 08:08:12 -0600
Subject: [PATCH 11/11] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst
b/clang/docs/ClangFormatStyleOptions.rst
index 0823b687cef29..109d40d01a8b4 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3423,8 +3423,8 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
true: false:
- if constexpr ( vs. if constexpr (a ||
- a || b b)
+ if constexpr ( vs. if constexpr (
+ a || b a || b )
)
.. _BreakBeforeCloseBracketLoop:
@@ -3438,8 +3438,8 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
true: false:
- while ( vs. while (a &&
- a && b b) {
+ while ( vs. while (
+ a && b a && b) {
) {
.. _BreakBeforeCloseBracketSwitch:
@@ -3453,8 +3453,8 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
true: false:
- switch ( vs. switch (a &&
- a && b b) {
+ switch ( vs. switch (
+ a && b a && b) {
) {
.. _BreakBeforeConceptDeclarations:
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits