[clang-format] Add SpaceInEmptyBlock option for WebKit

See PR40840

Differential Revision: https://reviews.llvm.org/D65925

llvm-svn: 368507
This commit is contained in:
Owen Pan 2019-08-10 07:51:21 +00:00
parent 74c43a2277
commit db4ad3603a
5 changed files with 60 additions and 33 deletions

View File

@ -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<std::string>``)
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<std::string>``)
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.

View File

@ -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 &&

View File

@ -494,6 +494,7 @@ template <> struct MappingTraits<FormatStyle> {
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;
}

View File

@ -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() &&

View File

@ -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);