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 32d8b97cc8dadb..3bc6915b8df0a7 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 df7e9add1cd446..69398fe411502f 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 a12a20359c2fad..cd4c93e8427723 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" + " { AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaa, " + "AAAAAAAAAAAAAAAAAAAAAAAAA::aaaaaaaaaaaaaaaaaaaaaaaa, 1, 0.000000000f, " + "\"00000000000000000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000\" },\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 cd4c93e8427723..a579746fd4b68e 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); verifyNoCrash("Type object[X][Y] = {\n" " {{val}, {val}, {val}},\n" " {{val}, {val}, // some comment\n" From fd71cc278e9226bec948df831a27ff220ce261ef Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 17 Nov 2023 02:12:17 -0800 Subject: [PATCH 3/4] Added a test from #72628. --- clang/unittests/Format/FormatTest.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a579746fd4b68e..cc8ae548f4dfdd 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -20714,6 +20714,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { "table({}, table({{\"\", false}}, {}))\n" "});", Style); + verifyNoCrash("Bar a[1] = {\n" + " #define buf(a, b) \\\n" + " { #a, #b },\n" + " { Test, bar }\n" + "};", + Style); Style.AlignConsecutiveAssignments.Enabled = true; Style.AlignConsecutiveDeclarations.Enabled = true; From a378be8e29267fe1afb7df06bdfdb4b09a19179b Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 17 Nov 2023 14:26:07 -0800 Subject: [PATCH 4/4] Update clang/unittests/Format/FormatTest.cpp --- clang/unittests/Format/FormatTest.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index cc8ae548f4dfdd..b5021f924a8090 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -20709,17 +20709,10 @@ 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); Style.AlignConsecutiveAssignments.Enabled = true; Style.AlignConsecutiveDeclarations.Enabled = true;