-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-format][NFC] Maximize usage of isOneOf() in TokenAnnotator #151658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Also make a StringRef literal `static constexpr`.
Member
|
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesAlso make a StringRef literal Full diff: https://github.com/llvm/llvm-project/pull/151658.diff 2 Files Affected:
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)) {
|
HazardyKnusperkeks
approved these changes
Aug 2, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Also make a StringRef literal
static constexpr.