From db4ad3603ac9bc2f118aa5f176dc04979f836c25 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sat, 10 Aug 2019 07:51:21 +0000 Subject: [PATCH] [clang-format] Add SpaceInEmptyBlock option for WebKit See PR40840 Differential Revision: https://reviews.llvm.org/D65925 llvm-svn: 368507 --- clang/docs/ClangFormatStyleOptions.rst | 73 ++++++++++++++++------------- clang/include/clang/Format/Format.h | 9 ++++ clang/lib/Format/Format.cpp | 3 ++ clang/lib/Format/UnwrappedLineFormatter.cpp | 2 +- clang/unittests/Format/FormatTest.cpp | 6 +++ 5 files changed, 60 insertions(+), 33 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 07b3792..3ff61d2 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -192,20 +192,6 @@ the configuration (without a prefix: ``Auto``). -**AlignConsecutiveMacros** (``bool``) - If ``true``, aligns consecutive C/C++ preprocessor macros. - - This will align the C/C++ preprocessor macros of consecutive lines. This - will result in formattings like - - .. code-block:: c++ - - #define SHORT_NAME 42 - #define LONGER_NAME 0x007f - #define EVEN_LONGER_NAME (2) - #define foo(x) (x * x) - #define bar(y, z) (y + z) - **AlignConsecutiveAssignments** (``bool``) If ``true``, aligns consecutive assignments. @@ -230,6 +216,20 @@ the configuration (without a prefix: ``Auto``). float b = 23; std::string ccc = 23; +**AlignConsecutiveMacros** (``bool``) + If ``true``, aligns consecutive C/C++ preprocessor macros. + + This will align C/C++ preprocessor macros of consecutive lines. + Will result in formattings like + + .. code-block:: c++ + + #define SHORT_NAME 42 + #define LONGER_NAME 0x007f + #define EVEN_LONGER_NAME (2) + #define foo(x) (x * x) + #define bar(y, z) (y + z) + **AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) Options for aligning backslashes in escaped newlines. @@ -1390,24 +1390,6 @@ the configuration (without a prefix: ``Auto``). For example: BOOST_FOREACH. -**TypenameMacros** (``std::vector``) - A vector of macros that should be interpreted as type declarations - instead of as function calls. - - These are expected to be macros of the form: - - .. code-block: c++ - - STACK_OF(...) - - In the .clang-format configuration file, this can be configured like: - - .. code-block: yaml - - TypenameMacros: ['STACK_OF', 'LIST'] - - For example: OpenSSL STACK_OF, BSD LIST_ENTRY. - **IncludeBlocks** (``IncludeBlocksStyle``) Dependent on the value, multiple ``#include`` blocks can be sorted as one and divided based on category. @@ -2134,6 +2116,15 @@ the configuration (without a prefix: ``Auto``). true: false: for (auto v : values) {} vs. for(auto v: values) {} +**SpaceInEmptyBlock** (``bool``) + If ``true``, spaces will be inserted into ``{}``. + + .. code-block:: c++ + + true: false: + void f() { } vs. void f() {} + while (true) { } while (true) {} + **SpaceInEmptyParentheses** (``bool``) If ``true``, spaces may be inserted into ``()``. @@ -2241,6 +2232,24 @@ the configuration (without a prefix: ``Auto``). **TabWidth** (``unsigned``) The number of columns used for tab stops. +**TypenameMacros** (``std::vector``) + A vector of macros that should be interpreted as type declarations + instead of as function calls. + + These are expected to be macros of the form: + + .. code-block:: c++ + + STACK_OF(...) + + In the .clang-format configuration file, this can be configured like: + + .. code-block:: yaml + + TypenameMacros: ['STACK_OF', 'LIST'] + + For example: OpenSSL STACK_OF, BSD LIST_ENTRY. + **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 6388e4f..657ec4e 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1799,6 +1799,14 @@ struct FormatStyle { /// \endcode bool SpaceBeforeRangeBasedForLoopColon; + /// If ``true``, spaces will be inserted into ``{}``. + /// \code + /// true: false: + /// void f() { } vs. void f() {} + /// while (true) { } while (true) {} + /// \endcode + bool SpaceInEmptyBlock; + /// If ``true``, spaces may be inserted into ``()``. /// \code /// true: false: @@ -1995,6 +2003,7 @@ struct FormatStyle { SpaceBeforeParens == R.SpaceBeforeParens && SpaceBeforeRangeBasedForLoopColon == R.SpaceBeforeRangeBasedForLoopColon && + SpaceInEmptyBlock == R.SpaceInEmptyBlock && SpaceInEmptyParentheses == R.SpaceInEmptyParentheses && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInAngles == R.SpacesInAngles && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 589cf96..6c4fedb 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -494,6 +494,7 @@ template <> struct MappingTraits { IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens); IO.mapOptional("SpaceBeforeRangeBasedForLoopColon", Style.SpaceBeforeRangeBasedForLoopColon); + IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock); IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses); IO.mapOptional("SpacesBeforeTrailingComments", Style.SpacesBeforeTrailingComments); @@ -734,6 +735,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.ReflowComments = true; LLVMStyle.SpacesInParentheses = false; LLVMStyle.SpacesInSquareBrackets = false; + LLVMStyle.SpaceInEmptyBlock = false; LLVMStyle.SpaceInEmptyParentheses = false; LLVMStyle.SpacesInContainerLiterals = true; LLVMStyle.SpacesInCStyleCastParentheses = false; @@ -979,6 +981,7 @@ FormatStyle getWebKitStyle() { Style.ObjCSpaceAfterProperty = true; Style.PointerAlignment = FormatStyle::PAS_Left; Style.SpaceBeforeCpp11BracedList = true; + Style.SpaceInEmptyBlock = true; return Style; } diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 3f3c80b..f41c77e 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -551,7 +551,7 @@ private: (Tok->getNextNonComment() == nullptr || Tok->getNextNonComment()->is(tok::semi))) { // We merge empty blocks even if the line exceeds the column limit. - Tok->SpacesRequiredBefore = 0; + Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0; Tok->CanBreakBefore = true; return 1; } else if (Limit != 0 && !Line.startsWithNamespace() && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 4e1b6f2..197c08b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3692,6 +3692,11 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { EXPECT_EQ("{}", format("{}")); verifyFormat("enum E {};"); verifyFormat("enum E {}"); + EXPECT_EQ("void f() { }", format("void f() {}", getWebKitStyle())); + FormatStyle Style = getLLVMStyle(); + Style.AllowShortBlocksOnASingleLine = true; + Style.SpaceInEmptyBlock = true; + EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); } TEST_F(FormatTest, FormatBeginBlockEndMacros) { @@ -11765,6 +11770,7 @@ TEST_F(FormatTest, ParsesConfigurationBools) { CHECK_PARSE_BOOL(SpacesInParentheses); CHECK_PARSE_BOOL(SpacesInSquareBrackets); CHECK_PARSE_BOOL(SpacesInAngles); + CHECK_PARSE_BOOL(SpaceInEmptyBlock); CHECK_PARSE_BOOL(SpaceInEmptyParentheses); CHECK_PARSE_BOOL(SpacesInContainerLiterals); CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); -- 2.7.4