Skip to content

Commit 093e6bd

Browse files
authored
[clang-format] Fix crash involving array designators (#77045)
Fixes #76716 Fixes parsing of `[0]{}`. Before this patch it was begin parsed as a lambda, now it is correctly parsed as a designator initializer.
1 parent b2c0c6f commit 093e6bd

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
23082308
LeftSquare->isCppStructuredBinding(Style)) {
23092309
return false;
23102310
}
2311-
if (FormatTok->is(tok::l_square))
2311+
if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
23122312
return false;
23132313
if (FormatTok->is(tok::r_square)) {
23142314
const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);

clang/lib/Format/WhitespaceManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class WhitespaceManager {
282282
for (auto PrevIter = Start; PrevIter != End; ++PrevIter) {
283283
// If we broke the line the initial spaces are already
284284
// accounted for.
285+
assert(PrevIter->Index < Changes.size());
285286
if (Changes[PrevIter->Index].NewlinesBefore > 0)
286287
NetWidth = 0;
287288
NetWidth +=

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20931,6 +20931,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
2093120931
"};",
2093220932
Style);
2093320933

20934+
verifyNoCrash("Foo foo[] = {\n"
20935+
" [0] = {1, 1},\n"
20936+
" [1] { 1, 1, },\n"
20937+
" [2] { 1, 1, },\n"
20938+
"};");
20939+
2093420940
verifyFormat("return GradForUnaryCwise(g, {\n"
2093520941
" {{\"sign\"}, \"Sign\", "
2093620942
" {\"x\", \"dy\"}},\n"
@@ -21173,6 +21179,12 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
2117321179
"};",
2117421180
Style);
2117521181

21182+
verifyNoCrash("Foo foo[] = {\n"
21183+
" [0] = {1, 1},\n"
21184+
" [1] { 1, 1, },\n"
21185+
" [2] { 1, 1, },\n"
21186+
"};");
21187+
2117621188
verifyFormat("return GradForUnaryCwise(g, {\n"
2117721189
" {{\"sign\"}, \"Sign\", {\"x\", "
2117821190
"\"dy\"} },\n"

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,11 @@ TEST_F(TokenAnnotatorTest, UnderstandDesignatedInitializers) {
23242324
EXPECT_BRACE_KIND(Tokens[1], BK_BracedInit);
23252325
EXPECT_TOKEN(Tokens[6], tok::period, TT_DesignatedInitializerPeriod);
23262326
EXPECT_TOKEN(Tokens[13], tok::period, TT_DesignatedInitializerPeriod);
2327+
2328+
Tokens = annotate("Foo foo[] = {[0]{}};");
2329+
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
2330+
EXPECT_TOKEN(Tokens[6], tok::l_square, TT_DesignatedInitializerLSquare);
2331+
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
23272332
}
23282333

23292334
TEST_F(TokenAnnotatorTest, UnderstandsJavaScript) {

0 commit comments

Comments
 (0)