Skip to content

Commit 682323a

Browse files
committed
[clang-format] Fix a crash with AlignArrayOfStructures option
Fixes #86109.
1 parent 47423e9 commit 682323a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
14911491
: Cell);
14921492
// Go to the next non-comment and ensure there is a break in front
14931493
const auto *NextNonComment = C.Tok->getNextNonComment();
1494-
while (NextNonComment->is(tok::comma))
1494+
while (NextNonComment && NextNonComment->is(tok::comma))
14951495
NextNonComment = NextNonComment->getNextNonComment();
14961496
auto j = i;
14971497
while (j < End && Changes[j].Tok != NextNonComment)

clang/unittests/Format/FormatTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21101,6 +21101,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2110121101
" [1] { 1, 1, },\n"
2110221102
" [2] { 1, 1, },\n"
2110321103
"};");
21104+
verifyNoCrash("test arr[] = {\n"
21105+
"#define FOO(i) {i, i},\n"
21106+
"SOME_GENERATOR(FOO)\n"
21107+
"{2, 2}\n"
21108+
"};");
2110421109

2110521110
verifyFormat("return GradForUnaryCwise(g, {\n"
2110621111
" {{\"sign\"}, \"Sign\", "
@@ -21354,6 +21359,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2135421359
" [1] { 1, 1, },\n"
2135521360
" [2] { 1, 1, },\n"
2135621361
"};");
21362+
verifyNoCrash("test arr[] = {\n"
21363+
"#define FOO(i) {i, i},\n"
21364+
"SOME_GENERATOR(FOO)\n"
21365+
"{2, 2}\n"
21366+
"};");
2135721367

2135821368
verifyFormat("return GradForUnaryCwise(g, {\n"
2135921369
" {{\"sign\"}, \"Sign\", {\"x\", "

0 commit comments

Comments
 (0)