@@ -211,12 +211,10 @@ class LineJoiner {
211
211
const AnnotatedLine *TheLine = *I;
212
212
if (TheLine->Last ->is (TT_LineComment))
213
213
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 )
217
215
return 0 ;
218
216
if (TheLine->InPPDirective &&
219
- (!NextLine. InPPDirective || NextLine. First ->HasUnescapedNewline ))
217
+ (!I[ 1 ]-> InPPDirective || I[ 1 ]-> First ->HasUnescapedNewline ))
220
218
return 0 ;
221
219
222
220
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit )
@@ -233,15 +231,15 @@ class LineJoiner {
233
231
if (TheLine->Last ->is (TT_FunctionLBrace) &&
234
232
TheLine->First == TheLine->Last &&
235
233
!Style.BraceWrapping .SplitEmptyFunction &&
236
- NextLine. First ->is (tok::r_brace))
234
+ I[ 1 ]-> First ->is (tok::r_brace))
237
235
return tryMergeSimpleBlock (I, E, Limit);
238
236
239
237
// Handle empty record blocks where the brace has already been wrapped
240
238
if (TheLine->Last ->is (tok::l_brace) && TheLine->First == TheLine->Last &&
241
239
I != AnnotatedLines.begin ()) {
242
- bool EmptyBlock = NextLine. First ->is (tok::r_brace);
240
+ bool EmptyBlock = I[ 1 ]-> First ->is (tok::r_brace);
243
241
244
- const FormatToken *Tok = PreviousLine. First ;
242
+ const FormatToken *Tok = I[- 1 ]-> First ;
245
243
if (Tok && Tok->is (tok::comment))
246
244
Tok = Tok->getNextNonComment ();
247
245
@@ -269,7 +267,7 @@ class LineJoiner {
269
267
bool MergeShortFunctions =
270
268
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
271
269
(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
272
- NextLine. First ->is (tok::r_brace)) ||
270
+ I[ 1 ]-> First ->is (tok::r_brace)) ||
273
271
(Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly &&
274
272
TheLine->Level != 0 );
275
273
@@ -314,75 +312,73 @@ class LineJoiner {
314
312
return MergeShortFunctions ? tryMergeSimpleBlock (I, E, Limit) : 0 ;
315
313
}
316
314
// 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 &&
318
316
TheLine->First ->isOneOf (tok::kw_if, tok::kw_while, tok::kw_for)) {
319
317
return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
320
318
? tryMergeSimpleBlock (I, E, Limit)
321
319
: 0 ;
322
320
}
323
321
// 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 ;
360
357
}
361
358
// Don't merge block with left brace wrapped after ObjC special blocks
362
359
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 ();
366
362
if (kwId == clang::tok::objc_autoreleasepool ||
367
363
kwId == clang::tok::objc_synchronized)
368
364
return 0 ;
369
365
}
370
366
// Don't merge block with left brace wrapped after case labels
371
367
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))
373
369
return 0 ;
374
370
375
371
// Don't merge an empty template class or struct if SplitEmptyRecords
376
372
// is defined.
377
373
if (Style.BraceWrapping .SplitEmptyRecord &&
378
374
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 ;
381
377
if (Previous) {
382
378
if (Previous->is (tok::comment))
383
379
Previous = Previous->getPreviousNonComment ();
384
380
if (Previous) {
385
- if (Previous->is (tok::greater) && !PreviousLine. InPPDirective )
381
+ if (Previous->is (tok::greater) && !I[- 1 ]-> InPPDirective )
386
382
return 0 ;
387
383
if (Previous->is (tok::identifier)) {
388
384
const FormatToken *PreviousPrevious =
@@ -405,21 +401,21 @@ class LineJoiner {
405
401
}
406
402
if (Tok->isOneOf (tok::kw_class, tok::kw_struct)) {
407
403
ShouldMerge = !Style.BraceWrapping .AfterClass ||
408
- (NextLine. First ->is (tok::r_brace) &&
404
+ (I[ 1 ]-> First ->is (tok::r_brace) &&
409
405
!Style.BraceWrapping .SplitEmptyRecord );
410
406
} else if (Tok->is (tok::kw_enum)) {
411
407
ShouldMerge = Style.AllowShortEnumsOnASingleLine ;
412
408
} else {
413
409
ShouldMerge = !Style.BraceWrapping .AfterFunction ||
414
- (NextLine. First ->is (tok::r_brace) &&
410
+ (I[ 1 ]-> First ->is (tok::r_brace) &&
415
411
!Style.BraceWrapping .SplitEmptyFunction );
416
412
}
417
413
return ShouldMerge ? tryMergeSimpleBlock (I, E, Limit) : 0 ;
418
414
}
419
415
// 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) &&
421
417
Style.BraceWrapping .AfterFunction ) {
422
- if (NextLine. Last ->is (TT_LineComment))
418
+ if (I[ 1 ]-> Last ->is (TT_LineComment))
423
419
return 0 ;
424
420
425
421
// Check for Limit <= 2 to account for the " {".
@@ -430,7 +426,7 @@ class LineJoiner {
430
426
unsigned MergedLines = 0 ;
431
427
if (MergeShortFunctions ||
432
428
(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
433
- NextLine. First == NextLine. Last && I + 2 != E &&
429
+ I[ 1 ]-> First == I[ 1 ]-> Last && I + 2 != E &&
434
430
I[2 ]->First ->is (tok::r_brace))) {
435
431
MergedLines = tryMergeSimpleBlock (I + 1 , E, Limit);
436
432
// If we managed to merge the block, count the function header, which is
0 commit comments