[PATCH] Correctly detect colon in bit fields. Fixes PR17333.

Alexander Kornienko alexfh at google.com
Wed Oct 9 17:25:34 PDT 2013


Hi djasper,

Colon was incorrectly detected as a start of inheritance list. Fixed.

http://llvm-reviews.chandlerc.com/D1884

Files:
  lib/Format/FormatToken.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/FormatToken.h
===================================================================
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -26,6 +26,7 @@
 
 enum TokenType {
   TT_BinaryOperator,
+  TT_BitFieldColon,
   TT_BlockComment,
   TT_CastRParen,
   TT_ConditionalExpr,
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -328,11 +328,12 @@
         Tok->Previous->Type = TT_ObjCSelectorName;
         if (Tok->Previous->ColumnWidth >
             Contexts.back().LongestObjCSelectorName) {
-          Contexts.back().LongestObjCSelectorName =
-              Tok->Previous->ColumnWidth;
+          Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth;
         }
         if (Contexts.back().FirstObjCSelectorName == NULL)
           Contexts.back().FirstObjCSelectorName = Tok->Previous;
+      } else if (Tok->Next && Tok->Next->is(tok::numeric_constant)) {
+        Tok->Type = TT_BitFieldColon;
       } else if (Contexts.back().ColonIsForRangeExpr) {
         Tok->Type = TT_RangeBasedForLoopColon;
       } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) {
@@ -1370,8 +1371,7 @@
              !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
     return true;
   } else if (Right.Previous->BlockKind == BK_Block &&
-             Right.Previous->isNot(tok::r_brace) &&
-             Right.isNot(tok::r_brace)) {
+             Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {
     return true;
   } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) {
     return Style.BreakBeforeBraces == FormatStyle::BS_Allman;
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1635,6 +1635,10 @@
                "  unsigned sClass : 8;\n"
                "  unsigned ValueKind : 2;\n"
                "};");
+  verifyFormat("struct A {\n"
+               "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
+               "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
+               "};");
 }
 
 TEST_F(FormatTest, FormatsNamespaces) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1884.1.patch
Type: text/x-patch
Size: 2293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131009/5e029b04/attachment.bin>


More information about the cfe-commits mailing list