Skip to content

Commit 80f34e2

Browse files
authored
[clang-format] Change BracedInitializerIndentWidth to int (#128988)
Fixes #108526
1 parent bafd44b commit 80f34e2

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,9 +2582,9 @@ the configuration (without a prefix: ``Auto``).
25822582

25832583
.. _BracedInitializerIndentWidth:
25842584

2585-
**BracedInitializerIndentWidth** (``Unsigned``) :versionbadge:`clang-format 17` :ref:`<BracedInitializerIndentWidth>`
2585+
**BracedInitializerIndentWidth** (``Integer``) :versionbadge:`clang-format 17` :ref:`<BracedInitializerIndentWidth>`
25862586
The number of columns to use to indent the contents of braced init lists.
2587-
If unset, ``ContinuationIndentWidth`` is used.
2587+
If unset or negative, ``ContinuationIndentWidth`` is used.
25882588

25892589
.. code-block:: c++
25902590

clang/include/clang/Format/Format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ struct FormatStyle {
12891289
BitFieldColonSpacingStyle BitFieldColonSpacing;
12901290

12911291
/// The number of columns to use to indent the contents of braced init lists.
1292-
/// If unset, ``ContinuationIndentWidth`` is used.
1292+
/// If unset or negative, ``ContinuationIndentWidth`` is used.
12931293
/// \code
12941294
/// AlignAfterOpenBracket: AlwaysBreak
12951295
/// BracedInitializerIndentWidth: 2
@@ -1319,7 +1319,7 @@ struct FormatStyle {
13191319
/// }
13201320
/// \endcode
13211321
/// \version 17
1322-
std::optional<unsigned> BracedInitializerIndentWidth;
1322+
int BracedInitializerIndentWidth;
13231323

13241324
/// Different ways to wrap braces after control statements.
13251325
enum BraceWrappingAfterControlStatementStyle : int8_t {

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,9 +1921,9 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
19211921
NewIndent = Style.IndentWidth +
19221922
std::min(State.Column, CurrentState.NestedBlockIndent);
19231923
} else if (Current.is(tok::l_brace)) {
1924-
NewIndent =
1925-
CurrentState.LastSpace + Style.BracedInitializerIndentWidth.value_or(
1926-
Style.ContinuationIndentWidth);
1924+
const auto Width = Style.BracedInitializerIndentWidth;
1925+
NewIndent = CurrentState.LastSpace +
1926+
(Width < 0 ? Style.ContinuationIndentWidth : Width);
19271927
} else {
19281928
NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth;
19291929
}

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15121512
LLVMStyle.BinPackLongBracedList = true;
15131513
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
15141514
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
1515-
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
1515+
LLVMStyle.BracedInitializerIndentWidth = -1;
15161516
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
15171517
/*AfterClass=*/false,
15181518
/*AfterControlStatement=*/FormatStyle::BWACS_Never,

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ TEST(ConfigParseTest, ParsesConfigurationIntegers) {
265265
Style.Language = FormatStyle::LK_Cpp;
266266

267267
CHECK_PARSE_INT(AccessModifierOffset);
268+
CHECK_PARSE_INT(BracedInitializerIndentWidth);
268269
CHECK_PARSE_INT(PPIndentWidth);
269270

270-
CHECK_PARSE_UNSIGNED(BracedInitializerIndentWidth);
271271
CHECK_PARSE_UNSIGNED(ColumnLimit);
272272
CHECK_PARSE_UNSIGNED(ConstructorInitializerIndentWidth);
273273
CHECK_PARSE_UNSIGNED(ContinuationIndentWidth);
@@ -1441,8 +1441,10 @@ TEST(ConfigParseTest, GetStyleOfFile) {
14411441
ASSERT_EQ(*Style9, SubSubStyle);
14421442

14431443
// Test 9.8: use inheritance from a file without BasedOnStyle
1444-
ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
1445-
llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
1444+
ASSERT_TRUE(FS.addFile(
1445+
"/e/withoutbase/.clang-format", 0,
1446+
llvm::MemoryBuffer::getMemBuffer("BracedInitializerIndentWidth: 2\n"
1447+
"ColumnLimit: 123")));
14461448
ASSERT_TRUE(
14471449
FS.addFile("/e/withoutbase/sub/.clang-format", 0,
14481450
llvm::MemoryBuffer::getMemBuffer(
@@ -1452,6 +1454,7 @@ TEST(ConfigParseTest, GetStyleOfFile) {
14521454
ASSERT_TRUE(static_cast<bool>(Style9));
14531455
ASSERT_EQ(*Style9, [] {
14541456
auto Style = getLLVMStyle();
1457+
Style.BracedInitializerIndentWidth = 2;
14551458
Style.ColumnLimit = 123;
14561459
return Style;
14571460
}());
@@ -1460,6 +1463,7 @@ TEST(ConfigParseTest, GetStyleOfFile) {
14601463
ASSERT_TRUE(static_cast<bool>(Style9));
14611464
ASSERT_EQ(*Style9, [] {
14621465
auto Style = getLLVMStyle();
1466+
Style.BracedInitializerIndentWidth = 2;
14631467
Style.ColumnLimit = 123;
14641468
Style.IndentWidth = 7;
14651469
return Style;

0 commit comments

Comments
 (0)