Skip to content

Commit 836919b

Browse files
authored
[clang-format] Fix a crash in AlignArrayOfStructures (llvm#167099)
Fixes llvm#157405
1 parent d406c15 commit 836919b

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
@@ -1232,7 +1232,10 @@ void WhitespaceManager::alignArrayInitializers() {
12321232
bool FoundComplete = false;
12331233
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
12341234
++InsideIndex) {
1235-
if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
1235+
const auto *Tok = Changes[InsideIndex].Tok;
1236+
if (Tok->is(tok::pp_define))
1237+
break;
1238+
if (Tok == C.Tok->MatchingParen) {
12361239
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
12371240
ChangeIndex = InsideIndex + 1;
12381241
FoundComplete = true;

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22272,6 +22272,19 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2227222272
"});",
2227322273
Style);
2227422274

22275+
verifyNoCrash(
22276+
"PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
22277+
" {\n"
22278+
" {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
22279+
" {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
22280+
"\n"
22281+
"#define PICALL1(a, b, c, d) \\\n"
22282+
" { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
22283+
"\n"
22284+
" PICALL1(1, 1, 75, 50),\n"
22285+
"};",
22286+
Style);
22287+
2227522288
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2227622289
verifyFormat("#define FOO \\\n"
2227722290
" int foo[][2] = { \\\n"

0 commit comments

Comments
 (0)