[clang] [clang-format] Add BreakAfterOpenBracket* and BreakBeforeCloseBracket* (PR #108332)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 2 13:58:46 PDT 2025
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332
>From 92b575ca4db29477f4e7064841e49f475f0ca33a 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/41] 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 | 299 +++++++++++++++++++++
6 files changed, 392 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 46080c3369d36..821bd03d5e48b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9366,6 +9366,305 @@ TEST_F(FormatTest, AlignsAfterReturn) {
" code == a || code == b;");
}
+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);
+}
+
+>>>>>>> 5823461e9528 (Format: add AlignAfterControlStatement)
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
>From 91ab28471cc81f21af1fa171fd15a99de78bda2f 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/41] 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 3ad9e566a5d93..24a4533762d8d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -404,6 +404,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 5f3d8d86221185becf642f8e543ef7809a475efb 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/41] 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 821bd03d5e48b..1b99e15eaba47 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9370,7 +9370,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"
@@ -9421,7 +9423,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"
@@ -9470,7 +9474,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"
@@ -9519,7 +9525,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"
@@ -9566,7 +9574,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"
@@ -9618,7 +9631,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 6106d2d9ab86376244f452a868850a51823027e0 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/41] 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 1b99e15eaba47..2b34a2a1d98a7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9370,9 +9370,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"
@@ -9423,9 +9423,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"
@@ -9474,9 +9474,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"
@@ -9525,9 +9525,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"
@@ -9574,12 +9574,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"
@@ -9631,12 +9631,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 06ee29df2de7e2563d90f6ab5635a37a07e618e3 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/41] 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 1b7bc980cd3c0b6504100a06048abe8ddd700424 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/41] update tests
---
clang/unittests/Format/FormatTest.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2b34a2a1d98a7..c70ce2d5b2aa9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9441,6 +9441,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 44b1637a3ee68f71f284ba1132bab5087f241d00 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/41] 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 c70ce2d5b2aa9..bc54fe2cae98b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9426,6 +9426,9 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) {
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 4d4bb5528c0b7e98d263f8d1d72aef71863e3ada 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/41] 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 6fb1ad56e2ce4412d4a9513c434132247a244f09 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/41] 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 dea8c08fa68eab6f7956eedad0c71a36dcf6b1dd 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/41] 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 70a9fb12c45db1d22e6bc429433b466695a30255 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/41] 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 | 409 ++++++++++++++++++++-
6 files changed, 453 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 bc54fe2cae98b..f7f6b5bb816ca 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);
@@ -9366,7 +9368,7 @@ TEST_F(FormatTest, AlignsAfterReturn) {
" code == a || code == b;");
}
-TEST_F(FormatTest, AlignAfterConditionalStatements) {
+TEST_F(FormatTest, AlignAndBreakControlStatements) {
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
@@ -14558,7 +14560,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"
@@ -27549,6 +27551,409 @@ TEST_F(FormatTest, MultilineLambdaInConditional) {
Style);
}
+<<<<<<< HEAD
+=======
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
+ auto Style = getLLVMStyle();
+
+ StringRef Short = "functionCall(paramA, paramB, paramC);\n"
+ "void functionDecl(int a, int b, int c);";
+
+ StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
+ "paramF, paramG, paramH, paramI);\n"
+ "void functionDecl(int argumentA, int argumentB, int "
+ "argumentC, int argumentD, int argumentE);";
+
+ verifyFormat(Short, Style);
+
+ StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
+ "paramF, paramG, paramH,\n"
+ " paramI);\n"
+ "void functionDecl(int argumentA, int argumentB, int "
+ "argumentC, int argumentD,\n"
+ " int argumentE);";
+
+ verifyFormat(NoBreak, Medium, Style);
+ verifyFormat(NoBreak,
+ "functionCall(\n"
+ " paramA,\n"
+ " paramB,\n"
+ " paramC,\n"
+ " paramD,\n"
+ " paramE,\n"
+ " paramF,\n"
+ " paramG,\n"
+ " paramH,\n"
+ " paramI\n"
+ ");\n"
+ "void functionDecl(\n"
+ " int argumentA,\n"
+ " int argumentB,\n"
+ " int argumentC,\n"
+ " int argumentD,\n"
+ " int argumentE\n"
+ ");",
+ Style);
+
+ verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
+ " nestedLongFunctionCall(argument1, "
+ "argument2, argument3,\n"
+ " argument4, "
+ "argument5));",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+
+ verifyFormat(Short, Style);
+ verifyFormat(
+ "functionCall(\n"
+ " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
+ "paramI\n"
+ ");\n"
+ "void functionDecl(\n"
+ " int argumentA, int argumentB, int argumentC, int argumentD, int "
+ "argumentE\n"
+ ");",
+ Medium, Style);
+
+ Style.AllowAllArgumentsOnNextLine = false;
+ Style.AllowAllParametersOfDeclarationOnNextLine = false;
+
+ verifyFormat(Short, Style);
+ verifyFormat(
+ "functionCall(\n"
+ " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
+ "paramI\n"
+ ");\n"
+ "void functionDecl(\n"
+ " int argumentA, int argumentB, int argumentC, int argumentD, int "
+ "argumentE\n"
+ ");",
+ Medium, Style);
+
+ Style.BinPackArguments = false;
+ Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
+
+ verifyFormat(Short, Style);
+
+ verifyFormat("functionCall(\n"
+ " paramA,\n"
+ " paramB,\n"
+ " paramC,\n"
+ " paramD,\n"
+ " paramE,\n"
+ " paramF,\n"
+ " paramG,\n"
+ " paramH,\n"
+ " paramI\n"
+ ");\n"
+ "void functionDecl(\n"
+ " int argumentA,\n"
+ " int argumentB,\n"
+ " int argumentC,\n"
+ " int argumentD,\n"
+ " int argumentE\n"
+ ");",
+ Medium, Style);
+
+ verifyFormat("outerFunctionCall(\n"
+ " nestedFunctionCall(argument1),\n"
+ " nestedLongFunctionCall(\n"
+ " argument1,\n"
+ " argument2,\n"
+ " argument3,\n"
+ " argument4,\n"
+ " argument5\n"
+ " )\n"
+ ");",
+ Style);
+
+ verifyFormat("int a = (int)b;", Style);
+ verifyFormat("int a = (int)b;",
+ "int a = (\n"
+ " int\n"
+ ") b;",
+ Style);
+
+ verifyFormat("return (true);", Style);
+ verifyFormat("return (true);",
+ "return (\n"
+ " true\n"
+ ");",
+ Style);
+
+ verifyFormat("void foo();", Style);
+ verifyFormat("void foo();",
+ "void foo(\n"
+ ");",
+ Style);
+
+ verifyFormat("void foo() {}", Style);
+ verifyFormat("void foo() {}",
+ "void foo(\n"
+ ") {\n"
+ "}",
+ Style);
+
+ verifyFormat("auto string = std::string();", Style);
+ verifyFormat("auto string = std::string();",
+ "auto string = std::string(\n"
+ ");",
+ Style);
+
+ verifyFormat("void (*functionPointer)() = nullptr;", Style);
+ verifyFormat("void (*functionPointer)() = nullptr;",
+ "void (\n"
+ " *functionPointer\n"
+ ")\n"
+ "(\n"
+ ") = nullptr;",
+ Style);
+}
+
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
+ auto Style = getLLVMStyle();
+
+ verifyFormat("if (foo()) {\n"
+ " return;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (quiteLongArg !=\n"
+ " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
+ "comment\n"
+ " return;\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+
+ verifyFormat("if (foo()) {\n"
+ " return;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (quiteLongArg !=\n"
+ " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
+ "comment\n"
+ " return;\n"
+ "}",
+ Style);
+
+ verifyFormat("void foo() {\n"
+ " if (camelCaseName < alsoLongName ||\n"
+ " anotherEvenLongerName <=\n"
+ " thisReallyReallyReallyReallyReallyReallyLongerName ||"
+ "\n"
+ " otherName < thisLastName) {\n"
+ " return;\n"
+ " } else if (quiteLongName < alsoLongName ||\n"
+ " anotherEvenLongerName <=\n"
+ " thisReallyReallyReallyReallyReallyReallyLonger"
+ "Name ||\n"
+ " otherName < thisLastName) {\n"
+ " return;\n"
+ " }\n"
+ "}",
+ Style);
+
+ Style.ContinuationIndentWidth = 2;
+ verifyFormat("void foo() {\n"
+ " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+ " ontoMultipleLines && whenFormattedCorrectly) {\n"
+ " if (false) {\n"
+ " return;\n"
+ " } else if (thisIsRatherALongIfClause && "
+ "thatIExpectToBeBroken ||\n"
+ " ontoMultipleLines && whenFormattedCorrectly) {\n"
+ " return;\n"
+ " }\n"
+ " }\n"
+ "}",
+ Style);
+}
+
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
+ auto Style = getLLVMStyle();
+
+ verifyFormat("for (int i = 0; i < 5; ++i) {\n"
+ " doSomething();\n"
+ "}",
+ Style);
+
+ verifyFormat("for (int myReallyLongCountVariable = 0; "
+ "myReallyLongCountVariable < count;\n"
+ " myReallyLongCountVariable++) {\n"
+ " doSomething();\n"
+ "}",
+ Style);
+
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+
+ verifyFormat("for (int i = 0; i < 5; ++i) {\n"
+ " doSomething();\n"
+ "}",
+ Style);
+
+ verifyFormat("for (int myReallyLongCountVariable = 0; "
+ "myReallyLongCountVariable < count;\n"
+ " myReallyLongCountVariable++) {\n"
+ " doSomething();\n"
+ "}",
+ Style);
+}
+
+TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
+ auto Style = getLLVMStyleWithColumns(60);
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakBeforeCloseBracketBracedList = true;
+ // Aggregate initialization.
+ verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
+ " 10000000, 20000000\n"
+ "};",
+ Style);
+ verifyFormat("SomeStruct s{\n"
+ " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzzzzz\"\n"
+ "};",
+ Style);
+ // Designated initializers.
+ verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
+ " [0] = 10000000, [1] = 20000000\n"
+ "};",
+ Style);
+ verifyFormat("SomeStruct s{\n"
+ " .foo = \"xxxxxxxxxxxxx\",\n"
+ " .bar = \"yyyyyyyyyyyyy\",\n"
+ " .baz = \"zzzzzzzzzzzzz\"\n"
+ "};",
+ Style);
+ // List initialization.
+ verifyFormat("SomeStruct s{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ verifyFormat("SomeStruct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ verifyFormat("new SomeStruct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Member initializer.
+ verifyFormat("class SomeClass {\n"
+ " SomeStruct s{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ " };\n"
+ "};",
+ Style);
+ // Constructor member initializer.
+ verifyFormat("SomeClass::SomeClass : strct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ " } {}",
+ Style);
+ // Copy initialization.
+ verifyFormat("SomeStruct s = SomeStruct{\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Copy list initialization.
+ verifyFormat("SomeStruct s = {\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Assignment operand initialization.
+ verifyFormat("s = {\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Returned object initialization.
+ verifyFormat("return {\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Initializer list.
+ verifyFormat("auto initializerList = {\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "};",
+ Style);
+ // Function parameter initialization.
+ verifyFormat("func({\n"
+ " \"xxxxxxxxxxxxx\",\n"
+ " \"yyyyyyyyyyyyy\",\n"
+ " \"zzzzzzzzzzzzz\",\n"
+ "});",
+ Style);
+ // Nested init lists.
+ verifyFormat("SomeStruct s = {\n"
+ " {{init1, init2, init3, init4, init5},\n"
+ " {init1, init2, init3, init4, init5}}\n"
+ "};",
+ Style);
+ verifyFormat("SomeStruct s = {\n"
+ " {{\n"
+ " .init1 = 1,\n"
+ " .init2 = 2,\n"
+ " .init3 = 3,\n"
+ " .init4 = 4,\n"
+ " .init5 = 5,\n"
+ " },\n"
+ " {init1, init2, init3, init4, init5}}\n"
+ "};",
+ Style);
+ verifyFormat("SomeArrayT a[3] = {\n"
+ " {\n"
+ " foo,\n"
+ " bar,\n"
+ " },\n"
+ " {\n"
+ " foo,\n"
+ " bar,\n"
+ " },\n"
+ " SomeArrayT{},\n"
+ "};",
+ Style);
+ verifyFormat("SomeArrayT a[3] = {\n"
+ " {foo},\n"
+ " {\n"
+ " {\n"
+ " init1,\n"
+ " init2,\n"
+ " init3,\n"
+ " },\n"
+ " {\n"
+ " init1,\n"
+ " init2,\n"
+ " init3,\n"
+ " },\n"
+ " },\n"
+ " {baz},\n"
+ "};",
+ Style);
+}
+
+>>>>>>> cbd14c5d622a (add support for braced list initializers)
TEST_F(FormatTest, UnderstandsDigraphs) {
verifyFormat("int arr<:5:> = {};");
verifyFormat("int arr[5] = <%%>;");
>From d38d11174adbc442d3a374b4dc3ff751bc1098e7 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/41] 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/AlignBracketsTest.cpp | 33 +-
clang/unittests/Format/ConfigParseTest.cpp | 2 +
clang/unittests/Format/FormatTest.cpp | 496 +++----------------
clang/unittests/Format/FormatTestJS.cpp | 2 +-
8 files changed, 139 insertions(+), 446 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/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp
index c4380ae415751..c280a4b84d615 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -64,7 +64,7 @@ TEST_F(AlignBracketsTest, 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"
@@ -115,7 +115,9 @@ TEST_F(AlignBracketsTest, 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;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@@ -254,7 +256,8 @@ TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndent) {
"argument5));",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat(Short, Style);
verifyFormat(
@@ -378,7 +381,8 @@ TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("if (foo()) {\n"
" return;\n"
@@ -440,7 +444,8 @@ TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndentForStatement) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
verifyFormat("for (int i = 0; i < 5; ++i) {\n"
" doSomething();\n"
@@ -457,7 +462,8 @@ TEST_F(AlignBracketsTest, AlignAfterOpenBracketBlockIndentForStatement) {
TEST_F(AlignBracketsTest, 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"
@@ -625,13 +631,14 @@ TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
Input, Style);
// However, BAS_AlwaysBreak and BAS_BlockIndent 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"
@@ -678,13 +685,14 @@ TEST_F(AlignBracketsTest, 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"
@@ -734,7 +742,7 @@ TEST_F(AlignBracketsTest, 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"
@@ -743,7 +751,8 @@ TEST_F(AlignBracketsTest, FormatsDefinitionBreakAlways) {
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, b);\n"
"}",
BreakAlways);
- BreakAlways.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
+ BreakAlways.BreakAfterOpenBracketFunction = true;
+ BreakAlways.BreakBeforeCloseBracketFunction = true;
verifyFormat(
"void someLongFunctionName(\n"
" int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
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 f7f6b5bb816ca..a4f434e66128c 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;
@@ -8053,6 +8053,71 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
Style);
}
+TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
+ // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
+ // and BAS_Align.
+ 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;
+ verifyFormat(StringRef("functionCall(paramA, paramB,\n"
+ " paramC);\n"
+ "void functionDecl(int A, int B,\n"
+ " int C);"),
+ Input, Style);
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ verifyFormat(StringRef("functionCall(paramA, paramB,\n"
+ " paramC);\n"
+ "void functionDecl(int A, int B,\n"
+ " int C);"),
+ Input, Style);
+ // However, BreakAfterOpenBracketFunction should take precedence over
+ // AllowAllArgumentsOnNextLine.
+ Style.BreakAfterOpenBracketFunction = true;
+ verifyFormat(StringRef("functionCall(\n"
+ " paramA, paramB, paramC);\n"
+ "void functionDecl(\n"
+ " int A, int B, int C);"),
+ Input, Style);
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = true;
+ verifyFormat("functionCall(\n"
+ " paramA, paramB, paramC\n"
+ ");\n"
+ "void functionDecl(\n"
+ " int A, int B, int C\n"
+ ");",
+ Input, Style);
+
+ // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
+ // first argument.
+ Style.AllowAllArgumentsOnNextLine = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakBeforeCloseBracketFunction = false;
+ verifyFormat(StringRef("functionCall(\n"
+ " paramA, paramB, paramC);\n"
+ "void functionDecl(\n"
+ " int A, int B, int C);"),
+ 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.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;
+ verifyFormat(StringRef("functionCall(\n"
+ " paramA, paramB, paramC);\n"
+ "void functionDecl(\n"
+ " int A, int B, int C);"),
+ Input, Style);
+}
+
+>>>>>>> d1e73bcfe430 (Add support for functions and replace AlwaysBreak, BlockIndent)
TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
"void emptyFunctionDefinition() {}\n"
@@ -9424,7 +9489,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;
@@ -9492,7 +9557,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -9543,7 +9607,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
Style.BreakAfterOpenBracketIf = false;
Style.BreakAfterOpenBracketLoop = false;
Style.BreakAfterOpenBracketSwitch = false;
@@ -9592,7 +9655,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
Style.BreakAfterOpenBracketIf = true;
Style.BreakAfterOpenBracketLoop = true;
Style.BreakAfterOpenBracketSwitch = true;
@@ -9649,7 +9711,6 @@ TEST_F(FormatTest, AlignAndBreakControlStatements) {
"}",
Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
Style.BreakAfterOpenBracketIf = false;
Style.BreakAfterOpenBracketLoop = false;
Style.BreakAfterOpenBracketSwitch = false;
@@ -11522,7 +11583,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"
@@ -11532,7 +11593,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"
@@ -16218,13 +16279,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"
@@ -18019,7 +18081,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, "
@@ -18044,7 +18106,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, "
@@ -22964,7 +23027,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>",
@@ -24219,7 +24282,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
@@ -27551,409 +27614,6 @@ TEST_F(FormatTest, MultilineLambdaInConditional) {
Style);
}
-<<<<<<< HEAD
-=======
-TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
- auto Style = getLLVMStyle();
-
- StringRef Short = "functionCall(paramA, paramB, paramC);\n"
- "void functionDecl(int a, int b, int c);";
-
- StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, "
- "paramF, paramG, paramH, paramI);\n"
- "void functionDecl(int argumentA, int argumentB, int "
- "argumentC, int argumentD, int argumentE);";
-
- verifyFormat(Short, Style);
-
- StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, "
- "paramF, paramG, paramH,\n"
- " paramI);\n"
- "void functionDecl(int argumentA, int argumentB, int "
- "argumentC, int argumentD,\n"
- " int argumentE);";
-
- verifyFormat(NoBreak, Medium, Style);
- verifyFormat(NoBreak,
- "functionCall(\n"
- " paramA,\n"
- " paramB,\n"
- " paramC,\n"
- " paramD,\n"
- " paramE,\n"
- " paramF,\n"
- " paramG,\n"
- " paramH,\n"
- " paramI\n"
- ");\n"
- "void functionDecl(\n"
- " int argumentA,\n"
- " int argumentB,\n"
- " int argumentC,\n"
- " int argumentD,\n"
- " int argumentE\n"
- ");",
- Style);
-
- verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n"
- " nestedLongFunctionCall(argument1, "
- "argument2, argument3,\n"
- " argument4, "
- "argument5));",
- Style);
-
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
-
- verifyFormat(Short, Style);
- verifyFormat(
- "functionCall(\n"
- " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
- "paramI\n"
- ");\n"
- "void functionDecl(\n"
- " int argumentA, int argumentB, int argumentC, int argumentD, int "
- "argumentE\n"
- ");",
- Medium, Style);
-
- Style.AllowAllArgumentsOnNextLine = false;
- Style.AllowAllParametersOfDeclarationOnNextLine = false;
-
- verifyFormat(Short, Style);
- verifyFormat(
- "functionCall(\n"
- " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, "
- "paramI\n"
- ");\n"
- "void functionDecl(\n"
- " int argumentA, int argumentB, int argumentC, int argumentD, int "
- "argumentE\n"
- ");",
- Medium, Style);
-
- Style.BinPackArguments = false;
- Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
-
- verifyFormat(Short, Style);
-
- verifyFormat("functionCall(\n"
- " paramA,\n"
- " paramB,\n"
- " paramC,\n"
- " paramD,\n"
- " paramE,\n"
- " paramF,\n"
- " paramG,\n"
- " paramH,\n"
- " paramI\n"
- ");\n"
- "void functionDecl(\n"
- " int argumentA,\n"
- " int argumentB,\n"
- " int argumentC,\n"
- " int argumentD,\n"
- " int argumentE\n"
- ");",
- Medium, Style);
-
- verifyFormat("outerFunctionCall(\n"
- " nestedFunctionCall(argument1),\n"
- " nestedLongFunctionCall(\n"
- " argument1,\n"
- " argument2,\n"
- " argument3,\n"
- " argument4,\n"
- " argument5\n"
- " )\n"
- ");",
- Style);
-
- verifyFormat("int a = (int)b;", Style);
- verifyFormat("int a = (int)b;",
- "int a = (\n"
- " int\n"
- ") b;",
- Style);
-
- verifyFormat("return (true);", Style);
- verifyFormat("return (true);",
- "return (\n"
- " true\n"
- ");",
- Style);
-
- verifyFormat("void foo();", Style);
- verifyFormat("void foo();",
- "void foo(\n"
- ");",
- Style);
-
- verifyFormat("void foo() {}", Style);
- verifyFormat("void foo() {}",
- "void foo(\n"
- ") {\n"
- "}",
- Style);
-
- verifyFormat("auto string = std::string();", Style);
- verifyFormat("auto string = std::string();",
- "auto string = std::string(\n"
- ");",
- Style);
-
- verifyFormat("void (*functionPointer)() = nullptr;", Style);
- verifyFormat("void (*functionPointer)() = nullptr;",
- "void (\n"
- " *functionPointer\n"
- ")\n"
- "(\n"
- ") = nullptr;",
- Style);
-}
-
-TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) {
- auto Style = getLLVMStyle();
-
- verifyFormat("if (foo()) {\n"
- " return;\n"
- "}",
- Style);
-
- verifyFormat("if (quiteLongArg !=\n"
- " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
- "comment\n"
- " return;\n"
- "}",
- Style);
-
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
-
- verifyFormat("if (foo()) {\n"
- " return;\n"
- "}",
- Style);
-
- verifyFormat("if (quiteLongArg !=\n"
- " (alsoLongArg - 1)) { // ABC is a very longgggggggggggg "
- "comment\n"
- " return;\n"
- "}",
- Style);
-
- verifyFormat("void foo() {\n"
- " if (camelCaseName < alsoLongName ||\n"
- " anotherEvenLongerName <=\n"
- " thisReallyReallyReallyReallyReallyReallyLongerName ||"
- "\n"
- " otherName < thisLastName) {\n"
- " return;\n"
- " } else if (quiteLongName < alsoLongName ||\n"
- " anotherEvenLongerName <=\n"
- " thisReallyReallyReallyReallyReallyReallyLonger"
- "Name ||\n"
- " otherName < thisLastName) {\n"
- " return;\n"
- " }\n"
- "}",
- Style);
-
- Style.ContinuationIndentWidth = 2;
- verifyFormat("void foo() {\n"
- " if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
- " ontoMultipleLines && whenFormattedCorrectly) {\n"
- " if (false) {\n"
- " return;\n"
- " } else if (thisIsRatherALongIfClause && "
- "thatIExpectToBeBroken ||\n"
- " ontoMultipleLines && whenFormattedCorrectly) {\n"
- " return;\n"
- " }\n"
- " }\n"
- "}",
- Style);
-}
-
-TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
- auto Style = getLLVMStyle();
-
- verifyFormat("for (int i = 0; i < 5; ++i) {\n"
- " doSomething();\n"
- "}",
- Style);
-
- verifyFormat("for (int myReallyLongCountVariable = 0; "
- "myReallyLongCountVariable < count;\n"
- " myReallyLongCountVariable++) {\n"
- " doSomething();\n"
- "}",
- Style);
-
- Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
-
- verifyFormat("for (int i = 0; i < 5; ++i) {\n"
- " doSomething();\n"
- "}",
- Style);
-
- verifyFormat("for (int myReallyLongCountVariable = 0; "
- "myReallyLongCountVariable < count;\n"
- " myReallyLongCountVariable++) {\n"
- " doSomething();\n"
- "}",
- Style);
-}
-
-TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentInitializers) {
- auto Style = getLLVMStyleWithColumns(60);
- Style.BreakAfterOpenBracketBracedList = true;
- Style.BreakBeforeCloseBracketBracedList = true;
- // Aggregate initialization.
- verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
- " 10000000, 20000000\n"
- "};",
- Style);
- verifyFormat("SomeStruct s{\n"
- " \"xxxxxxxxxxxxxxxx\", \"yyyyyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzzzzz\"\n"
- "};",
- Style);
- // Designated initializers.
- verifyFormat("int LooooooooooooooooooooooooongVariable[2] = {\n"
- " [0] = 10000000, [1] = 20000000\n"
- "};",
- Style);
- verifyFormat("SomeStruct s{\n"
- " .foo = \"xxxxxxxxxxxxx\",\n"
- " .bar = \"yyyyyyyyyyyyy\",\n"
- " .baz = \"zzzzzzzzzzzzz\"\n"
- "};",
- Style);
- // List initialization.
- verifyFormat("SomeStruct s{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- verifyFormat("SomeStruct{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- verifyFormat("new SomeStruct{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Member initializer.
- verifyFormat("class SomeClass {\n"
- " SomeStruct s{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- " };\n"
- "};",
- Style);
- // Constructor member initializer.
- verifyFormat("SomeClass::SomeClass : strct{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- " } {}",
- Style);
- // Copy initialization.
- verifyFormat("SomeStruct s = SomeStruct{\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Copy list initialization.
- verifyFormat("SomeStruct s = {\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Assignment operand initialization.
- verifyFormat("s = {\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Returned object initialization.
- verifyFormat("return {\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Initializer list.
- verifyFormat("auto initializerList = {\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "};",
- Style);
- // Function parameter initialization.
- verifyFormat("func({\n"
- " \"xxxxxxxxxxxxx\",\n"
- " \"yyyyyyyyyyyyy\",\n"
- " \"zzzzzzzzzzzzz\",\n"
- "});",
- Style);
- // Nested init lists.
- verifyFormat("SomeStruct s = {\n"
- " {{init1, init2, init3, init4, init5},\n"
- " {init1, init2, init3, init4, init5}}\n"
- "};",
- Style);
- verifyFormat("SomeStruct s = {\n"
- " {{\n"
- " .init1 = 1,\n"
- " .init2 = 2,\n"
- " .init3 = 3,\n"
- " .init4 = 4,\n"
- " .init5 = 5,\n"
- " },\n"
- " {init1, init2, init3, init4, init5}}\n"
- "};",
- Style);
- verifyFormat("SomeArrayT a[3] = {\n"
- " {\n"
- " foo,\n"
- " bar,\n"
- " },\n"
- " {\n"
- " foo,\n"
- " bar,\n"
- " },\n"
- " SomeArrayT{},\n"
- "};",
- Style);
- verifyFormat("SomeArrayT a[3] = {\n"
- " {foo},\n"
- " {\n"
- " {\n"
- " init1,\n"
- " init2,\n"
- " init3,\n"
- " },\n"
- " {\n"
- " init1,\n"
- " init2,\n"
- " init3,\n"
- " },\n"
- " },\n"
- " {baz},\n"
- "};",
- Style);
-}
-
->>>>>>> cbd14c5d622a (add support for braced list initializers)
TEST_F(FormatTest, UnderstandsDigraphs) {
verifyFormat("int arr<:5:> = {};");
verifyFormat("int arr[5] = <%%>;");
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 6d62072f0d299249c05c41cb16d7282c82a77323 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/41] 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 24a4533762d8d..8454069b6b533 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -407,6 +407,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 934d258c5836c1e287774d1972e315c24303fd39 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/41] 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 8454069b6b533..7eab624d87531 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -404,8 +404,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 f29c8350dc292a809ca00507a3e013c607598922 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/41] 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 5893b38f89ceb1cfab46740399fb8c511627bea7 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/41] 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 603f5927b2cec61d83da0837f416aa2b33411fe8 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/41] 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 613275265d02a6146a87e82bddaf62d4234b6d5e 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/41] 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 f139cbbb8f830d7b403b22fdd13df51dfd3c40cc 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/41] 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 4a567872420f831a87ca7873fc7e9a4a636c674d 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/41] 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 3928f337d58c387904f7928a47f52d17bbcf34a1 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/41] 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 449fe8794ce52424c2f959e58d99b4708bbeda7b 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/41] 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 c74c1f3ed05525376838fb21af0bfe071eee2b1c 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/41] 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 6afc98ce2e51ecdd643777755b9e980e2fc1e7c6 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/41] 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 66db93672a5edac77f4d2f94eeb3814a1576502c 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/41] 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/AlignBracketsTest.cpp | 16 +-
clang/unittests/Format/ConfigParseTest.cpp | 13 +-
clang/unittests/Format/FormatTest.cpp | 352 +------------------
8 files changed, 44 insertions(+), 413 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/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp
index c280a4b84d615..359bb354eaf32 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -28,7 +28,7 @@ TEST_F(AlignBracketsTest, 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);
@@ -617,13 +617,13 @@ TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
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"
@@ -658,13 +658,13 @@ TEST_F(AlignBracketsTest, 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;
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"
@@ -770,17 +770,17 @@ TEST_F(AlignBracketsTest, 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);",
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 a4f434e66128c..6777680d3f9b4 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"
@@ -9433,336 +9433,6 @@ TEST_F(FormatTest, AlignsAfterReturn) {
" code == a || code == b;");
}
-TEST_F(FormatTest, AlignAndBreakControlStatements) {
- FormatStyle Style = getLLVMStyle();
-
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- Style.BreakAfterOpenBracketIf = true;
- Style.BreakAfterOpenBracketLoop = true;
- Style.BreakAfterOpenBracketSwitch = true;
-
- 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_Align;
- 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"
- " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
- ") == 0) {\n"
- " return;\n"
- " } else if (\n"
- " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &\n"
- " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
- ") == 0) {\n"
- " return;\n"
- " }\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"
- " 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.BreakAfterOpenBracketIf = true;
- Style.BreakAfterOpenBracketLoop = true;
- Style.BreakAfterOpenBracketSwitch = true;
-
- 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.BreakAfterOpenBracketIf = false;
- Style.BreakAfterOpenBracketLoop = false;
- Style.BreakAfterOpenBracketSwitch = 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 (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.BreakAfterOpenBracketIf = true;
- Style.BreakAfterOpenBracketLoop = true;
- Style.BreakAfterOpenBracketSwitch = true;
- Style.BreakBeforeCloseBracketIf = true;
- Style.BreakBeforeCloseBracketLoop = true;
- Style.BreakBeforeCloseBracketSwitch = true;
-
- 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.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"
- " 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);
-}
-
->>>>>>> 5823461e9528 (Format: add AlignAfterControlStatement)
TEST_F(FormatTest, BreaksConditionalExpressions) {
verifyFormat(
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
@@ -11567,7 +11237,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"
@@ -11575,7 +11245,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 183e03c482804fabb49978fb9be739945ffeceb7 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/41] 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:
>From 10038eebcd3fb4ee5e885d333ba87d88ed0c98ab Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 13:19:49 -0600
Subject: [PATCH 27/41] drop the this
---
clang/lib/Format/FormatToken.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index a0e17c07f035c..12c8268e5cfbc 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -651,9 +651,9 @@ struct FormatToken {
}
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));
+ return isOneOf(tok::kw_for, tok::kw_while) ||
+ (Style.isJavaScript() && isNot(tok::l_paren) && Previous &&
+ Previous->is(tok::kw_for));
}
bool closesScopeAfterBlock() const {
>From 9f93c13302b5e500d5a68bac2b85beb33d61534e Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 13:46:45 -0600
Subject: [PATCH 28/41] Revert "deprecate and remove BAS_Align and
BAS_DontAlign"
This reverts commit 4062b608d728723ae55082e7922017ba84daba14.
---
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 | 22 ++++++------
7 files changed, 75 insertions(+), 36 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 170d33763e505..a90d6846d3508 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,25 +62,39 @@ 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
- bool AlignAfterOpenBracket;
+ BracketAlignmentStyle 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 09a82ccffeb19..929033dfdec4f 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 &&
+ if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!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 || PrecedenceLevel > prec::Comma ||
- Current.NestingLevel == 0) &&
+ (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
+ PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
(Previous && Previous->isOneOf(TT_TableGenDAGArgListComma,
TT_TableGenDAGArgListCommaToBreak)))) {
@@ -1903,7 +1903,8 @@ 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) {
+ Current.isNot(TT_UnaryOperator) &&
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
NewParenState.StartOfFunctionCall = State.Column;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index dbe65b923716b..3861864200bf9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,6 +203,17 @@ 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> {
@@ -1531,7 +1542,7 @@ static void expandPresetsSpacesInParens(FormatStyle &Expanded) {
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2;
- LLVMStyle.AlignAfterOpenBracket = true;
+ LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignConsecutiveAssignments = {};
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
@@ -1852,7 +1863,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
if (Language == FormatStyle::LK_Java) {
- GoogleStyle.AlignAfterOpenBracket = false;
+ GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign;
GoogleStyle.AlignTrailingComments = {};
GoogleStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
@@ -2005,7 +2016,7 @@ FormatStyle getMozillaStyle() {
FormatStyle getWebKitStyle() {
FormatStyle Style = getLLVMStyle();
Style.AccessModifierOffset = -4;
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
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 be9823b9e02e9..e84afe3dd9814 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)
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7d85cb0edefb6..d0a81b0854f33 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4408,8 +4408,10 @@ 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)
+ if (Left.is(tok::l_paren) && InFunctionDecl &&
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
return 100;
+ }
if (Left.is(tok::l_paren) && Left.Previous &&
(Left.Previous->isOneOf(tok::kw_for, tok::kw__Generic) ||
Left.Previous->isIf())) {
@@ -4425,7 +4427,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 &&
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
return 0;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index cd9760975dcc3..aa499f3c267e3 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -159,7 +159,6 @@ 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);
@@ -544,6 +543,18 @@ 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 6777680d3f9b4..7581ad665accb 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 = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
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 = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
"\t&& (someOtherLongishConditionPart1\n"
@@ -7556,7 +7556,7 @@ TEST_F(FormatTest, NoOperandAlignment) {
" * cccccccccccccccccccccccccccccccccccc;",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
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
- // AlignAfterOpenBracket.
+ // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
+ // and BAS_Align.
FormatStyle Style = getLLVMStyleWithColumns(35);
StringRef Input = "functionCall(paramA, paramB, paramC);\n"
"void functionDecl(int A, int B, int C);";
Style.AllowAllArgumentsOnNextLine = false;
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
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 = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
Style.BreakAfterOpenBracketFunction = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -11237,7 +11237,7 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
TEST_F(FormatTest, WrapsTemplateParameters) {
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
verifyFormat(
"template <typename... a> struct q {};\n"
@@ -11245,7 +11245,7 @@ TEST_F(FormatTest, WrapsTemplateParameters) {
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
" y;",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
verifyFormat(
"template <typename... a> struct r {};\n"
>From c4b033074a9ad8435c2f81e071fa9328a1b0e273 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 14:12:57 -0600
Subject: [PATCH 29/41] handle backward compatibility for clang-format users
---
clang/include/clang/Format/Format.h | 4 +++
clang/lib/Format/Format.cpp | 35 ++++++++++++++++++++++
clang/unittests/Format/ConfigParseTest.cpp | 6 +++-
3 files changed, 44 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index a90d6846d3508..65a8866e0ba02 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -80,6 +80,8 @@ struct FormatStyle {
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// BAS_AlwaysBreak,
+ /// For backward compatibility. Do not use.
+ BAS_ABDeprecated,
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
@@ -87,6 +89,8 @@ struct FormatStyle {
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
/// BAS_BlockIndent,
+ /// For backward compatibility. Do not use.
+ BAS_BIDeprecated,
};
/// If ``true``, horizontally aligns arguments after an open bracket.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 3861864200bf9..628bae68f988a 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -211,6 +211,8 @@ 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_ABDeprecated);
+ IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BIDeprecated);
}
};
@@ -1253,6 +1255,39 @@ 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_ABDeprecated) {
+ if (!Style.BreakAfterOpenBracketBracedList &&
+ !Style.BreakAfterOpenBracketFunction &&
+ !Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
+ !Style.BreakAfterOpenBracketSwitch) {
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakAfterOpenBracketIf = true;
+ }
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BIDeprecated) {
+ 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;
+ }
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ }
+
// 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 aa499f3c267e3..b7fdb5e88ee94 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -548,10 +548,14 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::BAS_Align);
CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
FormatStyle::BAS_DontAlign);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
// For backward compatibility:
+ CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
FormatStyle::BAS_DontAlign);
+ CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
>From fb9d7e93ff56c3847fea1e077b1779037987b211 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 14:16:14 -0600
Subject: [PATCH 30/41] update Format.h
---
clang/include/clang/Format/Format.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 65a8866e0ba02..6c361f24da0b4 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -76,19 +76,19 @@ struct FormatStyle {
/// argument2);
/// \endcode
BAS_DontAlign,
+ /// ``BAS_AlwaysBreak``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- /// BAS_AlwaysBreak,
/// For backward compatibility. Do not use.
BAS_ABDeprecated,
+ /// ``BAS_BlockIndent``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// in combination with ``BreakBeforeCloseBracketBracedList``,
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- /// BAS_BlockIndent,
/// For backward compatibility. Do not use.
BAS_BIDeprecated,
};
>From 8a23cb9a660a9d2b04aa235855b35f5055782f25 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 25 Aug 2025 14:16:18 -0600
Subject: [PATCH 31/41] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 49 +++++++++++++++++++-------
1 file changed, 36 insertions(+), 13 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index c4f166f2c4f3e..c7f3312c67305 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -197,25 +197,48 @@ the configuration (without a prefix: ``Auto``).
.. _AlignAfterOpenBracket:
-**AlignAfterOpenBracket** (``Boolean``) :versionbadge:`clang-format 3.8` :ref:`¶ <AlignAfterOpenBracket>`
+**AlignAfterOpenBracket** (``BracketAlignmentStyle``) :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.
- .. code-block:: c++
+ Possible values:
- true: false:
- someLongFunction(argument1, vs. someLongFunction(argument1,
- argument2); argument2);
+ * ``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_ABDeprecated`` (in configuration: ``ABDeprecated``)
+ ``BAS_AlwaysBreak``
+ This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ For backward compatibility. Do not use.
+
+ * ``BAS_BIDeprecated`` (in configuration: ``BIDeprecated``)
+ ``BAS_BlockIndent``
+ This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
+ ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
+ ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ in combination with ``BreakBeforeCloseBracketBracedList``,
+ ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
+ ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
+ For backward compatibility. Do not use.
- 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:
>From 374121a65da7d4bfc8f8991265dcf519959d34b6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:11:57 -0600
Subject: [PATCH 32/41] try to handle bool for the public api
---
clang/docs/ClangFormatStyleOptions.rst | 8 ++---
clang/include/clang/Format/Format.h | 12 +++----
clang/lib/Format/ContinuationIndenter.cpp | 6 ++--
clang/lib/Format/Format.cpp | 38 ++++++++++++----------
clang/lib/Format/FormatToken.cpp | 2 +-
clang/lib/Format/TokenAnnotator.cpp | 4 +--
clang/unittests/Format/ConfigParseTest.cpp | 22 +++++--------
clang/unittests/Format/FormatTest.cpp | 18 +++++-----
8 files changed, 51 insertions(+), 59 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index c7f3312c67305..7211ed82e553c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -221,22 +221,18 @@ the configuration (without a prefix: ``Auto``).
someLongFunction(argument1,
argument2);
- * ``BAS_ABDeprecated`` (in configuration: ``ABDeprecated``)
- ``BAS_AlwaysBreak``
+ * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- For backward compatibility. Do not use.
- * ``BAS_BIDeprecated`` (in configuration: ``BIDeprecated``)
- ``BAS_BlockIndent``
+ * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
in combination with ``BreakBeforeCloseBracketBracedList``,
``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- For backward compatibility. Do not use.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6c361f24da0b4..dd9b4bbf13722 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,7 +62,9 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
+ /*
/// Different styles for aligning after open brackets.
+ /// This is **deprecated**. For backward compatibility only.
enum BracketAlignmentStyle : int8_t {
/// Align parameters on the open bracket, e.g.:
/// \code
@@ -76,22 +78,20 @@ struct FormatStyle {
/// argument2);
/// \endcode
BAS_DontAlign,
- /// ``BAS_AlwaysBreak``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- /// For backward compatibility. Do not use.
- BAS_ABDeprecated,
- /// ``BAS_BlockIndent``
+ BAS_AlwaysBreak,
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// in combination with ``BreakBeforeCloseBracketBracedList``,
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- /// For backward compatibility. Do not use.
- BAS_BIDeprecated,
+ BAS_BlockIndent,
};
+ */
+ LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
/// If ``true``, horizontally aligns arguments after an open bracket.
///
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 929033dfdec4f..edf253e628073 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 != false &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
Previous.isNot(TT_TableGenDAGArgOpener) &&
@@ -1862,7 +1862,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(!Style.isJava() && PrecedenceLevel > 0)) &&
- (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
+ (Style.AlignAfterOpenBracket != false ||
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
(Previous && Previous->isOneOf(TT_TableGenDAGArgListComma,
@@ -1904,7 +1904,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
if (PrecedenceLevel != prec::Conditional &&
Current.isNot(TT_UnaryOperator) &&
- Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
+ Style.AlignAfterOpenBracket != false) {
NewParenState.StartOfFunctionCall = State.Column;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 628bae68f988a..1f89f32bbbf05 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,16 +203,18 @@ 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);
- IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_ABDeprecated);
- IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BIDeprecated);
+// For backward compatibility.
+template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
+ static void enumInput(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
+ IO.enumCase(Value, "Align", FormatStyle::BracketAlignmentStyle(true));
+ IO.enumCase(Value, "DontAlign", FormatStyle::BracketAlignmentStyle(false));
+ IO.enumCase(Value, "AlwaysBreak", FormatStyle::BracketAlignmentStyle(true));
+ IO.enumCase(Value, "BlockIndent", FormatStyle::BracketAlignmentStyle(true));
+ IO.enumCase(Value, "true", FormatStyle::BracketAlignmentStyle(true));
+ IO.enumCase(Value, "false", FormatStyle::BracketAlignmentStyle(true));
+ }
+ static void mapping(IO &IO, FormatStyle::BracketAlignmentStyle &BAS) {
+ IO.mapOptional("AlignAfterOpenBracket", BAS.value);
}
};
@@ -981,7 +983,6 @@ template <> struct MappingTraits<FormatStyle> {
}
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
- IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures);
IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
@@ -1258,7 +1259,8 @@ template <> struct MappingTraits<FormatStyle> {
// If AlwaysBreak or BlockIndent were specified but individual
// options for BreakAfterOpenBracket* (CloseAfterOpenBracket*),
// initialize the latter to preserve backwards compatibility.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_ABDeprecated) {
+ bool set = false;
+ if (set) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
@@ -1267,8 +1269,9 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakAfterOpenBracketFunction = true;
Style.BreakAfterOpenBracketIf = true;
}
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BIDeprecated) {
+ set = false;
+ }
+ if (set) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
@@ -1285,7 +1288,6 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakBeforeCloseBracketFunction = true;
Style.BreakBeforeCloseBracketIf = true;
}
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
}
// If AlwaysBreakAfterDefinitionReturnType was specified but
@@ -1577,7 +1579,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;
@@ -1898,7 +1900,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;
@@ -2051,7 +2053,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..1dd26532407a7 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 == false)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d0a81b0854f33..f369597880a3e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4409,7 +4409,7 @@ 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) {
+ Style.AlignAfterOpenBracket != false) {
return 100;
}
if (Left.is(tok::l_paren) && Left.Previous &&
@@ -4427,7 +4427,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 == false &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
return 0;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index b7fdb5e88ee94..2736c8d101475 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -543,21 +543,15 @@ 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 = false;
+ CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, true);
+ CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, false);
// For backward compatibility:
- CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
- CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
- FormatStyle::BAS_DontAlign);
- CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
+ CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, true);
+ CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, false);
+ CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, true);
+ Style.AlignAfterOpenBracket = false;
+ CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, true);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7581ad665accb..6e4b7798e7b12 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"
@@ -8060,13 +8060,13 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
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"
@@ -11237,7 +11237,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"
@@ -11245,7 +11245,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 8add832d8bb06fbc45a392e7541eca8f1ee7fd49 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:32:23 -0600
Subject: [PATCH 33/41] wip
---
clang/include/clang/Format/Format.h | 32 +-----------------
clang/lib/Format/Format.cpp | 50 ++++++++++++++++++++++++++++-
2 files changed, 50 insertions(+), 32 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index dd9b4bbf13722..d44d9de5a3dd8 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,42 +62,12 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /*
- /// Different styles for aligning after open brackets.
- /// This is **deprecated**. For backward compatibility only.
- 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,
- };
- */
- LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
-
/// If ``true``, horizontally aligns arguments after an open bracket.
///
/// This applies to round brackets (parentheses), angle brackets and square
/// brackets.
/// \version 3.8
+ LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
BracketAlignmentStyle AlignAfterOpenBracket;
/// Different style for aligning array initializers.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 1f89f32bbbf05..5dbf195cc6d91 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -205,6 +205,52 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
// For backward compatibility.
template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
+
+ class NormalizedBracketAlignmentStyle {
+ public:
+
+ NormalizedBracketAlignmentStyle(IO &IO,
+ FormatStyle::BracketAlignmentStyle &BAS) :
+ AlignAfterOpenBracket(BAS.value == true ? BAS_Align : BAS_DontAlign) {
+ }
+
+ FormatStyle::BracketAlignmentStyle denormalize(IO &IO) {
+ if (AlignAfterOpenBracket == BAS_DontAlign)
+ return FormatStyle::BracketAlignmentStyle(false);
+ return true;
+ }
+
+ /// Different styles for aligning after open brackets.
+ /// This is **deprecated**. For backward compatibility only.
+ enum BracketAlignmentStyleEnum : 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,
+ };
+
+ BracketAlignmentStyleEnum AlignAfterOpenBracket;
+ };
+
static void enumInput(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
IO.enumCase(Value, "Align", FormatStyle::BracketAlignmentStyle(true));
IO.enumCase(Value, "DontAlign", FormatStyle::BracketAlignmentStyle(false));
@@ -214,7 +260,9 @@ template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
IO.enumCase(Value, "false", FormatStyle::BracketAlignmentStyle(true));
}
static void mapping(IO &IO, FormatStyle::BracketAlignmentStyle &BAS) {
- IO.mapOptional("AlignAfterOpenBracket", BAS.value);
+ MappingNormalization<NormalizedBracketAlignmentStyle,
+ FormatStyle::BracketAlignmentStyle> keys(IO, BAS);
+ IO.mapOptional("AlignAfterOpenBracket", keys->AlignAfterOpenBracket);
}
};
>From b38c36b27eb8137797f0bfd1de79bd4c2070e005 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:32:41 -0600
Subject: [PATCH 34/41] Revert "wip"
This reverts commit 8dc407986190c8c4260b61a67709265a00ef5777.
---
clang/include/clang/Format/Format.h | 32 +++++++++++++++++-
clang/lib/Format/Format.cpp | 50 +----------------------------
2 files changed, 32 insertions(+), 50 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index d44d9de5a3dd8..dd9b4bbf13722 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,12 +62,42 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
+ /*
+ /// Different styles for aligning after open brackets.
+ /// This is **deprecated**. For backward compatibility only.
+ 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,
+ };
+ */
+ LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
+
/// If ``true``, horizontally aligns arguments after an open bracket.
///
/// This applies to round brackets (parentheses), angle brackets and square
/// brackets.
/// \version 3.8
- LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
BracketAlignmentStyle AlignAfterOpenBracket;
/// Different style for aligning array initializers.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 5dbf195cc6d91..1f89f32bbbf05 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -205,52 +205,6 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
// For backward compatibility.
template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
-
- class NormalizedBracketAlignmentStyle {
- public:
-
- NormalizedBracketAlignmentStyle(IO &IO,
- FormatStyle::BracketAlignmentStyle &BAS) :
- AlignAfterOpenBracket(BAS.value == true ? BAS_Align : BAS_DontAlign) {
- }
-
- FormatStyle::BracketAlignmentStyle denormalize(IO &IO) {
- if (AlignAfterOpenBracket == BAS_DontAlign)
- return FormatStyle::BracketAlignmentStyle(false);
- return true;
- }
-
- /// Different styles for aligning after open brackets.
- /// This is **deprecated**. For backward compatibility only.
- enum BracketAlignmentStyleEnum : 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,
- };
-
- BracketAlignmentStyleEnum AlignAfterOpenBracket;
- };
-
static void enumInput(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
IO.enumCase(Value, "Align", FormatStyle::BracketAlignmentStyle(true));
IO.enumCase(Value, "DontAlign", FormatStyle::BracketAlignmentStyle(false));
@@ -260,9 +214,7 @@ template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
IO.enumCase(Value, "false", FormatStyle::BracketAlignmentStyle(true));
}
static void mapping(IO &IO, FormatStyle::BracketAlignmentStyle &BAS) {
- MappingNormalization<NormalizedBracketAlignmentStyle,
- FormatStyle::BracketAlignmentStyle> keys(IO, BAS);
- IO.mapOptional("AlignAfterOpenBracket", keys->AlignAfterOpenBracket);
+ IO.mapOptional("AlignAfterOpenBracket", BAS.value);
}
};
>From 7415b7f89bfa4df2b3f96292bcce9fed131a9640 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:42:40 -0600
Subject: [PATCH 35/41] Revert "try to handle bool for the public api"
This reverts commit 49e7415ff029d9a6b12df0f523fa7a866209c099.
---
clang/docs/ClangFormatStyleOptions.rst | 8 +++--
clang/include/clang/Format/Format.h | 12 +++----
clang/lib/Format/ContinuationIndenter.cpp | 6 ++--
clang/lib/Format/Format.cpp | 38 ++++++++++------------
clang/lib/Format/FormatToken.cpp | 2 +-
clang/lib/Format/TokenAnnotator.cpp | 4 +--
clang/unittests/Format/ConfigParseTest.cpp | 22 ++++++++-----
clang/unittests/Format/FormatTest.cpp | 18 +++++-----
8 files changed, 59 insertions(+), 51 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 7211ed82e553c..c7f3312c67305 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -221,18 +221,22 @@ the configuration (without a prefix: ``Auto``).
someLongFunction(argument1,
argument2);
- * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
+ * ``BAS_ABDeprecated`` (in configuration: ``ABDeprecated``)
+ ``BAS_AlwaysBreak``
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
+ For backward compatibility. Do not use.
- * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
+ * ``BAS_BIDeprecated`` (in configuration: ``BIDeprecated``)
+ ``BAS_BlockIndent``
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
in combination with ``BreakBeforeCloseBracketBracedList``,
``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
+ For backward compatibility. Do not use.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index dd9b4bbf13722..6c361f24da0b4 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,9 +62,7 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;
- /*
/// Different styles for aligning after open brackets.
- /// This is **deprecated**. For backward compatibility only.
enum BracketAlignmentStyle : int8_t {
/// Align parameters on the open bracket, e.g.:
/// \code
@@ -78,20 +76,22 @@ struct FormatStyle {
/// argument2);
/// \endcode
BAS_DontAlign,
+ /// ``BAS_AlwaysBreak``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- BAS_AlwaysBreak,
+ /// For backward compatibility. Do not use.
+ BAS_ABDeprecated,
+ /// ``BAS_BlockIndent``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// in combination with ``BreakBeforeCloseBracketBracedList``,
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- BAS_BlockIndent,
+ /// For backward compatibility. Do not use.
+ BAS_BIDeprecated,
};
- */
- LLVM_YAML_STRONG_TYPEDEF(bool, BracketAlignmentStyle)
/// If ``true``, horizontally aligns arguments after an open bracket.
///
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index edf253e628073..929033dfdec4f 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 != false &&
+ if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
Previous.isNot(TT_TableGenDAGArgOpener) &&
@@ -1862,7 +1862,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(!Style.isJava() && PrecedenceLevel > 0)) &&
- (Style.AlignAfterOpenBracket != false ||
+ (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
(Previous && Previous->isOneOf(TT_TableGenDAGArgListComma,
@@ -1904,7 +1904,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
if (PrecedenceLevel != prec::Conditional &&
Current.isNot(TT_UnaryOperator) &&
- Style.AlignAfterOpenBracket != false) {
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
NewParenState.StartOfFunctionCall = State.Column;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 1f89f32bbbf05..628bae68f988a 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -203,18 +203,16 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
}
};
-// For backward compatibility.
-template <> struct MappingTraits<FormatStyle::BracketAlignmentStyle> {
- static void enumInput(IO &IO, FormatStyle::BracketAlignmentStyle &Value) {
- IO.enumCase(Value, "Align", FormatStyle::BracketAlignmentStyle(true));
- IO.enumCase(Value, "DontAlign", FormatStyle::BracketAlignmentStyle(false));
- IO.enumCase(Value, "AlwaysBreak", FormatStyle::BracketAlignmentStyle(true));
- IO.enumCase(Value, "BlockIndent", FormatStyle::BracketAlignmentStyle(true));
- IO.enumCase(Value, "true", FormatStyle::BracketAlignmentStyle(true));
- IO.enumCase(Value, "false", FormatStyle::BracketAlignmentStyle(true));
- }
- static void mapping(IO &IO, FormatStyle::BracketAlignmentStyle &BAS) {
- IO.mapOptional("AlignAfterOpenBracket", BAS.value);
+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);
+ IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_ABDeprecated);
+ IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BIDeprecated);
}
};
@@ -983,6 +981,7 @@ template <> struct MappingTraits<FormatStyle> {
}
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
+ IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures);
IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
@@ -1259,8 +1258,7 @@ template <> struct MappingTraits<FormatStyle> {
// If AlwaysBreak or BlockIndent were specified but individual
// options for BreakAfterOpenBracket* (CloseAfterOpenBracket*),
// initialize the latter to preserve backwards compatibility.
- bool set = false;
- if (set) {
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_ABDeprecated) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
@@ -1269,9 +1267,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakAfterOpenBracketFunction = true;
Style.BreakAfterOpenBracketIf = true;
}
- set = false;
- }
- if (set) {
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BIDeprecated) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
@@ -1288,6 +1285,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakBeforeCloseBracketFunction = true;
Style.BreakBeforeCloseBracketIf = true;
}
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
}
// If AlwaysBreakAfterDefinitionReturnType was specified but
@@ -1579,7 +1577,7 @@ static void expandPresetsSpacesInParens(FormatStyle &Expanded) {
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.AccessModifierOffset = -2;
- LLVMStyle.AlignAfterOpenBracket = true;
+ LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignConsecutiveAssignments = {};
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
@@ -1900,7 +1898,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
if (Language == FormatStyle::LK_Java) {
- GoogleStyle.AlignAfterOpenBracket = false;
+ GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign;
GoogleStyle.AlignTrailingComments = {};
GoogleStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Never;
@@ -2053,7 +2051,7 @@ FormatStyle getMozillaStyle() {
FormatStyle getWebKitStyle() {
FormatStyle Style = getLLVMStyle();
Style.AccessModifierOffset = -4;
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
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 1dd26532407a7..e84afe3dd9814 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 == false)
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f369597880a3e..d0a81b0854f33 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4409,7 +4409,7 @@ 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 != false) {
+ Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
return 100;
}
if (Left.is(tok::l_paren) && Left.Previous &&
@@ -4427,7 +4427,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 == false &&
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
return 0;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 2736c8d101475..b7fdb5e88ee94 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -543,15 +543,21 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
- Style.AlignAfterOpenBracket = false;
- CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, true);
- CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, false);
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
+ CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
+ FormatStyle::BAS_DontAlign);
// For backward compatibility:
- CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, true);
- CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, false);
- CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, true);
- Style.AlignAfterOpenBracket = false;
- CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, true);
+ CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
+ CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
+ FormatStyle::BAS_DontAlign);
+ CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
+ CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
+ FormatStyle::BAS_Align);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6e4b7798e7b12..7581ad665accb 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 = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
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 = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n"
"\t&& (someOtherLongishConditionPart1\n"
@@ -7556,7 +7556,7 @@ TEST_F(FormatTest, NoOperandAlignment) {
" * cccccccccccccccccccccccccccccccccccc;",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat("return (a > b\n"
" // comment1\n"
" // comment2\n"
@@ -8060,13 +8060,13 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
StringRef Input = "functionCall(paramA, paramB, paramC);\n"
"void functionDecl(int A, int B, int C);";
Style.AllowAllArgumentsOnNextLine = false;
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
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 = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
Style.BreakAfterOpenBracketFunction = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -11237,7 +11237,7 @@ TEST_F(FormatTest, BreakBeforeTemplateCloser) {
TEST_F(FormatTest, WrapsTemplateParameters) {
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
verifyFormat(
"template <typename... a> struct q {};\n"
@@ -11245,7 +11245,7 @@ TEST_F(FormatTest, WrapsTemplateParameters) {
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
" y;",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
verifyFormat(
"template <typename... a> struct r {};\n"
>From c37ffba21eebdee25ebaeeae40dafcb3e5d712e2 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:45:00 -0600
Subject: [PATCH 36/41] Get back to previous state
---
clang/include/clang/Format/Format.h | 8 ++------
clang/lib/Format/Format.cpp | 8 ++++----
2 files changed, 6 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 6c361f24da0b4..c9a5850dbe5a3 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -76,21 +76,17 @@ struct FormatStyle {
/// argument2);
/// \endcode
BAS_DontAlign,
- /// ``BAS_AlwaysBreak``
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- /// For backward compatibility. Do not use.
- BAS_ABDeprecated,
- /// ``BAS_BlockIndent``
+ BAS_AlwaysBreak,
/// This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
/// ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
/// ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
/// in combination with ``BreakBeforeCloseBracketBracedList``,
/// ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
/// ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- /// For backward compatibility. Do not use.
- BAS_BIDeprecated,
+ 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 628bae68f988a..537afa3bf7576 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -211,8 +211,8 @@ 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_ABDeprecated);
- IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BIDeprecated);
+ IO.enumCase(Value, "AlwaysBreak", FormatStyle::BAS_AlwaysBreak);
+ IO.enumCase(Value, "BlockIndent", FormatStyle::BAS_BlockIndent);
}
};
@@ -1258,7 +1258,7 @@ template <> struct MappingTraits<FormatStyle> {
// If AlwaysBreak or BlockIndent were specified but individual
// options for BreakAfterOpenBracket* (CloseAfterOpenBracket*),
// initialize the latter to preserve backwards compatibility.
- if (Style.AlignAfterOpenBracket == FormatStyle::BAS_ABDeprecated) {
+ if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
@@ -1268,7 +1268,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakAfterOpenBracketIf = true;
}
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BIDeprecated) {
+ } else if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
if (!Style.BreakAfterOpenBracketBracedList &&
!Style.BreakAfterOpenBracketFunction &&
!Style.BreakAfterOpenBracketIf && !Style.BreakAfterOpenBracketLoop &&
>From a39545c1bd92fe80f9b1e56096a9ae5294b327ef Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 10:45:30 -0600
Subject: [PATCH 37/41] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index c7f3312c67305..7211ed82e553c 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -221,22 +221,18 @@ the configuration (without a prefix: ``Auto``).
someLongFunction(argument1,
argument2);
- * ``BAS_ABDeprecated`` (in configuration: ``ABDeprecated``)
- ``BAS_AlwaysBreak``
+ * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- For backward compatibility. Do not use.
- * ``BAS_BIDeprecated`` (in configuration: ``BIDeprecated``)
- ``BAS_BlockIndent``
+ * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
in combination with ``BreakBeforeCloseBracketBracedList``,
``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
- For backward compatibility. Do not use.
>From 607db3eebc9a18d3719f29368d267773253773c9 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 26 Aug 2025 11:06:33 -0600
Subject: [PATCH 38/41] clean up test
---
clang/unittests/Format/AlignBracketsTest.cpp | 20 +++---
clang/unittests/Format/FormatTest.cpp | 65 --------------------
2 files changed, 11 insertions(+), 74 deletions(-)
diff --git a/clang/unittests/Format/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp
index 359bb354eaf32..1d9200a77441a 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -28,7 +28,7 @@ TEST_F(AlignBracketsTest, AlignsAfterOpenBracket) {
"SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaaaaa));");
FormatStyle Style = getLLVMStyle();
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
Style);
@@ -617,13 +617,13 @@ TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
StringRef Input = "functionCall(paramA, paramB, paramC);\n"
"void functionDecl(int A, int B, int C);";
Style.AllowAllArgumentsOnNextLine = false;
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
@@ -646,11 +646,12 @@ TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
" int A, int B, int C\n"
");",
Input, Style);
+ Style.BreakBeforeCloseBracketFunction = false;
// When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
// first argument.
Style.AllowAllArgumentsOnNextLine = true;
- Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
+ Style.BreakAfterOpenBracketFunction = true;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -658,13 +659,14 @@ TEST_F(AlignBracketsTest, 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 = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
+ Style.BreakAfterOpenBracketFunction = false;
verifyFormat(StringRef("functionCall(paramA, paramB,\n"
" paramC);\n"
"void functionDecl(int A, int B,\n"
" int C);"),
Input, Style);
- Style.AlignAfterOpenBracket = false;
+ Style.BreakAfterOpenBracketFunction = true;
verifyFormat(StringRef("functionCall(\n"
" paramA, paramB, paramC);\n"
"void functionDecl(\n"
@@ -770,17 +772,17 @@ TEST_F(AlignBracketsTest, ParenthesesAndOperandAlignment) {
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = true;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = FormatStyle::OAS_Align;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
Style);
- Style.AlignAfterOpenBracket = false;
+ Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
" bbbbbbbbbbbbbbbbbbbbbb);",
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7581ad665accb..767a2bb326e35 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8053,71 +8053,6 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
Style);
}
-TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) {
- // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign
- // and BAS_Align.
- 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;
- verifyFormat(StringRef("functionCall(paramA, paramB,\n"
- " paramC);\n"
- "void functionDecl(int A, int B,\n"
- " int C);"),
- Input, Style);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- verifyFormat(StringRef("functionCall(paramA, paramB,\n"
- " paramC);\n"
- "void functionDecl(int A, int B,\n"
- " int C);"),
- Input, Style);
- // However, BreakAfterOpenBracketFunction should take precedence over
- // AllowAllArgumentsOnNextLine.
- Style.BreakAfterOpenBracketFunction = true;
- verifyFormat(StringRef("functionCall(\n"
- " paramA, paramB, paramC);\n"
- "void functionDecl(\n"
- " int A, int B, int C);"),
- Input, Style);
- Style.BreakAfterOpenBracketFunction = true;
- Style.BreakBeforeCloseBracketFunction = true;
- verifyFormat("functionCall(\n"
- " paramA, paramB, paramC\n"
- ");\n"
- "void functionDecl(\n"
- " int A, int B, int C\n"
- ");",
- Input, Style);
-
- // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the
- // first argument.
- Style.AllowAllArgumentsOnNextLine = true;
- Style.BreakAfterOpenBracketFunction = true;
- Style.BreakBeforeCloseBracketFunction = false;
- verifyFormat(StringRef("functionCall(\n"
- " paramA, paramB, paramC);\n"
- "void functionDecl(\n"
- " int A, int B, int C);"),
- 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.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;
- verifyFormat(StringRef("functionCall(\n"
- " paramA, paramB, paramC);\n"
- "void functionDecl(\n"
- " int A, int B, int C);"),
- Input, Style);
-}
-
->>>>>>> d1e73bcfe430 (Add support for functions and replace AlwaysBreak, BlockIndent)
TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
"void emptyFunctionDefinition() {}\n"
>From c3baf987180323118880de1691f5ef9cadb48549 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 29 Aug 2025 19:48:12 -0600
Subject: [PATCH 39/41] Trying Hazardy's suggestions
---
clang/include/clang/Format/Format.h | 34 ++-----
clang/lib/Format/ContinuationIndenter.cpp | 6 +-
clang/lib/Format/Format.cpp | 99 +++++++++++---------
clang/lib/Format/FormatToken.cpp | 2 +-
clang/lib/Format/TokenAnnotator.cpp | 4 +-
clang/unittests/Format/AlignBracketsTest.cpp | 14 +--
clang/unittests/Format/ConfigParseTest.cpp | 19 ++--
clang/unittests/Format/FormatTest.cpp | 10 +-
8 files changed, 86 insertions(+), 102 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index c9a5850dbe5a3..94917a3cda85c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -62,39 +62,17 @@ 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: vs. false
+ /// someLongFunction(argument1, someLongFunction(argument1,
+ /// argument2); argument2);
+ /// \endcode
/// 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..edf253e628073 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 != false &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
Previous.isNot(TT_TableGenDAGArgOpener) &&
@@ -1862,7 +1862,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
PrecedenceLevel < prec::Assignment) &&
(!Previous || Previous->isNot(tok::kw_return) ||
(!Style.isJava() && PrecedenceLevel > 0)) &&
- (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
+ (Style.AlignAfterOpenBracket != false ||
PrecedenceLevel > prec::Comma || Current.NestingLevel == 0) &&
(!Style.isTableGen() ||
(Previous && Previous->isOneOf(TT_TableGenDAGArgListComma,
@@ -1904,7 +1904,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
if (PrecedenceLevel != prec::Conditional &&
Current.isNot(TT_UnaryOperator) &&
- Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) {
+ Style.AlignAfterOpenBracket != false) {
NewParenState.StartOfFunctionCall = State.Column;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 537afa3bf7576..602e0d1a6bcfb 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -30,6 +30,12 @@
using clang::format::FormatStyle;
LLVM_YAML_IS_SEQUENCE_VECTOR(FormatStyle::RawStringFormat)
+enum BracketAlignmentStyle : int8_t {
+ BAS_Align,
+ BAS_DontAlign,
+ BAS_AlwaysBreak,
+ BAS_BlockIndent
+};
namespace llvm {
namespace yaml {
@@ -203,16 +209,16 @@ 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);
+template <> struct ScalarEnumerationTraits<BracketAlignmentStyle> {
+ static void enumeration(IO &IO, BracketAlignmentStyle &Value) {
+ IO.enumCase(Value, "Align", BAS_Align);
+ IO.enumCase(Value, "DontAlign", BAS_DontAlign);
// 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);
+ IO.enumCase(Value, "true", BAS_Align);
+ IO.enumCase(Value, "false", BAS_DontAlign);
+ IO.enumCase(Value, "AlwaysBreak", BAS_AlwaysBreak);
+ IO.enumCase(Value, "BlockIndent", BAS_BlockIndent);
}
};
@@ -946,6 +952,43 @@ template <> struct MappingTraits<FormatStyle> {
bool SpacesInCStyleCastParentheses = false;
bool SpacesInParentheses = false;
+ if (IO.outputting()) {
+ IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
+ } else {
+ // For backward compatibility.
+ BracketAlignmentStyle local;
+ IO.mapOptional("AlignAfterOpenBracket", local);
+ Style.BreakAfterOpenBracketBracedList = false;
+ Style.BreakAfterOpenBracketFunction = false;
+ Style.BreakAfterOpenBracketIf = false;
+ Style.BreakAfterOpenBracketLoop = false;
+ Style.BreakAfterOpenBracketSwitch = false;
+ Style.BreakBeforeCloseBracketBracedList = false;
+ Style.BreakBeforeCloseBracketFunction = false;
+ Style.BreakBeforeCloseBracketIf = false;
+ Style.BreakBeforeCloseBracketLoop = false;
+ Style.BreakBeforeCloseBracketSwitch = false;
+
+ if (local == BAS_Align)
+ Style.AlignAfterOpenBracket = true;
+ else if (local == BAS_DontAlign)
+ Style.AlignAfterOpenBracket = false;
+ else if (local == BAS_AlwaysBreak) {
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.AlignAfterOpenBracket = true;
+ } else if (local == BAS_BlockIndent) {
+ Style.BreakAfterOpenBracketBracedList = true;
+ Style.BreakAfterOpenBracketFunction = true;
+ Style.BreakAfterOpenBracketIf = true;
+ Style.BreakBeforeCloseBracketBracedList = true;
+ Style.BreakBeforeCloseBracketFunction = true;
+ Style.BreakBeforeCloseBracketIf = true;
+ Style.AlignAfterOpenBracket = true;
+ }
+ }
+
// For backward compatibility.
if (!IO.outputting()) {
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
@@ -981,7 +1024,6 @@ template <> struct MappingTraits<FormatStyle> {
}
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
- IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
IO.mapOptional("AlignArrayOfStructures", Style.AlignArrayOfStructures);
IO.mapOptional("AlignConsecutiveAssignments",
Style.AlignConsecutiveAssignments);
@@ -1255,39 +1297,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;
- }
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- } 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;
- }
- Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
- }
-
// If AlwaysBreakAfterDefinitionReturnType was specified but
// BreakAfterReturnType was not, initialize the latter from the former for
// backwards compatibility.
@@ -1577,7 +1586,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;
@@ -1898,7 +1907,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;
@@ -2051,7 +2060,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..1dd26532407a7 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 == false)
return;
FormatToken *ItemBegin = Token->Next;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d0a81b0854f33..f369597880a3e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4409,7 +4409,7 @@ 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) {
+ Style.AlignAfterOpenBracket != false) {
return 100;
}
if (Left.is(tok::l_paren) && Left.Previous &&
@@ -4427,7 +4427,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 == false &&
(Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine)) {
return 0;
}
diff --git a/clang/unittests/Format/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp
index 1d9200a77441a..514a26607d7fa 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -28,7 +28,7 @@ TEST_F(AlignBracketsTest, 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);
@@ -617,13 +617,13 @@ TEST_F(AlignBracketsTest, AllowAllArgumentsOnNextLineDontAlign) {
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"
@@ -659,7 +659,7 @@ TEST_F(AlignBracketsTest, 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"
@@ -772,17 +772,17 @@ TEST_F(AlignBracketsTest, 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);",
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index b7fdb5e88ee94..0e1c15e2904f9 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -543,21 +543,18 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("EnumTrailingComma: Remove", EnumTrailingComma,
FormatStyle::ETC_Remove);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
+ Style.AlignAfterOpenBracket = false;
+ CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, true);
CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
- FormatStyle::BAS_DontAlign);
+ false);
// For backward compatibility:
CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
- CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
- FormatStyle::BAS_DontAlign);
+ true);
+ CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, false);
CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
- Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
- CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
- FormatStyle::BAS_Align);
+ true);
+ Style.AlignAfterOpenBracket = false;
+ CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, true);
Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 767a2bb326e35..059c5a3a79a78 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"
@@ -11172,7 +11172,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"
@@ -11180,7 +11180,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 10e167d6cd5c3d1f84bf0b593d9712cc0c11e572 Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Fri, 29 Aug 2025 19:48:33 -0600
Subject: [PATCH 40/41] dump style
---
clang/docs/ClangFormatStyleOptions.rst | 41 +++++---------------------
1 file changed, 7 insertions(+), 34 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 7211ed82e553c..94691e1d27774 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -197,44 +197,17 @@ 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``.
-
- * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
- This is **deprecated**. See ``BreakAfterOpenBracketBracedList``,
- ``BreakAfterOpenBracketFunction``, ``BreakAfterOpenBracketIf``,
- ``BreakAfterOpenBracketLoop``, ``BreakAfterOpenBracketSwitch``.
- in combination with ``BreakBeforeCloseBracketBracedList``,
- ``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
- ``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch``.
+ .. code-block:: c++
+ true: vs. false
+ someLongFunction(argument1, someLongFunction(argument1,
+ argument2); argument2);
+ This applies to round brackets (parentheses), angle brackets and square
+ brackets.
.. _AlignArrayOfStructures:
>From 507a2e6ea3ada954c180afb7481bca52d422a92a Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Tue, 2 Sep 2025 14:11:43 -0600
Subject: [PATCH 41/41] Fix defaults for BasedOnStyle
---
clang/lib/Format/Format.cpp | 18 ++++++++++++------
clang/unittests/Format/ConfigParseTest.cpp | 3 +--
2 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 602e0d1a6bcfb..f929106b6af09 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -956,7 +956,15 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignAfterOpenBracket", Style.AlignAfterOpenBracket);
} else {
// For backward compatibility.
- BracketAlignmentStyle local;
+ BracketAlignmentStyle local = BAS_Align;
+ if (IsGoogleOrChromium) {
+ if (Style.Language == FormatStyle::LK_JavaScript)
+ local = BAS_AlwaysBreak;
+ else if (Style.Language == FormatStyle::LK_Java)
+ local = BAS_DontAlign;
+ } else if (BasedOnStyle.equals_insensitive("webkit")) {
+ local = BAS_DontAlign;
+ }
IO.mapOptional("AlignAfterOpenBracket", local);
Style.BreakAfterOpenBracketBracedList = false;
Style.BreakAfterOpenBracketFunction = false;
@@ -969,11 +977,11 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakBeforeCloseBracketLoop = false;
Style.BreakBeforeCloseBracketSwitch = false;
- if (local == BAS_Align)
+ if (local == BAS_Align) {
Style.AlignAfterOpenBracket = true;
- else if (local == BAS_DontAlign)
+ } else if (local == BAS_DontAlign) {
Style.AlignAfterOpenBracket = false;
- else if (local == BAS_AlwaysBreak) {
+ } else if (local == BAS_AlwaysBreak) {
Style.BreakAfterOpenBracketBracedList = true;
Style.BreakAfterOpenBracketFunction = true;
Style.BreakAfterOpenBracketIf = true;
@@ -1922,8 +1930,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
GoogleStyle.BreakAfterOpenBracketBracedList = true;
GoogleStyle.BreakAfterOpenBracketFunction = true;
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/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 0e1c15e2904f9..7ba2bb650aa14 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -545,8 +545,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
Style.AlignAfterOpenBracket = false;
CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, true);
- CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
- false);
+ CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, false);
// For backward compatibility:
CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
true);
More information about the cfe-commits
mailing list