Skip to content

Commit f1491c7

Browse files
owencatstellar
authored andcommitted
[clang-format] Fix a crash with AlignArrayOfStructures option (llvm#86420)
Fixes llvm#86109. (cherry picked from commit cceedc9)
1 parent 8d65f14 commit f1491c7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

clang/lib/Format/WhitespaceManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
14661466
: Cell);
14671467
// Go to the next non-comment and ensure there is a break in front
14681468
const auto *NextNonComment = C.Tok->getNextNonComment();
1469-
while (NextNonComment->is(tok::comma))
1469+
while (NextNonComment && NextNonComment->is(tok::comma))
14701470
NextNonComment = NextNonComment->getNextNonComment();
14711471
auto j = i;
14721472
while (Changes[j].Tok != NextNonComment && j < End)

clang/unittests/Format/FormatTest.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -20955,7 +20955,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2095520955
" [0] = {1, 1},\n"
2095620956
" [1] { 1, 1, },\n"
2095720957
" [2] { 1, 1, },\n"
20958-
"};");
20958+
"};",
20959+
Style);
20960+
verifyNoCrash("test arr[] = {\n"
20961+
"#define FOO(i) {i, i},\n"
20962+
"SOME_GENERATOR(FOO)\n"
20963+
"{2, 2}\n"
20964+
"};",
20965+
Style);
2095920966

2096020967
verifyFormat("return GradForUnaryCwise(g, {\n"
2096120968
" {{\"sign\"}, \"Sign\", "
@@ -21208,7 +21215,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2120821215
" [0] = {1, 1},\n"
2120921216
" [1] { 1, 1, },\n"
2121021217
" [2] { 1, 1, },\n"
21211-
"};");
21218+
"};",
21219+
Style);
21220+
verifyNoCrash("test arr[] = {\n"
21221+
"#define FOO(i) {i, i},\n"
21222+
"SOME_GENERATOR(FOO)\n"
21223+
"{2, 2}\n"
21224+
"};",
21225+
Style);
2121221226

2121321227
verifyFormat("return GradForUnaryCwise(g, {\n"
2121421228
" {{\"sign\"}, \"Sign\", {\"x\", "

0 commit comments

Comments
 (0)