Skip to content

Commit

Permalink
[clang-format] Fix a crash with AlignArrayOfStructures option (llvm#8…
Browse files Browse the repository at this point in the history
…6420)

Fixes llvm#86109.

(cherry picked from commit cceedc9)
  • Loading branch information
owenca authored and tstellar committed May 14, 2024
1 parent 8d65f14 commit f1491c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
: Cell);
// Go to the next non-comment and ensure there is a break in front
const auto *NextNonComment = C.Tok->getNextNonComment();
while (NextNonComment->is(tok::comma))
while (NextNonComment && NextNonComment->is(tok::comma))
NextNonComment = NextNonComment->getNextNonComment();
auto j = i;
while (Changes[j].Tok != NextNonComment && j < End)
Expand Down
18 changes: 16 additions & 2 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20955,7 +20955,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
" [2] { 1, 1, },\n"
"};");
"};",
Style);
verifyNoCrash("test arr[] = {\n"
"#define FOO(i) {i, i},\n"
"SOME_GENERATOR(FOO)\n"
"{2, 2}\n"
"};",
Style);

verifyFormat("return GradForUnaryCwise(g, {\n"
" {{\"sign\"}, \"Sign\", "
Expand Down Expand Up @@ -21208,7 +21215,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
" [2] { 1, 1, },\n"
"};");
"};",
Style);
verifyNoCrash("test arr[] = {\n"
"#define FOO(i) {i, i},\n"
"SOME_GENERATOR(FOO)\n"
"{2, 2}\n"
"};",
Style);

verifyFormat("return GradForUnaryCwise(g, {\n"
" {{\"sign\"}, \"Sign\", {\"x\", "
Expand Down

0 comments on commit f1491c7

Please sign in to comment.