Skip to content

Commit 981d3ac

Browse files
committed
[clang-format] Fix a crash in AlignArrayOfStructures
Fixes #157405
1 parent 3c0d4aa commit 981d3ac

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,10 @@ void WhitespaceManager::alignArrayInitializers() {
12151215
bool FoundComplete = false;
12161216
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
12171217
++InsideIndex) {
1218-
if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
1218+
const auto *Tok = Changes[InsideIndex].Tok;
1219+
if (Tok->is(tok::pp_define))
1220+
break;
1221+
if (Tok == C.Tok->MatchingParen) {
12191222
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
12201223
ChangeIndex = InsideIndex + 1;
12211224
FoundComplete = true;

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22265,6 +22265,19 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2226522265
"});",
2226622266
Style);
2226722267

22268+
verifyNoCrash(
22269+
"PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
22270+
" {\n"
22271+
" {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
22272+
" {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
22273+
"\n"
22274+
"#define PICALL1(a, b, c, d) \\\n"
22275+
" { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
22276+
"\n"
22277+
" PICALL1(1, 1, 75, 50),\n"
22278+
"};",
22279+
Style);
22280+
2226822281
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2226922282
verifyFormat("#define FOO \\\n"
2227022283
" int foo[][2] = { \\\n"

0 commit comments

Comments
 (0)