[clang] 4851886 - [clang-format] Handle bit-field colon of non-numeric-constant size (#142110)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 30 18:51:05 PDT 2025


Author: Owen Pan
Date: 2025-05-30T18:51:01-07:00
New Revision: 4851886693f3fa8b083f49cc09a32659125b45a4

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

LOG: [clang-format] Handle bit-field colon of non-numeric-constant size (#142110)

Fix #142050

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/TokenAnnotatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 51ececc0c0e81..f8272811bbe6c 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1411,7 +1411,9 @@ class AnnotatingParser {
         assert(Prev);
         if (Prev->isPointerOrReference())
           Prev->setFinalizedType(TT_PointerOrReference);
-      } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
+      } else if ((CurrentToken && CurrentToken->is(tok::numeric_constant)) ||
+                 (Prev && Prev->is(TT_StartOfName) && !Scopes.empty() &&
+                  Scopes.back() == ST_Class)) {
         Tok->setType(TT_BitFieldColon);
       } else if (Contexts.size() == 1 &&
                  !Line.getFirstNonComment()->isOneOf(tok::kw_enum, tok::kw_case,

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f39468424a393..1a5ed4b9040c2 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -4076,6 +4076,14 @@ TEST_F(TokenAnnotatorTest, EnumColonInTypedef) {
   EXPECT_TOKEN(Tokens[2], tok::colon, TT_Unknown); // Not TT_InheritanceColon.
 }
 
+TEST_F(TokenAnnotatorTest, BitFieldColon) {
+  auto Tokens = annotate("class C {\n"
+                         "  int f : SIZE;\n"
+                         "};");
+  ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_BitFieldColon);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang


        


More information about the cfe-commits mailing list