Manikishan created this revision.
Manikishan added reviewers: aaron.ballman, rsmith.
Herald added a subscriber: krytarowski.
Herald added a project: clang.

This new Style rule is made as a part of adding support for NetBSD KNF in 
clang-format. This style Lines up BitField Declarations on consecutive lines 
with correct Indentation. The working of this Style rule shown below:

//Configuration
BitFieldDeclsOnSeparateLines: true

//Before Formatting:

unsigned int bas :3,  hh : 4, jjj : 8;

unsigned int baz:1,

  fuz:5, 
  zap:2;

//After Formatting:

unsigned int bas : 3,

  hh : 4,
  jjj : 8;

unsigned int baz : 1,

  fuz : 5,
  zap : 2;

This style is formatted even if the one-line declaration line is less than the 
column limit.

There is a minor Bug:
Comments after the bitfield in before the break line will not cause a proper 
indentation.

//Before Formatting:

unsigned int baz:1,     /* foo*/

  fuz:5,          /*bar*/
  zap:2;

//After Formatting:
unsigned int  baz : 1, /* Bitfield; line up entries if desire*/

  fuz : 5,                 /*jgifjjggirrj*/
  zap : 2;


Repository:
  rC Clang

https://reviews.llvm.org/D63062

Files:
  docs/ClangFormatStyleOptions.rst
  include/clang/Format/Format.h
  lib/Format/ContinuationIndenter.cpp
  lib/Format/Format.cpp
  lib/Format/TokenAnnotator.cpp

Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2916,6 +2916,11 @@
 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
                                      const FormatToken &Right) {
   const FormatToken &Left = *Right.Previous;
+  if(Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && Right.is(tok::identifier)){
+      if(Right.Next->is(tok::colon)){
+        return true;
+      }
+  }
   if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
     return true;
 
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -451,6 +451,7 @@
     IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports);
     IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
                    Style.KeepEmptyLinesAtTheStartOfBlocks);
+    IO.mapOptional("BitFieldDeclsOnSeparateLines", Style.BitFieldDeclsOnSeparateLines);
     IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
     IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
     IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -278,6 +278,10 @@
   const FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *Current.Previous;
   assert(&Previous == Current.Previous);
+  if(Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && Current.is(tok::identifier)){
+      if(Current.Next->is(tok::colon))
+        return true;
+  }
   if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
                                    Current.closesBlockOrBlockTypeList(Style)))
     return false;
@@ -329,6 +333,11 @@
 bool ContinuationIndenter::mustBreak(const LineState &State) {
   const FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *Current.Previous;
+  if(Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && Current.is(tok::identifier)){
+      if(Current.Next->is(tok::colon)){
+        return true;
+      }
+  }
   if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
     return true;
   if (State.Stack.back().BreakBeforeClosingBrace &&
@@ -541,7 +550,7 @@
                                                  unsigned ExtraSpaces) {
   FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *State.NextToken->Previous;
-  if (Current.is(tok::equal) &&
+  if (Current.isOneOf(tok::equal, tok::colon) &&
       (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
       State.Stack.back().VariablePos == 0) {
     State.Stack.back().VariablePos = State.Column;
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -101,6 +101,16 @@
   /// \endcode
   bool AlignConsecutiveDeclarations;
 
+  /// If ``true``, Linesup Bitfield Declarations.
+  /// This will lineup Bitfield declarations on consecutive lines. This
+  /// will result in formatting like
+  /// \code
+  ///  unsigned int  baz : 1, /* Bitfield; line up entries if desire*/
+  ///    fuz : 5,
+  ///    zap : 2;
+  /// \endcode
+  bool BitFieldDeclsOnSeparateLines;
+
   /// Different styles for aligning escaped newlines.
   enum EscapedNewlineAlignmentStyle {
     /// Don't align escaped newlines.
@@ -1950,6 +1960,7 @@
            JavaScriptWrapImports == R.JavaScriptWrapImports &&
            KeepEmptyLinesAtTheStartOfBlocks ==
                R.KeepEmptyLinesAtTheStartOfBlocks &&
+           BitFieldDeclsOnSeparateLines == R.BitFieldDeclsOnSeparateLines &&
            MacroBlockBegin == R.MacroBlockBegin &&
            MacroBlockEnd == R.MacroBlockEnd &&
            MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
Index: docs/ClangFormatStyleOptions.rst
===================================================================
--- docs/ClangFormatStyleOptions.rst
+++ docs/ClangFormatStyleOptions.rst
@@ -191,6 +191,18 @@
           argument1, argument2);
 
 
+**BitFieldDeclsOnSeparateLines** (``bool``)
+  If ``true``, Linesup Bitfield Declarations.
+
+  This will lineup Bitfield declarations on consecutive lines. This
+  will result in formatting like
+
+  .. code-block:: c++
+
+    unsigned int  baz : 1,
+                  fuz : 5,
+                  zap : 2;
+
 
 **AlignConsecutiveAssignments** (``bool``)
   If ``true``, aligns consecutive assignments.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to