[llvm-branch-commits] [clang] release/21.x: [clang-format] Fix a crash in AlignArrayOfStructures (#167099) (PR #170967)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Dec 5 20:22:40 PST 2025


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/170967

Backport 836919bb34493333767fc1734e402d3ebf989acb

Requested by: @owenca

>From 4f777458a44be780c6ddd42c6622aadf22e2fc75 Mon Sep 17 00:00:00 2001
From: owenca <owenpiano at gmail.com>
Date: Sun, 9 Nov 2025 17:35:54 -0800
Subject: [PATCH] [clang-format] Fix a crash in AlignArrayOfStructures
 (#167099)

Fixes #157405

(cherry picked from commit 836919bb34493333767fc1734e402d3ebf989acb)
---
 clang/lib/Format/WhitespaceManager.cpp |  5 ++++-
 clang/unittests/Format/FormatTest.cpp  | 13 +++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index cc3cc0f6906cc..ecc696c539226 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1295,7 +1295,10 @@ void WhitespaceManager::alignArrayInitializers() {
       bool FoundComplete = false;
       for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
            ++InsideIndex) {
-        if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
+        const auto *Tok = Changes[InsideIndex].Tok;
+        if (Tok->is(tok::pp_define))
+          break;
+        if (Tok == C.Tok->MatchingParen) {
           alignArrayInitializers(ChangeIndex, InsideIndex + 1);
           ChangeIndex = InsideIndex + 1;
           FoundComplete = true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8db0500573ec0..760c5a4ce4195 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22429,6 +22429,19 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
                "});",
                Style);
 
+  verifyNoCrash(
+      "PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
+      "    {\n"
+      "        {PIC(0),   PIC(0),   PIC(99),  PIC(81),  0}, // Backbox\n"
+      "        {PIC(1),   PIC(83),  PIC(191), PIC(137), 0}, // AK47\n"
+      "\n"
+      "#define PICALL1(a, b, c, d) \\\n"
+      "    { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
+      "\n"
+      "        PICALL1(1, 1, 75, 50),\n"
+      "};",
+      Style);
+
   Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
   verifyFormat("#define FOO \\\n"
                "  int foo[][2] = { \\\n"



More information about the llvm-branch-commits mailing list