Skip to content

Commit

Permalink
[clang-format] Fix crash involving array designators (llvm#77045)
Browse files Browse the repository at this point in the history
Fixes llvm#76716
Fixes parsing of `[0]{}`. Before this patch it was begin parsed as a
lambda, now it is correctly parsed as a designator initializer.
  • Loading branch information
XDeme1 authored Jan 11, 2024
1 parent b2c0c6f commit 093e6bd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
LeftSquare->isCppStructuredBinding(Style)) {
return false;
}
if (FormatTok->is(tok::l_square))
if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
return false;
if (FormatTok->is(tok::r_square)) {
const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Format/WhitespaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class WhitespaceManager {
for (auto PrevIter = Start; PrevIter != End; ++PrevIter) {
// If we broke the line the initial spaces are already
// accounted for.
assert(PrevIter->Index < Changes.size());
if (Changes[PrevIter->Index].NewlinesBefore > 0)
NetWidth = 0;
NetWidth +=
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20931,6 +20931,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
"};",
Style);

verifyNoCrash("Foo foo[] = {\n"
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
" [2] { 1, 1, },\n"
"};");

verifyFormat("return GradForUnaryCwise(g, {\n"
" {{\"sign\"}, \"Sign\", "
" {\"x\", \"dy\"}},\n"
Expand Down Expand Up @@ -21173,6 +21179,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
"};",
Style);

verifyNoCrash("Foo foo[] = {\n"
" [0] = {1, 1},\n"
" [1] { 1, 1, },\n"
" [2] { 1, 1, },\n"
"};");

verifyFormat("return GradForUnaryCwise(g, {\n"
" {{\"sign\"}, \"Sign\", {\"x\", "
"\"dy\"} },\n"
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,11 @@ TEST_F(TokenAnnotatorTest, UnderstandDesignatedInitializers) {
EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
EXPECT_TOKEN(Tokens[6], tok::period, TT_DesignatedInitializerPeriod);
EXPECT_TOKEN(Tokens[13], tok::period, TT_DesignatedInitializerPeriod);

Tokens = annotate("Foo foo[] = {[0]{}};");
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::l_square, TT_DesignatedInitializerLSquare);
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
}

TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {
Expand Down

0 comments on commit 093e6bd

Please sign in to comment.