[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/72520

Fixed #55493.
Fixed #68431.

>From efdf321e9447e8b3f1c27ccdf6da842107deb6dd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 06:36:41 -0800
Subject: [PATCH] [clang-format] Fix crashes in AlignArrayOfStructures

Fixed #55493.
Fixed #68431.
---
 clang/lib/Format/WhitespaceManager.cpp |  4 +++-
 clang/lib/Format/WhitespaceManager.h   |  2 +-
 clang/unittests/Format/FormatTest.cpp  | 18 ++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 32d8b97cc8dadb1..3bc6915b8df0a70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
   ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void 
WhitespaceManager::alignArrayInitializersLeftJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > CellDescs.CellCounts.size())
+  if (RowCount >= CellDescs.CellCounts.size())
 break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
diff --git a/clang/lib/Format/WhitespaceManager.h 
b/clang/lib/Format/WhitespaceManager.h
index df7e9add1cd446f..69398fe411502f1 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -317,7 +317,7 @@ class WhitespaceManager {
 auto Offset = std::distance(CellStart, CellStop);
 for (const auto *Next = CellStop->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > MaxRowCount)
+  if (RowCount >= MaxRowCount)
 break;
   auto Start = (CellStart + RowCount * CellCount);
   auto End = Start + Offset;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index a12a20359c2fad2..cd4c93e8427723f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21140,6 +21140,24 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   "that really, in any just world, ought to be split over multiple "
   "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
   Style);
+
+  Style.ColumnLimit = 25;
+  verifyNoCrash("Type object[X][Y] = {\n"
+"{{val}, {val}, {val}},\n"
+"{{val}, {val}, // some comment\n"
+"   {val}}\n"
+"};",
+Style);
+
+  Style.ColumnLimit = 120;
+  verifyNoCrash(
+  "T v[] {\n"
+  "{ A::aaa, "
+  "A::, 1, 0.0f, "
+  "\""
+  "\" },\n"
+  "};",
+  Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)


Changes

Fixed #55493.
Fixed #68431.

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


3 Files Affected:

- (modified) clang/lib/Format/WhitespaceManager.cpp (+3-1) 
- (modified) clang/lib/Format/WhitespaceManager.h (+1-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+18) 


``diff
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 32d8b97cc8dadb1..3bc6915b8df0a70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
   ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void 
WhitespaceManager::alignArrayInitializersLeftJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > CellDescs.CellCounts.size())
+  if (RowCount >= CellDescs.CellCounts.size())
 break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
diff --git a/clang/lib/Format/WhitespaceManager.h 
b/clang/lib/Format/WhitespaceManager.h
index df7e9add1cd446f..69398fe411502f1 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -317,7 +317,7 @@ class WhitespaceManager {
 auto Offset = std::distance(CellStart, CellStop);
 for (const auto *Next = CellStop->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > MaxRowCount)
+  if (RowCount >= MaxRowCount)
 break;
   auto Start = (CellStart + RowCount * CellCount);
   auto End = Start + Offset;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index a12a20359c2fad2..cd4c93e8427723f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21140,6 +21140,24 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   "that really, in any just world, ought to be split over multiple "
   "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
   Style);
+
+  Style.ColumnLimit = 25;
+  verifyNoCrash("Type object[X][Y] = {\n"
+"{{val}, {val}, {val}},\n"
+"{{val}, {val}, // some comment\n"
+"   {val}}\n"
+"};",
+Style);
+
+  Style.ColumnLimit = 120;
+  verifyNoCrash(
+  "T v[] {\n"
+  "{ A::aaa, "
+  "A::, 1, 0.0f, "
+  "\""
+  "\" },\n"
+  "};",
+  Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {

``




https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks edited 
https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Björn Schäpers via cfe-commits


@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;

HazardyKnusperkeks wrote:

```suggestion
for (const auto *Next = CellIter->NextColumnElement; Next && RowCount < 
CellDescs.CellCounts.size();
 Next = Next->NextColumnElement, ++RowCount) {
```
Maybe?

Then the `++RowCount` below must be removed.

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Owen Pan via cfe-commits


@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;

owenca wrote:

I intentionally made it the same as lines 1384-1385 below so that lines 
1315-1323 would remain copy-pasted from lines 1380-1388. This will make future 
refactoring easier.

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-16 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/72520

>From efdf321e9447e8b3f1c27ccdf6da842107deb6dd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 06:36:41 -0800
Subject: [PATCH 1/2] [clang-format] Fix crashes in AlignArrayOfStructures

Fixed #55493.
Fixed #68431.
---
 clang/lib/Format/WhitespaceManager.cpp |  4 +++-
 clang/lib/Format/WhitespaceManager.h   |  2 +-
 clang/unittests/Format/FormatTest.cpp  | 18 ++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 32d8b97cc8dadb1..3bc6915b8df0a70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
   ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void 
WhitespaceManager::alignArrayInitializersLeftJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > CellDescs.CellCounts.size())
+  if (RowCount >= CellDescs.CellCounts.size())
 break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
diff --git a/clang/lib/Format/WhitespaceManager.h 
b/clang/lib/Format/WhitespaceManager.h
index df7e9add1cd446f..69398fe411502f1 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -317,7 +317,7 @@ class WhitespaceManager {
 auto Offset = std::distance(CellStart, CellStop);
 for (const auto *Next = CellStop->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > MaxRowCount)
+  if (RowCount >= MaxRowCount)
 break;
   auto Start = (CellStart + RowCount * CellCount);
   auto End = Start + Offset;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index a12a20359c2fad2..cd4c93e8427723f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21140,6 +21140,24 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   "that really, in any just world, ought to be split over multiple "
   "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
   Style);
+
+  Style.ColumnLimit = 25;
+  verifyNoCrash("Type object[X][Y] = {\n"
+"{{val}, {val}, {val}},\n"
+"{{val}, {val}, // some comment\n"
+"   {val}}\n"
+"};",
+Style);
+
+  Style.ColumnLimit = 120;
+  verifyNoCrash(
+  "T v[] {\n"
+  "{ A::aaa, "
+  "A::, 1, 0.0f, "
+  "\""
+  "\" },\n"
+  "};",
+  Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {

>From 4a73cc6c52be6824d34f4ba5608a008a3dfdedf0 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 18:46:23 -0800
Subject: [PATCH 2/2] Added tests from #54815 and #55269.

---
 clang/unittests/Format/FormatTest.cpp | 15 +++
 1 file changed, 15 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index cd4c93e8427723f..a579746fd4b68e8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20709,6 +20709,12 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
   auto Style = getLLVMStyle();
   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
+
+  verifyNoCrash("f({\n"
+"table({}, table({{\"\", false}}, {}))\n"
+"});",
+Style);
+
   Style.AlignConsecutiveAssignments.Enabled = true;
   Style.AlignConsecutiveDeclarations.Enabled = true;
   verifyFormat("struct test demo[] = {\n"
@@ -21142,6 +21148,15 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   Style);
 
   Style.ColumnLimit = 25;
+  verifyNoCrash("Type foo{\n"
+"{\n"
+"1,  // A\n"
+"2,  // B\n"
+"3,  // C\n"
+"},\n"
+"\"hello\",\n"
+"};",
+Style);
   ve

[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Björn Schäpers via cfe-commits


@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;

HazardyKnusperkeks wrote:

I would change the other lines too. I think it's better to see the condition in 
the `for`, but it's your call.

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits


@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;

owenca wrote:

The same/similar code appeared twice in this file and once in the header file. 
If I were to refactor it, I might not use the same for-loop, so I'll leave it 
as is for now. (There are also much more cleanup that can be done with this 
option, but I don't know if it's worthwhile as it's so buggy even after 
@mydeveloperday limited it to matrices.)

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/72520

>From efdf321e9447e8b3f1c27ccdf6da842107deb6dd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 06:36:41 -0800
Subject: [PATCH 1/3] [clang-format] Fix crashes in AlignArrayOfStructures

Fixed #55493.
Fixed #68431.
---
 clang/lib/Format/WhitespaceManager.cpp |  4 +++-
 clang/lib/Format/WhitespaceManager.h   |  2 +-
 clang/unittests/Format/FormatTest.cpp  | 18 ++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 32d8b97cc8dadb1..3bc6915b8df0a70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
   ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void 
WhitespaceManager::alignArrayInitializersLeftJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > CellDescs.CellCounts.size())
+  if (RowCount >= CellDescs.CellCounts.size())
 break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
diff --git a/clang/lib/Format/WhitespaceManager.h 
b/clang/lib/Format/WhitespaceManager.h
index df7e9add1cd446f..69398fe411502f1 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -317,7 +317,7 @@ class WhitespaceManager {
 auto Offset = std::distance(CellStart, CellStop);
 for (const auto *Next = CellStop->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > MaxRowCount)
+  if (RowCount >= MaxRowCount)
 break;
   auto Start = (CellStart + RowCount * CellCount);
   auto End = Start + Offset;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index a12a20359c2fad2..cd4c93e8427723f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21140,6 +21140,24 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   "that really, in any just world, ought to be split over multiple "
   "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
   Style);
+
+  Style.ColumnLimit = 25;
+  verifyNoCrash("Type object[X][Y] = {\n"
+"{{val}, {val}, {val}},\n"
+"{{val}, {val}, // some comment\n"
+"   {val}}\n"
+"};",
+Style);
+
+  Style.ColumnLimit = 120;
+  verifyNoCrash(
+  "T v[] {\n"
+  "{ A::aaa, "
+  "A::, 1, 0.0f, "
+  "\""
+  "\" },\n"
+  "};",
+  Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {

>From 4a73cc6c52be6824d34f4ba5608a008a3dfdedf0 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 18:46:23 -0800
Subject: [PATCH 2/3] Added tests from #54815 and #55269.

---
 clang/unittests/Format/FormatTest.cpp | 15 +++
 1 file changed, 15 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index cd4c93e8427723f..a579746fd4b68e8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20709,6 +20709,12 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
   auto Style = getLLVMStyle();
   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
+
+  verifyNoCrash("f({\n"
+"table({}, table({{\"\", false}}, {}))\n"
+"});",
+Style);
+
   Style.AlignConsecutiveAssignments.Enabled = true;
   Style.AlignConsecutiveDeclarations.Enabled = true;
   verifyFormat("struct test demo[] = {\n"
@@ -21142,6 +21148,15 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   Style);
 
   Style.ColumnLimit = 25;
+  verifyNoCrash("Type foo{\n"
+"{\n"
+"1,  // A\n"
+"2,  // B\n"
+"3,  // C\n"
+"},\n"
+"\"hello\",\n"
+"};",
+Style);
   ve

[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

owenca wrote:

We probably should backport it to 17.0.6. What do you all think? @tru 

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Tobias Hieta via cfe-commits

tru wrote:

> We probably should backport it to 17.0.6. What do you all think? @tru

Yep - seems like a good and small fix to have in 17.x

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Tobias Hieta via cfe-commits

tru wrote:

@owenca I picked da1b1fba5cfd41521a840202d8cf4c3796c5e10b on top of the 17.x 
branch and my test case was not fixed, it still crashes in the same way as 
described in #72628 

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

owenca wrote:

> @owenca I picked da1b1fba5cfd41521a840202d8cf4c3796c5e10b on top of the 17.x 
> branch and my test case was not fixed, it still crashes in the same way as 
> described in #72628

Thanks for trying it on 17.x. We can't backport it then.

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Tobias Hieta via cfe-commits

tru wrote:

> Thanks for trying it on 17.x. We can't backport it then.

Any idea if there is another workaround or fix that we could do to target 17.x? 
18 is still pretty far off and clang-format for 17 will soon be included in a 
lot of downstream tools. Happy to help out fixing it if you have any pointers.

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Emilia Kond via cfe-commits

https://github.com/rymiel approved this pull request.

Thanks for doing this! There were probably dozens of duplicates of this same 
crash across multiple years, so this is great

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

https://github.com/owenca edited https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits


@@ -20709,6 +20709,18 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
   auto Style = getLLVMStyle();
   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
+
+  verifyNoCrash("f({\n"
+"table({}, table({{\"\", false}}, {}))\n"
+"});",
+Style);
+  verifyNoCrash("Bar a[1] = {\n"
+"#define buf(a, b) \\\n"
+"  { #a, #b },\n"
+"{ Test, bar }\n"
+"};",
+Style);

owenca wrote:

```suggestion
  verifyNoCrash("f({\n"
"table({}, table({{\"\", false}}, {}))\n"
"});",
Style);
```

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

https://github.com/owenca updated 
https://github.com/llvm/llvm-project/pull/72520

>From efdf321e9447e8b3f1c27ccdf6da842107deb6dd Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 06:36:41 -0800
Subject: [PATCH 1/4] [clang-format] Fix crashes in AlignArrayOfStructures

Fixed #55493.
Fixed #68431.
---
 clang/lib/Format/WhitespaceManager.cpp |  4 +++-
 clang/lib/Format/WhitespaceManager.h   |  2 +-
 clang/unittests/Format/FormatTest.cpp  | 18 ++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 32d8b97cc8dadb1..3bc6915b8df0a70 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1316,6 +1316,8 @@ void 
WhitespaceManager::alignArrayInitializersRightJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
+  if (RowCount >= CellDescs.CellCounts.size())
+break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
   ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
@@ -1379,7 +1381,7 @@ void 
WhitespaceManager::alignArrayInitializersLeftJustified(
 auto Offset = std::distance(Cells.begin(), CellIter);
 for (const auto *Next = CellIter->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > CellDescs.CellCounts.size())
+  if (RowCount >= CellDescs.CellCounts.size())
 break;
   auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
   auto *End = Start + Offset;
diff --git a/clang/lib/Format/WhitespaceManager.h 
b/clang/lib/Format/WhitespaceManager.h
index df7e9add1cd446f..69398fe411502f1 100644
--- a/clang/lib/Format/WhitespaceManager.h
+++ b/clang/lib/Format/WhitespaceManager.h
@@ -317,7 +317,7 @@ class WhitespaceManager {
 auto Offset = std::distance(CellStart, CellStop);
 for (const auto *Next = CellStop->NextColumnElement; Next;
  Next = Next->NextColumnElement) {
-  if (RowCount > MaxRowCount)
+  if (RowCount >= MaxRowCount)
 break;
   auto Start = (CellStart + RowCount * CellCount);
   auto End = Start + Offset;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index a12a20359c2fad2..cd4c93e8427723f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21140,6 +21140,24 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   "that really, in any just world, ought to be split over multiple "
   "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};",
   Style);
+
+  Style.ColumnLimit = 25;
+  verifyNoCrash("Type object[X][Y] = {\n"
+"{{val}, {val}, {val}},\n"
+"{{val}, {val}, // some comment\n"
+"   {val}}\n"
+"};",
+Style);
+
+  Style.ColumnLimit = 120;
+  verifyNoCrash(
+  "T v[] {\n"
+  "{ A::aaa, "
+  "A::, 1, 0.0f, "
+  "\""
+  "\" },\n"
+  "};",
+  Style);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {

>From 4a73cc6c52be6824d34f4ba5608a008a3dfdedf0 Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 16 Nov 2023 18:46:23 -0800
Subject: [PATCH 2/4] Added tests from #54815 and #55269.

---
 clang/unittests/Format/FormatTest.cpp | 15 +++
 1 file changed, 15 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index cd4c93e8427723f..a579746fd4b68e8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20709,6 +20709,12 @@ TEST_F(FormatTest, CatchExceptionReferenceBinding) {
 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
   auto Style = getLLVMStyle();
   Style.AlignArrayOfStructures = FormatStyle::AIAS_Right;
+
+  verifyNoCrash("f({\n"
+"table({}, table({{\"\", false}}, {}))\n"
+"});",
+Style);
+
   Style.AlignConsecutiveAssignments.Enabled = true;
   Style.AlignConsecutiveDeclarations.Enabled = true;
   verifyFormat("struct test demo[] = {\n"
@@ -21142,6 +21148,15 @@ TEST_F(FormatTest, 
CatchAlignArrayOfStructuresLeftAlignment) {
   Style);
 
   Style.ColumnLimit = 25;
+  verifyNoCrash("Type foo{\n"
+"{\n"
+"1,  // A\n"
+"2,  // B\n"
+"3,  // C\n"
+"},\n"
+"\"hello\",\n"
+"};",
+Style);
   ve

[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

owenca wrote:

> > Thanks for trying it on 17.x. We can't backport it then.
> 
> Any idea if there is another workaround or fix that we could do to target 
> 17.x? 18 is still pretty far off and clang-format for 17 will soon be 
> included in a lot of downstream tools. Happy to help out fixing it if you 
> have any pointers.

My bad. #72628 was already "fixed" on 18 before this patch. Can you help bisect 
it?

https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix crashes in AlignArrayOfStructures (PR #72520)

2023-11-17 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/72520
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits