Skip to content

Commit

Permalink
[clang-format] Fix crashes in AlignArrayOfStructures (llvm#72520)
Browse files Browse the repository at this point in the history
Fixed llvm#54815.
Fixed llvm#55269.
Fixed llvm#55493.
Fixed llvm#68431.
  • Loading branch information
owenca authored and tru committed Nov 27, 2023
1 parent f74f3e6 commit f6c231c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,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);
Expand Down Expand Up @@ -1324,7 +1326,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;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/WhitespaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20734,6 +20734,11 @@ 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"
Expand Down Expand Up @@ -21142,6 +21147,33 @@ 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 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"
" {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) {
Expand Down

0 comments on commit f6c231c

Please sign in to comment.