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