[PATCH] D84090: [clang-format] Add SpaceAroundBitFieldColon option
Anders Waldenborg via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 18 03:54:27 PDT 2020
wanders created this revision.
wanders added reviewers: MyDeveloperDay, klimek.
wanders added a project: clang-format.
Herald added a project: clang.
This new option allows controlling if there should be spaces around
the ':' in a bitfield declaration.
Decides if it should be the existing (and therefore default with the
new option) behavior:
unsigned bitfield : 5; // SpaceAroundBitFieldColon = true
or:
unsigned bitfield:5; // SpaceAroundBitFieldColon = false
----
A bit unsure about naming of the option. Should there be separate Before/After? I guess some might find `uint x :1` and `uint x: 1` useful.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D84090
Files:
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ 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.
/*
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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)) {
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ 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;
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ 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<set<int>>
@@ -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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84090.278981.patch
Type: text/x-patch
Size: 4043 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200718/0dca2e99/attachment-0001.bin>
More information about the cfe-commits
mailing list