[clang-format] Fix a crash with AlignArrayOfStructures option (#86420)
authorOwen Pan <owenpiano@gmail.com>
Sun, 24 Mar 2024 22:22:40 +0000 (15:22 -0700)
committerTom Stellard <tstellar@redhat.com>
Tue, 14 May 2024 00:55:32 +0000 (17:55 -0700)
Fixes #86109.

(cherry picked from commit cceedc939a43c7c732a5888364251775bffc2dba)

clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp

index df84f97a8e8ac139db14625ce2ded35dcaecad29..7525e6ee650b6237f6ca4721d2436abcd118a4eb 100644 (file)
@@ -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)
index 923128672c31662940efd71d9ff3668ccf03d979..0161a1685eb121d604c37ebb9513e29f5495eaaa 100644 (file)
@@ -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\",  "
@@ -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\", "