Skip to content

Conversation

HazardyKnusperkeks
Copy link
Contributor

And apply throughout the code base.

@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2025

@llvm/pr-subscribers-clang-format

Author: Björn Schäpers (HazardyKnusperkeks)

Changes

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 --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]

@owenca
Copy link
Contributor

owenca commented Sep 28, 2025

Running grep in clang/lib/Format/ gives the following (likely) missed ones:

$ grep -nw isOneOf *.cpp | grep \!
ContinuationIndenter.cpp:371:  if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
ContinuationIndenter.cpp:448:      !Current.isOneOf(tok::r_paren, tok::r_brace)) {
ContinuationIndenter.cpp:651:      !Current.isOneOf(tok::r_brace, tok::comment)) {
ContinuationIndenter.cpp:886:    if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
ContinuationIndenter.cpp:965:      !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
ContinuationIndenter.cpp:995:  } else if (!Current.isOneOf(tok::comment, tok::caret) &&
ContinuationIndenter.cpp:1102:       !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) {
ContinuationIndenter.cpp:1594:      !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) {
ContinuationIndenter.cpp:1761:  } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
ContinuationIndenter.cpp:2060:      !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
Format.cpp:2438:        if (!Token->Optional || !Token->isOneOf(tok::l_brace, tok::r_brace))
FormatToken.cpp:111:  if (!LBrace || !LBrace->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
FormatToken.cpp:180:      !Token->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
FormatTokenLexer.cpp:736:  if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
FormatTokenLexer.cpp:1044:  if (!HashToken->isOneOf(tok::hash, tok::hashhash))
MacroExpander.cpp:89:    if (!Current->isOneOf(tok::equal, tok::eof))
NamespaceEndCommentsFixer.cpp:73:    while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
ObjCPropertyAttributeOrderFixer.cpp:64:    if (!Tok->isOneOf(tok::identifier, tok::kw_class)) {
QualifierAlignmentFixer.cpp:511:    if (!Previous || !Previous->isOneOf(tok::kw_struct, tok::kw_class)) {
SortJavaScriptImports.cpp:442:    if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
SortJavaScriptImports.cpp:573:      if (!Current->isOneOf(tok::r_brace, tok::comma))
TokenAnnotator.cpp:206:              (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
TokenAnnotator.cpp:254:      if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
TokenAnnotator.cpp:707:        !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
TokenAnnotator.cpp:1342:             !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
TokenAnnotator.cpp:1419:             Token && !Token->isOneOf(tok::semi, tok::l_paren);
TokenAnnotator.cpp:1570:          !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
TokenAnnotator.cpp:1736:            (!Next || !Next->isOneOf(tok::identifier, tok::string_literal) ||
TokenAnnotator.cpp:1737:             !Next->Next || !Next->Next->isOneOf(tok::colon, tok::question))) {
TokenAnnotator.cpp:1804:                   (!Next || !Next->isOneOf(tok::colon, tok::l_brace))) {
TokenAnnotator.cpp:2238:          return !Current.Previous->Previous->isOneOf(tok::kw_typename,
TokenAnnotator.cpp:2274:             !Previous->Previous->isOneOf(tok::comma, tok::semi);
TokenAnnotator.cpp:2438:               !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
TokenAnnotator.cpp:2666:             !PreviousNotConst->MatchingParen->Previous->isOneOf(
TokenAnnotator.cpp:2926:        !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
TokenAnnotator.cpp:3137:      return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
TokenAnnotator.cpp:3865:        !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
TokenAnnotator.cpp:4583:      !Right.isOneOf(tok::semi, tok::r_paren)) {
TokenAnnotator.cpp:4660:    return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
TokenAnnotator.cpp:4733:    return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
TokenAnnotator.cpp:4898:    if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
TokenAnnotator.cpp:5303:         !Right.isOneOf(tok::r_paren, tok::semi)) ||
TokenAnnotator.cpp:5352:        !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
TokenAnnotator.cpp:5571:    return !Left.isOneOf(tok::amp, tok::ampamp) ||
TokenAnnotator.cpp:5621:         !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
TokenAnnotator.cpp:5690:        !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
TokenAnnotator.cpp:5835:    return !Left.isOneOf(BK_BracedInit, TT_CtorInitializerColon) &&
TokenAnnotator.cpp:5877:      return !Right.isOneOf(tok::semi, tok::l_brace);
TokenAnnotator.cpp:6004:      !Right.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
TokenAnnotator.cpp:6382:  if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
TokenAnnotator.cpp:6452:      !Left.isOneOf(tok::arrowstar, tok::lessless) &&
UnwrappedLineFormatter.cpp:780:    if (!Style.isJava() && Line.First->isOneOf(tok::at, tok::minus, tok::plus))
UnwrappedLineParser.cpp:408:            !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
UnwrappedLineParser.cpp:430:      if (!Next->isOneOf(tok::colon, tok::arrow)) {
UnwrappedLineParser.cpp:1160:      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
UnwrappedLineParser.cpp:1259:          !FormatTok->isOneOf(
UnwrappedLineParser.cpp:1325:      !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
UnwrappedLineParser.cpp:1348:      !Token->isOneOf(tok::colon, tok::less, tok::string_literal)) {
UnwrappedLineParser.cpp:1360:      while (!FormatTok->isOneOf(tok::semi, tok::greater) && !eof()) {
UnwrappedLineParser.cpp:2392:        !Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
UnwrappedLineParser.cpp:2403:      if (!BeforeRParen || !BeforeRParen->isOneOf(tok::greater, tok::r_paren))
UnwrappedLineParser.cpp:2452:      while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
UnwrappedLineParser.cpp:3110:    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
UnwrappedLineParser.cpp:3292:                          !FormatTok->isOneOf(tok::kw_for, tok::kw_while);
UnwrappedLineParser.cpp:4341:  if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&

Comment on lines 1883 to 1885
(!Next ||
Next->isNotOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
kw_input, kw_output, kw_sequence)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For pointers, I prefer !(Next && Next->isOneOf(A, B) to !Next || Next->isNotOneOf(A, B) because the Next && part is just for avoiding dereferencing a null pointer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the ! to be more in the inner parenthesis, and !A || B reads also nice as that is the canonical representation of the implication. If Next exists, it is not one of ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Next exists, it is not one of ...

Isn't it really "if Next doesn't exist or (if it exists and) it is not one of ..."?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logically equivalent.
If you look at the diff, the implication is quite common.
But if that's the last thing, I can change it back.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if that's the last thing, I can change it back.

Yes, please. This is also the style in isVerilogHierarchy() above. We can have another NFC patch for whichever style the majority of active clang-format contributors wants.

Right now, we have both styles in the clang-format codebase. For example:

!(Tok && Tok->is(tok::comma))
!(Tok && Tok->isOneOf(tok::comma, tok::period))

vs.

(!Tok || Tok->isNot(tok::comma))
(!Tok || Tok->isNotOneOf(tok::comma, tok::period))

@mydeveloperday @sstwcw @gedare do you have a preference on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. They are the same to me. Though I find the name isNotOneOf to be slightly harder to read than isNoneOf.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also have no strong preference, although I probably tend toward using && with NULL ptr checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. They are the same to me. Though I find the name isNotOneOf to be slightly harder to read than isNoneOf.

That sounds better, yes!

@HazardyKnusperkeks
Copy link
Contributor Author

Running grep in clang/lib/Format/ gives the following (likely) missed ones:

$ grep -nw isOneOf *.cpp | grep \!
ContinuationIndenter.cpp:371:  if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
ContinuationIndenter.cpp:448:      !Current.isOneOf(tok::r_paren, tok::r_brace)) {
ContinuationIndenter.cpp:651:      !Current.isOneOf(tok::r_brace, tok::comment)) {
ContinuationIndenter.cpp:886:    if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
ContinuationIndenter.cpp:965:      !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
ContinuationIndenter.cpp:995:  } else if (!Current.isOneOf(tok::comment, tok::caret) &&
ContinuationIndenter.cpp:1102:       !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) {
ContinuationIndenter.cpp:1594:      !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) {
ContinuationIndenter.cpp:1761:  } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
ContinuationIndenter.cpp:2060:      !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
Format.cpp:2438:        if (!Token->Optional || !Token->isOneOf(tok::l_brace, tok::r_brace))
FormatToken.cpp:111:  if (!LBrace || !LBrace->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
FormatToken.cpp:180:      !Token->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
FormatTokenLexer.cpp:736:  if (!RegexToken->isOneOf(tok::slash, tok::slashequal))
FormatTokenLexer.cpp:1044:  if (!HashToken->isOneOf(tok::hash, tok::hashhash))
MacroExpander.cpp:89:    if (!Current->isOneOf(tok::equal, tok::eof))
NamespaceEndCommentsFixer.cpp:73:    while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) {
ObjCPropertyAttributeOrderFixer.cpp:64:    if (!Tok->isOneOf(tok::identifier, tok::kw_class)) {
QualifierAlignmentFixer.cpp:511:    if (!Previous || !Previous->isOneOf(tok::kw_struct, tok::kw_class)) {
SortJavaScriptImports.cpp:442:    if (!Current || !Current->isOneOf(Keywords.kw_import, tok::kw_export))
SortJavaScriptImports.cpp:573:      if (!Current->isOneOf(tok::r_brace, tok::comma))
TokenAnnotator.cpp:206:              (!Next || !Next->isOneOf(tok::l_paren, tok::l_brace))) {
TokenAnnotator.cpp:254:      if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
TokenAnnotator.cpp:707:        !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
TokenAnnotator.cpp:1342:             !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
TokenAnnotator.cpp:1419:             Token && !Token->isOneOf(tok::semi, tok::l_paren);
TokenAnnotator.cpp:1570:          !Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
TokenAnnotator.cpp:1736:            (!Next || !Next->isOneOf(tok::identifier, tok::string_literal) ||
TokenAnnotator.cpp:1737:             !Next->Next || !Next->Next->isOneOf(tok::colon, tok::question))) {
TokenAnnotator.cpp:1804:                   (!Next || !Next->isOneOf(tok::colon, tok::l_brace))) {
TokenAnnotator.cpp:2238:          return !Current.Previous->Previous->isOneOf(tok::kw_typename,
TokenAnnotator.cpp:2274:             !Previous->Previous->isOneOf(tok::comma, tok::semi);
TokenAnnotator.cpp:2438:               !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
TokenAnnotator.cpp:2666:             !PreviousNotConst->MatchingParen->Previous->isOneOf(
TokenAnnotator.cpp:2926:        !AfterRParen->Next->isOneOf(tok::identifier, tok::numeric_constant)) {
TokenAnnotator.cpp:3137:      return !token || token->isOneOf(tok::amp, tok::period, tok::arrow,
TokenAnnotator.cpp:3865:        !Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
TokenAnnotator.cpp:4583:      !Right.isOneOf(tok::semi, tok::r_paren)) {
TokenAnnotator.cpp:4660:    return !Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
TokenAnnotator.cpp:4733:    return !BeforeLeft->isOneOf(tok::l_paren, tok::l_square);
TokenAnnotator.cpp:4898:    if (!BeforeLeft || !BeforeLeft->isOneOf(tok::period, tok::arrow)) {
TokenAnnotator.cpp:5303:         !Right.isOneOf(tok::r_paren, tok::semi)) ||
TokenAnnotator.cpp:5352:        !(Left.isOneOf(Keywords.kw_assign, Keywords.kw_unique) ||
TokenAnnotator.cpp:5571:    return !Left.isOneOf(tok::amp, tok::ampamp) ||
TokenAnnotator.cpp:5621:         !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
TokenAnnotator.cpp:5690:        !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) {
TokenAnnotator.cpp:5835:    return !Left.isOneOf(BK_BracedInit, TT_CtorInitializerColon) &&
TokenAnnotator.cpp:5877:      return !Right.isOneOf(tok::semi, tok::l_brace);
TokenAnnotator.cpp:6004:      !Right.isOneOf(TT_LeadingJavaAnnotation, tok::l_paren) &&
TokenAnnotator.cpp:6382:  if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
TokenAnnotator.cpp:6452:      !Left.isOneOf(tok::arrowstar, tok::lessless) &&
UnwrappedLineFormatter.cpp:780:    if (!Style.isJava() && Line.First->isOneOf(tok::at, tok::minus, tok::plus))
UnwrappedLineParser.cpp:408:            !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
UnwrappedLineParser.cpp:430:      if (!Next->isOneOf(tok::colon, tok::arrow)) {
UnwrappedLineParser.cpp:1160:      if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
UnwrappedLineParser.cpp:1259:          !FormatTok->isOneOf(
UnwrappedLineParser.cpp:1325:      !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
UnwrappedLineParser.cpp:1348:      !Token->isOneOf(tok::colon, tok::less, tok::string_literal)) {
UnwrappedLineParser.cpp:1360:      while (!FormatTok->isOneOf(tok::semi, tok::greater) && !eof()) {
UnwrappedLineParser.cpp:2392:        !Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
UnwrappedLineParser.cpp:2403:      if (!BeforeRParen || !BeforeRParen->isOneOf(tok::greater, tok::r_paren))
UnwrappedLineParser.cpp:2452:      while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
UnwrappedLineParser.cpp:3110:    if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
UnwrappedLineParser.cpp:3292:                          !FormatTok->isOneOf(tok::kw_for, tok::kw_while);
UnwrappedLineParser.cpp:4341:  if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&

Don't know anymore what my search was, but I'm a bit embarrassed to have missed that much...

Comment on lines 1883 to 1885
(!Next ||
Next->isNotOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
kw_input, kw_output, kw_sequence)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Next exists, it is not one of ...

Isn't it really "if Next doesn't exist or (if it exists and) it is not one of ..."?

@@ -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) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should have Line.beginsWith(), Line.beingWithOneOf() and Line.doesNotBeingWithOneOf() I get a bit blind after a while in this code...

@HazardyKnusperkeks HazardyKnusperkeks changed the title [clang-format][NFC] Introduce isNotOneOf [clang-format][NFC] Introduce isNoneOf Oct 1, 2025
@HazardyKnusperkeks
Copy link
Contributor Author

Renamed, rebased, and squased.

Copy link

github-actions bot commented Oct 1, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

And apply throughout the code base.
@HazardyKnusperkeks HazardyKnusperkeks merged commit 847e1e1 into llvm:main Oct 2, 2025
8 checks passed
@HazardyKnusperkeks HazardyKnusperkeks deleted the isNotOneOf branch October 2, 2025 18:52
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
And apply throughout the code base.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants