[clang] a887b95 - [clang-format] Turn global COperatorsFollowingVar vector into a switch

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 5 09:02:13 PST 2022


Author: Benjamin Kramer
Date: 2022-03-05T18:00:16+01:00
New Revision: a887b95edf34117b0274e5c9443ef6afa58b058a

URL: https://github.com/llvm/llvm-project/commit/a887b95edf34117b0274e5c9443ef6afa58b058a
DIFF: https://github.com/llvm/llvm-project/commit/a887b95edf34117b0274e5c9443ef6afa58b058a.diff

LOG: [clang-format] Turn global COperatorsFollowingVar vector into a switch

LLVM optimizes this into a bit test. NFCI.

Added: 
    

Modified: 
    clang/lib/Format/FormatToken.h
    clang/lib/Format/UnwrappedLineParser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index e72df7d377823..b00b8ab7b927c 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -134,34 +134,6 @@ namespace format {
   TYPE(CSharpGenericTypeConstraintComma)                                       \
   TYPE(Unknown)
 
-/// Sorted operators that can follow a C variable.
-static const std::vector<clang::tok::TokenKind> COperatorsFollowingVar = [] {
-  std::vector<clang::tok::TokenKind> ReturnVal = {
-      tok::l_square,     tok::r_square,
-      tok::l_paren,      tok::r_paren,
-      tok::r_brace,      tok::period,
-      tok::ellipsis,     tok::ampamp,
-      tok::ampequal,     tok::star,
-      tok::starequal,    tok::plus,
-      tok::plusplus,     tok::plusequal,
-      tok::minus,        tok::arrow,
-      tok::minusminus,   tok::minusequal,
-      tok::exclaim,      tok::exclaimequal,
-      tok::slash,        tok::slashequal,
-      tok::percent,      tok::percentequal,
-      tok::less,         tok::lessless,
-      tok::lessequal,    tok::lesslessequal,
-      tok::greater,      tok::greatergreater,
-      tok::greaterequal, tok::greatergreaterequal,
-      tok::caret,        tok::caretequal,
-      tok::pipe,         tok::pipepipe,
-      tok::pipeequal,    tok::question,
-      tok::semi,         tok::equal,
-      tok::equalequal,   tok::comma};
-  assert(std::is_sorted(ReturnVal.begin(), ReturnVal.end()));
-  return ReturnVal;
-}();
-
 /// Determines the semantic type of a syntactic token, e.g. whether "<" is a
 /// template opener or binary operator.
 enum TokenType : uint8_t {

diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 68b0e2a630bcd..646374c0cd626 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2816,6 +2816,57 @@ void UnwrappedLineParser::parseSwitch() {
     NestedTooDeep.pop_back();
 }
 
+// Operators that can follow a C variable.
+static bool isCOperatorFollowingVar(tok::TokenKind kind) {
+  switch (kind) {
+  case tok::ampamp:
+  case tok::ampequal:
+  case tok::arrow:
+  case tok::caret:
+  case tok::caretequal:
+  case tok::comma:
+  case tok::ellipsis:
+  case tok::equal:
+  case tok::equalequal:
+  case tok::exclaim:
+  case tok::exclaimequal:
+  case tok::greater:
+  case tok::greaterequal:
+  case tok::greatergreater:
+  case tok::greatergreaterequal:
+  case tok::l_paren:
+  case tok::l_square:
+  case tok::less:
+  case tok::lessequal:
+  case tok::lessless:
+  case tok::lesslessequal:
+  case tok::minus:
+  case tok::minusequal:
+  case tok::minusminus:
+  case tok::percent:
+  case tok::percentequal:
+  case tok::period:
+  case tok::pipe:
+  case tok::pipeequal:
+  case tok::pipepipe:
+  case tok::plus:
+  case tok::plusequal:
+  case tok::plusplus:
+  case tok::question:
+  case tok::r_brace:
+  case tok::r_paren:
+  case tok::r_square:
+  case tok::semi:
+  case tok::slash:
+  case tok::slashequal:
+  case tok::star:
+  case tok::starequal:
+    return true;
+  default:
+    return false;
+  }
+}
+
 void UnwrappedLineParser::parseAccessSpecifier() {
   FormatToken *AccessSpecifierCandidate = FormatTok;
   nextToken();
@@ -2827,9 +2878,7 @@ void UnwrappedLineParser::parseAccessSpecifier() {
     nextToken();
     addUnwrappedLine();
   } else if (!FormatTok->is(tok::coloncolon) &&
-             !std::binary_search(COperatorsFollowingVar.begin(),
-                                 COperatorsFollowingVar.end(),
-                                 FormatTok->Tok.getKind())) {
+             !isCOperatorFollowingVar(FormatTok->Tok.getKind())) {
     // Not a variable name nor namespace name.
     addUnwrappedLine();
   } else if (AccessSpecifierCandidate) {


        


More information about the cfe-commits mailing list