Skip to content

Commit ca044f5

Browse files
committed
Revert "[clang-format][NFC] Code Tidies in UnwrappedLineFormatter"
This reverts commit f014ab9. These tests are failing with asan: clang/unittests:format_tests clang/unittests:format_tests clang-tools-extra/unittests:clang_move_tests clang/unittests:tooling_tests clang-tools-extra/test/clang-move:move-template-class.cpp.test clang-tools-extra/test/clang-move:move-multiple-classes.cpp.test clang-tools-extra/test/clang-move:move-used-helper-decls.cpp.test clang-tools-extra/clangd/unittests:clangd_tests clang/test/Format:access-modifiers.cpp.test clang/unittests:rename_tests clang/unittests:rename_tests
1 parent 20f8f46 commit ca044f5

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,10 @@ class LineJoiner {
211211
const AnnotatedLine *TheLine = *I;
212212
if (TheLine->Last->is(TT_LineComment))
213213
return 0;
214-
const auto &NextLine = *I[1];
215-
const auto &PreviousLine = *I[-1];
216-
if (NextLine.Type == LT_Invalid || NextLine.First->MustBreakBefore)
214+
if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore)
217215
return 0;
218216
if (TheLine->InPPDirective &&
219-
(!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline))
217+
(!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
220218
return 0;
221219

222220
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
@@ -233,15 +231,15 @@ class LineJoiner {
233231
if (TheLine->Last->is(TT_FunctionLBrace) &&
234232
TheLine->First == TheLine->Last &&
235233
!Style.BraceWrapping.SplitEmptyFunction &&
236-
NextLine.First->is(tok::r_brace))
234+
I[1]->First->is(tok::r_brace))
237235
return tryMergeSimpleBlock(I, E, Limit);
238236

239237
// Handle empty record blocks where the brace has already been wrapped
240238
if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last &&
241239
I != AnnotatedLines.begin()) {
242-
bool EmptyBlock = NextLine.First->is(tok::r_brace);
240+
bool EmptyBlock = I[1]->First->is(tok::r_brace);
243241

244-
const FormatToken *Tok = PreviousLine.First;
242+
const FormatToken *Tok = I[-1]->First;
245243
if (Tok && Tok->is(tok::comment))
246244
Tok = Tok->getNextNonComment();
247245

@@ -269,7 +267,7 @@ class LineJoiner {
269267
bool MergeShortFunctions =
270268
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
271269
(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
272-
NextLine.First->is(tok::r_brace)) ||
270+
I[1]->First->is(tok::r_brace)) ||
273271
(Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly &&
274272
TheLine->Level != 0);
275273

@@ -314,75 +312,73 @@ class LineJoiner {
314312
return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
315313
}
316314
// Try to merge a control statement block with left brace unwrapped
317-
if (TheLine->Last->is(tok::l_brace) &&
315+
if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last &&
318316
TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
319317
return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
320318
? tryMergeSimpleBlock(I, E, Limit)
321319
: 0;
322320
}
323321
// Try to merge a control statement block with left brace wrapped
324-
if (NextLine.First->is(tok::l_brace)) {
325-
if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
326-
tok::kw_for, tok::kw_switch, tok::kw_try,
327-
tok::kw_do, TT_ForEachMacro) ||
328-
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
329-
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
330-
Style.BraceWrapping.AfterControlStatement ==
331-
FormatStyle::BWACS_MultiLine) {
332-
// If possible, merge the next line's wrapped left brace with the
333-
// current line. Otherwise, leave it on the next line, as this is a
334-
// multi-line control statement.
335-
return (Style.ColumnLimit == 0 ||
336-
TheLine->Last->TotalLength <= Style.ColumnLimit)
337-
? 1
338-
: 0;
339-
}
340-
if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
341-
tok::kw_for)) {
342-
return (Style.BraceWrapping.AfterControlStatement ==
343-
FormatStyle::BWACS_Always)
344-
? tryMergeSimpleBlock(I, E, Limit)
345-
: 0;
346-
}
347-
if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
348-
Style.BraceWrapping.AfterControlStatement ==
349-
FormatStyle::BWACS_MultiLine) {
350-
// This case if different from the upper BWACS_MultiLine processing
351-
// in that a preceding r_brace is not on the same line as else/catch
352-
// most likely because of BeforeElse/BeforeCatch set to true.
353-
// If the line length doesn't fit ColumnLimit, leave l_brace on the
354-
// next line to respect the BWACS_MultiLine.
355-
return (Style.ColumnLimit == 0 ||
356-
TheLine->Last->TotalLength <= Style.ColumnLimit)
357-
? 1
358-
: 0;
359-
}
322+
if (I[1]->First->is(tok::l_brace) &&
323+
(TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
324+
tok::kw_for, tok::kw_switch, tok::kw_try,
325+
tok::kw_do, TT_ForEachMacro) ||
326+
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
327+
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
328+
Style.BraceWrapping.AfterControlStatement ==
329+
FormatStyle::BWACS_MultiLine) {
330+
// If possible, merge the next line's wrapped left brace with the current
331+
// line. Otherwise, leave it on the next line, as this is a multi-line
332+
// control statement.
333+
return (Style.ColumnLimit == 0 ||
334+
TheLine->Last->TotalLength <= Style.ColumnLimit)
335+
? 1
336+
: 0;
337+
} else if (I[1]->First->is(tok::l_brace) &&
338+
TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
339+
tok::kw_for)) {
340+
return (Style.BraceWrapping.AfterControlStatement ==
341+
FormatStyle::BWACS_Always)
342+
? tryMergeSimpleBlock(I, E, Limit)
343+
: 0;
344+
} else if (I[1]->First->is(tok::l_brace) &&
345+
TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
346+
Style.BraceWrapping.AfterControlStatement ==
347+
FormatStyle::BWACS_MultiLine) {
348+
// This case if different from the upper BWACS_MultiLine processing
349+
// in that a preceding r_brace is not on the same line as else/catch
350+
// most likely because of BeforeElse/BeforeCatch set to true.
351+
// If the line length doesn't fit ColumnLimit, leave l_brace on the
352+
// next line to respect the BWACS_MultiLine.
353+
return (Style.ColumnLimit == 0 ||
354+
TheLine->Last->TotalLength <= Style.ColumnLimit)
355+
? 1
356+
: 0;
360357
}
361358
// Don't merge block with left brace wrapped after ObjC special blocks
362359
if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
363-
PreviousLine.First->is(tok::at) && PreviousLine.First->Next) {
364-
tok::ObjCKeywordKind kwId =
365-
PreviousLine.First->Next->Tok.getObjCKeywordID();
360+
I[-1]->First->is(tok::at) && I[-1]->First->Next) {
361+
tok::ObjCKeywordKind kwId = I[-1]->First->Next->Tok.getObjCKeywordID();
366362
if (kwId == clang::tok::objc_autoreleasepool ||
367363
kwId == clang::tok::objc_synchronized)
368364
return 0;
369365
}
370366
// Don't merge block with left brace wrapped after case labels
371367
if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
372-
PreviousLine.First->isOneOf(tok::kw_case, tok::kw_default))
368+
I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
373369
return 0;
374370

375371
// Don't merge an empty template class or struct if SplitEmptyRecords
376372
// is defined.
377373
if (Style.BraceWrapping.SplitEmptyRecord &&
378374
TheLine->Last->is(tok::l_brace) && I != AnnotatedLines.begin() &&
379-
PreviousLine.Last) {
380-
const FormatToken *Previous = PreviousLine.Last;
375+
I[-1]->Last) {
376+
const FormatToken *Previous = I[-1]->Last;
381377
if (Previous) {
382378
if (Previous->is(tok::comment))
383379
Previous = Previous->getPreviousNonComment();
384380
if (Previous) {
385-
if (Previous->is(tok::greater) && !PreviousLine.InPPDirective)
381+
if (Previous->is(tok::greater) && !I[-1]->InPPDirective)
386382
return 0;
387383
if (Previous->is(tok::identifier)) {
388384
const FormatToken *PreviousPrevious =
@@ -405,21 +401,21 @@ class LineJoiner {
405401
}
406402
if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
407403
ShouldMerge = !Style.BraceWrapping.AfterClass ||
408-
(NextLine.First->is(tok::r_brace) &&
404+
(I[1]->First->is(tok::r_brace) &&
409405
!Style.BraceWrapping.SplitEmptyRecord);
410406
} else if (Tok->is(tok::kw_enum)) {
411407
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
412408
} else {
413409
ShouldMerge = !Style.BraceWrapping.AfterFunction ||
414-
(NextLine.First->is(tok::r_brace) &&
410+
(I[1]->First->is(tok::r_brace) &&
415411
!Style.BraceWrapping.SplitEmptyFunction);
416412
}
417413
return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
418414
}
419415
// Try to merge a function block with left brace wrapped
420-
if (NextLine.First->is(TT_FunctionLBrace) &&
416+
if (I[1]->First->is(TT_FunctionLBrace) &&
421417
Style.BraceWrapping.AfterFunction) {
422-
if (NextLine.Last->is(TT_LineComment))
418+
if (I[1]->Last->is(TT_LineComment))
423419
return 0;
424420

425421
// Check for Limit <= 2 to account for the " {".
@@ -430,7 +426,7 @@ class LineJoiner {
430426
unsigned MergedLines = 0;
431427
if (MergeShortFunctions ||
432428
(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
433-
NextLine.First == NextLine.Last && I + 2 != E &&
429+
I[1]->First == I[1]->Last && I + 2 != E &&
434430
I[2]->First->is(tok::r_brace))) {
435431
MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
436432
// If we managed to merge the block, count the function header, which is

0 commit comments

Comments
 (0)