diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2228,6 +2228,13 @@ /// \endcode bool SpaceBeforeSquareBrackets; + /// If ``false``, space will be removed around ``:`` in bitfield declarations. + /// \code + /// true: false: + /// unsigned bf : 3; vs. unsigned bf:3; + /// \endcode + bool SpaceAroundBitFieldColon; + /// Supported language standards for parsing and formatting C++ constructs. /// \code /// Latest: vector> @@ -2404,6 +2411,7 @@ SpacesInParentheses == R.SpacesInParentheses && SpacesInSquareBrackets == R.SpacesInSquareBrackets && SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets && + SpaceAroundBitFieldColon == R.SpaceAroundBitFieldColon && 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 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -593,6 +593,8 @@ IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); IO.mapOptional("SpaceBeforeSquareBrackets", Style.SpaceBeforeSquareBrackets); + IO.mapOptional("SpaceAroundBitFieldColon", + Style.SpaceAroundBitFieldColon); IO.mapOptional("Standard", Style.Standard); IO.mapOptional("StatementMacros", Style.StatementMacros); IO.mapOptional("TabWidth", Style.TabWidth); @@ -918,6 +920,7 @@ LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpaceBeforeCpp11BracedList = false; LLVMStyle.SpaceBeforeSquareBrackets = false; + LLVMStyle.SpaceAroundBitFieldColon = true; LLVMStyle.SpacesInAngles = false; LLVMStyle.SpacesInConditionalStatement = false; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3251,6 +3251,8 @@ if (Right.is(TT_RangeBasedForLoopColon) && !Style.SpaceBeforeRangeBasedForLoopColon) return false; + if (Left.is(TT_BitFieldColon)) + return Style.SpaceAroundBitFieldColon; if (Right.is(tok::colon)) { if (Line.First->isOneOf(tok::kw_case, tok::kw_default) || !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) @@ -3267,6 +3269,8 @@ return false; if (Right.is(TT_CSharpNamedArgumentColon)) return false; + if (Right.is(TT_BitFieldColon)) + return Style.SpaceAroundBitFieldColon; return true; } if (Left.is(TT_UnaryOperator)) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2165,6 +2165,13 @@ " uchar : 8;\n" " uchar other;\n" "};"); + FormatStyle LLVMWithNoSpaceAroundBitfields = getLLVMStyle(); + LLVMWithNoSpaceAroundBitfields.SpaceAroundBitFieldColon = false; + verifyFormat("struct Bitfields {\n" + " unsigned sClass:8;\n" + " unsigned ValueKind:2;\n" + " uchar other;\n" + "};", LLVMWithNoSpaceAroundBitfields); } TEST_F(FormatTest, FormatsNamespaces) { @@ -12156,6 +12163,11 @@ "int oneTwoThree : 23 = 0;", Alignment); + Alignment.SpaceAroundBitFieldColon = false; + verifyFormat("int const a :5;\n" + "int oneTwoThree:23;", + Alignment); + // Known limitations: ':' is only recognized as a bitfield colon when // followed by a number. /*