[clang] [clang-format][NFC] Introduce isNotOneOf (PR #161021)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 27 13:33:20 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Björn Schäpers (HazardyKnusperkeks)
<details>
<summary>Changes</summary>
And apply throughout the code base.
---
Patch is 25.35 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/161021.diff
7 Files Affected:
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+16-16)
- (modified) clang/lib/Format/FormatToken.h (+8-4)
- (modified) clang/lib/Format/NamespaceEndCommentsFixer.cpp (+1-1)
- (modified) clang/lib/Format/TokenAnnotator.cpp (+38-38)
- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+4-4)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+20-20)
- (modified) clang/lib/Format/WhitespaceManager.cpp (+2-2)
``````````diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 9413c13a4137e..6e1c8fcd00f02 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -523,9 +523,9 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
if (Style.AlwaysBreakBeforeMultilineStrings &&
(NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
Previous.is(tok::comma) || Current.NestingLevel < 2) &&
- !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at,
- Keywords.kw_dollar) &&
- !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
+ Previous.isNotOneOf(tok::kw_return, tok::lessless, tok::at,
+ Keywords.kw_dollar) &&
+ Previous.isNotOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
nextIsMultilineString(State)) {
return true;
}
@@ -752,7 +752,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return false;
const auto *Next = Comma->getNextNonComment();
- return Next && !Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
+ return Next && Next->isNotOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret);
};
if (DisallowLineBreaks())
@@ -835,7 +835,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
Style.Cpp11BracedListStyle;
};
- if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
+ if (Tok.isNotOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
!IsStartOfBracedList()) {
return false;
}
@@ -843,8 +843,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
return true;
if (Tok.Previous->isIf())
return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
- return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
- tok::kw_switch) &&
+ return Tok.Previous->isNotOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+ tok::kw_switch) &&
!(Style.isJavaScript() && Tok.Previous->is(Keywords.kw_await));
};
auto IsFunctionCallParen = [](const FormatToken &Tok) {
@@ -920,9 +920,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// align the commas with the opening paren.
if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
!CurrentState.IsCSharpGenericTypeConstraint && Previous.opensScope() &&
- Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
- Previous.isNot(TT_TableGenDAGArgOpener) &&
- Previous.isNot(TT_TableGenDAGArgOpenerToBreak) &&
+ Previous.isNotOneOf(TT_ObjCMethodExpr, TT_RequiresClause,
+ TT_TableGenDAGArgOpener,
+ TT_TableGenDAGArgOpenerToBreak) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
@@ -1239,11 +1239,11 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
}
if (PreviousNonComment &&
- !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
+ PreviousNonComment->isNotOneOf(tok::comma, tok::colon, tok::semi) &&
((PreviousNonComment->isNot(TT_TemplateCloser) &&
!PreviousNonComment->ClosesRequiresClause) ||
Current.NestingLevel != 0) &&
- !PreviousNonComment->isOneOf(
+ PreviousNonComment->isNotOneOf(
TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
TT_LeadingJavaAnnotation) &&
Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope() &&
@@ -1281,8 +1281,8 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
bool AllowAllConstructorInitializersOnNextLine =
Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine ||
Style.PackConstructorInitializers == FormatStyle::PCIS_NextLineOnly;
- if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
- PreviousIsBreakingCtorInitializerColon) ||
+ if ((Previous.isNotOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) &&
+ !PreviousIsBreakingCtorInitializerColon) ||
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
State.Line->MustBeDeclaration) ||
(!Style.AllowAllArgumentsOnNextLine &&
@@ -1734,8 +1734,8 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
}
if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) ||
(Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) &&
- !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr,
- TT_CtorInitializerColon)))) {
+ Previous->isNotOneOf(TT_DictLiteral, TT_ObjCMethodExpr,
+ TT_CtorInitializerColon)))) {
CurrentState.NestedBlockInlined =
!Newline && hasNestedBlockInlined(Previous, Current, Style);
}
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index e04b0e7af10c0..e69091e72bbc3 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -645,6 +645,9 @@ struct FormatToken {
return is(K1) || isOneOf(K2, Ks...);
}
template <typename T> bool isNot(T Kind) const { return !is(Kind); }
+ template <typename... Ts> bool isNotOneOf(Ts... Ks) const {
+ return !isOneOf(Ks...);
+ }
bool isIf(bool AllowConstexprMacro = true) const {
return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
@@ -748,8 +751,8 @@ struct FormatToken {
/// Returns \c true if this is a "." or "->" accessing a member.
bool isMemberAccess() const {
return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
- !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
- TT_LambdaArrow, TT_LeadingJavaAnnotation);
+ isNotOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
+ TT_LambdaArrow, TT_LeadingJavaAnnotation);
}
bool isPointerOrReference() const {
@@ -1877,8 +1880,9 @@ struct AdditionalKeywords {
// In Verilog the colon in a default label is optional.
return Tok.is(TT_CaseLabelColon) ||
(Tok.is(tok::kw_default) &&
- !(Next && Next->isOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
- kw_input, kw_output, kw_sequence)));
+ (!Next ||
+ Next->isNotOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
+ kw_input, kw_output, kw_sequence)));
}
/// Returns whether \p Tok is a Verilog keyword that starts a
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 08f8d6840fe00..6ecd09428da09 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -85,7 +85,7 @@ std::string computeName(const FormatToken *NamespaceTok) {
// one token before that up until the '{'. A '(' might be a macro with
// arguments.
const FormatToken *FirstNSTok = nullptr;
- while (Tok && !Tok->isOneOf(tok::l_brace, tok::coloncolon, tok::l_paren)) {
+ while (Tok && Tok->isNotOneOf(tok::l_brace, tok::coloncolon, tok::l_paren)) {
if (FirstNSTok)
FirstNSName += FirstNSTok->TokenText;
FirstNSTok = Tok;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 6a8286da73442..8235f357af74f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -577,7 +577,7 @@ class AnnotatingParser {
if (IsIf && CurrentToken->is(tok::semi)) {
for (auto *Tok = OpeningParen.Next;
Tok != CurrentToken &&
- !Tok->isOneOf(tok::equal, tok::l_paren, tok::l_brace);
+ Tok->isNotOneOf(tok::equal, tok::l_paren, tok::l_brace);
Tok = Tok->Next) {
if (Tok->isPointerOrReference())
Tok->setFinalizedType(TT_PointerOrReference);
@@ -1430,8 +1430,8 @@ class AnnotatingParser {
Scopes.back() == ST_Class)) {
Tok->setType(TT_BitFieldColon);
} else if (Contexts.size() == 1 &&
- !Line.getFirstNonComment()->isOneOf(tok::kw_enum, tok::kw_case,
- tok::kw_default) &&
+ Line.getFirstNonComment()->isNotOneOf(
+ tok::kw_enum, tok::kw_case, tok::kw_default) &&
!Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
Prev->ClosesRequiresClause) {
@@ -1570,8 +1570,8 @@ class AnnotatingParser {
!Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
if (!Prev ||
(!Prev->isAttribute() &&
- !Prev->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
- TT_BinaryOperator))) {
+ Prev->isNotOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation,
+ TT_BinaryOperator))) {
Line.MightBeFunctionDecl = true;
Tok->MightBeFunctionDeclParen = true;
}
@@ -1669,7 +1669,7 @@ class AnnotatingParser {
}
}
while (CurrentToken &&
- !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
+ CurrentToken->isNotOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
if (CurrentToken->isOneOf(tok::star, tok::amp))
CurrentToken->setType(TT_PointerOrReference);
auto Next = CurrentToken->getNextNonComment();
@@ -2099,7 +2099,7 @@ class AnnotatingParser {
// Reset token type in case we have already looked at it and then
// recovered from an error (e.g. failure to find the matching >).
if (!CurrentToken->isTypeFinalized() &&
- !CurrentToken->isOneOf(
+ CurrentToken->isNotOneOf(
TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
@@ -2516,7 +2516,7 @@ class AnnotatingParser {
Current.setType(TT_CastRParen);
if (Current.MatchingParen && Current.Next &&
!Current.Next->isBinaryOperator() &&
- !Current.Next->isOneOf(
+ Current.Next->isNotOneOf(
tok::semi, tok::colon, tok::l_brace, tok::l_paren, tok::comma,
tok::period, tok::arrow, tok::coloncolon, tok::kw_noexcept)) {
if (FormatToken *AfterParen = Current.MatchingParen->Next;
@@ -2574,9 +2574,9 @@ class AnnotatingParser {
} else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
tok::kw_requires) &&
Current.Previous &&
- !Current.Previous->isOneOf(tok::equal, tok::at,
- TT_CtorInitializerComma,
- TT_CtorInitializerColon) &&
+ Current.Previous->isNotOneOf(tok::equal, tok::at,
+ TT_CtorInitializerComma,
+ TT_CtorInitializerColon) &&
Line.MightBeFunctionDecl && Contexts.size() == 1) {
// Line.MightBeFunctionDecl can only be true after the parentheses of a
// function declaration have been found.
@@ -2785,8 +2785,8 @@ class AnnotatingParser {
// If there is an identifier (or with a few exceptions a keyword) right
// before the parentheses, this is unlikely to be a cast.
if (LeftOfParens->Tok.getIdentifierInfo() &&
- !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
- tok::kw_delete, tok::kw_throw)) {
+ LeftOfParens->isNotOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
+ tok::kw_delete, tok::kw_throw)) {
return false;
}
@@ -2953,7 +2953,7 @@ class AnnotatingParser {
// Search for unexpected tokens.
for (Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous)
- if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
+ if (Prev->isNotOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
return false;
return true;
@@ -3745,8 +3745,8 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
const bool InRequiresExpression = Line.Type == LT_RequiresExpression;
for (auto &Child : Line.Children) {
if (InRequiresExpression &&
- !Child->First->isOneOf(tok::kw_typename, tok::kw_requires,
- TT_CompoundRequirementLBrace)) {
+ Child->First->isNotOneOf(tok::kw_typename, tok::kw_requires,
+ TT_CompoundRequirementLBrace)) {
Child->Type = LT_SimpleRequirement;
}
annotate(*Child);
@@ -4329,9 +4329,9 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
// Slightly prefer formatting local lambda definitions like functions.
if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
return 35;
- if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
- TT_ArrayInitializerLSquare,
- TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
+ if (Right.isNotOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
+ TT_ArrayInitializerLSquare,
+ TT_DesignatedInitializerLSquare, TT_AttributeSquare)) {
return 500;
}
}
@@ -4520,7 +4520,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
const FormatToken &Left,
const FormatToken &Right) const {
if (Left.is(tok::kw_return) &&
- !Right.isOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
+ Right.isNotOneOf(tok::semi, tok::r_paren, tok::hashhash)) {
return true;
}
if (Left.is(tok::kw_throw) && Right.is(tok::l_paren) && Right.MatchingParen &&
@@ -4809,10 +4809,10 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
TT_LambdaLSquare)));
}
if (Right.is(tok::l_square) &&
- !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
- TT_DesignatedInitializerLSquare,
- TT_StructuredBindingLSquare, TT_AttributeSquare) &&
- !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
+ Right.isNotOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
+ TT_DesignatedInitializerLSquare,
+ TT_StructuredBindingLSquare, TT_AttributeSquare) &&
+ Left.isNotOneOf(tok::numeric_constant, TT_DictLiteral) &&
!(Left.isNot(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
Right.is(TT_ArraySubscriptLSquare))) {
return false;
@@ -4918,7 +4918,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Left.is(tok::at) && Right.isNot(tok::objc_not_keyword))
return false;
if (Right.is(TT_UnaryOperator)) {
- return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
+ return Left.isNotOneOf(tok::l_paren, tok::l_square, tok::at) &&
(Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
}
// No space between the variable name and the initializer list.
@@ -5261,8 +5261,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Left.is(tok::ellipsis))
return false;
if (Left.is(TT_TemplateCloser) &&
- !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
- Keywords.kw_implements, Keywords.kw_extends)) {
+ Right.isNotOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
+ Keywords.kw_implements, Keywords.kw_extends)) {
// Type assertions ('<type>expr') are not followed by whitespace. Other
// locations that should have whitespace following are identified by the
// above set of follower tokens.
@@ -5550,14 +5550,14 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return Right.hasWhitespaceBefore();
}
if (Right.is(tok::coloncolon) &&
- !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
+ Left.isNotOneOf(tok::l_brace, tok::comment, tok::l_paren)) {
// Put a space between < and :: in vector< ::std::string >
return (Left.is(TT_TemplateOpener) &&
((Style.Standard < FormatStyle::LS_Cpp11) ||
ShouldAddSpacesInAngles())) ||
- !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
- tok::kw___super, TT_TemplateOpener,
- TT_TemplateCloser)) ||
+ Left.isNotOneOf(tok::l_paren, tok::r_paren, tok::l_square,
+ tok::kw___super, TT_TemplateOpener,
+ TT_TemplateCloser) ||
(Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
}
if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
@@ -5600,7 +5600,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
// Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
static bool isAllmanBrace(const FormatToken &Tok) {
return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
- !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
+ Tok.isNotOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
}
// Returns 'true' if 'Tok' is a function argument.
@@ -6207,8 +6207,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
return false;
// Avoid to break after '(' in the cases that is in bang operators.
if (Right.is(tok::l_paren)) {
- return !Left.isOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
- TT_TemplateCloser);
+ return Left.isNotOneOf(TT_TableGenBangOperator, TT_TableGenCondOperator,
+ TT_TemplateCloser);
}
// Avoid to break between the value and its suffix part.
if (Left.is(TT_TableGenValueSuffix))
@@ -6295,8 +6295,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
}
if (Right.is(tok::colon) &&
- !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon,
- TT_BitFieldColon)) {
+ Right.isNotOneOf(TT_CtorInitializerColon, TT_InlineASMColon,
+ TT_BitFieldColon)) {
return false;
}
if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
@@ -6406,8 +6406,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
// Allow breaking after a trailing annotation, e.g. after a method
// declaration.
if (Left.is(TT_TrailingAnnotation)) {
- return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
- tok::less, tok::coloncolon);
+ return Right.isNotOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
+ tok::less, tok::coloncolon);
}
if (Right.isAttribute())
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index ac9d147defc13..95135d77a1523 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -506,8 +506,8 @@ class LineJoiner {
(NextLine.First->is(tok::r_brace) &&
!Style.BraceWrapping.SplitEmptyRecord);
} else if (TheLine->InPPDirective ||
- !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
- tok::kw_struct)) {
+ TheLine->First->isNotOneOf(tok::kw_class, tok::kw_enum,
+ tok::kw_struct)) {
// Try to merge a block with left brace unwrapped that wasn't yet
// covered.
ShouldMerge = !Style.BraceWrapping.AfterFunction ...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/161021
More information about the cfe-commits
mailing list