-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] Change BracedInitializerIndentWidth to int #128988
Merged
Merged
+15
−11
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format @llvm/pr-subscribers-clang Author: Owen Pan (owenca) ChangesFixes #108526 Full diff: https://github.com/llvm/llvm-project/pull/128988.diff 5 Files Affected:
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index d157c07c9cef8..9ecac68ae72bf 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2582,9 +2582,9 @@ the configuration (without a prefix: ``Auto``).
.. _BracedInitializerIndentWidth:
-**BracedInitializerIndentWidth** (``Unsigned``) :versionbadge:`clang-format 17` :ref:`¶ <BracedInitializerIndentWidth>`
+**BracedInitializerIndentWidth** (``Integer``) :versionbadge:`clang-format 17` :ref:`¶ <BracedInitializerIndentWidth>`
The number of columns to use to indent the contents of braced init lists.
- If unset, ``ContinuationIndentWidth`` is used.
+ If unset or negative, ``ContinuationIndentWidth`` is used.
.. code-block:: c++
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 46fb1d52701b3..fec47a248abb4 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1289,7 +1289,7 @@ struct FormatStyle {
BitFieldColonSpacingStyle BitFieldColonSpacing;
/// The number of columns to use to indent the contents of braced init lists.
- /// If unset, ``ContinuationIndentWidth`` is used.
+ /// If unset or negative, ``ContinuationIndentWidth`` is used.
/// \code
/// AlignAfterOpenBracket: AlwaysBreak
/// BracedInitializerIndentWidth: 2
@@ -1319,7 +1319,7 @@ struct FormatStyle {
/// }
/// \endcode
/// \version 17
- std::optional<unsigned> BracedInitializerIndentWidth;
+ int BracedInitializerIndentWidth;
/// Different ways to wrap braces after control statements.
enum BraceWrappingAfterControlStatementStyle : int8_t {
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index d49128c2b40f8..972dceb697a8b 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -1921,9 +1921,9 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
NewIndent = Style.IndentWidth +
std::min(State.Column, CurrentState.NestedBlockIndent);
} else if (Current.is(tok::l_brace)) {
- NewIndent =
- CurrentState.LastSpace + Style.BracedInitializerIndentWidth.value_or(
- Style.ContinuationIndentWidth);
+ const auto Width = Style.BracedInitializerIndentWidth;
+ NewIndent = CurrentState.LastSpace +
+ (Width < 0 ? Style.ContinuationIndentWidth : Width);
} else {
NewIndent = CurrentState.LastSpace + Style.ContinuationIndentWidth;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 68ef119fb4d65..92678a031178a 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1512,7 +1512,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BinPackLongBracedList = true;
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
- LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
+ LLVMStyle.BracedInitializerIndentWidth = -1;
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
/*AfterClass=*/false,
/*AfterControlStatement=*/FormatStyle::BWACS_Never,
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 9cd262960b724..cda60440874be 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -265,9 +265,9 @@ TEST(ConfigParseTest, ParsesConfigurationIntegers) {
Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE_INT(AccessModifierOffset);
+ CHECK_PARSE_INT(BracedInitializerIndentWidth);
CHECK_PARSE_INT(PPIndentWidth);
- CHECK_PARSE_UNSIGNED(BracedInitializerIndentWidth);
CHECK_PARSE_UNSIGNED(ColumnLimit);
CHECK_PARSE_UNSIGNED(ConstructorInitializerIndentWidth);
CHECK_PARSE_UNSIGNED(ContinuationIndentWidth);
@@ -1441,8 +1441,9 @@ TEST(ConfigParseTest, GetStyleOfFile) {
ASSERT_EQ(*Style9, SubSubStyle);
// Test 9.8: use inheritance from a file without BasedOnStyle
- ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0,
- llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123")));
+ ASSERT_TRUE(FS.addFile(
+ "/e/withoutbase/.clang-format", 0,
+ llvm::MemoryBuffer::getMemBuffer("BracedInitializerIndentWidth: 2")));
ASSERT_TRUE(
FS.addFile("/e/withoutbase/sub/.clang-format", 0,
llvm::MemoryBuffer::getMemBuffer(
@@ -1452,7 +1453,7 @@ TEST(ConfigParseTest, GetStyleOfFile) {
ASSERT_TRUE(static_cast<bool>(Style9));
ASSERT_EQ(*Style9, [] {
auto Style = getLLVMStyle();
- Style.ColumnLimit = 123;
+ Style.BracedInitializerIndentWidth = 2;
return Style;
}());
@@ -1460,7 +1461,7 @@ TEST(ConfigParseTest, GetStyleOfFile) {
ASSERT_TRUE(static_cast<bool>(Style9));
ASSERT_EQ(*Style9, [] {
auto Style = getLLVMStyle();
- Style.ColumnLimit = 123;
+ Style.BracedInitializerIndentWidth = 2;
Style.IndentWidth = 7;
return Style;
}());
|
HazardyKnusperkeks
approved these changes
Feb 27, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #108526