Skip to content

Commit 72b2c37

Browse files
committed
[clang-format][NFC] Clean up TokenAnnotator::mustBreakBefore()
1 parent 2ece5cc commit 72b2c37

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5405,6 +5405,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54055405
return true;
54065406
}
54075407

5408+
const auto *BeforeLeft = Left.Previous;
5409+
const auto *AfterRight = Right.Next;
5410+
54085411
if (Style.isCSharp()) {
54095412
if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
54105413
Style.BraceWrapping.AfterFunction) {
@@ -5416,7 +5419,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54165419
}
54175420
if (Right.is(TT_CSharpGenericTypeConstraint))
54185421
return true;
5419-
if (Right.Next && Right.Next->is(TT_FatArrow) &&
5422+
if (AfterRight && AfterRight->is(TT_FatArrow) &&
54205423
(Right.is(tok::numeric_constant) ||
54215424
(Right.is(tok::identifier) && Right.TokenText == "_"))) {
54225425
return true;
@@ -5433,15 +5436,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54335436
Left.is(tok::r_square) && Right.is(tok::l_square)) {
54345437
return true;
54355438
}
5436-
54375439
} else if (Style.isJavaScript()) {
54385440
// FIXME: This might apply to other languages and token kinds.
5439-
if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
5440-
Left.Previous->is(tok::string_literal)) {
5441+
if (Right.is(tok::string_literal) && Left.is(tok::plus) && BeforeLeft &&
5442+
BeforeLeft->is(tok::string_literal)) {
54415443
return true;
54425444
}
54435445
if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
5444-
Left.Previous && Left.Previous->is(tok::equal) &&
5446+
BeforeLeft && BeforeLeft->is(tok::equal) &&
54455447
Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
54465448
tok::kw_const) &&
54475449
// kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
@@ -5460,8 +5462,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54605462
// instead of bin-packing.
54615463
return true;
54625464
}
5463-
if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
5464-
Left.Previous->is(TT_FatArrow)) {
5465+
if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && BeforeLeft &&
5466+
BeforeLeft->is(TT_FatArrow)) {
54655467
// JS arrow function (=> {...}).
54665468
switch (Style.AllowShortLambdasOnASingleLine) {
54675469
case FormatStyle::SLS_All:
@@ -5489,8 +5491,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54895491
FormatStyle::SFS_InlineOnly);
54905492
}
54915493
} else if (Style.Language == FormatStyle::LK_Java) {
5492-
if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
5493-
Right.Next->is(tok::string_literal)) {
5494+
if (Right.is(tok::plus) && Left.is(tok::string_literal) && AfterRight &&
5495+
AfterRight->is(tok::string_literal)) {
54945496
return true;
54955497
}
54965498
} else if (Style.isVerilog()) {
@@ -5543,8 +5545,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
55435545
}
55445546
return Style.BreakArrays;
55455547
}
5546-
}
5547-
if (Style.isTableGen()) {
5548+
} else if (Style.isTableGen()) {
55485549
// Break the comma in side cond operators.
55495550
// !cond(case1:1,
55505551
// case2:0);
@@ -5600,8 +5601,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
56005601
return true;
56015602
if (Left.IsUnterminatedLiteral)
56025603
return true;
5603-
if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
5604-
Right.Next->is(tok::string_literal)) {
5604+
if (Right.is(tok::lessless) && AfterRight && Left.is(tok::string_literal) &&
5605+
AfterRight->is(tok::string_literal)) {
56055606
return true;
56065607
}
56075608
if (Right.is(TT_RequiresClause)) {
@@ -5678,8 +5679,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
56785679
// string literal accordingly. Thus, we try keep existing line breaks.
56795680
return Right.IsMultiline && Right.NewlinesBefore > 0;
56805681
}
5681-
if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
5682-
Left.Previous->is(tok::equal))) &&
5682+
if ((Left.is(tok::l_brace) ||
5683+
(Left.is(tok::less) && BeforeLeft && BeforeLeft->is(tok::equal))) &&
56835684
Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
56845685
// Don't put enums or option definitions onto single lines in protocol
56855686
// buffers.
@@ -5793,7 +5794,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
57935794
//
57945795
// We ensure elsewhere that extensions are always on their own line.
57955796
if (Style.isProto() && Right.is(TT_SelectorName) &&
5796-
Right.isNot(tok::r_square) && Right.Next) {
5797+
Right.isNot(tok::r_square) && AfterRight) {
57975798
// Keep `@submessage` together in:
57985799
// @submessage { key: value }
57995800
if (Left.is(tok::at))
@@ -5802,7 +5803,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
58025803
// selector { ...
58035804
// selector: { ...
58045805
// selector: @base { ...
5805-
FormatToken *LBrace = Right.Next;
5806+
const auto *LBrace = AfterRight;
58065807
if (LBrace && LBrace->is(tok::colon)) {
58075808
LBrace = LBrace->Next;
58085809
if (LBrace && LBrace->is(tok::at)) {

0 commit comments

Comments
 (0)