[clang-format][NFC] Store FormatToken::Type as an enum instead of bitfield
authorAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Wed, 7 Oct 2020 15:29:10 +0000 (16:29 +0100)
committerAlex Richardson <Alexander.Richardson@cl.cam.ac.uk>
Wed, 7 Oct 2020 16:17:40 +0000 (17:17 +0100)
This improves the debugging experience since LLDB will print the enumerator
name instead of a decimal number. This changes TokenType to have uint8_t
as the underlying type and moves it after the remaining bitfields to avoid
increasing the size of FormatToken.

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D87006

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

index c6af71a..9cc65bb 100644 (file)
@@ -118,7 +118,7 @@ namespace format {
 
 /// Determines the semantic type of a syntactic token, e.g. whether "<" is a
 /// template opener or binary operator.
-enum TokenType {
+enum TokenType : uint8_t {
 #define TYPE(X) TT_##X,
   LIST_TOKEN_TYPES
 #undef TYPE
@@ -211,8 +211,8 @@ struct FormatToken {
         ClosesTemplateDeclaration(false), StartsBinaryExpression(false),
         EndsBinaryExpression(false), PartOfMultiVariableDeclStmt(false),
         ContinuesLineCommentSection(false), Finalized(false),
-        BlockKind(BK_Unknown), Type(TT_Unknown), Decision(FD_Unformatted),
-        PackingKind(PPK_Inconclusive) {}
+        BlockKind(BK_Unknown), Decision(FD_Unformatted),
+        PackingKind(PPK_Inconclusive), Type(TT_Unknown) {}
 
   /// The \c Token.
   Token Tok;
@@ -298,18 +298,6 @@ public:
   }
 
 private:
-  unsigned Type : 8;
-
-public:
-  /// Returns the token's type, e.g. whether "<" is a template opener or
-  /// binary operator.
-  TokenType getType() const { return static_cast<TokenType>(Type); }
-  void setType(TokenType T) {
-    Type = T;
-    assert(getType() == T && "TokenType overflow!");
-  }
-
-private:
   /// Stores the formatting decision for the token once it was made.
   unsigned Decision : 2;
 
@@ -335,6 +323,15 @@ public:
     assert(getPackingKind() == K && "ParameterPackingKind overflow!");
   }
 
+private:
+  TokenType Type;
+
+public:
+  /// Returns the token's type, e.g. whether "<" is a template opener or
+  /// binary operator.
+  TokenType getType() const { return Type; }
+  void setType(TokenType T) { Type = T; }
+
   /// The number of newlines immediately before the \c Token.
   ///
   /// This can be used to determine what the user wrote in the original code
index b599168..7075a6f 100644 (file)
@@ -2753,7 +2753,7 @@ LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
                                                     E = Line.Tokens.end();
        I != E; ++I) {
     llvm::dbgs() << I->Tok->Tok.getName() << "["
-                 << "T=" << I->Tok->getType()
+                 << "T=" << (unsigned)I->Tok->getType()
                  << ", OC=" << I->Tok->OriginalColumn << "] ";
   }
   for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),