[clang] [clang-format][NFC] Maximize usage of isOneOf() in TokenAnnotator (PR #151658)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 1 00:03:12 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/151658
Also make a StringRef literal `static constexpr`.
>From 4ada74523ecab8e5cdeaabf1769ee85461cc5c55 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 31 Jul 2025 23:57:57 -0700
Subject: [PATCH] [clang-format][NFC] Maximize usage of isOneOf() in
TokenAnnotator
Also make a StringRef literal `static constexpr`.
---
.../Format/IntegerLiteralSeparatorFixer.cpp | 2 +-
clang/lib/Format/TokenAnnotator.cpp | 48 +++++++++----------
2 files changed, 23 insertions(+), 27 deletions(-)
diff --git a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
index 7772a5619c4bd..b51991bfeff4b 100644
--- a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
+++ b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
@@ -117,7 +117,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
}
if (Style.isCpp()) {
// Hex alpha digits a-f/A-F must be at the end of the string literal.
- StringRef Suffixes = "_himnsuyd";
+ static constexpr StringRef Suffixes("_himnsuyd");
if (const auto Pos =
Text.find_first_of(IsBase16 ? Suffixes.drop_back() : Suffixes);
Pos != StringRef::npos) {
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d28d2fd375fb3..4801d27b1395a 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1939,7 +1939,7 @@ class AnnotatingParser {
Contexts.back().IsExpression = true;
next();
if (CurrentToken)
- CurrentToken->SpacesRequiredBefore = true;
+ CurrentToken->SpacesRequiredBefore = 1;
parseLine();
break;
default:
@@ -2639,8 +2639,8 @@ class AnnotatingParser {
if (PreviousNotConst->is(TT_TemplateCloser)) {
return PreviousNotConst && PreviousNotConst->MatchingParen &&
PreviousNotConst->MatchingParen->Previous &&
- PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
- PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
+ !PreviousNotConst->MatchingParen->Previous->isOneOf(
+ tok::period, tok::kw_template);
}
if ((PreviousNotConst->is(tok::r_paren) &&
@@ -3369,7 +3369,7 @@ class ExpressionParser {
Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) {
return prec::Relational;
}
- if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
+ if (Current->isOneOf(TT_BinaryOperator, tok::comma))
return Current->getPrecedence();
if (Current->isOneOf(tok::period, tok::arrow) &&
Current->isNot(TT_TrailingReturnArrow)) {
@@ -4314,8 +4314,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(tok::coloncolon))
return Style.PenaltyBreakScopeResolution;
- if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
- Right.is(tok::kw_operator)) {
+ if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
+ tok::kw_operator)) {
if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
return 3;
if (Left.is(TT_StartOfName))
@@ -4757,7 +4757,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Previous) {
if (Previous->endsSequence(tok::kw_operator))
return Style.PointerAlignment != FormatStyle::PAS_Left;
- if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile)) {
+ if (Previous->isOneOf(tok::kw_const, tok::kw_volatile)) {
return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
(Style.SpaceAroundPointerQualifiers ==
FormatStyle::SAPQ_After) ||
@@ -4931,8 +4931,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
}
if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
Left.MatchingParen->Previous &&
- (Left.MatchingParen->Previous->is(tok::period) ||
- Left.MatchingParen->Previous->is(tok::coloncolon))) {
+ Left.MatchingParen->Previous->isOneOf(tok::period, tok::coloncolon)) {
// Java call to generic function with explicit type:
// A.<B<C<...>>>DoSomething();
// A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
@@ -5207,8 +5206,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
// (e.g. as "const x of y" in a for loop), or after a destructuring
// operation (const [x, y] of z, const {a, b} of c).
(Left.is(Keywords.kw_of) && BeforeLeft &&
- (BeforeLeft->is(tok::identifier) ||
- BeforeLeft->isOneOf(tok::r_square, tok::r_brace)))) &&
+ BeforeLeft->isOneOf(tok::identifier, tok::r_square, tok::r_brace))) &&
(!BeforeLeft || BeforeLeft->isNot(tok::period))) {
return true;
}
@@ -5516,7 +5514,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return false;
}
if (Style.isJava() && Right.is(tok::coloncolon) &&
- (Left.is(tok::identifier) || Left.is(tok::kw_this))) {
+ Left.isOneOf(tok::identifier, tok::kw_this)) {
return false;
}
if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) {
@@ -5587,8 +5585,8 @@ static bool IsFunctionArgument(const FormatToken &Tok) {
}
static bool
-isItAnEmptyLambdaAllowed(const FormatToken &Tok,
- FormatStyle::ShortLambdaStyle ShortLambdaOption) {
+isEmptyLambdaAllowed(const FormatToken &Tok,
+ FormatStyle::ShortLambdaStyle ShortLambdaOption) {
return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
}
@@ -5808,8 +5806,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
}
if (Right.is(tok::comment)) {
- return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
- (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
+ return !Left.isOneOf(BK_BracedInit, TT_CtorInitializerColon) &&
+ Right.NewlinesBefore > 0 && Right.HasUnescapedNewline;
}
if (Left.isTrailingComment())
return true;
@@ -5977,7 +5975,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
// Put multiple Java annotation on a new line.
if ((Style.isJava() || Style.isJavaScript()) &&
Left.is(TT_LeadingJavaAnnotation) &&
- Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
+ !Right.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
(Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) {
return true;
}
@@ -6043,7 +6041,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
((LBrace->is(tok::l_brace) &&
(LBrace->is(TT_DictLiteral) ||
(LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
- LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
+ LBrace->isOneOf(TT_ArrayInitializerLSquare, tok::less))) {
// If Left.ParameterCount is 0, then this submessage entry is not the
// first in its parent submessage, and we want to break before this entry.
// If Left.ParameterCount is greater than 0, then its parent submessage
@@ -6257,9 +6255,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
}
if (Left.is(tok::question) && Right.is(tok::colon))
return false;
- if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
+ if (Right.isOneOf(TT_ConditionalExpr, tok::question))
return Style.BreakBeforeTernaryOperators;
- if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
+ if (Left.isOneOf(TT_ConditionalExpr, tok::question))
return !Style.BreakBeforeTernaryOperators;
if (Left.is(TT_InheritanceColon))
return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
@@ -6302,7 +6300,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
// submessage: { ... }
// submessage: < ... >
// repeated: [ ... ]
- if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
+ if ((Right.isOneOf(tok::l_brace, tok::less) &&
Right.is(TT_DictLiteral)) ||
Right.is(TT_ArrayInitializerLSquare)) {
return false;
@@ -6352,10 +6350,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
Right.getPrecedence() != prec::Assignment)) {
return true;
}
- if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
- Left.is(tok::kw_operator)) {
+ if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator, tok::kw_operator))
return false;
- }
if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) {
return false;
@@ -6440,9 +6436,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
if (isAllmanLambdaBrace(Left))
- return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
+ return !isEmptyLambdaAllowed(Left, ShortLambdaOption);
if (isAllmanLambdaBrace(Right))
- return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
+ return !isEmptyLambdaAllowed(Right, ShortLambdaOption);
}
if (Right.is(tok::kw_noexcept) && Right.is(TT_TrailingAnnotation)) {
More information about the cfe-commits
mailing list