llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] Fix a crash with AlignArrayOfStructures option

Open owenca opened this issue 1 year ago • 3 comments

Fixes #86109.

owenca avatar Mar 24 '24 05:03 owenca

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixes #86109.


Full diff: https://github.com/llvm/llvm-project/pull/86420.diff

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+1-1)
  • (modified) clang/unittests/Format/FormatTest.cpp (+10)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index fef85abf79a38c..710bf8d8a8ec70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -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)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index cf8d6ab691d9a0..a33520d81b8421 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21101,6 +21101,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
                 "    [1] { 1, 1, },\n"
                 "    [2] { 1, 1, },\n"
                 "};");
+  verifyNoCrash("test arr[] = {\n"
+                "#define FOO(i) {i, i},\n"
+                "SOME_GENERATOR(FOO)\n"
+                "{2, 2}\n"
+                "};");
 
   verifyFormat("return GradForUnaryCwise(g, {\n"
                "                                {{\"sign\"}, \"Sign\",  "
@@ -21354,6 +21359,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
                 "    [1] { 1, 1, },\n"
                 "    [2] { 1, 1, },\n"
                 "};");
+  verifyNoCrash("test arr[] = {\n"
+                "#define FOO(i) {i, i},\n"
+                "SOME_GENERATOR(FOO)\n"
+                "{2, 2}\n"
+                "};");
 
   verifyFormat("return GradForUnaryCwise(g, {\n"
                "                                {{\"sign\"}, \"Sign\", {\"x\", "

llvmbot avatar Mar 24 '24 05:03 llvmbot

:white_check_mark: With the latest revision this PR passed the Python code formatter.

github-actions[bot] avatar Mar 24 '24 05:03 github-actions[bot]

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.

github-actions[bot] avatar Mar 24 '24 05:03 github-actions[bot]

/cherry-pick cceedc939a43

owenca avatar May 04 '24 06:05 owenca

/pull-request llvm/llvm-project#91049

llvmbot avatar May 04 '24 06:05 llvmbot