https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/71672
From 4c5525b88a4f191270fa363a2c62699fd5c1fa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <[email protected]> Date: Sat, 27 Sep 2025 22:32:12 +0200 Subject: [PATCH 1/4] [clang-format][NFC] Introduce isNotOneOf And apply throughout the code base. --- clang/lib/Format/ContinuationIndenter.cpp | 34 ++++----- clang/lib/Format/FormatToken.h | 12 ++- .../lib/Format/NamespaceEndCommentsFixer.cpp | 2 +- clang/lib/Format/TokenAnnotator.cpp | 76 +++++++++---------- clang/lib/Format/UnwrappedLineFormatter.cpp | 8 +- clang/lib/Format/UnwrappedLineParser.cpp | 40 +++++----- clang/lib/Format/WhitespaceManager.cpp | 4 +- 7 files changed, 90 insertions(+), 86 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 9413c13a4137e..21e1856e639d7 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 && @@ -1576,7 +1576,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { if (Previous.is(tok::r_paren) && Previous.isNot(TT_TableGenDAGArgOperatorToBreak) && !Current.isBinaryOperator() && - !Current.isOneOf(tok::colon, tok::comment)) { + Current.isNotOneOf(tok::colon, tok::comment)) { return ContinuationIndent; } if (Current.is(TT_ProtoExtensionLSquare)) @@ -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 || @@ -686,8 +686,8 @@ class LineJoiner { } Limit = limitConsideringMacros(I + 1, E, Limit); AnnotatedLine &Line = **I; - if (Line.First->isNot(tok::kw_do) && Line.First->isNot(tok::kw_else) && - Line.Last->isNot(tok::kw_else) && Line.Last->isNot(tok::r_paren)) { + if (Line.First->isNotOneOf(tok::kw_do, tok::kw_else) && + Line.Last->isNotOneOf(tok::kw_else, tok::r_paren)) { return 0; } // Only merge `do while` if `do` is the only statement on the line. diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 2c9766c9b7bc0..e5759eb100ded 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -584,7 +584,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { ProbablyBracedList = ProbablyBracedList || (NextTok->is(tok::identifier) && - !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)); + PrevTok->isNotOneOf(tok::semi, tok::r_brace, tok::l_brace)); ProbablyBracedList = ProbablyBracedList || (NextTok->is(tok::semi) && @@ -607,7 +607,7 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { // A statement can end with only `;` (simple statement), a block // closing brace (compound statement), or `:` (label statement). // If PrevTok is a block opening brace, Tok ends an empty block. - !PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) { + PrevTok->isNotOneOf(tok::semi, BK_Block, tok::colon)) { ProbablyBracedList = true; } } @@ -1233,22 +1233,22 @@ void UnwrappedLineParser::parsePPUnknown() { static bool tokenCanStartNewLine(const FormatToken &Tok) { // Semicolon can be a null-statement, l_square can be a start of a macro or // a C++11 attribute, but this doesn't seem to be common. - return !Tok.isOneOf(tok::semi, tok::l_brace, - // Tokens that can only be used as binary operators and a - // part of overloaded operator names. - tok::period, tok::periodstar, tok::arrow, tok::arrowstar, - tok::less, tok::greater, tok::slash, tok::percent, - tok::lessless, tok::greatergreater, tok::equal, - tok::plusequal, tok::minusequal, tok::starequal, - tok::slashequal, tok::percentequal, tok::ampequal, - tok::pipeequal, tok::caretequal, tok::greatergreaterequal, - tok::lesslessequal, - // Colon is used in labels, base class lists, initializer - // lists, range-based for loops, ternary operator, but - // should never be the first token in an unwrapped line. - tok::colon, - // 'noexcept' is a trailing annotation. - tok::kw_noexcept); + return Tok.isNotOneOf( + tok::semi, tok::l_brace, + // Tokens that can only be used as binary operators and a + // part of overloaded operator names. + tok::period, tok::periodstar, tok::arrow, tok::arrowstar, tok::less, + tok::greater, tok::slash, tok::percent, tok::lessless, + tok::greatergreater, tok::equal, tok::plusequal, tok::minusequal, + tok::starequal, tok::slashequal, tok::percentequal, tok::ampequal, + tok::pipeequal, tok::caretequal, tok::greatergreaterequal, + tok::lesslessequal, + // Colon is used in labels, base class lists, initializer lists, + // range-based for loops, ternary operator, but should never be the first + // token in an unwrapped line. + tok::colon, + // 'noexcept' is a trailing annotation. + tok::kw_noexcept); } static bool mustBeJSIdent(const AdditionalKeywords &Keywords, @@ -4885,8 +4885,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) { const auto *Next = Tokens->peekNextToken(); if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(*Next)) || (Style.isTableGen() && - !Next->isOneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef, - tok::pp_ifndef, tok::pp_endif))) { + Next->isNotOneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef, + tok::pp_ifndef, tok::pp_endif))) { break; } distributeComments(Comments, FormatTok); diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 30c06bbb4d071..4be3f61bc05c1 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -462,8 +462,8 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, if ((Style.PointerAlignment == FormatStyle::PAS_Right || Style.ReferenceAlignment == FormatStyle::RAS_Right) && CurrentChange.Spaces != 0 && - !CurrentChange.Tok->isOneOf(tok::equal, tok::r_paren, - TT_TemplateCloser)) { + CurrentChange.Tok->isNotOneOf(tok::equal, tok::r_paren, + TT_TemplateCloser)) { const bool ReferenceNotRightAligned = Style.ReferenceAlignment != FormatStyle::RAS_Right && Style.ReferenceAlignment != FormatStyle::RAS_Pointer; From 362238be101198618f9ead895a97e018dee85b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <[email protected]> Date: Sun, 28 Sep 2025 23:13:07 +0200 Subject: [PATCH 2/4] Port missed calls --- clang/lib/Format/ContinuationIndenter.cpp | 24 ++++---- clang/lib/Format/Format.cpp | 2 +- clang/lib/Format/FormatToken.cpp | 4 +- clang/lib/Format/FormatTokenLexer.cpp | 4 +- clang/lib/Format/MacroExpander.cpp | 2 +- .../lib/Format/NamespaceEndCommentsFixer.cpp | 2 +- .../ObjCPropertyAttributeOrderFixer.cpp | 2 +- clang/lib/Format/QualifierAlignmentFixer.cpp | 2 +- clang/lib/Format/SortJavaScriptImports.cpp | 4 +- clang/lib/Format/TokenAnnotator.cpp | 59 ++++++++++--------- clang/lib/Format/UnwrappedLineParser.cpp | 37 ++++++------ 11 files changed, 73 insertions(+), 69 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 21e1856e639d7..ee90949570bd0 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -368,7 +368,7 @@ bool ContinuationIndenter::canBreak(const LineState &State) { // If binary operators are moved to the next line (including commas for some // styles of constructor initializers), that's always ok. - if (!Current.isOneOf(TT_BinaryOperator, tok::comma) && + if (Current.isNotOneOf(TT_BinaryOperator, tok::comma) && // Allow breaking opening brace of lambdas (when passed as function // arguments) to a new line when BeforeLambdaBody brace wrapping is // enabled. @@ -445,7 +445,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { (!Style.BreakBeforeTernaryOperators && Previous.is(TT_ConditionalExpr))) && CurrentState.BreakBeforeParameter && !Current.isTrailingComment() && - !Current.isOneOf(tok::r_paren, tok::r_brace)) { + Current.isNotOneOf(tok::r_paren, tok::r_brace)) { return true; } if (CurrentState.IsChainedConditional && @@ -648,7 +648,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // into the ColumnLimit, they are checked here in the ContinuationIndenter. if (Style.ColumnLimit != 0 && Previous.is(BK_Block) && Previous.is(tok::l_brace) && - !Current.isOneOf(tok::r_brace, tok::comment)) { + Current.isNotOneOf(tok::r_brace, tok::comment)) { return true; } @@ -882,9 +882,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) { return true; } - const auto *Previous = Tok.Previous; - if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen, - TT_LambdaDefinitionLParen) && + if (const auto *Previous = Tok.Previous; + !Previous || (Previous->isNotOneOf(TT_FunctionDeclarationLParen, + TT_LambdaDefinitionLParen) && !IsFunctionCallParen(*Previous))) { return true; } @@ -962,7 +962,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, if (Current.isNot(tok::comment) && P && (P->isOneOf(TT_BinaryOperator, tok::comma) || (P->is(TT_ConditionalExpr) && P->is(tok::colon))) && - !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) && + P->isNotOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) && P->getPrecedence() != prec::Assignment && P->getPrecedence() != prec::Relational && P->getPrecedence() != prec::Spaceship) { @@ -992,7 +992,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, // parameter, i.e. let nested calls have a continuation indent. CurrentState.LastSpace = State.Column; CurrentState.NestedBlockIndent = State.Column; - } else if (!Current.isOneOf(tok::comment, tok::caret) && + } else if (Current.isNotOneOf(tok::comment, tok::caret) && ((Previous.is(tok::comma) && Previous.isNot(TT_OverloadedOperator)) || (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) { @@ -1099,7 +1099,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, if (Current.isNot(TT_LambdaArrow) && (!Style.isJavaScript() || Current.NestingLevel != 0 || !PreviousNonComment || PreviousNonComment->isNot(tok::equal) || - !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) { + Current.isNotOneOf(Keywords.kw_async, Keywords.kw_function))) { CurrentState.NestedBlockIndent = State.Column; } @@ -1591,7 +1591,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { NextNonComment->SpacesRequiredBefore; } if (CurrentState.Indent == State.FirstIndent && PreviousNonComment && - !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) { + PreviousNonComment->isNotOneOf(tok::r_brace, TT_CtorInitializerComma)) { // Ensure that we fall back to the continuation indent width instead of // just flushing continuations left. return CurrentState.Indent + Style.ContinuationIndentWidth; @@ -1758,7 +1758,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, State.StartOfStringLiteral = State.Column + 1; } else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) { State.StartOfStringLiteral = State.Column; - } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && + } else if (Current.isNotOneOf(tok::comment, tok::identifier, tok::hash) && !Current.isStringLiteral()) { State.StartOfStringLiteral = 0; } @@ -2057,7 +2057,7 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, // array literals as these follow different indentation rules. bool NoLineBreak = Current.Children.empty() && - !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) && + Current.isNotOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) && (CurrentState.NoLineBreak || CurrentState.NoLineBreakInOperand || (Current.is(TT_TemplateOpener) && CurrentState.ContainsUnwrappedBuilder)); diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 835071dbe715d..6f571f1e085e4 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2435,7 +2435,7 @@ class BracesRemover : public TokenAnalyzer { const auto *NextLine = I + 1 == End ? nullptr : I[1]; for (const auto *Token = Line->First; Token && !Token->Finalized; Token = Token->Next) { - if (!Token->Optional || !Token->isOneOf(tok::l_brace, tok::r_brace)) + if (!Token->Optional || Token->isNotOneOf(tok::l_brace, tok::r_brace)) continue; auto *Next = Token->Next; assert(Next || Token == Line->Last); diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index c60ae8f0d2852..a534b6f04475d 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -108,7 +108,7 @@ unsigned CommaSeparatedList::formatAfterToken(LineState &State, // Ensure that we start on the opening brace. const FormatToken *LBrace = State.NextToken->Previous->getPreviousNonComment(); - if (!LBrace || !LBrace->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || + if (!LBrace || LBrace->isNotOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || LBrace->is(BK_Block) || LBrace->is(TT_DictLiteral) || LBrace->Next->is(TT_DesignatedInitializerPeriod)) { return 0; @@ -177,7 +177,7 @@ static unsigned CodePointsBetween(const FormatToken *Begin, void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { // FIXME: At some point we might want to do this for other lists, too. if (!Token->MatchingParen || - !Token->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) { + Token->isNotOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) { return; } diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 3f4aa52a87d2e..163dcac350c35 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -733,7 +733,7 @@ void FormatTokenLexer::tryParseJavaTextBlock() { // its text if successful. void FormatTokenLexer::tryParseJSRegexLiteral() { FormatToken *RegexToken = Tokens.back(); - if (!RegexToken->isOneOf(tok::slash, tok::slashequal)) + if (RegexToken->isNotOneOf(tok::slash, tok::slashequal)) return; FormatToken *Prev = nullptr; @@ -1041,7 +1041,7 @@ void FormatTokenLexer::handleTemplateStrings() { void FormatTokenLexer::tryParsePythonComment() { FormatToken *HashToken = Tokens.back(); - if (!HashToken->isOneOf(tok::hash, tok::hashhash)) + if (HashToken->isNotOneOf(tok::hash, tok::hashhash)) return; // Turn the remainder of this line into a comment. const char *CommentBegin = diff --git a/clang/lib/Format/MacroExpander.cpp b/clang/lib/Format/MacroExpander.cpp index 85a53c9bb12fe..d4ab933e554ad 100644 --- a/clang/lib/Format/MacroExpander.cpp +++ b/clang/lib/Format/MacroExpander.cpp @@ -86,7 +86,7 @@ class MacroExpander::DefinitionParser { } bool parseExpansion() { - if (!Current->isOneOf(tok::equal, tok::eof)) + if (Current->isNotOneOf(tok::equal, tok::eof)) return false; if (Current->is(tok::equal)) nextToken(); diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp index 6ecd09428da09..a563b4b6d4a63 100644 --- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp @@ -70,7 +70,7 @@ std::string computeName(const FormatToken *NamespaceTok) { // and closing parenthesis or comma. assert(Tok && Tok->is(tok::l_paren) && "expected an opening parenthesis"); Tok = Tok->getNextNonComment(); - while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) { + while (Tok && Tok->isNotOneOf(tok::r_paren, tok::comma)) { name += Tok->TokenText; Tok = Tok->getNextNonComment(); } diff --git a/clang/lib/Format/ObjCPropertyAttributeOrderFixer.cpp b/clang/lib/Format/ObjCPropertyAttributeOrderFixer.cpp index b885942efcb55..563196581bcf2 100644 --- a/clang/lib/Format/ObjCPropertyAttributeOrderFixer.cpp +++ b/clang/lib/Format/ObjCPropertyAttributeOrderFixer.cpp @@ -61,7 +61,7 @@ void ObjCPropertyAttributeOrderFixer::sortPropertyAttributes( } // Most attributes look like identifiers, but `class` is a keyword. - if (!Tok->isOneOf(tok::identifier, tok::kw_class)) { + if (Tok->isNotOneOf(tok::identifier, tok::kw_class)) { // If we hit any other kind of token, just bail. return; } diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp index 441a37a4902b7..c56dd42b52222 100644 --- a/clang/lib/Format/QualifierAlignmentFixer.cpp +++ b/clang/lib/Format/QualifierAlignmentFixer.cpp @@ -508,7 +508,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft( // Don't change declarations such as // `foo(struct Foo const a);` -> `foo(struct Foo const a);` - if (!Previous || !Previous->isOneOf(tok::kw_struct, tok::kw_class)) { + if (!Previous || Previous->isNotOneOf(tok::kw_struct, tok::kw_class)) { insertQualifierBefore(SourceMgr, Fixes, TypeToken, Qualifier); removeToken(SourceMgr, Fixes, Tok); } diff --git a/clang/lib/Format/SortJavaScriptImports.cpp b/clang/lib/Format/SortJavaScriptImports.cpp index ace3dffebec40..a0e3d374c49d9 100644 --- a/clang/lib/Format/SortJavaScriptImports.cpp +++ b/clang/lib/Format/SortJavaScriptImports.cpp @@ -439,7 +439,7 @@ class JavaScriptImportSorter : public TokenAnalyzer { // for grammar EBNF (production ModuleItem). bool parseModuleReference(const AdditionalKeywords &Keywords, JsModuleReference &Reference) { - if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export)) + if (!Current || Current->isNotOneOf(Keywords.kw_import, tok::kw_export)) return false; Reference.IsExport = Current->is(tok::kw_export); @@ -570,7 +570,7 @@ class JavaScriptImportSorter : public TokenAnalyzer { Symbol.Range.setEnd(Current->Tok.getLocation()); Reference.Symbols.push_back(Symbol); - if (!Current->isOneOf(tok::r_brace, tok::comma)) + if (Current->isNotOneOf(tok::r_brace, tok::comma)) return false; } Reference.SymbolsEnd = Current->Tok.getLocation(); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 8235f357af74f..c044b7c7deb23 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -203,7 +203,7 @@ class AnnotatingParser { return false; } if (InExpr && SeenTernaryOperator && - (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) { + (!Next || Next->isNotOneOf(tok::l_paren, tok::l_brace))) { return false; } if (!MaybeAngles) @@ -704,7 +704,7 @@ class AnnotatingParser { !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates && IsCpp && !IsCpp11AttributeSpecifier && !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && - !CurrentToken->isOneOf(tok::l_brace, tok::r_square) && + CurrentToken->isNotOneOf(tok::l_brace, tok::r_square) && (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, tok::kw_return, tok::kw_throw) || @@ -1339,7 +1339,7 @@ class AnnotatingParser { if (Style.isJavaScript()) { if (Contexts.back().ColonIsForRangeExpr || // colon in for loop (Contexts.size() == 1 && // switch/case labels - !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || + Line.First->isNotOneOf(tok::kw_enum, tok::kw_case)) || Contexts.back().ContextKind == tok::l_paren || // function params Contexts.back().ContextKind == tok::l_square || // array type (!Contexts.back().IsExpression && @@ -1416,7 +1416,7 @@ class AnnotatingParser { } else if (Contexts.back().ColonIsForRangeExpr) { Tok->setType(TT_RangeBasedForLoopColon); for (auto *Token = Prev; - Token && !Token->isOneOf(tok::semi, tok::l_paren); + Token && Token->isNotOneOf(tok::semi, tok::l_paren); Token = Token->Previous) { if (Token->isPointerOrReference()) Token->setFinalizedType(TT_PointerOrReference); @@ -1567,7 +1567,8 @@ class AnnotatingParser { if (Line.MustBeDeclaration && Contexts.size() == 1 && !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) && !Line.startsWith(tok::l_paren) && - !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) { + Tok->isNotOneOf(TT_TypeDeclarationParen, + TT_RequiresExpressionLParen)) { if (!Prev || (!Prev->isAttribute() && Prev->isNotOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation, @@ -1733,8 +1734,9 @@ class AnnotatingParser { // cond ? id : "B"; // cond ? cond2 ? "A" : "B" : "C"; if (!Contexts.back().IsExpression && Line.MustBeDeclaration && - (!Next || !Next->isOneOf(tok::identifier, tok::string_literal) || - !Next->Next || !Next->Next->isOneOf(tok::colon, tok::question))) { + (!Next || Next->isNotOneOf(tok::identifier, tok::string_literal) || + !Next->Next || + Next->Next->isNotOneOf(tok::colon, tok::question))) { Tok->setType(TT_CSharpNullable); break; } @@ -1801,7 +1803,7 @@ class AnnotatingParser { if (!parseTableGenValue()) return false; } else if (Tok->isOneOf(Keywords.kw_def, Keywords.kw_defm) && - (!Next || !Next->isOneOf(tok::colon, tok::l_brace))) { + (!Next || Next->isNotOneOf(tok::colon, tok::l_brace))) { // The case NameValue appears. if (!parseTableGenValue(true)) return false; @@ -2235,8 +2237,8 @@ class AnnotatingParser { // type or non-type. if (Contexts.back().ContextKind == tok::less) { assert(Current.Previous->Previous); - return !Current.Previous->Previous->isOneOf(tok::kw_typename, - tok::kw_class); + return Current.Previous->Previous->isNotOneOf(tok::kw_typename, + tok::kw_class); } Tok = Tok->MatchingParen; @@ -2663,7 +2665,7 @@ class AnnotatingParser { if (PreviousNotConst->is(TT_TemplateCloser)) { return PreviousNotConst && PreviousNotConst->MatchingParen && PreviousNotConst->MatchingParen->Previous && - !PreviousNotConst->MatchingParen->Previous->isOneOf( + PreviousNotConst->MatchingParen->Previous->isNotOneOf( tok::period, tok::kw_template); } @@ -2923,7 +2925,7 @@ class AnnotatingParser { const bool NextIsAmpOrStar = AfterRParen->isOneOf(tok::amp, tok::star); if (!(AfterRParen->isUnaryOperator() || NextIsAmpOrStar) || AfterRParen->is(tok::plus) || - !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) { + AfterRParen->Next->isNotOneOf(tok::identifier, tok::numeric_constant)) { return false; } @@ -3862,7 +3864,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts, // Find parentheses of parameter list. if (Current.is(tok::kw_operator)) { if (Previous.Tok.getIdentifierInfo() && - !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) { + Previous.isNotOneOf(tok::kw_return, tok::kw_co_return)) { return true; } if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) { @@ -4580,7 +4582,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, } // co_await (x), co_yield (x), co_return (x) if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) && - !Right.isOneOf(tok::semi, tok::r_paren)) { + Right.isNotOneOf(tok::semi, tok::r_paren)) { return true; } @@ -4657,7 +4659,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, return getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left; } - return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) && + return Left.isNotOneOf(TT_PointerOrReference, tok::l_paren) && (getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left || (Line.IsMultiVariableDeclStmt && @@ -4730,7 +4732,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, const auto *LParen = Right.Next->MatchingParen; return !LParen || LParen->isNot(TT_FunctionTypeLParen); } - return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square); + return BeforeLeft->isNotOneOf(tok::l_paren, tok::l_square); } // Ensure right pointer alignment with ellipsis e.g. int *...P if (Left.is(tok::ellipsis) && BeforeLeft && @@ -4895,7 +4897,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName || spaceRequiredBeforeParens(Right); } - if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) { + if (!BeforeLeft || BeforeLeft->isNotOneOf(tok::period, tok::arrow)) { if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch)) { return Style.SpaceBeforeParensOptions.AfterControlStatements || spaceRequiredBeforeParens(Right); @@ -5300,7 +5302,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, // Add space between things in a primitive's state table unless in a // transition like `(0?)`. if ((Left.is(TT_VerilogTableItem) && - !Right.isOneOf(tok::r_paren, tok::semi)) || + Right.isNotOneOf(tok::r_paren, tok::semi)) || (Right.is(TT_VerilogTableItem) && Left.isNot(tok::l_paren))) { const FormatToken *Next = Right.getNextNonComment(); return !(Next && Next->is(tok::r_paren)); @@ -5349,8 +5351,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, // previous rule. if ((Right.is(Keywords.kw_apostrophe) || (Right.is(BK_BracedInit) && Right.is(tok::l_brace))) && - !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) || - Keywords.isVerilogWordOperator(Left)) && + Left.isNotOneOf(Keywords.kw_assign, Keywords.kw_unique) && + !Keywords.isVerilogWordOperator(Left) && (Left.isOneOf(tok::r_square, tok::r_paren, tok::r_brace, tok::numeric_constant) || Keywords.isWordLike(Left))) { @@ -5568,7 +5570,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, } // Space before TT_StructuredBindingLSquare. if (Right.is(TT_StructuredBindingLSquare)) { - return !Left.isOneOf(tok::amp, tok::ampamp) || + return Left.isNotOneOf(tok::amp, tok::ampamp) || getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right; } // Space before & or && following a TT_StructuredBindingLSquare. @@ -5618,7 +5620,7 @@ isEmptyLambdaAllowed(const FormatToken &Tok, static bool isAllmanLambdaBrace(const FormatToken &Tok) { return Tok.is(tok::l_brace) && Tok.is(BK_Block) && - !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral); + Tok.isNotOneOf(TT_ObjCBlockLBrace, TT_DictLiteral); } bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, @@ -5687,7 +5689,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, tok::kw_const) && // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match // above. - !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) { + Line.First->isNotOneOf(Keywords.kw_var, Keywords.kw_let)) { // Object literals on the top level of a file are treated as "enum-style". // Each key/value pair is put on a separate line, instead of bin-packing. return true; @@ -5832,7 +5834,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, } if (Right.is(tok::comment)) { - return !Left.isOneOf(BK_BracedInit, TT_CtorInitializerColon) && + return Left.isNotOneOf(BK_BracedInit, TT_CtorInitializerColon) && Right.NewlinesBefore > 0 && Right.HasUnescapedNewline; } if (Left.isTrailingComment()) @@ -5874,7 +5876,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, case FormatStyle::RCPS_WithPreceding: return Right.isNot(tok::semi); case FormatStyle::RCPS_OwnLineWithBrace: - return !Right.isOneOf(tok::semi, tok::l_brace); + return Right.isNotOneOf(tok::semi, tok::l_brace); default: break; } @@ -6001,7 +6003,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.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) && + Right.isNotOneOf(TT_LeadingJavaAnnotation, tok::l_paren) && (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) { return true; } @@ -6379,7 +6381,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, } 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) && + if (Left.is(tok::equal) && + Right.isNotOneOf(tok::kw_default, tok::kw_delete) && Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) { return false; } @@ -6449,7 +6452,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const)) return true; if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) && - !Left.isOneOf(tok::arrowstar, tok::lessless) && + Left.isNotOneOf(tok::arrowstar, tok::lessless) && Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All && (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None || Left.getPrecedence() == prec::Assignment)) { diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index e5759eb100ded..5267278148f02 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -405,7 +405,8 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace, case tok::r_brace: if (OpeningBrace) { if (!Style.RemoveBracesLLVM || Line->InPPDirective || - !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) { + OpeningBrace->isNotOneOf(TT_ControlStatementLBrace, + TT_ElseLBrace)) { return false; } if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel || @@ -427,7 +428,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace, unsigned StoredPosition = Tokens->getPosition(); auto *Next = Tokens->getNextNonComment(); FormatTok = Tokens->setPosition(StoredPosition); - if (!Next->isOneOf(tok::colon, tok::arrow)) { + if (Next->isNotOneOf(tok::colon, tok::arrow)) { // default not followed by `:` or `->` is not a case label; treat it // like an identifier. parseStructuralElement(); @@ -1157,7 +1158,7 @@ void UnwrappedLineParser::parsePPDefine() { IncludeGuard = IG_Defined; IncludeGuardToken = nullptr; for (auto &Line : Lines) { - if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { + if (Line.Tokens.front().Tok->isNotOneOf(tok::comment, tok::hash)) { IncludeGuard = IG_Rejected; break; } @@ -1256,7 +1257,7 @@ static bool mustBeJSIdent(const AdditionalKeywords &Keywords, // FIXME: This returns true for C/C++ keywords like 'struct'. return FormatTok->is(tok::identifier) && (!FormatTok->Tok.getIdentifierInfo() || - !FormatTok->isOneOf( + FormatTok->isNotOneOf( Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, @@ -1322,7 +1323,7 @@ static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next, return false; if (!isC78Type(*Tok) && - !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) { + Tok->isNotOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) { return false; } @@ -1345,7 +1346,7 @@ bool UnwrappedLineParser::parseModuleImport() { if (auto Token = Tokens->peekNextToken(/*SkipComment=*/true); !Token->Tok.getIdentifierInfo() && - !Token->isOneOf(tok::colon, tok::less, tok::string_literal)) { + Token->isNotOneOf(tok::colon, tok::less, tok::string_literal)) { return false; } @@ -1357,7 +1358,7 @@ bool UnwrappedLineParser::parseModuleImport() { // Handle import <foo/bar.h> as we would an include statement. else if (FormatTok->is(tok::less)) { nextToken(); - while (!FormatTok->isOneOf(tok::semi, tok::greater) && !eof()) { + while (FormatTok->isNotOneOf(tok::semi, tok::greater) && !eof()) { // Mark tokens up to the trailing line comments as implicit string // literals. if (FormatTok->isNot(tok::comment) && @@ -2389,8 +2390,8 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() { nextToken(); if (Previous) { if (Previous->Tok.getIdentifierInfo() && - !Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield, - tok::kw_co_return)) { + Previous->isNotOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield, + tok::kw_co_return)) { return false; } if (Previous->closesScope()) { @@ -2400,7 +2401,7 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() { const auto *BeforeRParen = Previous->getPreviousNonComment(); // Lambdas can be cast to function types only, e.g. `std::function<int()>` // and `int (*)()`. - if (!BeforeRParen || !BeforeRParen->isOneOf(tok::greater, tok::r_paren)) + if (!BeforeRParen || BeforeRParen->isNotOneOf(tok::greater, tok::r_paren)) return false; } } @@ -2449,7 +2450,7 @@ void UnwrappedLineParser::tryToParseJSFunction() { if (FormatTok->is(tok::l_brace)) tryToParseBracedList(); else - while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) + while (FormatTok->isNotOneOf(tok::l_brace, tok::semi) && !eof()) nextToken(); } @@ -3107,11 +3108,11 @@ void UnwrappedLineParser::parseTryCatch() { for (bool SeenCatch = false;;) { if (FormatTok->is(tok::at)) nextToken(); - if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, - tok::kw___finally, tok::objc_catch, - tok::objc_finally) || - ((Style.isJava() || Style.isJavaScript()) && - FormatTok->is(Keywords.kw_finally)))) { + if (FormatTok->isNotOneOf(tok::kw_catch, Keywords.kw___except, + tok::kw___finally, tok::objc_catch, + tok::objc_finally) && + !((Style.isJava() || Style.isJavaScript()) && + FormatTok->is(Keywords.kw_finally))) { break; } if (FormatTok->is(tok::kw_catch)) @@ -3289,7 +3290,7 @@ void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) { Keywords.kw_repeat))) && "'for', 'while' or foreach macro expected"); const bool KeepBraces = !Style.RemoveBracesLLVM || - !FormatTok->isOneOf(tok::kw_for, tok::kw_while); + FormatTok->isNotOneOf(tok::kw_for, tok::kw_while); nextToken(); // JS' for await ( ... @@ -4338,7 +4339,7 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { // to the terminating `;`. For everything else, just return and continue // parsing the structural element, i.e. the declaration or expression for // `export default`. - if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && + if (!IsImport && FormatTok->isNotOneOf(tok::l_brace, tok::star) && !FormatTok->isStringLiteral() && !(FormatTok->is(Keywords.kw_type) && Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) { From 349e25f605890f199624d95eefbb649db87d1c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <[email protected]> Date: Mon, 29 Sep 2025 22:50:57 +0200 Subject: [PATCH 3/4] More missed spots --- clang/lib/Format/TokenAnnotator.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index c044b7c7deb23..3b0af16bec1dd 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2273,7 +2273,7 @@ class AnnotatingParser { if (!Line.startsWith(TT_UnaryOperator)) { for (FormatToken *Previous = Current.Previous; Previous && Previous->Previous && - !Previous->Previous->isOneOf(tok::comma, tok::semi); + Previous->Previous->isNotOneOf(tok::comma, tok::semi); Previous = Previous->Previous) { if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) { Previous = Previous->MatchingParen; @@ -2437,7 +2437,8 @@ class AnnotatingParser { Current.setType(TT_BinaryOperator); } else if (Current.is(tok::arrow) && AutoFound && Line.MightBeFunctionDecl && Current.NestingLevel == 0 && - !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) { + Current.Previous->isNotOneOf(tok::kw_operator, + tok::identifier)) { // not auto operator->() -> xxx; Current.setType(TT_TrailingReturnArrow); } else if (Current.is(tok::arrow) && Current.Previous && From d7f7fb64557159d6c77deefc84f413f45ca86ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <[email protected]> Date: Wed, 24 Sep 2025 23:02:39 +0200 Subject: [PATCH 4/4] [clang-format] Remove special handling of comments after brace/paren Fixes http://llvm.org/PR55487 (#55487) The code did not match the documentation about Cpp11BracedListStyle. Changed handling of comments after opening braces, which are supposedly function call like to behave exactly like their parenthesis counter part. --- clang/lib/Format/ContinuationIndenter.cpp | 5 +- clang/lib/Format/TokenAnnotator.cpp | 13 +-- clang/unittests/Format/FormatTest.cpp | 18 ++-- clang/unittests/Format/FormatTestComments.cpp | 85 +++++++++++++++++-- 4 files changed, 94 insertions(+), 27 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index ee90949570bd0..05b9f8b19f1c3 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -925,7 +925,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, TT_TableGenDAGArgOpenerToBreak) && !(Current.MacroParent && Previous.MacroParent) && (Current.isNot(TT_LineComment) || - Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) && + (Previous.is(BK_BracedInit) && + (!Style.Cpp11BracedListStyle || !Previous.Previous || + Previous.Previous->isNotOneOf(tok::identifier, tok::l_paren))) || + Previous.is(TT_VerilogMultiLineListLParen)) && !IsInTemplateString(Current)) { CurrentState.Indent = State.Column + Spaces; CurrentState.IsAligned = true; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 3b0af16bec1dd..61fdd62312583 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4102,16 +4102,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const { for (auto *Current = First->Next; Current; Current = Current->Next) { const FormatToken *Prev = Current->Previous; if (Current->is(TT_LineComment)) { - if (Prev->is(BK_BracedInit) && Prev->opensScope()) { - Current->SpacesRequiredBefore = - (Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other) - ? 0 - : 1; - } else if (Prev->is(TT_VerilogMultiLineListLParen)) { - Current->SpacesRequiredBefore = 0; - } else { - Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; - } + Current->SpacesRequiredBefore = Prev->is(TT_VerilogMultiLineListLParen) + ? 0 + : Style.SpacesBeforeTrailingComments; // If we find a trailing comment, iterate backwards to determine whether // it seems to relate to a specific parameter. If so, break before that diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 6a3385a56f53e..f03e31327db99 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -14291,20 +14291,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " CDDDP83848_RBR_REGISTER};", NoBinPacking); - // FIXME: The alignment of these trailing comments might be bad. Then again, - // this might be utterly useless in real code. verifyFormat("Constructor::Constructor()\n" - " : some_value{ //\n" - " aaaaaaa, //\n" - " bbbbbbb} {}"); + " : some_value{ //\n" + " aaaaaaa, //\n" + " bbbbbbb} {}"); // In braced lists, the first comment is always assumed to belong to the // first element. Thus, it can be moved to the next or previous line as // appropriate. - verifyFormat("function({// First element:\n" - " 1,\n" - " // Second element:\n" - " 2});", + verifyFormat("function({ // First element:\n" + " 1,\n" + " // Second element:\n" + " 2});", "function({\n" " // First element:\n" " 1,\n" @@ -14426,7 +14424,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); verifyFormat("vector< int > x{ // comment 1\n" - " 1, 2, 3, 4 };", + " 1, 2, 3, 4 };", SpaceBetweenBraces); SpaceBetweenBraces.ColumnLimit = 20; verifyFormat("vector< int > x{\n" diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index 69026bce98705..f0aff7947b58a 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -1474,12 +1474,12 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) { verifyFormat("S s = {{a, b, c}, // Group #1\n" " {d, e, f}, // Group #2\n" " {g, h, i}}; // Group #3"); - verifyFormat("S s = {{// Group #1\n" - " a, b, c},\n" - " {// Group #2\n" - " d, e, f},\n" - " {// Group #3\n" - " g, h, i}};"); + verifyFormat("S s = {{ // Group #1\n" + " a, b, c},\n" + " { // Group #2\n" + " d, e, f},\n" + " { // Group #3\n" + " g, h, i}};"); EXPECT_EQ("S s = {\n" " // Some comment\n" @@ -4699,6 +4699,79 @@ TEST_F(FormatTestComments, SplitCommentIntroducers) { getLLVMStyleWithColumns(10))); } +TEST_F(FormatTestComments, LineCommentsOnStartOfFunctionCall) { + auto Style = getLLVMStyle(); + + EXPECT_TRUE(Style.Cpp11BracedListStyle); + + verifyFormat("T foo( // Comment\n" + " arg);", + Style); + + verifyFormat("T bar{ // Comment\n" + " arg};", + Style); + + verifyFormat("T baz({ // Comment\n" + " arg});", + Style); + + verifyFormat("T baz{{ // Comment\n" + " arg}};", + Style); + + verifyFormat("T b0z(f( // Comment\n" + " arg));", + Style); + + verifyFormat("T b0z(F{ // Comment\n" + " arg});", + Style); + + verifyFormat("func( // Comment\n" + " arg);", + Style); + + verifyFormat("func({ // Comment\n" + " arg});", + Style); + + Style.Cpp11BracedListStyle = false; + + verifyFormat("T foo( // Comment\n" + " arg);", + Style); + + verifyFormat("T bar{ // Comment\n" + " arg\n" + "};", + Style); + + verifyFormat("T baz({ // Comment\n" + " arg });", + Style); + + verifyFormat("T baz{ { // Comment\n" + " arg } };", + Style); + + verifyFormat("T b0z(f( // Comment\n" + " arg));", + Style); + + verifyFormat("T b0z(F{ // Comment\n" + " arg });", + Style); + + verifyFormat("func( // Comment\n" + " arg);", + Style); + + verifyFormat("func({ // Comment\n" + " arg });", + Style); +} + } // end namespace } // namespace test } // end namespace format _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
