Skip to content

Commit a565880

Browse files
owencadyung
authored andcommitted
[clang-format] Fix a crash in AlignArrayOfStructures (llvm#167099)
Fixes llvm#157405 (cherry picked from commit 836919b)
1 parent 94c40c6 commit a565880

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
@@ -1295,7 +1295,10 @@ void WhitespaceManager::alignArrayInitializers() {
12951295
bool FoundComplete = false;
12961296
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
12971297
++InsideIndex) {
1298-
if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
1298+
const auto *Tok = Changes[InsideIndex].Tok;
1299+
if (Tok->is(tok::pp_define))
1300+
break;
1301+
if (Tok == C.Tok->MatchingParen) {
12991302
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
13001303
ChangeIndex = InsideIndex + 1;
13011304
FoundComplete = true;

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22429,6 +22429,19 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2242922429
"});",
2243022430
Style);
2243122431

22432+
verifyNoCrash(
22433+
"PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
22434+
" {\n"
22435+
" {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
22436+
" {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
22437+
"\n"
22438+
"#define PICALL1(a, b, c, d) \\\n"
22439+
" { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
22440+
"\n"
22441+
" PICALL1(1, 1, 75, 50),\n"
22442+
"};",
22443+
Style);
22444+
2243222445
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2243322446
verifyFormat("#define FOO \\\n"
2243422447
" int foo[][2] = { \\\n"

0 commit comments

Comments
 (0)