Skip to content

Commit f014ab9

Browse files
[clang-format][NFC] Code Tidies in UnwrappedLineFormatter
* Give I[1] and I[-1] a name: - Easier to understand - Easier to debug (since you don't go through operator[] everytime) * TheLine->First != TheLine->Last follows since last is a l brace and first isn't. * Factor the check for is(tok::l_brace) out. * Drop else after return. Differential Revision: https://reviews.llvm.org/D115060
1 parent c40049d commit f014ab9

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,12 @@ class LineJoiner {
211211
const AnnotatedLine *TheLine = *I;
212212
if (TheLine->Last->is(TT_LineComment))
213213
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)
215217
return 0;
216218
if (TheLine->InPPDirective &&
217-
(!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
219+
(!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline))
218220
return 0;
219221

220222
if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
@@ -231,15 +233,15 @@ class LineJoiner {
231233
if (TheLine->Last->is(TT_FunctionLBrace) &&
232234
TheLine->First == TheLine->Last &&
233235
!Style.BraceWrapping.SplitEmptyFunction &&
234-
I[1]->First->is(tok::r_brace))
236+
NextLine.First->is(tok::r_brace))
235237
return tryMergeSimpleBlock(I, E, Limit);
236238

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

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

@@ -267,7 +269,7 @@ class LineJoiner {
267269
bool MergeShortFunctions =
268270
Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
269271
(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
270-
I[1]->First->is(tok::r_brace)) ||
272+
NextLine.First->is(tok::r_brace)) ||
271273
(Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly &&
272274
TheLine->Level != 0);
273275

@@ -312,73 +314,75 @@ class LineJoiner {
312314
return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
313315
}
314316
// 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) &&
316318
TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
317319
return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
318320
? tryMergeSimpleBlock(I, E, Limit)
319321
: 0;
320322
}
321323
// 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+
}
357360
}
358361
// Don't merge block with left brace wrapped after ObjC special blocks
359362
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();
362366
if (kwId == clang::tok::objc_autoreleasepool ||
363367
kwId == clang::tok::objc_synchronized)
364368
return 0;
365369
}
366370
// Don't merge block with left brace wrapped after case labels
367371
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))
369373
return 0;
370374

371375
// Don't merge an empty template class or struct if SplitEmptyRecords
372376
// is defined.
373377
if (Style.BraceWrapping.SplitEmptyRecord &&
374378
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;
377381
if (Previous) {
378382
if (Previous->is(tok::comment))
379383
Previous = Previous->getPreviousNonComment();
380384
if (Previous) {
381-
if (Previous->is(tok::greater) && !I[-1]->InPPDirective)
385+
if (Previous->is(tok::greater) && !PreviousLine.InPPDirective)
382386
return 0;
383387
if (Previous->is(tok::identifier)) {
384388
const FormatToken *PreviousPrevious =
@@ -401,21 +405,21 @@ class LineJoiner {
401405
}
402406
if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
403407
ShouldMerge = !Style.BraceWrapping.AfterClass ||
404-
(I[1]->First->is(tok::r_brace) &&
408+
(NextLine.First->is(tok::r_brace) &&
405409
!Style.BraceWrapping.SplitEmptyRecord);
406410
} else if (Tok->is(tok::kw_enum)) {
407411
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
408412
} else {
409413
ShouldMerge = !Style.BraceWrapping.AfterFunction ||
410-
(I[1]->First->is(tok::r_brace) &&
414+
(NextLine.First->is(tok::r_brace) &&
411415
!Style.BraceWrapping.SplitEmptyFunction);
412416
}
413417
return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
414418
}
415419
// 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) &&
417421
Style.BraceWrapping.AfterFunction) {
418-
if (I[1]->Last->is(TT_LineComment))
422+
if (NextLine.Last->is(TT_LineComment))
419423
return 0;
420424

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

0 commit comments

Comments
 (0)