[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
MyDeveloperDay added a comment. mark sure you mark off the comments as you consider them done. Comment at: lib/Format/FormatToken.h:519 +/// Returns whether the token is a Bit field, and checks whether +/// the Style is C / C++. this looks like it needs a clang-format as its not aligned with function that follows Comment at: lib/Format/FormatToken.h:527 +return true; + return false; +} couldn't you write this as just and lose the ``if`` ``` return (T->Tok.is(tok::comma) && Tok.is(tok::identifier) && T->Next->Tok.is(tok::colon)); ``` CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 204040. Manikishan marked an inline comment as done. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 Files: docs/ClangFormatStyleOptions.rst include/clang/Format/Format.h lib/Format/ContinuationIndenter.cpp lib/Format/Format.cpp lib/Format/FormatToken.h lib/Format/TokenAnnotator.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -3656,6 +3656,19 @@ "#define A Just forcing a new line\n" "ddd);"); } +TEST_F(FormatTest, AlignBitFieldDeclarationsOnConsecutiveLines){ + FormatStyle Style = {}; + Style.OnePerLineBitFieldDecl = true; + verifyFormat( +"unsigned int baz : 11, + aaa : 2, + foo : 3" + ); + Style.OnePerLineBitFieldDecl = false; + verifyFormat( +"unsigned int baz : 11, aaa : 2, foo : 3" + ); +} TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( Index: lib/Format/TokenAnnotator.cpp === --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2916,6 +2916,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.OnePerLineBitFieldDecl && + Right.is(tok::identifier) && (Right.Next->is(tok::colon))) +return true; if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) return true; Index: lib/Format/FormatToken.h === --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -516,6 +516,16 @@ return T && T->is(tok::kw_auto); } +/// Returns whether the token is a Bit field, and checks whether +/// the Style is C / C++. +bool isBitField() const { + const FormatToken *T = this; + T = T->getPreviousNonComment(); + if (T->Tok.is(tok::comma) && Tok.is(tok::identifier) && + T->Next->Tok.is(tok::colon)) +return true; + return false; +} /// Same as opensBlockOrBlockTypeList, but for the closing token. bool closesBlockOrBlockTypeList(const FormatStyle ) const { if (is(TT_TemplateString) && closesScope()) 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("OnePerLineBitFieldDecl", Style.OnePerLineBitFieldDecl); 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 @@ -329,6 +329,8 @@ bool ContinuationIndenter::mustBreak(const LineState ) { const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; + if (Style.isCpp() && Current.isBitField() && Style.OnePerLineBitFieldDecl) +return true; if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) return true; if (State.Stack.back().BreakBeforeClosingBrace && @@ -541,7 +543,7 @@ unsigned ExtraSpaces) { FormatToken = *State.NextToken; const FormatToken = *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 OnePerLineBitFieldDecl; + /// Different styles for aligning escaped newlines. enum EscapedNewlineAlignmentStyle { /// Don't align escaped newlines. @@ -1950,6 +1960,7 @@ JavaScriptWrapImports == R.JavaScriptWrapImports && KeepEmptyLinesAtTheStartOfBlocks ==
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 204022. Manikishan marked 4 inline comments as done. Manikishan added a comment. Changed Style name to OnePerLineBitFieldDecl Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 Files: docs/ClangFormatStyleOptions.rst include/clang/Format/Format.h lib/Format/ContinuationIndenter.cpp lib/Format/Format.cpp lib/Format/FormatToken.h lib/Format/TokenAnnotator.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -3656,6 +3656,19 @@ "#define A Just forcing a new line\n" "ddd);"); } +TEST_F(FormatTest, AlignBitFieldDeclarationsOnConsecutiveLines){ + FormatStyle Style = {}; + Style.OnePerLineBitFieldDecl = true; + verifyFormat( +"unsigned int baz : 11, + aaa : 2, + foo : 3" + ); + Style.OnePerLineBitFieldDecl = false; + verifyFormat( +"unsigned int baz : 11, aaa : 2, foo : 3" + ); +} TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( Index: lib/Format/TokenAnnotator.cpp === --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2916,6 +2916,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.OnePerLineBitFieldDecl && + Right.is(tok::identifier) && (Right.Next->is(tok::colon))) +return true; if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) return true; Index: lib/Format/FormatToken.h === --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -516,6 +516,16 @@ return T && T->is(tok::kw_auto); } +/// Returns whether the token is a Bit field, and checks whether +/// the Style is C / C++. +bool isBitField() const { + const FormatToken *T = this; + T = T->getPreviousNonComment(); + if (T->Tok.is(tok::comma) && Tok.is(tok::identifier) && + T->Next->Tok.is(tok::colon)) +return true; + return false; +} /// Same as opensBlockOrBlockTypeList, but for the closing token. bool closesBlockOrBlockTypeList(const FormatStyle ) const { if (is(TT_TemplateString) && closesScope()) 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 @@ -329,6 +329,8 @@ bool ContinuationIndenter::mustBreak(const LineState ) { const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; + if (Style.isCpp() && Current.isBitField() && Style.OnePerLineBitFieldDecl) +return true; if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) return true; if (State.Stack.back().BreakBeforeClosingBrace && @@ -541,7 +543,7 @@ unsigned ExtraSpaces) { FormatToken = *State.NextToken; const FormatToken = *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 OnePerLineBitFieldDecl; + /// Different styles for aligning escaped newlines. enum EscapedNewlineAlignmentStyle { /// Don't align escaped newlines. @@ -1950,6 +1960,7 @@
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
MyDeveloperDay requested changes to this revision. MyDeveloperDay added inline comments. This revision now requires changes to proceed. Comment at: docs/ClangFormatStyleOptions.rst:194 +**BitFieldDeclsOnSeparateLines** (``bool``) + If ``true``, Align Bitfield Declarations on separate lines. Isn't the documentation normally alphabetric? shouldn't it be after AlignConsecutiveAssignments Comment at: include/clang/Format/Format.h:104 + /// If ``true``, Linesup Bitfield Declarations. + /// This will lineup Bitfield declarations on consecutive lines. This Align Comment at: include/clang/Format/Format.h:112 + /// \endcode + bool BitFieldDeclsOnSeparateLines; + I think this should be alphabetic in this file (not sure though check the rest of it) are you happy with the name? BitFieldDeclsOnSeparateLines? 1) people often spell Separate incorrectly (didn't you?), this could lead to misconfigured 2) isn't this really a ''Break'' rule I want to say this might better as something like "BreakAfterBitFieldDecl" Comment at: lib/Format/ContinuationIndenter.cpp:284 +return true; if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && Current.closesBlockOrBlockTypeList(Style))) something here doesn't feel quite right,, without trying the code change myself I cannot tell, did you ever try this code without having the same clause in canBreak() and mustBreak()? (i.e. just put it in mustBreak) The reason I ask is I'm unclear as to why the other mustBreak() rules aren't here in canBreak() if thats the case Comment at: lib/Format/TokenAnnotator.cpp:2921 + Right.is(tok::identifier) && (Right.Next->is(tok::colon))) + return true; if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) mgorny wrote: > Misindent. This code appears 3 times (does it need to appear 3 times?), do we need some sort of ``` bool isBitField(FormatToken) { ... } ``` Should a bit field check for the existence of a number after the colon? I can't think of other C++ constructs that appear as ``` comma identifier colon ``` but given that clang-format is used for ObjC,ProtoBuf,Java,JavaScript,C# I'm pretty sure something odd is going to happen with JavaScript named parameters, to be honest I think this is going to cause the following to get reformatted MyFunctionCall({ xPosition: 20**, yPosition: 50,** width: 100, height: 5, drawingNow: true }); ``` MyFunctionCall({ xPosition: 20**, yPosition: 50,** width: 100, height: 5, drawingNow: true }); ``` or something like that Comment at: unittests/Format/FormatTest.cpp:3671 + ); +} please add a test with comments (it will get logged) ``` unsigned int baz : 11, /*motor control flags*/ add: 2/* control code for turning the lights on */ , foo: 3 /* (unused */ ``` Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 203860. Manikishan marked 4 inline comments as done. Manikishan added a comment. Updated unittest Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ 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 unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -3656,6 +3656,19 @@ "#define A Just forcing a new line\n" "ddd);"); } +TEST_F(FormatTest, AlignBitFieldDeclarationsOnConsecutiveLines){ + FormatStyle Style = {}; + Style.BitFieldDeclsOnSeparateLines = true; + verifyFormat( +"unsigned int baz : 11, + aaa : 2, + foo : 3" + ); + Style.BitFieldDeclsOnSeparateLines = false; + verifyFormat( +"unsigned int baz : 11, aaa : 2, foo : 3" + ); +} TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( Index: lib/Format/TokenAnnotator.cpp === --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2916,6 +2916,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Right.is(tok::identifier) && (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,9 @@ const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; assert( == Current.Previous); + if (Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Current.is(tok::identifier) && Current.Next->is(tok::colon)) +return true; if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && Current.closesBlockOrBlockTypeList(Style))) return false; @@ -329,6 +332,9 @@ bool ContinuationIndenter::mustBreak(const LineState ) { const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; + if (Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Current.is(tok::identifier) && Current.Next->is(tok::colon)) +return true; if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) return true; if (State.Stack.back().BreakBeforeClosingBrace && @@ -541,7 +547,7 @@ unsigned ExtraSpaces) { FormatToken = *State.NextToken; const FormatToken = *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 ==
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 203841. Manikishan added a comment. Made some missing style modifications in the last revision Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ 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 unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -3656,6 +3656,19 @@ "#define A Just forcing a new line\n" "ddd);"); } +TEST_F(FormatTest, AlignBitFieldDeclarationsOnConsecutiveLines){ + FormatStyle Style = {}; + Style.BitFieldDeclsOnSeparateLines = true; + verifyFormat( +"unsigned int baz : 11, + aaa : 2, + foo : 3" + ); + Style.BitFieldDeclsOnSeparateLines = false; + verifyFormat( +"unsigned int baz : 11, aaa : 2, foo : 3" + ); +} TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( Index: lib/Format/TokenAnnotator.cpp === --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2916,6 +2916,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Right.is(tok::identifier) && (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,9 @@ const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; assert( == Current.Previous); + if (Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Current.is(tok::identifier) && Current.Next->is(tok::colon)) +return true; if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && Current.closesBlockOrBlockTypeList(Style))) return false; @@ -329,6 +332,9 @@ bool ContinuationIndenter::mustBreak(const LineState ) { const FormatToken = *State.NextToken; const FormatToken = *Current.Previous; + if (Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Current.is(tok::identifier) && Current.Next->is(tok::colon)) +return true; if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) return true; if (State.Stack.back().BreakBeforeClosingBrace && @@ -541,7 +547,7 @@ unsigned ExtraSpaces) { FormatToken = *State.NextToken; const FormatToken = *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 ==
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 203838. Manikishan marked 4 inline comments as done. Manikishan added a comment. Added unittests and made the changes suggested by @mgorny Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ 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 unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp === --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -3656,6 +3656,19 @@ "#define A Just forcing a new line\n" "ddd);"); } +TEST_F(FormatTest, AlignBitFieldDeclarationsOnConsecutiveLines){ + FormatStyle Style = {}; + Style.BitFieldDeclsOnSeparateLines = true; + verifyFormat( +"unsigned int baz : 11, + aaa : 2, + foo : 3" + ); + Style.BitFieldDeclsOnSeparateLines = false; + verifyFormat( +"unsigned int baz : 11, aaa : 2, foo : 3" + ); +} TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat( Index: lib/Format/TokenAnnotator.cpp === --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -2916,6 +2916,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Right.is(tok::identifier) && (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 = *State.NextToken; const FormatToken = *Current.Previous; assert( == 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 ) { const FormatToken = *State.NextToken; const FormatToken = *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 = *State.NextToken; const FormatToken = *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 ==
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
mgorny requested changes to this revision. mgorny added inline comments. This revision now requires changes to proceed. Comment at: docs/ClangFormatStyleOptions.rst:195 +**BitFieldDeclsOnSeparateLines** (``bool``) + If ``true``, Align Bitfield Declarations on seperate lines. + 'separate' Comment at: include/clang/Format/Format.h:104 + /// If ``true``, Linesup Bitfield Declarations. + /// This will lineup Bitfield declarations on consecutive lines. This Also update this to match docs. Comment at: lib/Format/ContinuationIndenter.cpp:281 assert( == Current.Previous); + if(Previous.is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && Current.is(tok::identifier)){ + if(Current.Next->is(tok::colon)) Space after 'if' and before '{'. Also below. Comment at: lib/Format/TokenAnnotator.cpp:2921 + Right.is(tok::identifier) && (Right.Next->is(tok::colon))) + return true; if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) Misindent. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
Manikishan updated this revision to Diff 203833. Manikishan added a subscriber: mgorny. Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ 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,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine , const FormatToken ) { const FormatToken = *Right.Previous; + if (Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && + Right.is(tok::identifier) && (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 = *State.NextToken; const FormatToken = *Current.Previous; assert( == 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 ) { const FormatToken = *State.NextToken; const FormatToken = *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 = *State.NextToken; const FormatToken = *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``, Align Bitfield Declarations on seperate lines. + + This will align 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
[PATCH] D63062: [clang-format] Added New Style Rule: BitFieldDeclsOnSeparateLines
MyDeveloperDay requested changes to this revision. MyDeveloperDay added a comment. This revision now requires changes to proceed. You need to add a unit test in clang/unittests Comment at: docs/ClangFormatStyleOptions.rst:195 +**BitFieldDeclsOnSeparateLines** (``bool``) + If ``true``, Linesup Bitfield Declarations. + Nit: Aligns Bitfield Declarations I think Align is a better work than Linesup Comment at: docs/ClangFormatStyleOptions.rst:197 + + This will lineup Bitfield declarations on consecutive lines. This + will result in formatting like align Comment at: docs/ClangFormatStyleOptions.rst:198 + This will lineup Bitfield declarations on consecutive lines. This + will result in formatting like + end with : Comment at: lib/Format/TokenAnnotator.cpp:2920 + if(Right.Previous->is(tok::comma) && Style.BitFieldDeclsOnSeparateLines && Right.is(tok::identifier)){ + if(Right.Next->is(tok::colon)){ +return true; elide the {} Repository: rC Clang CHANGES SINCE LAST ACTION https://reviews.llvm.org/D63062/new/ https://reviews.llvm.org/D63062 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits