Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -410,6 +410,9 @@ /// \brief If \c true, spaces will be inserted after '[' and before ']'. bool SpacesInSquareBrackets; + /// \brief If \c true, a space will be inserted before '[' in array subscripts. + bool SpaceBeforeSquareBrackets; + /// \brief Supported language standards. enum LanguageStandard { /// Use C++03-compatible syntax. @@ -515,6 +518,7 @@ SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses && SpacesInParentheses == R.SpacesInParentheses && SpacesInSquareBrackets == R.SpacesInSquareBrackets && + SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets && Standard == R.Standard && TabWidth == R.TabWidth && UseTab == R.UseTab; Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -279,6 +279,7 @@ Style.SpacesInCStyleCastParentheses); IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses); IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); + IO.mapOptional("SpaceBeforeSquareBrackets", Style.SpaceBeforeSquareBrackets); IO.mapOptional("Standard", Style.Standard); IO.mapOptional("TabWidth", Style.TabWidth); IO.mapOptional("UseTab", Style.UseTab); @@ -391,6 +392,7 @@ LLVMStyle.UseTab = FormatStyle::UT_Never; LLVMStyle.SpacesInParentheses = false; LLVMStyle.SpacesInSquareBrackets = false; + LLVMStyle.SpaceBeforeSquareBrackets = false; LLVMStyle.SpaceInEmptyParentheses = false; LLVMStyle.SpacesInContainerLiterals = true; LLVMStyle.SpacesInCStyleCastParentheses = false; Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -1882,7 +1882,9 @@ Right.MatchingParen->is(TT_ArraySubscriptLSquare))); if (Right.is(tok::l_square) && !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) && - !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) + !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) && + !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets && + Right.is(TT_ArraySubscriptLSquare))) return false; if (Left.is(tok::colon)) return !Left.is(TT_ObjCMethodExpr); Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8358,6 +8358,43 @@ verifyFormat("X A::operator++ (T);", Space); } +TEST_F(FormatTest, ConfigurableSpaceBeforeSquareBrackets) { + FormatStyle NoSpace = getLLVMStyle(); + NoSpace.SpaceBeforeSquareBrackets = false; + + verifyFormat("int a[5];", NoSpace); + verifyFormat("a[3] += 42;", NoSpace); + + verifyFormat("int a[1];", NoSpace); + verifyFormat("int 1 [a];", NoSpace); + verifyFormat("int a[1][2];", NoSpace); + verifyFormat("a[7] = 5;", NoSpace); + verifyFormat("int a = (f())[23];", NoSpace); + verifyFormat("f([] {})", NoSpace); + + FormatStyle Space = getLLVMStyle(); + Space.SpaceBeforeSquareBrackets = true; + verifyFormat("int c = []() -> int { return 2; }();\n", Space); + verifyFormat("return [i, args...] {};", Space); + + verifyFormat("int a [5];", Space); + verifyFormat("a [3] += 42;", Space); + verifyFormat("constexpr char hello []{\"hello\"};", Space); + verifyFormat("double &operator[](int i) { return 0; }\n" + "int i;", + Space); + verifyFormat("std::unique_ptr foo() {}", Space); + verifyFormat("int i = a [a][a]->f();", Space); + verifyFormat("int i = (*b) [a]->f();", Space); + + verifyFormat("int a [1];", Space); + verifyFormat("int 1 [a];", Space); + verifyFormat("int a [1][2];", Space); + verifyFormat("a [7] = 5;", Space); + verifyFormat("int a = (f()) [23];", Space); + verifyFormat("f([] {})", Space); +} + TEST_F(FormatTest, ConfigurableSpacesInParentheses) { FormatStyle Spaces = getLLVMStyle(); @@ -9303,6 +9340,7 @@ CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); CHECK_PARSE_BOOL(SpaceAfterCStyleCast); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); + CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); } #undef CHECK_PARSE_BOOL