Skip to content

Commit

Permalink
[clang-format] Fix a crash with AlignArrayOfStructures option (#86420)
Browse files Browse the repository at this point in the history
Fixes #86109.
  • Loading branch information
owenca committed Mar 24, 2024
1 parent 67f2267 commit cceedc9
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 @@ -1491,7 +1491,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 (j < End && Changes[j].Tok != NextNonComment)
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 @@ -21100,7 +21100,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 @@ -21353,7 +21360,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 cceedc9

Please sign in to comment.