Skip to content

Commit fa6025e

Browse files
authored
[clang-format] Don't confuse initializer equal signs in for loops (#77712)
clang-format has logic to align declarations of multiple variables of the same type, aligning them at the equals sign. This logic is applied in for loops as well. However, this alignment logic also erroneously affected the equals signs of designated initializers. This patch forbids alignment if the token 2 tokens back from the equals sign is a designated initializer period. Fixes #73902
1 parent 4821c90 commit fa6025e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
703703

704704
if (Current.is(tok::equal) &&
705705
(State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
706-
CurrentState.VariablePos == 0) {
706+
CurrentState.VariablePos == 0 &&
707+
(!Previous.Previous ||
708+
Previous.Previous->isNot(TT_DesignatedInitializerPeriod))) {
707709
CurrentState.VariablePos = State.Column;
708710
// Move over * and & if they are bound to the variable name.
709711
const FormatToken *Tok = &Previous;

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5008,6 +5008,18 @@ TEST_F(FormatTest, DesignatedInitializers) {
50085008
" [3] = cccccccccccccccccccccccccccccccccccccc,\n"
50095009
" [4] = dddddddddddddddddddddddddddddddddddddd,\n"
50105010
" [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
5011+
5012+
verifyFormat("for (const TestCase &test_case : {\n"
5013+
" TestCase{\n"
5014+
" .a = 1,\n"
5015+
" .b = 1,\n"
5016+
" },\n"
5017+
" TestCase{\n"
5018+
" .a = 2,\n"
5019+
" .b = 2,\n"
5020+
" },\n"
5021+
" }) {\n"
5022+
"}\n");
50115023
}
50125024

50135025
TEST_F(FormatTest, BracedInitializerIndentWidth) {

0 commit comments

Comments
 (0)