Author: Anders Waldenborg Date: 2020-07-20T20:55:51+02:00 New Revision: 52ab7aa0ba5a3c7a0b2fe1b48519f1d4dc52cacf
URL: https://github.com/llvm/llvm-project/commit/52ab7aa0ba5a3c7a0b2fe1b48519f1d4dc52cacf DIFF: https://github.com/llvm/llvm-project/commit/52ab7aa0ba5a3c7a0b2fe1b48519f1d4dc52cacf.diff LOG: [clang-format] Add BitFieldColonSpacing option This new option allows controlling if there should be spaces around the ':' in a bitfield declaration. BitFieldColonSpacing accepts four different values: // "Both" - default unsigned bitfield : 5 unsigned bf2 : 5 // AlignConsecutiveBitFields=true // "None" unsigned bitfield:5 unsigned bf2 :5 // "Before" unsigned bitfield :5 unsigned bf2 :5 // "After" unsigned bitfield: 5 unsigned bf2 : 5 Differential Revision: https://reviews.llvm.org/D84090 Added: Modified: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 6647b117ac59..c35718b51248 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -794,6 +794,43 @@ the configuration (without a prefix: ``Auto``). int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} +**BitFieldColonSpacing** (``BitFieldColonSpacingStyle``) + The BitFieldColonSpacingStyle to use for bitfields. + + Possible values: + + * ``BFCS_Both`` (in configuration: ``Both``) + Add one space on each side of the ``:`` + + .. code-block:: c++ + + unsigned bf : 2; + + * ``BFCS_None`` (in configuration: ``None``) + Add no space around the ``:`` (except when needed for + ``AlignConsecutiveBitFields``). + + .. code-block:: c++ + + unsigned bf:2; + + * ``BFCS_Before`` (in configuration: ``Before``) + Add space before the ``:`` only + + .. code-block:: c++ + + unsigned bf :2; + + * ``BFCS_After`` (in configuration: ``After``) + Add space after the ``:`` only (space may be added before if + needed for ``AlignConsecutiveBitFields``). + + .. code-block:: c++ + + unsigned bf: 2; + + + **BraceWrapping** (``BraceWrappingFlags``) Control of individual brace wrapping cases. diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 10ead604239c..21689c5c5d85 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -149,7 +149,33 @@ AST Matchers clang-format ------------ -- ... +- Option ``BitFieldColonSpacing`` has been added that decides how + space should be added around identifier, colon and bit-width in + bitfield definitions. + + .. code-block:: c++ + + // Both (default) + struct F { + unsigned dscp : 6; + unsigned ecn : 2; // AlignConsecutiveBitFields=true + }; + // None + struct F { + unsigned dscp:6; + unsigned ecn :2; + }; + // Before + struct F { + unsigned dscp :6; + unsigned ecn :2; + }; + // After + struct F { + unsigned dscp: 6; + unsigned ecn : 2; + }; + libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h old mode 100755 new mode 100644 index 7201c11f1158..269eab971a2c --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2233,6 +2233,34 @@ struct FormatStyle { /// \endcode bool SpaceBeforeSquareBrackets; + /// Styles for adding spacing around ``:`` in bitfield definitions. + enum BitFieldColonSpacingStyle { + /// Add one space on each side of the ``:`` + /// \code + /// unsigned bf : 2; + /// \endcode + BFCS_Both, + /// Add no space around the ``:`` (except when needed for + /// ``AlignConsecutiveBitFields``). + /// \code + /// unsigned bf:2; + /// \endcode + BFCS_None, + /// Add space before the ``:`` only + /// \code + /// unsigned bf :2; + /// \endcode + BFCS_Before, + /// Add space after the ``:`` only (space may be added before if + /// needed for ``AlignConsecutiveBitFields``). + /// \code + /// unsigned bf: 2; + /// \endcode + BFCS_After + }; + /// The BitFieldColonSpacingStyle to use for bitfields. + BitFieldColonSpacingStyle BitFieldColonSpacing; + /// Supported language standards for parsing and formatting C++ constructs. /// \code /// Latest: vector<set<int>> @@ -2409,6 +2437,7 @@ struct FormatStyle { SpacesInParentheses == R.SpacesInParentheses && SpacesInSquareBrackets == R.SpacesInSquareBrackets && SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets && + BitFieldColonSpacing == R.BitFieldColonSpacing && Standard == R.Standard && TabWidth == R.TabWidth && StatementMacros == R.StatementMacros && UseTab == R.UseTab && UseCRLF == R.UseCRLF && TypenameMacros == R.TypenameMacros; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0d277a6464af..3966f0a38639 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -366,6 +366,17 @@ struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensOptions> { } }; +template <> +struct ScalarEnumerationTraits<FormatStyle::BitFieldColonSpacingStyle> { + static void enumeration(IO &IO, + FormatStyle::BitFieldColonSpacingStyle &Value) { + IO.enumCase(Value, "Both", FormatStyle::BFCS_Both); + IO.enumCase(Value, "None", FormatStyle::BFCS_None); + IO.enumCase(Value, "Before", FormatStyle::BFCS_Before); + IO.enumCase(Value, "After", FormatStyle::BFCS_After); + } +}; + template <> struct MappingTraits<FormatStyle> { static void mapping(IO &IO, FormatStyle &Style) { // When reading, read the language first, we need it for getPredefinedStyle. @@ -593,6 +604,7 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); IO.mapOptional("SpaceBeforeSquareBrackets", Style.SpaceBeforeSquareBrackets); + IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing); IO.mapOptional("Standard", Style.Standard); IO.mapOptional("StatementMacros", Style.StatementMacros); IO.mapOptional("TabWidth", Style.TabWidth); @@ -918,6 +930,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpaceBeforeCpp11BracedList = false; LLVMStyle.SpaceBeforeSquareBrackets = false; + LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both; LLVMStyle.SpacesInAngles = false; LLVMStyle.SpacesInConditionalStatement = false; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 7f8e35126512..0ab09b4a1218 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3251,6 +3251,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, if (Right.is(TT_RangeBasedForLoopColon) && !Style.SpaceBeforeRangeBasedForLoopColon) return false; + if (Left.is(TT_BitFieldColon)) + return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || + Style.BitFieldColonSpacing == FormatStyle::BFCS_After; if (Right.is(tok::colon)) { if (Line.First->isOneOf(tok::kw_case, tok::kw_default) || !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) @@ -3267,6 +3270,9 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, return false; if (Right.is(TT_CSharpNamedArgumentColon)) return false; + if (Right.is(TT_BitFieldColon)) + return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || + Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; return true; } if (Left.is(TT_UnaryOperator)) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 6ac3ffbffd1c..83dd5bbba911 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2165,6 +2165,33 @@ TEST_F(FormatTest, FormatsBitfields) { " uchar : 8;\n" " uchar other;\n" "};"); + FormatStyle Style = getLLVMStyle(); + Style.BitFieldColonSpacing = FormatStyle::BFCS_None; + verifyFormat("struct Bitfields {\n" + " unsigned sClass:8;\n" + " unsigned ValueKind:2;\n" + " uchar other;\n" + "};", + Style); + verifyFormat("struct A {\n" + " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" + "};", + Style); + Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; + verifyFormat("struct Bitfields {\n" + " unsigned sClass :8;\n" + " unsigned ValueKind :2;\n" + " uchar other;\n" + "};", + Style); + Style.BitFieldColonSpacing = FormatStyle::BFCS_After; + verifyFormat("struct Bitfields {\n" + " unsigned sClass: 8;\n" + " unsigned ValueKind: 2;\n" + " uchar other;\n" + "};", + Style); } TEST_F(FormatTest, FormatsNamespaces) { @@ -12156,6 +12183,21 @@ TEST_F(FormatTest, AlignConsecutiveBitFields) { "int oneTwoThree : 23 = 0;", Alignment); + Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; + verifyFormat("int const a :5;\n" + "int oneTwoThree:23;", + Alignment); + + Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; + verifyFormat("int const a :5;\n" + "int oneTwoThree :23;", + Alignment); + + Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; + verifyFormat("int const a : 5;\n" + "int oneTwoThree: 23;", + Alignment); + // Known limitations: ':' is only recognized as a bitfield colon when // followed by a number. /* @@ -14004,6 +14046,16 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, FormatStyle::IEBS_NoIndent); + Style.BitFieldColonSpacing = FormatStyle::BFCS_None; + CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, + FormatStyle::BFCS_Both); + CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, + FormatStyle::BFCS_None); + CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, + FormatStyle::BFCS_Before); + CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, + FormatStyle::BFCS_After); + // FIXME: This is required because parsing a configuration simply overwrites // the first N elements of the list instead of resetting it. Style.ForEachMacros.clear(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits