[clang] a4a7c12 - [clang-format] Add SpaceBeforeBrackets
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 16 03:54:41 PST 2019
Author: mydeveloperday
Date: 2019-11-16T11:54:21Z
New Revision: a4a7c1259e8a8f2d11fa29686a6c2834948c1358
URL: https://github.com/llvm/llvm-project/commit/a4a7c1259e8a8f2d11fa29686a6c2834948c1358
DIFF: https://github.com/llvm/llvm-project/commit/a4a7c1259e8a8f2d11fa29686a6c2834948c1358.diff
LOG: [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
Added:
Modified:
clang/docs/ClangFormatStyleOptions.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 1a9c5b8395f4..f438ec7f871b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -1432,6 +1432,10 @@ the configuration (without a prefix: ``Auto``).
f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 };
+**DeriveLineEnding** (``bool``)
+ Analyze the formatted file for the most used line ending (``\r\n``
+ or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived.
+
**DerivePointerAlignment** (``bool``)
If ``true``, analyze the formatted file for the most common
alignment of ``&`` and ``*``.
@@ -2257,6 +2261,16 @@ the configuration (without a prefix: ``Auto``).
true: false:
for (auto v : values) {} vs. for(auto v: values) {}
+**SpaceBeforeSquareBrackets** (``bool``)
+ If ``true``, spaces will be before ``[``.
+ Lambdas will not be affected. Only the first ``[`` will get a space added.
+
+ .. code-block:: c++
+
+ true: false:
+ int a [5]; vs. int a[5];
+ int a [5][5]; vs. int a[5][5];
+
**SpaceInEmptyBlock** (``bool``)
If ``true``, spaces will be inserted into ``{}``.
@@ -2409,6 +2423,10 @@ the configuration (without a prefix: ``Auto``).
For example: OpenSSL STACK_OF, BSD LIST_ENTRY.
+**UseCRLF** (``bool``)
+ Use ``\r\n`` instead of ``\n`` for line breaks.
+ Also used as fallback if ``DeriveLineEnding`` is true.
+
**UseTab** (``UseTabStyle``)
The way to use tab characters in the resulting file.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index f5c356fa2262..604a935f3a28 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1986,6 +1986,15 @@ struct FormatStyle {
/// \endcode
bool SpacesInSquareBrackets;
+ /// If ``true``, spaces will be before ``[``.
+ /// Lambdas will not be affected. Only the first ``[`` will get a space added.
+ /// \code
+ /// true: false:
+ /// int a [5]; vs. int a[5];
+ /// int a [5][5]; vs. int a[5][5];
+ /// \endcode
+ bool SpaceBeforeSquareBrackets;
+
/// Supported language standards for parsing and formatting C++ constructs.
/// \code
/// Latest: vector<set<int>>
@@ -2150,10 +2159,10 @@ struct FormatStyle {
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpacesInParentheses == R.SpacesInParentheses &&
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
+ SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
Standard == R.Standard && TabWidth == R.TabWidth &&
StatementMacros == R.StatementMacros && UseTab == R.UseTab &&
- UseCRLF == R.UseCRLF &&
- TypenameMacros == R.TypenameMacros;
+ UseCRLF == R.UseCRLF && TypenameMacros == R.TypenameMacros;
}
llvm::Optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const;
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
old mode 100644
new mode 100755
index 083c3a8f02fd..4e42bab56182
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -543,6 +543,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.SpacesInCStyleCastParentheses);
IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
+ IO.mapOptional("SpaceBeforeSquareBrackets",
+ Style.SpaceBeforeSquareBrackets);
IO.mapOptional("Standard", Style.Standard);
IO.mapOptional("StatementMacros", Style.StatementMacros);
IO.mapOptional("TabWidth", Style.TabWidth);
@@ -813,6 +815,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true;
LLVMStyle.SpaceBeforeAssignmentOperators = true;
LLVMStyle.SpaceBeforeCpp11BracedList = false;
+ LLVMStyle.SpaceBeforeSquareBrackets = false;
LLVMStyle.SpacesInAngles = false;
LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
@@ -1354,10 +1357,11 @@ class Formatter : public TokenAnalyzer {
WhitespaceManager Whitespaces(
Env.getSourceManager(), Style,
- Style.DeriveLineEnding ?
- inputUsesCRLF(Env.getSourceManager().getBufferData(Env.getFileID()),
- Style.UseCRLF) :
- Style.UseCRLF);
+ Style.DeriveLineEnding
+ ? inputUsesCRLF(
+ Env.getSourceManager().getBufferData(Env.getFileID()),
+ Style.UseCRLF)
+ : Style.UseCRLF);
ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
Env.getSourceManager(), Whitespaces, Encoding,
BinPackInconclusiveFunctions);
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c5ed49e9a409..dcdc6e6e3364 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2724,7 +2724,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
TT_DesignatedInitializerLSquare,
TT_StructuredBindingLSquare, TT_AttributeSquare) &&
- !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::l_brace) && Right.is(tok::r_brace))
return !Left.Children.empty(); // No spaces in "{}".
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 122c59782b66..e900936a1e67 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10643,6 +10643,41 @@ TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
verifyFormat("int foo = [ &bar, = ]() {};", Spaces);
}
+TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) {
+ FormatStyle NoSpaceStyle = getLLVMStyle();
+ verifyFormat("int a[5];", NoSpaceStyle);
+ verifyFormat("a[3] += 42;", NoSpaceStyle);
+
+ verifyFormat("int a[1];", NoSpaceStyle);
+ verifyFormat("int 1 [a];", NoSpaceStyle);
+ verifyFormat("int a[1][2];", NoSpaceStyle);
+ verifyFormat("a[7] = 5;", NoSpaceStyle);
+ verifyFormat("int a = (f())[23];", NoSpaceStyle);
+ verifyFormat("f([] {})", NoSpaceStyle);
+
+ 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<int []> 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, ConfigurableSpaceBeforeAssignmentOperators) {
verifyFormat("int a = 5;");
verifyFormat("a += 42;");
@@ -12529,6 +12564,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon);
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
+ CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
CHECK_PARSE_BOOL(UseCRLF);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
More information about the cfe-commits
mailing list