[clang] [clang-format] Add BreakAfterOpenBracket* and BreakBeforeCloseBracket* (PR #108332)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 25 09:53:16 PDT 2025
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332
>From 5823461e952833db484074ec0b9db896f96d4a03 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Thu, 20 Jun 2024 17:35:39 -0600
Subject: [PATCH 01/26] 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 7b855ebf8d403aa1718e7d1b109626154b0e4217 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 11 Sep 2024 22:48:12 -0600
Subject: [PATCH 02/26] 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 0c44c5eed0afbe3557f5b6381f4b25c0b651dcb6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 16:11:23 -0600
Subject: [PATCH 03/26] 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 be2f49656172605e52a16fdd84c844c463f16b8d Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 16:44:48 -0600
Subject: [PATCH 04/26] 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 30016c3d33ac631c0ccf05c1418973103dc6a600 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 17:39:58 -0600
Subject: [PATCH 05/26] 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 5f8b18f4f233072c7464d77bf736611c409fac3c Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 17:47:09 -0600
Subject: [PATCH 06/26] 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 b13df16acf73b51752d0cb6a8fb785e629da0a31 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 20:59:10 -0600
Subject: [PATCH 07/26] 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 fc9b4f390eccf606cf2ed8faad7909c09abee985 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Thu, 22 May 2025 07:57:03 -0600
Subject: [PATCH 08/26] 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 cf1f9b46595aa52edbfb4fd298b7fade55484754 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Thu, 22 May 2025 08:07:59 -0600
Subject: [PATCH 09/26] 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 d51f84c0250f953825541865953bdbd103f1bdf7 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 10:43:12 -0600
Subject: [PATCH 10/26] update version numbers to 22
---
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 2d7a933cc7223..f01f09bc58c69 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -69,7 +69,7 @@ struct FormatStyle {
/// if constexpr ( vs. if constexpr (a ||
/// a || b) b)
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakAfterOpenBracketIf;
/// Force break after the left parenthesis of a loop control statement
@@ -79,7 +79,7 @@ struct FormatStyle {
/// while ( vs. while (a &&
/// a && b) { b) {
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakAfterOpenBracketLoop;
/// Force break after the left parenthesis of a switch control statement
@@ -89,7 +89,7 @@ struct FormatStyle {
/// switch ( vs. switch (a &&
/// a && b) { b) {
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakAfterOpenBracketSwitch;
/// Different styles for aligning after open brackets.
@@ -2255,7 +2255,7 @@ struct FormatStyle {
/// a || b a || b )
/// )
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakBeforeCloseBracketIf;
/// Force break before the right parenthesis of a loop control statement
@@ -2268,7 +2268,7 @@ struct FormatStyle {
/// a && b a && b) {
/// ) {
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakBeforeCloseBracketLoop;
/// Force break before the right parenthesis of a switch control statement
@@ -2281,7 +2281,7 @@ struct FormatStyle {
/// a && b a && b) {
/// ) {
/// \endcode
- /// \version 21
+ /// \version 22
bool BreakBeforeCloseBracketSwitch;
/// Different ways to break before concept declarations.
>From cbd14c5d622a3bca3574faf1aade8d388a5dcba6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 13:53:24 -0600
Subject: [PATCH 11/26] add support for braced list initializers
---
clang/include/clang/Format/Format.h | 28 ++++++++++++++++++++++
clang/lib/Format/ContinuationIndenter.cpp | 14 ++++++-----
clang/lib/Format/Format.cpp | 7 ++++++
clang/lib/Format/FormatToken.cpp | 2 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 ++
clang/unittests/Format/FormatTest.cpp | 8 +++++--
6 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index f01f09bc58c69..81970ab7ae387 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,6 +62,17 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
+ /// Force break after the left bracket of a braced initializer list (when
+ /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ /// limit.
+ /// \code
+ /// true: false:
+ /// vector<int> x { vs. vector<int> x {1,
+ /// 1, 2, 3} 2, 3}
+ /// \endcode
+ /// \version 22
+ bool BreakAfterOpenBracketBracedList;
+
/// Force break after the left parenthesis of an if control statement
/// when the expression exceeds the column limit.
/// \code
@@ -2245,6 +2256,19 @@ struct FormatStyle {
/// \version 3.7
BraceBreakingStyle BreakBeforeBraces;
+ /// Force break before the right bracket of a braced initializer list (when
+ /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ /// limit. The break before the right bracket is only made if there is a
+ /// break after the opening bracket.
+ /// \code
+ /// true: false:
+ /// vector<int> x { vs. vector<int> x {
+ /// 1, 2, 3 1, 2, 3}
+ /// }
+ /// \endcode
+ /// \version 22
+ bool BreakBeforeCloseBracketBracedList;
+
/// Force break before the right parenthesis of an if control statement
/// when the expression exceeds the column limit. The break before the
/// closing parenthesis is only made if there is a break after the opening
@@ -5475,6 +5499,8 @@ struct FormatStyle {
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
BreakAfterAttributes == R.BreakAfterAttributes &&
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
+ BreakAfterOpenBracketBracedList ==
+ R.BreakAfterOpenBracketBracedList &&
BreakAfterOpenBracketIf == R.BreakAfterOpenBracketIf &&
BreakAfterOpenBracketLoop == R.BreakAfterOpenBracketLoop &&
BreakAfterOpenBracketSwitch == R.BreakAfterOpenBracketSwitch &&
@@ -5482,6 +5508,8 @@ struct FormatStyle {
BreakArrays == R.BreakArrays &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
BreakBeforeBraces == R.BreakBeforeBraces &&
+ BreakBeforeCloseBracketBracedList ==
+ R.BreakBeforeCloseBracketBracedList &&
BreakBeforeCloseBracketIf == R.BreakBeforeCloseBracketIf &&
BreakBeforeCloseBracketLoop == R.BreakBeforeCloseBracketLoop &&
BreakBeforeCloseBracketSwitch == R.BreakBeforeCloseBracketSwitch &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index b50aa3674bebd..4e70abbfbb18d 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -841,10 +841,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
Style.Cpp11BracedListStyle;
};
- if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
- !IsStartOfBracedList()) {
+ if (IsStartOfBracedList())
+ return Style.BreakAfterOpenBracketBracedList;
+ if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square))
return false;
- }
if (!Tok.Previous)
return true;
if (Tok.Previous->isIf())
@@ -1445,9 +1445,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
- (Current.is(tok::r_paren) ||
- (Current.is(tok::r_brace) && Current.MatchingParen &&
- Current.MatchingParen->is(BK_BracedInit))) &&
+ Current.is(tok::r_paren) && State.Stack.size() > 1) {
+ return State.Stack[State.Stack.size() - 2].LastSpace;
+ }
+ if (Style.BreakBeforeCloseBracketBracedList && Current.is(tok::r_brace) &&
+ Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit) &&
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0ad56e30502ea..212a828d86926 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1044,6 +1044,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes);
IO.mapOptional("BreakAfterJavaFieldAnnotations",
Style.BreakAfterJavaFieldAnnotations);
+ IO.mapOptional("BreakAfterOpenBracketBracedList",
+ Style.BreakAfterOpenBracketBracedList);
IO.mapOptional("BreakAfterOpenBracketIf", Style.BreakAfterOpenBracketIf);
IO.mapOptional("BreakAfterOpenBracketLoop",
Style.BreakAfterOpenBracketLoop);
@@ -1053,6 +1055,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BreakArrays", Style.BreakArrays);
IO.mapOptional("BreakBeforeBinaryOperators",
Style.BreakBeforeBinaryOperators);
+ IO.mapOptional("BreakBeforeCloseBracketBracedList",
+ Style.BreakBeforeCloseBracketBracedList);
IO.mapOptional("BreakBeforeCloseBracketIf",
Style.BreakBeforeCloseBracketIf);
IO.mapOptional("BreakBeforeCloseBracketLoop",
@@ -1595,6 +1599,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakAdjacentStringLiterals = true;
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
+ LLVMStyle.BreakAfterOpenBracketBracedList = false;
LLVMStyle.BreakAfterOpenBracketIf = false;
LLVMStyle.BreakAfterOpenBracketLoop = false;
LLVMStyle.BreakAfterOpenBracketSwitch = false;
@@ -1602,6 +1607,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakArrays = true;
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
+ LLVMStyle.BreakBeforeCloseBracketBracedList = false;
LLVMStyle.BreakBeforeCloseBracketIf = false;
LLVMStyle.BreakBeforeCloseBracketLoop = false;
LLVMStyle.BreakBeforeCloseBracketSwitch = false;
@@ -1866,6 +1872,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.SpacesBeforeTrailingComments = 1;
} else if (Language == FormatStyle::LK_JavaScript) {
GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ GoogleStyle.BreakAfterOpenBracketBracedList = true;
GoogleStyle.BreakAfterOpenBracketIf = true;
GoogleStyle.BreakAfterOpenBracketLoop = false;
GoogleStyle.BreakAfterOpenBracketSwitch = false;
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index 0d8ae1c4a77eb..e476a874b8c50 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -54,7 +54,7 @@ bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const {
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
assert(is(tok::r_brace));
if (!Style.Cpp11BracedListStyle ||
- Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) {
+ Style.BreakBeforeCloseBracketBracedList == false) {
return false;
}
const auto *LBrace = MatchingParen;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index cefe6689ad15d..5be768fef3500 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -171,9 +171,11 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(BinPackLongBracedList);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
+ CHECK_PARSE_BOOL(BreakAfterOpenBracketBracedList);
CHECK_PARSE_BOOL(BreakAfterOpenBracketIf);
CHECK_PARSE_BOOL(BreakAfterOpenBracketLoop);
CHECK_PARSE_BOOL(BreakAfterOpenBracketSwitch);
+ CHECK_PARSE_BOOL(BreakBeforeCloseBracketBracedList);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketIf);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketLoop);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketSwitch);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 19a36c2b474bb..ced0e2ea3e35d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5106,6 +5106,7 @@ TEST_F(FormatTest, BracedInitializerIndentWidth) {
auto Style = getLLVMStyleWithColumns(60);
Style.BinPackArguments = true;
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketBracedList = true;
Style.BracedInitializerIndentWidth = 6;
// Non-initializing braces are unaffected by BracedInitializerIndentWidth.
@@ -5282,6 +5283,7 @@ TEST_F(FormatTest, BracedInitializerIndentWidth) {
// Aligning after open braces unaffected by BracedInitializerIndentWidth.
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.BreakAfterOpenBracketBracedList = false;
verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
" \"zzzzzzzzzzzzz\"};",
Style);
@@ -9617,6 +9619,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakBeforeCloseBracketBracedList = true;
Style.BinPackArguments = false;
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -14919,7 +14922,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"};",
NoBinPacking);
- NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ NoBinPacking.BreakAfterOpenBracketBracedList = true;
verifyFormat("static uint8 CddDp83848Reg[] = {\n"
" CDDDP83848_BMCR_REGISTER,\n"
" CDDDP83848_BMSR_REGISTER,\n"
@@ -28157,7 +28160,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
auto Style = getLLVMStyleWithColumns(60);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakBeforeCloseBracketBracedList = true;
// Aggregate initialization.
verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
" 10000000, 20000000\n"
>From d1e73bcfe4306aff917503f44454a516c8b4ba94 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 14:41:46 -0600
Subject: [PATCH 12/26] Add support for functions and replace AlwaysBreak,
BlockIndent
---
clang/include/clang/Format/Format.h | 24 +++++++++
clang/lib/Format/ContinuationIndenter.cpp | 20 +++----
clang/lib/Format/Format.cpp | 4 +-
clang/lib/Format/TokenAnnotator.cpp | 4 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 +
clang/unittests/Format/FormatTest.cpp | 63 ++++++++++++----------
clang/unittests/Format/FormatTestJS.cpp | 2 +-
7 files changed, 75 insertions(+), 44 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 81970ab7ae387..5c24dd9407aff 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -73,6 +73,16 @@ struct FormatStyle {
/// \version 22
bool BreakAfterOpenBracketBracedList;
+ /// Force break after the left parenthesis of a function (declaration,
+ /// definition, call) when the parameters exceed the column limit.
+ /// \code
+ /// true: false:
+ /// foo ( vs. foo (a ||
+ /// a || b) b)
+ /// \endcode
+ /// \version 22
+ bool BreakAfterOpenBracketFunction;
+
/// Force break after the left parenthesis of an if control statement
/// when the expression exceeds the column limit.
/// \code
@@ -2269,6 +2279,17 @@ struct FormatStyle {
/// \version 22
bool BreakBeforeCloseBracketBracedList;
+ /// Force break before the right parenthesis of a function (declaration,
+ /// definition, call) when the parameters exceed the column limit.
+ /// \code
+ /// true: false:
+ /// foo ( vs. foo (
+ /// a || b a || b)
+ /// )
+ /// \endcode
+ /// \version 22
+ bool BreakBeforeCloseBracketFunction;
+
/// Force break before the right parenthesis of an if control statement
/// when the expression exceeds the column limit. The break before the
/// closing parenthesis is only made if there is a break after the opening
@@ -5501,6 +5522,7 @@ struct FormatStyle {
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
BreakAfterOpenBracketBracedList ==
R.BreakAfterOpenBracketBracedList &&
+ BreakAfterOpenBracketFunction == R.BreakAfterOpenBracketFunction &&
BreakAfterOpenBracketIf == R.BreakAfterOpenBracketIf &&
BreakAfterOpenBracketLoop == R.BreakAfterOpenBracketLoop &&
BreakAfterOpenBracketSwitch == R.BreakAfterOpenBracketSwitch &&
@@ -5510,6 +5532,8 @@ struct FormatStyle {
BreakBeforeBraces == R.BreakBeforeBraces &&
BreakBeforeCloseBracketBracedList ==
R.BreakBeforeCloseBracketBracedList &&
+ BreakBeforeCloseBracketFunction ==
+ R.BreakBeforeCloseBracketFunction &&
BreakBeforeCloseBracketIf == R.BreakBeforeCloseBracketIf &&
BreakBeforeCloseBracketLoop == R.BreakBeforeCloseBracketLoop &&
BreakBeforeCloseBracketSwitch == R.BreakBeforeCloseBracketSwitch &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 4e70abbfbb18d..52fd9d60f9e27 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -358,10 +358,10 @@ 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.
- bool might_break_before =
- Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
- Style.BreakBeforeCloseBracketSwitch ||
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ bool might_break_before = Style.BreakBeforeCloseBracketFunction ||
+ Style.BreakBeforeCloseBracketIf ||
+ Style.BreakBeforeCloseBracketLoop ||
+ Style.BreakBeforeCloseBracketSwitch;
if (might_break_before && Current.is(tok::r_paren))
return CurrentState.BreakBeforeClosingParen;
@@ -853,8 +853,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return Style.BreakAfterOpenBracketLoop;
if (Tok.Previous->is(tok::kw_switch))
return Style.BreakAfterOpenBracketSwitch;
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
+ if (Style.BreakAfterOpenBracketFunction) {
return !Tok.Previous->is(TT_CastRParen) &&
!(Style.isJavaScript() && Tok.is(Keywords.kw_await));
}
@@ -1299,7 +1298,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
Style.BreakBeforeCloseBracketSwitch;
} else {
CurrentState.BreakBeforeClosingParen =
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ Style.BreakBeforeCloseBracketFunction;
}
}
}
@@ -1444,16 +1443,13 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
- Current.is(tok::r_paren) && State.Stack.size() > 1) {
- return State.Stack[State.Stack.size() - 2].LastSpace;
- }
if (Style.BreakBeforeCloseBracketBracedList && Current.is(tok::r_brace) &&
Current.MatchingParen && Current.MatchingParen->is(BK_BracedInit) &&
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
- if ((Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
+ if ((Style.BreakBeforeCloseBracketFunction ||
+ Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
Style.BreakBeforeCloseBracketSwitch) &&
Current.is(tok::r_paren) && State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 212a828d86926..7b981107b48a2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1600,6 +1600,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
LLVMStyle.BreakAfterOpenBracketBracedList = false;
+ LLVMStyle.BreakAfterOpenBracketFunction = false;
LLVMStyle.BreakAfterOpenBracketIf = false;
LLVMStyle.BreakAfterOpenBracketLoop = false;
LLVMStyle.BreakAfterOpenBracketSwitch = false;
@@ -1608,6 +1609,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
LLVMStyle.BreakBeforeCloseBracketBracedList = false;
+ LLVMStyle.BreakBeforeCloseBracketFunction = false;
LLVMStyle.BreakBeforeCloseBracketIf = false;
LLVMStyle.BreakBeforeCloseBracketLoop = false;
LLVMStyle.BreakBeforeCloseBracketSwitch = false;
@@ -1871,8 +1873,8 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.SpaceAfterCStyleCast = true;
GoogleStyle.SpacesBeforeTrailingComments = 1;
} else if (Language == FormatStyle::LK_JavaScript) {
- GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
GoogleStyle.BreakAfterOpenBracketBracedList = true;
+ GoogleStyle.BreakAfterOpenBracketFunction = true;
GoogleStyle.BreakAfterOpenBracketIf = true;
GoogleStyle.BreakAfterOpenBracketLoop = false;
GoogleStyle.BreakAfterOpenBracketSwitch = false;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 934d181f1c96e..c52f7521ff359 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6231,12 +6231,12 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return Style.BreakBeforeCloseBracketLoop;
if (Previous->is(tok::kw_switch))
return Style.BreakBeforeCloseBracketSwitch;
- return Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
+ return Style.BreakBeforeCloseBracketFunction;
}
if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) &&
Right.is(TT_TrailingAnnotation) &&
- Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
+ Style.BreakBeforeCloseBracketFunction) {
return false;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 5be768fef3500..bdc16ea1655a0 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -172,10 +172,12 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakAfterOpenBracketBracedList);
+ CHECK_PARSE_BOOL(BreakAfterOpenBracketFunction);
CHECK_PARSE_BOOL(BreakAfterOpenBracketIf);
CHECK_PARSE_BOOL(BreakAfterOpenBracketLoop);
CHECK_PARSE_BOOL(BreakAfterOpenBracketSwitch);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketBracedList);
+ CHECK_PARSE_BOOL(BreakBeforeCloseBracketFunction);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketIf);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketLoop);
CHECK_PARSE_BOOL(BreakBeforeCloseBracketSwitch);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ced0e2ea3e35d..8fff580658795 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5105,7 +5105,7 @@ TEST_F(FormatTest, DesignatedInitializers) {
TEST_F(FormatTest, BracedInitializerIndentWidth) {
auto Style = getLLVMStyleWithColumns(60);
Style.BinPackArguments = true;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
Style.BreakAfterOpenBracketBracedList = true;
Style.BracedInitializerIndentWidth = 6;
@@ -7394,7 +7394,7 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
Style);
Style = getLLVMStyleWithColumns(20);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
Style.ContinuationIndentWidth = 2;
@@ -8072,15 +8072,16 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over
+ // However, BreakAfterOpenBracketFunction should take precedence over
// AllowAllArgumentsOnNextLine.
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
" int A, int B, int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("functionCall(\n"
" paramA, paramB, paramC\n"
");\n"
@@ -8092,7 +8093,8 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
// When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
// first argument.
Style.AllowAllArgumentsOnNextLine = true;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = false;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -8101,6 +8103,7 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
// It wouldn't fit on one line with aligned parameters so this setting
// doesn't change anything for BAS_Align.
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.BreakAfterOpenBracketFunction = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
@@ -9045,13 +9048,14 @@ TEST_F(FormatTest, FormatsDeclarationBreakAlways) {
// Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
// to BPPS_AlwaysOnePerLine.
- BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ BreakAlways.BreakAfterOpenBracketFunction = true;
verifyFormat(
"void someLongFunctionName(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" int b);",
BreakAlways);
- BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ BreakAlways.BreakAfterOpenBracketFunction = true;
+ BreakAlways.BreakBeforeCloseBracketFunction = true;
verifyFormat(
"void someLongFunctionName(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -9101,7 +9105,7 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
// Ensure AlignAfterOpenBracket interacts correctly with BinPackParameters set
// to BPPS_AlwaysOnePerLine.
- BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ BreakAlways.BreakAfterOpenBracketFunction = true;
verifyFormat(
"void someLongFunctionName(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -9110,7 +9114,8 @@ TEST_F(FormatTest, FormatsDefinitionBreakAlways) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
"}",
BreakAlways);
- BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ BreakAlways.BreakAfterOpenBracketFunction = true;
+ BreakAlways.BreakBeforeCloseBracketFunction = true;
verifyFormat(
"void someLongFunctionName(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -9567,7 +9572,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
Style);
Style.ColumnLimit = 80;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
Style.BinPackArguments = false;
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -9618,7 +9623,8 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
" XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZZZZZZZZZZZZ()));",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
Style.BreakBeforeCloseBracketBracedList = true;
Style.BinPackArguments = false;
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
@@ -9787,7 +9793,7 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -9855,7 +9861,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -9906,7 +9911,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Style.BreakAfterOpenBracketIf = false;
Style.BreakAfterOpenBracketLoop = false;
Style.BreakAfterOpenBracketSwitch = false;
@@ -9955,7 +9959,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -10012,7 +10015,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
Style.BreakAfterOpenBracketIf = false;
Style.BreakAfterOpenBracketLoop = false;
Style.BreakAfterOpenBracketSwitch = false;
@@ -11884,7 +11886,7 @@ TEST_F(FormatTest, WrapsTemplateParameters) {
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
" y;",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
verifyFormat("template <typename... a> struct s {};\n"
"extern s<\n"
@@ -11894,7 +11896,7 @@ TEST_F(FormatTest, WrapsTemplateParameters) {
"aaaaaaaaaaaaaaaaaaaaaa>\n"
" y;",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
verifyFormat("template <typename... a> struct t {};\n"
"extern t<\n"
@@ -16580,13 +16582,14 @@ TEST_F(FormatTest, BreaksStringLiteralOperands) {
// In a function call with two operands, with AlignAfterOpenBracket enabled,
// the first must be broken with a line break before it.
FormatStyle Style = getLLVMStyleWithColumns(25);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
verifyFormat("someFunction(\n"
" \"long long long \"\n"
" \"long\",\n"
" a);",
"someFunction(\"long long long long\", a);", Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("someFunction(\n"
" \"long long long \"\n"
" \"long\",\n"
@@ -18381,7 +18384,7 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
Spaces.ColumnLimit = 80;
Spaces.IndentWidth = 4;
- Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Spaces.BreakAfterOpenBracketFunction = true;
verifyFormat("void foo( ) {\n"
" size_t foo = (*(function))(\n"
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
@@ -18406,7 +18409,8 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
"}",
Spaces);
- Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Spaces.BreakAfterOpenBracketFunction = true;
+ Spaces.BreakBeforeCloseBracketFunction = true;
verifyFormat("void foo( ) {\n"
" size_t foo = (*(function))(\n"
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, "
@@ -23326,7 +23330,7 @@ TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
verifyFormat(
"SomeLongTemplateVariableName<\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
@@ -24581,7 +24585,7 @@ TEST_F(FormatTest, FormatsLambdas) {
" return aFunkyFunctionCall(qux);\n"
" }} {}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
// FIXME: The following test should pass, but fails at the time of writing.
#if 0
// As long as all the non-lambda arguments fit on a single line, AlwaysBreak
@@ -27957,7 +27961,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
"argument5));",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat(Short, Style);
verifyFormat(
@@ -28081,7 +28086,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("if (foo()) {\n"
" return;\n"
@@ -28143,7 +28149,8 @@ TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("for (int i = 0; i < 5; ++i) {\n"
" doSomething();\n"
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 91577b9a49167..4847151c14b33 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -2883,7 +2883,7 @@ TEST_F(FormatTestJS, DontBreakFieldsAsGoToLabels) {
TEST_F(FormatTestJS, BreakAfterOpenBracket) {
auto Style = getGoogleStyle(FormatStyle::LK_JavaScript);
- EXPECT_EQ(Style.AlignAfterOpenBracket, FormatStyle::BAS_AlwaysBreak);
+ EXPECT_EQ(Style.BreakAfterOpenBracketFunction, true);
verifyFormat("ctrl.onCopy(/** @type {!WizEvent}*/ (\n"
" {event, targetElement: {el: () => selectedElement}}));",
Style);
>From fd27a5f4b13acf45d9e6f219b9f1b3c3a61e6b2d Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 14:50:43 -0600
Subject: [PATCH 13/26] deprecate AlwaysBreak and BlockIndent
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/include/clang/Format/Format.h | 28 ++++++++---------------
clang/lib/Format/Format.cpp | 35 +++++++++++++++++++++++++++--
3 files changed, 44 insertions(+), 21 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b6f1fbc94b33c..da2aac2abb6c1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -384,6 +384,8 @@ clang-format
- Add ``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch`` options.
+- Remove ``AlwaysBreak`` and ``BlockIndent`` suboptions from the
+ ``AlignAfterOpenBracket`` option.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 5c24dd9407aff..f79e9c9ebe057 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -127,26 +127,16 @@ struct FormatStyle {
/// argument2);
/// \endcode
BAS_DontAlign,
- /// Always break after an open bracket, if the parameters don't fit
- /// on a single line, e.g.:
- /// \code
- /// someLongFunction(
- /// argument1, argument2);
- /// \endcode
+ /// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ /// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ /// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
BAS_AlwaysBreak,
- /// Always break after an open bracket, if the parameters don't fit
- /// on a single line. Closing brackets will be placed on a new line.
- /// E.g.:
- /// \code
- /// someLongFunction(
- /// argument1, argument2
- /// )
- /// \endcode
- ///
- /// \note
- /// This currently only applies to braced initializer lists (when
- /// ``Cpp11BracedListStyle`` is ``true``) and parentheses.
- /// \endnote
+ /// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ /// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ /// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ /// in combination with ``BreakBeforeCloseBracketBracedList``,
+ /// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
+ /// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
BAS_BlockIndent,
};
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7b981107b48a2..8badf99d25fa8 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -207,12 +207,12 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
- IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
- IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
// For backward compatibility.
IO.enumCase(Value, "true", FormatStyle::BAS_Align);
IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
+ IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
+ IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
}
};
@@ -1251,6 +1251,37 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("WrapNamespaceBodyWithEmptyLines",
Style.WrapNamespaceBodyWithEmptyLines);
+ // If AlwaysBreak or BlockIndent were specified but individual
+ // options for BreakAfterOpenBracket* (CloseAfterOpenBracket*),
+ // initialize the latter to preserve backwards compatibility.
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak) {
+ if (!Style.BreakAfterOpenBracketBracedList &&
+ !Style.BreakAfterOpenBracketFunction &&
+ !Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
+ !Style.BreakAfterOpenBracketSwitch) {
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakAfterOpenBracketIf = true;
+ }
+ } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
+ if (!Style.BreakAfterOpenBracketBracedList &&
+ !Style.BreakAfterOpenBracketFunction &&
+ !Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
+ !Style.BreakAfterOpenBracketSwitch &&
+ !Style.BreakBeforeCloseBracketBracedList &&
+ !Style.BreakBeforeCloseBracketFunction &&
+ !Style.BreakBeforeCloseBracketIf &&
+ !Style.BreakBeforeCloseBracketLoop &&
+ !Style.BreakBeforeCloseBracketSwitch) {
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakBeforeCloseBracketBracedList = true;
+ Style.BreakBeforeCloseBracketFunction = true;
+ Style.BreakBeforeCloseBracketIf = true;
+ }
+ }
+
// If AlwaysBreakAfterDefinitionReturnType was specified but
// BreakAfterReturnType was not, initialize the latter from the former for
// backwards compatibility.
>From 781c6022e23cdafd0194f95e26b1c9e48379ad0f Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 13:54:27 -0600
Subject: [PATCH 14/26] release notes
---
clang/docs/ReleaseNotes.rst | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index da2aac2abb6c1..7c4ba58c2ffe6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -381,8 +381,10 @@ AST Matchers
clang-format
------------
- Add ``SpaceInEmptyBraces`` option and set it to ``Always`` for WebKit style.
-- Add ``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
- ``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketIf``,
+- Add ``BreakAfterOpenBracketBracedList'', ``BreakAfterOpenBracketFunction'',
+ ``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
+ ``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketBracedList'',
+ ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch`` options.
- Remove ``AlwaysBreak`` and ``BlockIndent`` suboptions from the
``AlignAfterOpenBracket`` option.
>From 5087ba5f1208796358dd0db2f9e59a30f976f4db Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Wed, 21 May 2025 20:59:31 -0600
Subject: [PATCH 15/26] dump format style
---
clang/docs/ClangFormatStyleOptions.rst | 135 ++++++++++++++++---------
clang/lib/Format/Format.cpp | 4 +
2 files changed, 93 insertions(+), 46 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index f0d2ceebab888..758064bf39be1 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -222,30 +222,17 @@ the configuration (without a prefix: ``Auto``).
argument2);
* ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
- Always break after an open bracket, if the parameters don't fit
- on a single line, e.g.:
-
- .. code-block:: c++
-
- someLongFunction(
- argument1, argument2);
+ This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
* ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
- Always break after an open bracket, if the parameters don't fit
- on a single line. Closing brackets will be placed on a new line.
- E.g.:
-
- .. code-block:: c++
-
- someLongFunction(
- argument1, argument2
- )
-
-
- .. note::
-
- This currently only applies to braced initializer lists (when
- ``Cpp11BracedListStyle`` is ``true``) and parentheses.
+ This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ in combination with ``BreakBeforeCloseBracketBracedList``,
+ ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
+ ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
@@ -2739,9 +2726,34 @@ the configuration (without a prefix: ``Auto``).
@Mock
DataLoad loader;
+.. _BreakAfterOpenBracketBracedList:
+
+**BreakAfterOpenBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketBracedList>`
+ Force break after the left bracket of a braced initializer list (when
+ ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ limit.
+
+ .. code-block:: c++
+
+ true: false:
+ vector<int> x { vs. vector<int> x {1,
+ 1, 2, 3} 2, 3}
+
+.. _BreakAfterOpenBracketFunction:
+
+**BreakAfterOpenBracketFunction** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketFunction>`
+ Force break after the left parenthesis of a function (declaration,
+ definition, call) when the parameters exceed the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ foo ( vs. foo (a ||
+ a || b) b)
+
.. _BreakAfterOpenBracketIf:
-**BreakAfterOpenBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketIf>`
+**BreakAfterOpenBracketIf** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketIf>`
Force break after the left parenthesis of an if control statement
when the expression exceeds the column limit.
@@ -2749,12 +2761,11 @@ the configuration (without a prefix: ``Auto``).
true: false:
if constexpr ( vs. if constexpr (a ||
- a || b)
- b)
+ a || b) b)
.. _BreakAfterOpenBracketLoop:
-**BreakAfterOpenBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketLoop>`
+**BreakAfterOpenBracketLoop** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketLoop>`
Force break after the left parenthesis of a loop control statement
when the expression exceeds the column limit.
@@ -2762,12 +2773,11 @@ the configuration (without a prefix: ``Auto``).
true: false:
while ( vs. while (a &&
- a && b) {
- b) {
+ a && b) { b) {
.. _BreakAfterOpenBracketSwitch:
-**BreakAfterOpenBracketSwitch** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketSwitch>`
+**BreakAfterOpenBracketSwitch** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketSwitch>`
Force break after the left parenthesis of a switch control statement
when the expression exceeds the column limit.
@@ -2775,8 +2785,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
switch ( vs. switch (a &&
- a && b) {
- b) {
+ a && b) { b) {
.. _BreakAfterReturnType:
@@ -3415,44 +3424,78 @@ the configuration (without a prefix: ``Auto``).
+.. _BreakBeforeCloseBracketBracedList:
+
+**BreakBeforeCloseBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakBeforeCloseBracketBracedList>`
+ Force break before the right bracket of a braced initializer list (when
+ ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ limit. The break before the right bracket is only made if there is a
+ break after the opening bracket.
+
+ .. code-block:: c++
+
+ true: false:
+ vector<int> x { vs. vector<int> x {
+ 1, 2, 3 1, 2, 3}
+ }
+
+.. _BreakBeforeCloseBracketFunction:
+
+**BreakBeforeCloseBracketFunction** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakBeforeCloseBracketFunction>`
+ Force break before the right parenthesis of a function (declaration,
+ definition, call) when the parameters exceed the column limit.
+
+ .. code-block:: c++
+
+ true: false:
+ foo ( vs. foo (
+ a || b a || b)
+ )
+
.. _BreakBeforeCloseBracketIf:
-**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketIf>`
+**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 22` :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 || b a || b )
+ )
.. _BreakBeforeCloseBracketLoop:
-**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketLoop>`
+**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 22` :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 && b a && b) {
+ ) {
.. _BreakBeforeCloseBracketSwitch:
-**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketSwitch>`
+**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format 22` :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 && b a && b) {
+ ) {
.. _BreakBeforeConceptDeclarations:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 8badf99d25fa8..87892746cebf9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1046,6 +1046,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakAfterJavaFieldAnnotations);
IO.mapOptional("BreakAfterOpenBracketBracedList",
Style.BreakAfterOpenBracketBracedList);
+ IO.mapOptional("BreakAfterOpenBracketFunction",
+ Style.BreakAfterOpenBracketFunction);
IO.mapOptional("BreakAfterOpenBracketIf", Style.BreakAfterOpenBracketIf);
IO.mapOptional("BreakAfterOpenBracketLoop",
Style.BreakAfterOpenBracketLoop);
@@ -1057,6 +1059,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakBeforeBinaryOperators);
IO.mapOptional("BreakBeforeCloseBracketBracedList",
Style.BreakBeforeCloseBracketBracedList);
+ IO.mapOptional("BreakBeforeCloseBracketFunction",
+ Style.BreakBeforeCloseBracketFunction);
IO.mapOptional("BreakBeforeCloseBracketIf",
Style.BreakBeforeCloseBracketIf);
IO.mapOptional("BreakBeforeCloseBracketLoop",
>From 3509cdb3b9a631f9913c76c875310029d3b29f3e Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 16:54:11 -0600
Subject: [PATCH 16/26] fix leading whitespace
---
clang/include/clang/Format/Format.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index f79e9c9ebe057..652399aa73fb0 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -63,7 +63,7 @@ struct FormatStyle {
int AccessModifierOffset;
/// Force break after the left bracket of a braced initializer list (when
- /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
/// limit.
/// \code
/// true: false:
>From 24392a578c1b64919034e88643ed7c409dc14ecc Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 16:54:21 -0600
Subject: [PATCH 17/26] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 758064bf39be1..210d51b6b22e5 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2730,7 +2730,7 @@ the configuration (without a prefix: ``Auto``).
**BreakAfterOpenBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakAfterOpenBracketBracedList>`
Force break after the left bracket of a braced initializer list (when
- ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
limit.
.. code-block:: c++
>From 5475e26336de5292c377a0bcdd6b82902ee8cdaa Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 18:09:56 -0600
Subject: [PATCH 18/26] fix leading whitespace
---
clang/include/clang/Format/Format.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 652399aa73fb0..b33e5c44a3b08 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2257,7 +2257,7 @@ struct FormatStyle {
BraceBreakingStyle BreakBeforeBraces;
/// Force break before the right bracket of a braced initializer list (when
- /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
/// limit. The break before the right bracket is only made if there is a
/// break after the opening bracket.
/// \code
>From 4aaee94ee81ee3343170dfe414238b9c15078888 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 22 Aug 2025 18:10:26 -0600
Subject: [PATCH 19/26] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 210d51b6b22e5..d7b7cceec7e70 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3428,7 +3428,7 @@ the configuration (without a prefix: ``Auto``).
**BreakBeforeCloseBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakBeforeCloseBracketBracedList>`
Force break before the right bracket of a braced initializer list (when
- ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
limit. The break before the right bracket is only made if there is a
break after the opening bracket.
>From 1e31a31006d2f22c7c5808601a14b32092ba7420 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 08:38:49 -0600
Subject: [PATCH 20/26] Format.h: update examples and sort options
---
clang/include/clang/Format/Format.h | 106 ++++++++++++++--------------
1 file changed, 53 insertions(+), 53 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index b33e5c44a3b08..57b8e8d1dfaae 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,57 +62,6 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /// Force break after the left bracket of a braced initializer list (when
- /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
- /// limit.
- /// \code
- /// true: false:
- /// vector<int> x { vs. vector<int> x {1,
- /// 1, 2, 3} 2, 3}
- /// \endcode
- /// \version 22
- bool BreakAfterOpenBracketBracedList;
-
- /// Force break after the left parenthesis of a function (declaration,
- /// definition, call) when the parameters exceed the column limit.
- /// \code
- /// true: false:
- /// foo ( vs. foo (a ||
- /// a || b) b)
- /// \endcode
- /// \version 22
- bool BreakAfterOpenBracketFunction;
-
- /// 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 22
- bool BreakAfterOpenBracketIf;
-
- /// 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 22
- bool BreakAfterOpenBracketLoop;
-
- /// 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 22
- bool BreakAfterOpenBracketSwitch;
-
/// Different styles for aligning after open brackets.
enum BracketAlignmentStyle : int8_t {
/// Align parameters on the open bracket, e.g.:
@@ -1743,6 +1692,57 @@ struct FormatStyle {
/// \version 16
AttributeBreakingStyle BreakAfterAttributes;
+ /// Force break after the left bracket of a braced initializer list (when
+ /// ``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
+ /// limit.
+ /// \code
+ /// true: false:
+ /// vector<int> x { vs. vector<int> x {1,
+ /// 1, 2, 3} 2, 3}
+ /// \endcode
+ /// \version 22
+ bool BreakAfterOpenBracketBracedList;
+
+ /// Force break after the left parenthesis of a function (declaration,
+ /// definition, call) when the parameters exceed the column limit.
+ /// \code
+ /// true: false:
+ /// foo ( vs. foo (a,
+ /// a , b) b)
+ /// \endcode
+ /// \version 22
+ bool BreakAfterOpenBracketFunction;
+
+ /// 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 22
+ bool BreakAfterOpenBracketIf;
+
+ /// 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 22
+ bool BreakAfterOpenBracketLoop;
+
+ /// 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 22
+ bool BreakAfterOpenBracketSwitch;
+
/// The function declaration return type breaking style to use.
/// \version 19
ReturnTypeBreakingStyle BreakAfterReturnType;
@@ -2274,7 +2274,7 @@ struct FormatStyle {
/// \code
/// true: false:
/// foo ( vs. foo (
- /// a || b a || b)
+ /// a , b a , b)
/// )
/// \endcode
/// \version 22
@@ -2313,7 +2313,7 @@ struct FormatStyle {
/// \code
/// true: false:
/// switch ( vs. switch (
- /// a && b a && b) {
+ /// a + b a + b) {
/// ) {
/// \endcode
/// \version 22
>From 05f56a6eb7bf151302f52b15843bfa02e5343328 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 08:39:03 -0600
Subject: [PATCH 21/26] dump format 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 d7b7cceec7e70..7211ed82e553c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2748,8 +2748,8 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
true: false:
- foo ( vs. foo (a ||
- a || b) b)
+ foo ( vs. foo (a,
+ a , b) b)
.. _BreakAfterOpenBracketIf:
@@ -2784,8 +2784,8 @@ the configuration (without a prefix: ``Auto``).
.. code-block:: c++
true: false:
- switch ( vs. switch (a &&
- a && b) { b) {
+ switch ( vs. switch (a +
+ a + b) { b) {
.. _BreakAfterReturnType:
@@ -3449,7 +3449,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
foo ( vs. foo (
- a || b a || b)
+ a , b a , b)
)
.. _BreakBeforeCloseBracketIf:
@@ -3494,7 +3494,7 @@ the configuration (without a prefix: ``Auto``).
true: false:
switch ( vs. switch (
- a && b a && b) {
+ a + b a + b) {
) {
.. _BreakBeforeConceptDeclarations:
>From 557fd38f974fd489299fd43004a13e95f531f5b7 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 08:58:22 -0600
Subject: [PATCH 22/26] refactor conditionals from comments
---
clang/lib/Format/ContinuationIndenter.cpp | 15 +++++++--------
clang/lib/Format/FormatToken.cpp | 4 +---
2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 52fd9d60f9e27..debbca70ec346 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -356,15 +356,14 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
return CurrentState.BreakBeforeClosingBrace;
}
- // Allow breaking before the right parens with block indentation if there was
- // a break after the left parens, which is tracked by BreakBeforeClosingParen.
- bool might_break_before = Style.BreakBeforeCloseBracketFunction ||
- Style.BreakBeforeCloseBracketIf ||
- Style.BreakBeforeCloseBracketLoop ||
- Style.BreakBeforeCloseBracketSwitch;
-
- if (might_break_before && Current.is(tok::r_paren))
+ // Check need to break before the right parens if there was a break after
+ // the left parens, which is tracked by BreakBeforeClosingParen.
+ if ((Style.BreakBeforeCloseBracketFunction ||
+ Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
+ Style.BreakBeforeCloseBracketSwitch) &&
+ Current.is(tok::r_paren)) {
return CurrentState.BreakBeforeClosingParen;
+ }
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser))
return CurrentState.BreakBeforeClosingAngle;
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index e476a874b8c50..e84afe3dd9814 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -53,10 +53,8 @@ bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const {
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
assert(is(tok::r_brace));
- if (!Style.Cpp11BracedListStyle ||
- Style.BreakBeforeCloseBracketBracedList == false) {
+ if (!Style.Cpp11BracedListStyle || !Style.BreakBeforeCloseBracketBracedList)
return false;
- }
const auto *LBrace = MatchingParen;
assert(LBrace && LBrace->is(tok::l_brace));
if (LBrace->is(BK_BracedInit))
>From 3d783022c155f9051ccdcdb85c2d21426d6242c2 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 09:58:23 -0600
Subject: [PATCH 23/26] refactor lambda to FormatToken::isLoop
---
clang/lib/Format/ContinuationIndenter.cpp | 17 +++--------------
clang/lib/Format/FormatToken.h | 6 ++++++
clang/lib/Format/TokenAnnotator.cpp | 7 +------
3 files changed, 10 insertions(+), 20 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index debbca70ec346..929033dfdec4f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -830,11 +830,6 @@ 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 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));
- };
auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
@@ -848,7 +843,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return true;
if (Tok.Previous->isIf())
return Style.BreakAfterOpenBracketIf;
- if (IsLoopConditional(*Tok.Previous))
+ if (Tok.Previous->isLoop(Style))
return Style.BreakAfterOpenBracketLoop;
if (Tok.Previous->is(tok::kw_switch))
return Style.BreakAfterOpenBracketSwitch;
@@ -896,7 +891,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
const auto *Previous = TokAfterLParen.Previous;
assert(Previous); // IsOpeningBracket(Previous)
if (Previous->Previous &&
- (Previous->Previous->isIf() || IsLoopConditional(*Previous->Previous) ||
+ (Previous->Previous->isIf() || Previous->Previous->isLoop(Style) ||
Previous->Previous->is(tok::kw_switch))) {
return false;
}
@@ -1281,15 +1276,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
auto Previous = PreviousNonComment->Previous;
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;
- } else if (IsLoopConditional(*Previous)) {
+ } else if (Previous->isLoop(Style)) {
CurrentState.BreakBeforeClosingParen =
Style.BreakBeforeCloseBracketLoop;
} else if (Previous->is(tok::kw_switch)) {
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 9252a795a0b5e..a0e17c07f035c 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -650,6 +650,12 @@ struct FormatToken {
(endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
}
+ bool isLoop(const FormatStyle &Style) const {
+ return this->isOneOf(tok::kw_for, tok::kw_while) ||
+ (Style.isJavaScript() && this->isNot(tok::l_paren) &&
+ this->Previous && this->Previous->is(tok::kw_for));
+ }
+
bool closesScopeAfterBlock() const {
if (getBlockKind() == BK_Block)
return true;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c52f7521ff359..d0a81b0854f33 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -6222,12 +6222,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
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))
+ if (Previous->isLoop(Style))
return Style.BreakBeforeCloseBracketLoop;
if (Previous->is(tok::kw_switch))
return Style.BreakBeforeCloseBracketSwitch;
>From 6fa2f10c8476c9f6763fe0fc02c3093f653a47f4 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 10:27:56 -0600
Subject: [PATCH 24/26] remove deprecated sub-options for AlwaysBreak and
BlockIndent
---
clang/include/clang/Format/Format.h | 4 +--
clang/lib/Format/Format.cpp | 33 ----------------------
clang/unittests/Format/ConfigParseTest.cpp | 7 ++---
3 files changed, 4 insertions(+), 40 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 57b8e8d1dfaae..a90d6846d3508 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -79,14 +79,14 @@ struct FormatStyle {
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- BAS_AlwaysBreak,
+ /// BAS_AlwaysBreak,
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// in combination with ``BreakBeforeCloseBracketBracedList``,
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- BAS_BlockIndent,
+ /// BAS_BlockIndent,
};
/// If ``true``, horizontally aligns arguments after an open bracket.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 87892746cebf9..3861864200bf9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -211,8 +211,6 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
// For backward compatibility.
IO.enumCase(Value, "true", FormatStyle::BAS_Align);
IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
- IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
- IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
}
};
@@ -1255,37 +1253,6 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("WrapNamespaceBodyWithEmptyLines",
Style.WrapNamespaceBodyWithEmptyLines);
- // If AlwaysBreak or BlockIndent were specified but individual
- // options for BreakAfterOpenBracket* (CloseAfterOpenBracket*),
- // initialize the latter to preserve backwards compatibility.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak) {
- if (!Style.BreakAfterOpenBracketBracedList &&
- !Style.BreakAfterOpenBracketFunction &&
- !Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
- !Style.BreakAfterOpenBracketSwitch) {
- Style.BreakAfterOpenBracketBracedList = true;
- Style.BreakAfterOpenBracketFunction = true;
- Style.BreakAfterOpenBracketIf = true;
- }
- } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
- if (!Style.BreakAfterOpenBracketBracedList &&
- !Style.BreakAfterOpenBracketFunction &&
- !Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
- !Style.BreakAfterOpenBracketSwitch &&
- !Style.BreakBeforeCloseBracketBracedList &&
- !Style.BreakBeforeCloseBracketFunction &&
- !Style.BreakBeforeCloseBracketIf &&
- !Style.BreakBeforeCloseBracketLoop &&
- !Style.BreakBeforeCloseBracketSwitch) {
- Style.BreakAfterOpenBracketBracedList = true;
- Style.BreakAfterOpenBracketFunction = true;
- Style.BreakAfterOpenBracketIf = true;
- Style.BreakBeforeCloseBracketBracedList = true;
- Style.BreakBeforeCloseBracketFunction = true;
- Style.BreakBeforeCloseBracketIf = true;
- }
- }
-
// If AlwaysBreakAfterDefinitionReturnType was specified but
// BreakAfterReturnType was not, initialize the latter from the former for
// backwards compatibility.
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index bdc16ea1655a0..aa499f3c267e3 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -543,15 +543,12 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
FormatStyle::BAS_DontAlign);
- CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
- FormatStyle::BAS_AlwaysBreak);
- CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
- FormatStyle::BAS_BlockIndent);
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
// For backward compatibility:
CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
FormatStyle::BAS_DontAlign);
>From 4062b608d728723ae55082e7922017ba84daba14 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 10:52:07 -0600
Subject: [PATCH 25/26] deprecate and remove BAS_Align and BAS_DontAlign
---
clang/include/clang/Format/Format.h | 42 ++++++++--------------
clang/lib/Format/ContinuationIndenter.cpp | 9 +++--
clang/lib/Format/Format.cpp | 17 ++-------
clang/lib/Format/FormatToken.cpp | 2 +-
clang/lib/Format/TokenAnnotator.cpp | 6 ++--
clang/unittests/Format/ConfigParseTest.cpp | 13 +------
clang/unittests/Format/FormatTest.cpp | 34 +++++++++---------
7 files changed, 42 insertions(+), 81 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index a90d6846d3508..170d33763e505 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,39 +62,25 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /// Different styles for aligning after open brackets.
- enum BracketAlignmentStyle : int8_t {
- /// Align parameters on the open bracket, e.g.:
- /// \code
- /// someLongFunction(argument1,
- /// argument2);
- /// \endcode
- BAS_Align,
- /// Don't align, instead use ``ContinuationIndentWidth``, e.g.:
- /// \code
- /// someLongFunction(argument1,
- /// argument2);
- /// \endcode
- BAS_DontAlign,
- /// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
- /// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
- /// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- /// BAS_AlwaysBreak,
- /// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
- /// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
- /// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- /// in combination with ``BreakBeforeCloseBracketBracedList``,
- /// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
- /// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- /// BAS_BlockIndent,
- };
-
/// If ``true``, horizontally aligns arguments after an open bracket.
///
+ /// \code
+ /// true: false:
+ /// someLongFunction(argument1, vs. someLongFunction(argument1,
+ /// argument2); argument2);
+ /// \endcode
+ ///
+ /// The values ``BAS_Align`` (configuration: ``Align``), ``BAS_DontAlign``
+ /// (configuration ``DontAlign``), ``BAS_Always`` (configuration:
+ /// ``Always``), and ``BAS_BlockIndent`` (configuration: ``BlockIndent``)
+ /// are *deprecated*. Individual control over breaking after open brackets
+ /// and before close brackets are provided by separate style options, e.g.,
+ /// ``BreakAfterOpenBracketFunction``, ``BreakBeforeCloseBracketFunction``.
+ ///
/// This applies to round brackets (parentheses), angle brackets and square
/// brackets.
/// \version 3.8
- BracketAlignmentStyle AlignAfterOpenBracket;
+ bool AlignAfterOpenBracket;
/// Different style for aligning array initializers.
enum ArrayInitializerAlignmentStyle : int8_t {
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 929033dfdec4f..09a82ccffeb19 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -928,7 +928,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// Note: This doesn't apply to macro expansion lines, which are MACRO( , , )
// with args as children of the '(' and ',' tokens. It does not make sense to
// align the commas with the opening paren.
- if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
+ if (Style.AlignAfterOpenBracket &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
Previous.isNot(TT_TableGenDAGArgOpener) &&
@@ -1862,8 +1862,8 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(!Style.isJava() && PrecedenceLevel > 0)) &&
- (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
- PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
+ (Style.AlignAfterOpenBracket || PrecedenceLevel > prec::Comma ||
+ Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
(Previous && Previous->isOneOf(TT_TableGenDAGArgListComma,
TT_TableGenDAGArgListCommaToBreak)))) {
@@ -1903,8 +1903,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
if (PrecedenceLevel > prec::Unknown)
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
if (PrecedenceLevel != prec::Conditional &&
- Current.isNot(TT_UnaryOperator) &&
- Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
+ Current.isNot(TT_UnaryOperator) && Style.AlignAfterOpenBracket) {
NewParenState.StartOfFunctionCall = State.Column;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 3861864200bf9..dbe65b923716b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,17 +203,6 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
}
};
-template <> struct ScalarEnumerationTraits<FormatStyle::BracketAlignmentStyle> {
- static void enumeration(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
- IO.enumCase(Value, "Align", FormatStyle::BAS_Align);
- IO.enumCase(Value, "DontAlign", FormatStyle::BAS_DontAlign);
-
- // For backward compatibility.
- IO.enumCase(Value, "true", FormatStyle::BAS_Align);
- IO.enumCase(Value, "false", FormatStyle::BAS_DontAlign);
- }
-};
-
template <>
struct ScalarEnumerationTraits<
FormatStyle::BraceWrappingAfterControlStatementStyle> {
@@ -1542,7 +1531,7 @@ static void expandPresetsSpacesInParens(FormatStyle &Expanded) {
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2;
- LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ LLVMStyle.AlignAfterOpenBracket = true;
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignConsecutiveAssignments = {};
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
@@ -1863,7 +1852,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
if (Language == FormatStyle::LK_Java) {
- GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ GoogleStyle.AlignAfterOpenBracket = false;
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign;
GoogleStyle.AlignTrailingComments = {};
GoogleStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
@@ -2016,7 +2005,7 @@ FormatStyle getMozillaStyle() {
FormatStyle getWebKitStyle() {
FormatStyle Style = getLLVMStyle();
Style.AccessModifierOffset = -4;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
Style.AlignTrailingComments = {};
Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp
index e84afe3dd9814..be9823b9e02e9 100644
--- a/clang/lib/Format/FormatToken.cpp
+++ b/clang/lib/Format/FormatToken.cpp
@@ -181,7 +181,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
return;
// Column format doesn't really make sense if we don't align after brackets.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
+ if (!Style.AlignAfterOpenBracket)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d0a81b0854f33..7d85cb0edefb6 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4408,10 +4408,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
return Style.PenaltyBreakOpenParenthesis;
- if (Left.is(tok::l_paren) && InFunctionDecl &&
- Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
+ if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket)
return 100;
- }
if (Left.is(tok::l_paren) && Left.Previous &&
(Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
Left.Previous->isIf())) {
@@ -4427,7 +4425,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
// If we aren't aligning after opening parens/braces we can always break
// here unless the style does not want us to place all arguments on the
// next line.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
+ if (!Style.AlignAfterOpenBracket &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
return 0;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index aa499f3c267e3..cd9760975dcc3 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -159,6 +159,7 @@ TEST(ConfigParseTest, GetsCorrectBasedOnStyle) {
TEST(ConfigParseTest, ParsesConfigurationBools) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
+ CHECK_PARSE_BOOL(AlignAfterOpenBracket);
CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortCaseExpressionOnASingleLine);
@@ -543,18 +544,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
- CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
- FormatStyle::BAS_DontAlign);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- // For backward compatibility:
- CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
- FormatStyle::BAS_DontAlign);
- CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
-
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
FormatStyle::ENAS_DontAlign);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8fff580658795..2c788be21da52 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5282,7 +5282,7 @@ TEST_F(FormatTest, BracedInitializerIndentWidth) {
Style);
// Aligning after open braces unaffected by BracedInitializerIndentWidth.
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.AlignAfterOpenBracket = true;
Style.BreakAfterOpenBracketBracedList = false;
verifyFormat("SomeStruct s{\"xxxxxxxxxxxxx\", \"yyyyyyyyyyyyy\",\n"
" \"zzzzzzzzzzzzz\"};",
@@ -7383,7 +7383,7 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
Style.IndentWidth = 4;
Style.TabWidth = 4;
Style.UseTab = FormatStyle::UT_Always;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
"\t&& (someOtherLongishConditionPart1\n"
@@ -7556,7 +7556,7 @@ TEST_F(FormatTest, NoOperandAlignment) {
" * cccccccccccccccccccccccccccccccccccc;",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
verifyFormat("return (a > b\n"
" // comment1\n"
" // comment2\n"
@@ -8054,19 +8054,19 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
}
TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
- // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
- // and BAS_Align.
+ // Check that AllowAllArgumentsOnNextLine is respected for
+ // AlignAfterOpenBracket.
FormatStyle Style = getLLVMStyleWithColumns(35);
StringRef Input = "functionCall(paramA, paramB, paramC);\n"
"void functionDecl(int A, int B, int C);";
Style.AllowAllArgumentsOnNextLine = false;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.AlignAfterOpenBracket = true;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
@@ -8102,14 +8102,14 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
Input, Style);
// It wouldn't fit on one line with aligned parameters so this setting
// doesn't change anything for BAS_Align.
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.AlignAfterOpenBracket = true;
Style.BreakAfterOpenBracketFunction = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -9536,7 +9536,7 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) {
"SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaa));");
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
Style);
@@ -9720,17 +9720,17 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.AlignAfterOpenBracket = true;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.AlignOperands = FormatStyle::OAS_Align;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
@@ -9740,7 +9740,7 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
TEST_F(FormatTest, AlignAndBreakControlStatements) {
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -9793,7 +9793,7 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.AlignAfterOpenBracket = true;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -11870,7 +11870,7 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
TEST_F(FormatTest, WrapsTemplateParameters) {
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
verifyFormat(
"template <typename... a> struct q {};\n"
@@ -11878,7 +11878,7 @@ TEST_F(FormatTest, WrapsTemplateParameters) {
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
" y;",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ Style.AlignAfterOpenBracket = false;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
verifyFormat(
"template <typename... a> struct r {};\n"
>From cbbfa1c3bf7d06a7e2285242ecf2726e4bd4b0c1 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 10:52:50 -0600
Subject: [PATCH 26/26] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 45 ++++++++------------------
1 file changed, 13 insertions(+), 32 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 7211ed82e553c..c4f166f2c4f3e 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -197,44 +197,25 @@ the configuration (without a prefix: ``Auto``).
.. _AlignAfterOpenBracket:
-**AlignAfterOpenBracket** (``BracketAlignmentStyle``) :versionbadge:`clang-format 3.8` :ref:`¶ <AlignAfterOpenBracket>`
+**AlignAfterOpenBracket** (``Boolean``) :versionbadge:`clang-format 3.8` :ref:`¶ <AlignAfterOpenBracket>`
If ``true``, horizontally aligns arguments after an open bracket.
- This applies to round brackets (parentheses), angle brackets and square
- brackets.
-
- Possible values:
-
- * ``BAS_Align`` (in configuration: ``Align``)
- Align parameters on the open bracket, e.g.:
-
- .. code-block:: c++
-
- someLongFunction(argument1,
- argument2);
- * ``BAS_DontAlign`` (in configuration: ``DontAlign``)
- Don't align, instead use ``ContinuationIndentWidth``, e.g.:
-
- .. code-block:: c++
-
- someLongFunction(argument1,
- argument2);
-
- * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
- This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
- ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
- ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ .. code-block:: c++
- * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
- This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
- ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
- ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- in combination with ``BreakBeforeCloseBracketBracedList``,
- ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
- ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
+ true: false:
+ someLongFunction(argument1, vs. someLongFunction(argument1,
+ argument2); argument2);
+ The values ``BAS_Align`` (configuration: ``Align``), ``BAS_DontAlign``
+ (configuration ``DontAlign``), ``BAS_Always`` (configuration:
+ ``Always``), and ``BAS_BlockIndent`` (configuration: ``BlockIndent``)
+ are *deprecated*. Individual control over breaking after open brackets
+ and before close brackets are provided by separate style options, e.g.,
+ ``BreakAfterOpenBracketFunction``, ``BreakBeforeCloseBracketFunction``.
+ This applies to round brackets (parentheses), angle brackets and square
+ brackets.
.. _AlignArrayOfStructures:
More information about the cfe-commits
mailing list