Skip to content

[clang-format] Fix a crash with AlignArrayOfStructures option #86420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Mar 24, 2024

Fixes #86109.

@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2024

@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\", "

Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link

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

@owenca owenca merged commit cceedc9 into llvm:main Mar 24, 2024
@owenca owenca deleted the 86109 branch March 24, 2024 22:22
@owenca
Copy link
Contributor Author

owenca commented May 4, 2024

/cherry-pick cceedc9

@owenca owenca added this to the LLVM 18.X Release milestone May 4, 2024
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request May 4, 2024
@llvmbot
Copy link
Member

llvmbot commented May 4, 2024

/pull-request #91049

tstellar pushed a commit to llvmbot/llvm-project that referenced this pull request May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

clang-format crashes with AlignArrayOfStructures
3 participants