Skip to content

Commit a4a7c12

Browse files
[clang-format] Add SpaceBeforeBrackets
Summary: Adds a new option SpaceBeforeBrackets to add spaces before brackets (i.e. int a[23]; -> int a [23];) This is present as an option in the Visual Studio C++ code formatting settings, but there was no matching setting in clang-format. Reviewers: djasper, MyDeveloperDay, mitchell-stellar Reviewed By: MyDeveloperDay Subscribers: llvm-commits, cfe-commits, klimek Patch by: Anteru Tags: #clang, #clang-format, #llvm Differential Revision: https://reviews.llvm.org/D6920
1 parent 42effc1 commit a4a7c12

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,10 @@ the configuration (without a prefix: ``Auto``).
14321432
f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
14331433
new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
14341434

1435+
**DeriveLineEnding** (``bool``)
1436+
Analyze the formatted file for the most used line ending (``\r\n``
1437+
or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived.
1438+
14351439
**DerivePointerAlignment** (``bool``)
14361440
If ``true``, analyze the formatted file for the most common
14371441
alignment of ``&`` and ``*``.
@@ -2257,6 +2261,16 @@ the configuration (without a prefix: ``Auto``).
22572261
true: false:
22582262
for (auto v : values) {} vs. for(auto v: values) {}
22592263

2264+
**SpaceBeforeSquareBrackets** (``bool``)
2265+
If ``true``, spaces will be before ``[``.
2266+
Lambdas will not be affected. Only the first ``[`` will get a space added.
2267+
2268+
.. code-block:: c++
2269+
2270+
true: false:
2271+
int a [5]; vs. int a[5];
2272+
int a [5][5]; vs. int a[5][5];
2273+
22602274
**SpaceInEmptyBlock** (``bool``)
22612275
If ``true``, spaces will be inserted into ``{}``.
22622276

@@ -2409,6 +2423,10 @@ the configuration (without a prefix: ``Auto``).
24092423
24102424
For example: OpenSSL STACK_OF, BSD LIST_ENTRY.
24112425

2426+
**UseCRLF** (``bool``)
2427+
Use ``\r\n`` instead of ``\n`` for line breaks.
2428+
Also used as fallback if ``DeriveLineEnding`` is true.
2429+
24122430
**UseTab** (``UseTabStyle``)
24132431
The way to use tab characters in the resulting file.
24142432

clang/include/clang/Format/Format.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,15 @@ struct FormatStyle {
19861986
/// \endcode
19871987
bool SpacesInSquareBrackets;
19881988

1989+
/// If ``true``, spaces will be before ``[``.
1990+
/// Lambdas will not be affected. Only the first ``[`` will get a space added.
1991+
/// \code
1992+
/// true: false:
1993+
/// int a [5]; vs. int a[5];
1994+
/// int a [5][5]; vs. int a[5][5];
1995+
/// \endcode
1996+
bool SpaceBeforeSquareBrackets;
1997+
19891998
/// Supported language standards for parsing and formatting C++ constructs.
19901999
/// \code
19912000
/// Latest: vector<set<int>>
@@ -2150,10 +2159,10 @@ struct FormatStyle {
21502159
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
21512160
SpacesInParentheses == R.SpacesInParentheses &&
21522161
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
2162+
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
21532163
Standard == R.Standard && TabWidth == R.TabWidth &&
21542164
StatementMacros == R.StatementMacros && UseTab == R.UseTab &&
2155-
UseCRLF == R.UseCRLF &&
2156-
TypenameMacros == R.TypenameMacros;
2165+
UseCRLF == R.UseCRLF && TypenameMacros == R.TypenameMacros;
21572166
}
21582167

21592168
llvm::Optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const;

clang/lib/Format/Format.cpp

100644100755
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ template <> struct MappingTraits<FormatStyle> {
543543
Style.SpacesInCStyleCastParentheses);
544544
IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
545545
IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
546+
IO.mapOptional("SpaceBeforeSquareBrackets",
547+
Style.SpaceBeforeSquareBrackets);
546548
IO.mapOptional("Standard", Style.Standard);
547549
IO.mapOptional("StatementMacros", Style.StatementMacros);
548550
IO.mapOptional("TabWidth", Style.TabWidth);
@@ -813,6 +815,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
813815
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
814816
LLVMStyle.SpaceBeforeAssignmentOperators = true;
815817
LLVMStyle.SpaceBeforeCpp11BracedList = false;
818+
LLVMStyle.SpaceBeforeSquareBrackets = false;
816819
LLVMStyle.SpacesInAngles = false;
817820

818821
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
@@ -1354,10 +1357,11 @@ class Formatter : public TokenAnalyzer {
13541357

13551358
WhitespaceManager Whitespaces(
13561359
Env.getSourceManager(), Style,
1357-
Style.DeriveLineEnding ?
1358-
inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID()),
1359-
Style.UseCRLF) :
1360-
Style.UseCRLF);
1360+
Style.DeriveLineEnding
1361+
? inputUsesCRLF(
1362+
Env.getSourceManager().getBufferData(Env.getFileID()),
1363+
Style.UseCRLF)
1364+
: Style.UseCRLF);
13611365
ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
13621366
Env.getSourceManager(), Whitespaces, Encoding,
13631367
BinPackInconclusiveFunctions);

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2724,7 +2724,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
27242724
!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
27252725
TT_DesignatedInitializerLSquare,
27262726
TT_StructuredBindingLSquare, TT_AttributeSquare) &&
2727-
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral))
2727+
!Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
2728+
!(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
2729+
Right.is(TT_ArraySubscriptLSquare)))
27282730
return false;
27292731
if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
27302732
return !Left.Children.empty(); // No spaces in "{}".

clang/unittests/Format/FormatTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10643,6 +10643,41 @@ TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
1064310643
verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
1064410644
}
1064510645

10646+
TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
10647+
FormatStyle NoSpaceStyle = getLLVMStyle();
10648+
verifyFormat("int a[5];", NoSpaceStyle);
10649+
verifyFormat("a[3] += 42;", NoSpaceStyle);
10650+
10651+
verifyFormat("int a[1];", NoSpaceStyle);
10652+
verifyFormat("int 1 [a];", NoSpaceStyle);
10653+
verifyFormat("int a[1][2];", NoSpaceStyle);
10654+
verifyFormat("a[7] = 5;", NoSpaceStyle);
10655+
verifyFormat("int a = (f())[23];", NoSpaceStyle);
10656+
verifyFormat("f([] {})", NoSpaceStyle);
10657+
10658+
FormatStyle Space = getLLVMStyle();
10659+
Space.SpaceBeforeSquareBrackets = true;
10660+
verifyFormat("int c = []() -> int { return 2; }();\n", Space);
10661+
verifyFormat("return [i, args...] {};", Space);
10662+
10663+
verifyFormat("int a [5];", Space);
10664+
verifyFormat("a [3] += 42;", Space);
10665+
verifyFormat("constexpr char hello []{\"hello\"};", Space);
10666+
verifyFormat("double &operator[](int i) { return 0; }\n"
10667+
"int i;",
10668+
Space);
10669+
verifyFormat("std::unique_ptr<int []> foo() {}", Space);
10670+
verifyFormat("int i = a [a][a]->f();", Space);
10671+
verifyFormat("int i = (*b) [a]->f();", Space);
10672+
10673+
verifyFormat("int a [1];", Space);
10674+
verifyFormat("int 1 [a];", Space);
10675+
verifyFormat("int a [1][2];", Space);
10676+
verifyFormat("a [7] = 5;", Space);
10677+
verifyFormat("int a = (f()) [23];", Space);
10678+
verifyFormat("f([] {})", Space);
10679+
}
10680+
1064610681
TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
1064710682
verifyFormat("int a = 5;");
1064810683
verifyFormat("a += 42;");
@@ -12529,6 +12564,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
1252912564
CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
1253012565
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
1253112566
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
12567+
CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
1253212568
CHECK_PARSE_BOOL(UseCRLF);
1253312569

1253412570
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);

0 commit comments

Comments
 (0)