Skip to content

Commit d696f81

Browse files
authored
[clang-format][NFC] Maximize usage of isOneOf() in TokenAnnotator (#151658)
Also make a StringRef literal `static constexpr`.
1 parent 39c3066 commit d696f81

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
117117
}
118118
if (Style.isCpp()) {
119119
// Hex alpha digits a-f/A-F must be at the end of the string literal.
120-
StringRef Suffixes = "_himnsuyd";
120+
static constexpr StringRef Suffixes("_himnsuyd");
121121
if (const auto Pos =
122122
Text.find_first_of(IsBase16 ? Suffixes.drop_back() : Suffixes);
123123
Pos != StringRef::npos) {

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ class AnnotatingParser {
19391939
Contexts.back().IsExpression = true;
19401940
next();
19411941
if (CurrentToken)
1942-
CurrentToken->SpacesRequiredBefore = true;
1942+
CurrentToken->SpacesRequiredBefore = 1;
19431943
parseLine();
19441944
break;
19451945
default:
@@ -2639,8 +2639,8 @@ class AnnotatingParser {
26392639
if (PreviousNotConst->is(TT_TemplateCloser)) {
26402640
return PreviousNotConst && PreviousNotConst->MatchingParen &&
26412641
PreviousNotConst->MatchingParen->Previous &&
2642-
PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
2643-
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
2642+
!PreviousNotConst->MatchingParen->Previous->isOneOf(
2643+
tok::period, tok::kw_template);
26442644
}
26452645

26462646
if ((PreviousNotConst->is(tok::r_paren) &&
@@ -3369,7 +3369,7 @@ class ExpressionParser {
33693369
Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
33703370
return prec::Relational;
33713371
}
3372-
if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
3372+
if (Current->isOneOf(TT_BinaryOperator, tok::comma))
33733373
return Current->getPrecedence();
33743374
if (Current->isOneOf(tok::period, tok::arrow) &&
33753375
Current->isNot(TT_TrailingReturnArrow)) {
@@ -4314,8 +4314,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
43144314

43154315
if (Left.is(tok::coloncolon))
43164316
return Style.PenaltyBreakScopeResolution;
4317-
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4318-
Right.is(tok::kw_operator)) {
4317+
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
4318+
tok::kw_operator)) {
43194319
if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
43204320
return 3;
43214321
if (Left.is(TT_StartOfName))
@@ -4757,7 +4757,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
47574757
if (Previous) {
47584758
if (Previous->endsSequence(tok::kw_operator))
47594759
return Style.PointerAlignment != FormatStyle::PAS_Left;
4760-
if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
4760+
if (Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
47614761
return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
47624762
(Style.SpaceAroundPointerQualifiers ==
47634763
FormatStyle::SAPQ_After) ||
@@ -4931,8 +4931,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
49314931
}
49324932
if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
49334933
Left.MatchingParen->Previous &&
4934-
(Left.MatchingParen->Previous->is(tok::period) ||
4935-
Left.MatchingParen->Previous->is(tok::coloncolon))) {
4934+
Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
49364935
// Java call to generic function with explicit type:
49374936
// A.<B<C<...>>>DoSomething();
49384937
// A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
@@ -5207,8 +5206,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
52075206
// (e.g. as "const x of y" in a for loop), or after a destructuring
52085207
// operation (const [x, y] of z, const {a, b} of c).
52095208
(Left.is(Keywords.kw_of) && BeforeLeft &&
5210-
(BeforeLeft->is(tok::identifier) ||
5211-
BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
5209+
BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
52125210
(!BeforeLeft || BeforeLeft->isNot(tok::period))) {
52135211
return true;
52145212
}
@@ -5516,7 +5514,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
55165514
return false;
55175515
}
55185516
if (Style.isJava() && Right.is(tok::coloncolon) &&
5519-
(Left.is(tok::identifier) || Left.is(tok::kw_this))) {
5517+
Left.isOneOf(tok::identifier, tok::kw_this)) {
55205518
return false;
55215519
}
55225520
if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
@@ -5587,8 +5585,8 @@ static bool IsFunctionArgument(const FormatToken &Tok) {
55875585
}
55885586

55895587
static bool
5590-
isItAnEmptyLambdaAllowed(const FormatToken &Tok,
5591-
FormatStyle::ShortLambdaStyle ShortLambdaOption) {
5588+
isEmptyLambdaAllowed(const FormatToken &Tok,
5589+
FormatStyle::ShortLambdaStyle ShortLambdaOption) {
55925590
return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
55935591
}
55945592

@@ -5808,8 +5806,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
58085806
}
58095807

58105808
if (Right.is(tok::comment)) {
5811-
return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
5812-
(Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
5809+
return !Left.isOneOf(BK_BracedInit, TT_CtorInitializerColon) &&
5810+
Right.NewlinesBefore > 0 && Right.HasUnescapedNewline;
58135811
}
58145812
if (Left.isTrailingComment())
58155813
return true;
@@ -5977,7 +5975,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
59775975
// Put multiple Java annotation on a new line.
59785976
if ((Style.isJava() || Style.isJavaScript()) &&
59795977
Left.is(TT_LeadingJavaAnnotation) &&
5980-
Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
5978+
!Right.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
59815979
(Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
59825980
return true;
59835981
}
@@ -6043,7 +6041,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
60436041
((LBrace->is(tok::l_brace) &&
60446042
(LBrace->is(TT_DictLiteral) ||
60456043
(LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
6046-
LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
6044+
LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
60476045
// If Left.ParameterCount is 0, then this submessage entry is not the
60486046
// first in its parent submessage, and we want to break before this entry.
60496047
// If Left.ParameterCount is greater than 0, then its parent submessage
@@ -6257,9 +6255,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
62576255
}
62586256
if (Left.is(tok::question) && Right.is(tok::colon))
62596257
return false;
6260-
if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
6258+
if (Right.isOneOf(TT_ConditionalExpr, tok::question))
62616259
return Style.BreakBeforeTernaryOperators;
6262-
if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
6260+
if (Left.isOneOf(TT_ConditionalExpr, tok::question))
62636261
return !Style.BreakBeforeTernaryOperators;
62646262
if (Left.is(TT_InheritanceColon))
62656263
return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
@@ -6302,7 +6300,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
63026300
// submessage: { ... }
63036301
// submessage: < ... >
63046302
// repeated: [ ... ]
6305-
if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
6303+
if ((Right.isOneOf(tok::l_brace, tok::less) &&
63066304
Right.is(TT_DictLiteral)) ||
63076305
Right.is(TT_ArrayInitializerLSquare)) {
63086306
return false;
@@ -6352,10 +6350,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
63526350
Right.getPrecedence() != prec::Assignment)) {
63536351
return true;
63546352
}
6355-
if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
6356-
Left.is(tok::kw_operator)) {
6353+
if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
63576354
return false;
6358-
}
63596355
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
63606356
Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
63616357
return false;
@@ -6440,9 +6436,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
64406436
auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
64416437
if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
64426438
if (isAllmanLambdaBrace(Left))
6443-
return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
6439+
return !isEmptyLambdaAllowed(Left, ShortLambdaOption);
64446440
if (isAllmanLambdaBrace(Right))
6445-
return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
6441+
return !isEmptyLambdaAllowed(Right, ShortLambdaOption);
64466442
}
64476443

64486444
if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {

0 commit comments

Comments
 (0)