[clang-format] Add SpacesInParens with SpacesInParensOptions

This is a refactoring of:
- SpacesInConditionalStatement
- SpacesInCStyleCastParentheses
- SpaceInEmptyParentheses
- SpacesInParentheses

These are now options under the new Style Option: SpacesInParens. The
existing options are maintained for backward compatibility.

Within SpacesInParens, there are currently options for:
- Never
- Custom

The currently available options for Custom are:
- InConditionalStatements
- InCStyleCasts
- InEmptyParentheses
- Other

Setting InConditionalStatements and Other to true enables the same space
additions as SpacesInParentheses.

This refactoring does not add or remove any existing features, but it makes
it possible to more easily extend and maintain the addition of spaces within
parentheses.

Related to #55428.

Differential Revision: https://reviews.llvm.org/D155239
This commit is contained in:
Gedare Bloom 2023-07-24 19:13:33 -07:00 committed by Owen Pan
parent 2398e26080
commit c669541c96
8 changed files with 395 additions and 98 deletions

View File

@ -5215,16 +5215,8 @@ the configuration (without a prefix: ``Auto``).
**SpaceInEmptyParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <SpaceInEmptyParentheses>`
If ``true``, spaces may be inserted into ``()``.
.. code-block:: c++
true: false:
void f( ) { vs. void f() {
int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
if (true) { if (true) {
f( ); f();
} }
} }
This option is **deprecated**. See ``InEmptyParentheses`` of
``SpacesInParensOptions``.
.. _SpacesBeforeTrailingComments:
@ -5281,23 +5273,16 @@ the configuration (without a prefix: ``Auto``).
**SpacesInCStyleCastParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <SpacesInCStyleCastParentheses>`
If ``true``, spaces may be inserted into C style casts.
.. code-block:: c++
true: false:
x = ( int32 )y vs. x = (int32)y
This option is **deprecated**. See ``InCStyleCasts`` of
``SpacesInParensOptions``.
.. _SpacesInConditionalStatement:
**SpacesInConditionalStatement** (``Boolean``) :versionbadge:`clang-format 10` :ref:`¶ <SpacesInConditionalStatement>`
If ``true``, spaces will be inserted around if/for/switch/while
conditions.
.. code-block:: c++
true: false:
if ( a ) { ... } vs. if (a) { ... }
while ( i < 5 ) { ... } while (i < 5) { ... }
This option is **deprecated**. See ``InConditionalStatements`` of
``SpacesInParensOptions``.
.. _SpacesInContainerLiterals:
@ -5358,15 +5343,104 @@ the configuration (without a prefix: ``Auto``).
* ``unsigned Maximum`` The maximum number of spaces at the start of the comment.
.. _SpacesInParentheses:
.. _SpacesInParens:
**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <SpacesInParentheses>`
If ``true``, spaces will be inserted after ``(`` and before ``)``.
**SpacesInParens** (``SpacesInParensStyle``) :versionbadge:`clang-format 17` :ref:`¶ <SpacesInParens>`
Defines in which cases spaces will be inserted after ``(`` and before
``)``.
Possible values:
* ``SIPO_Never`` (in configuration: ``Never``)
Never put a space in parentheses.
.. code-block:: c++
void f() {
if(true) {
f();
}
}
* ``SIPO_Custom`` (in configuration: ``Custom``)
Configure each individual space in parentheses in
`SpacesInParensOptions`.
.. _SpacesInParensOptions:
**SpacesInParensOptions** (``SpacesInParensCustom``) :versionbadge:`clang-format 17` :ref:`¶ <SpacesInParensOptions>`
Control of individual spaces in parentheses.
If ``SpacesInParens`` is set to ``Custom``, use this to specify
how each individual space in parentheses case should be handled.
Otherwise, this is ignored.
.. code-block:: yaml
# Example of usage:
SpacesInParens: Custom
SpacesInParensOptions:
InConditionalStatements: true
InEmptyParentheses: true
Nested configuration flags:
Precise control over the spacing in parentheses.
.. code-block:: c++
true: false:
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
# Should be declared this way:
SpacesInParens: Custom
SpacesInParensOptions:
InConditionalStatements: true
Other: true
* ``bool InConditionalStatements`` Put a space in parentheses only inside conditional statements
(``for/if/while/switch...``).
.. code-block:: c++
true: false:
if ( a ) { ... } vs. if (a) { ... }
while ( i < 5 ) { ... } while (i < 5) { ... }
* ``bool InCStyleCasts`` Put a space in C style casts.
.. code-block:: c++
true: false:
x = ( int32 )y vs. x = (int32)y
* ``bool InEmptyParentheses`` Put a space in parentheses only if the parentheses are empty i.e. '()'
.. code-block:: c++
true: false:
void f( ) { vs. void f() {
int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
if (true) { if (true) {
f( ); f();
} }
} }
* ``bool Other`` Put a space in parentheses not covered by preceding options.
.. code-block:: c++
true: false:
t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
.. _SpacesInParentheses:
**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <SpacesInParentheses>`
If ``true'', spaces will be inserted after ``(`` and before ``)``.
This option is **deprecated**. The previous behavior is preserved by using
``SpacesInParens`` with ``Custom`` and by setting all
``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and
``InEmptyParentheses``.
.. _SpacesInSquareBrackets:

View File

@ -990,6 +990,9 @@ clang-format
- Add ``TypeNames`` to treat listed non-keyword identifiers as type names.
- Add ``AlignConsecutiveShortCaseStatements`` which can be used to align case
labels in conjunction with ``AllowShortCaseLabelsOnASingleLine``.
- Add ``SpacesInParens`` style with ``SpacesInParensOptions`` to replace
``SpacesInConditionalStatement``, ``SpacesInCStyleCastParentheses``,
``SpaceInEmptyParentheses``, and ``SpacesInParentheses``.
libclang
--------

View File

@ -4151,17 +4151,10 @@ struct FormatStyle {
bool SpaceInEmptyBlock;
/// If ``true``, spaces may be inserted into ``()``.
/// \code
/// true: false:
/// void f( ) { vs. void f() {
/// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
/// if (true) { if (true) {
/// f( ); f();
/// } }
/// } }
/// \endcode
/// This option is **deprecated**. See ``InEmptyParentheses`` of
/// ``SpacesInParensOptions``.
/// \version 3.7
bool SpaceInEmptyParentheses;
// bool SpaceInEmptyParentheses;
/// The number of spaces before trailing line comments
/// (``//`` - comments).
@ -4208,13 +4201,10 @@ struct FormatStyle {
/// If ``true``, spaces will be inserted around if/for/switch/while
/// conditions.
/// \code
/// true: false:
/// if ( a ) { ... } vs. if (a) { ... }
/// while ( i < 5 ) { ... } while (i < 5) { ... }
/// \endcode
/// This option is **deprecated**. See ``InConditionalStatements`` of
/// ``SpacesInParensOptions``.
/// \version 10
bool SpacesInConditionalStatement;
// bool SpacesInConditionalStatement;
/// If ``true``, spaces are inserted inside container literals (e.g. ObjC and
/// Javascript array and dict literals). For JSON, use
@ -4228,12 +4218,10 @@ struct FormatStyle {
bool SpacesInContainerLiterals;
/// If ``true``, spaces may be inserted into C style casts.
/// \code
/// true: false:
/// x = ( int32 )y vs. x = (int32)y
/// \endcode
/// This option is **deprecated**. See ``InCStyleCasts`` of
/// ``SpacesInParensOptions``.
/// \version 3.7
bool SpacesInCStyleCastParentheses;
// bool SpacesInCStyleCastParentheses;
/// Control of spaces within a single line comment.
struct SpacesInLineComment {
@ -4277,13 +4265,112 @@ struct FormatStyle {
/// \version 13
SpacesInLineComment SpacesInLineCommentPrefix;
/// If ``true``, spaces will be inserted after ``(`` and before ``)``.
/// \code
/// true: false:
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
/// \endcode
/// Different ways to put a space before opening and closing parentheses.
enum SpacesInParensStyle : int8_t {
/// Never put a space in parentheses.
/// \code
/// void f() {
/// if(true) {
/// f();
/// }
/// }
/// \endcode
SIPO_Never,
/// Configure each individual space in parentheses in
/// `SpacesInParensOptions`.
SIPO_Custom,
};
/// If ``true'', spaces will be inserted after ``(`` and before ``)``.
/// This option is **deprecated**. The previous behavior is preserved by using
/// ``SpacesInParens`` with ``Custom`` and by setting all
/// ``SpacesInParensOptions`` to ``true`` except for ``InCStyleCasts`` and
/// ``InEmptyParentheses``.
/// \version 3.7
bool SpacesInParentheses;
// bool SpacesInParentheses;
/// Defines in which cases spaces will be inserted after ``(`` and before
/// ``)``.
/// \version 17
SpacesInParensStyle SpacesInParens;
/// Precise control over the spacing in parentheses.
/// \code
/// # Should be declared this way:
/// SpacesInParens: Custom
/// SpacesInParensOptions:
/// InConditionalStatements: true
/// Other: true
/// \endcode
struct SpacesInParensCustom {
/// Put a space in parentheses only inside conditional statements
/// (``for/if/while/switch...``).
/// \code
/// true: false:
/// if ( a ) { ... } vs. if (a) { ... }
/// while ( i < 5 ) { ... } while (i < 5) { ... }
/// \endcode
bool InConditionalStatements;
/// Put a space in C style casts.
/// \code
/// true: false:
/// x = ( int32 )y vs. x = (int32)y
/// \endcode
bool InCStyleCasts;
/// Put a space in parentheses only if the parentheses are empty i.e. '()'
/// \code
/// true: false:
/// void f( ) { vs. void f() {
/// int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()};
/// if (true) { if (true) {
/// f( ); f();
/// } }
/// } }
/// \endcode
bool InEmptyParentheses;
/// Put a space in parentheses not covered by preceding options.
/// \code
/// true: false:
/// t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete;
/// \endcode
bool Other;
SpacesInParensCustom()
: InConditionalStatements(false), InCStyleCasts(false),
InEmptyParentheses(false), Other(false) {}
SpacesInParensCustom(bool InConditionalStatements, bool InCStyleCasts,
bool InEmptyParentheses, bool Other)
: InConditionalStatements(InConditionalStatements),
InCStyleCasts(InCStyleCasts),
InEmptyParentheses(InEmptyParentheses),
Other(Other) {}
bool operator==(const SpacesInParensCustom &R) const {
return InConditionalStatements == R.InConditionalStatements &&
InCStyleCasts == R.InCStyleCasts &&
InEmptyParentheses == R.InEmptyParentheses &&
Other == R.Other;
}
bool operator!=(const SpacesInParensCustom &R) const {
return !(*this == R);
}
};
/// Control of individual spaces in parentheses.
///
/// If ``SpacesInParens`` is set to ``Custom``, use this to specify
/// how each individual space in parentheses case should be handled.
/// Otherwise, this is ignored.
/// \code{.yaml}
/// # Example of usage:
/// SpacesInParens: Custom
/// SpacesInParensOptions:
/// InConditionalStatements: true
/// InEmptyParentheses: true
/// \endcode
/// \version 17
SpacesInParensCustom SpacesInParensOptions;
/// If ``true``, spaces will be inserted after ``[`` and before ``]``.
/// Lambdas without arguments or unspecified size array declarations will not
@ -4587,17 +4674,15 @@ struct FormatStyle {
R.SpaceBeforeRangeBasedForLoopColon &&
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets &&
SpaceInEmptyBlock == R.SpaceInEmptyBlock &&
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
SpacesInAngles == R.SpacesInAngles &&
SpacesInConditionalStatement == R.SpacesInConditionalStatement &&
SpacesInContainerLiterals == R.SpacesInContainerLiterals &&
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpacesInLineCommentPrefix.Minimum ==
R.SpacesInLineCommentPrefix.Minimum &&
SpacesInLineCommentPrefix.Maximum ==
R.SpacesInLineCommentPrefix.Maximum &&
SpacesInParentheses == R.SpacesInParentheses &&
SpacesInParens == R.SpacesInParens &&
SpacesInParensOptions == R.SpacesInParensOptions &&
SpacesInSquareBrackets == R.SpacesInSquareBrackets &&
Standard == R.Standard &&
StatementAttributeLikeMacros == R.StatementAttributeLikeMacros &&

View File

@ -722,6 +722,22 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> {
}
};
template <> struct MappingTraits<FormatStyle::SpacesInParensCustom> {
static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) {
IO.mapOptional("InCStyleCasts", Spaces.InCStyleCasts);
IO.mapOptional("InConditionalStatements", Spaces.InConditionalStatements);
IO.mapOptional("InEmptyParentheses", Spaces.InEmptyParentheses);
IO.mapOptional("Other", Spaces.Other);
}
};
template <> struct ScalarEnumerationTraits<FormatStyle::SpacesInParensStyle> {
static void enumeration(IO &IO, FormatStyle::SpacesInParensStyle &Value) {
IO.enumCase(Value, "Never", FormatStyle::SIPO_Never);
IO.enumCase(Value, "Custom", FormatStyle::SIPO_Custom);
}
};
template <> struct ScalarEnumerationTraits<FormatStyle::TrailingCommaStyle> {
static void enumeration(IO &IO, FormatStyle::TrailingCommaStyle &Value) {
IO.enumCase(Value, "None", FormatStyle::TCS_None);
@ -837,6 +853,11 @@ template <> struct MappingTraits<FormatStyle> {
bool DeriveLineEnding = true;
bool UseCRLF = false;
bool SpaceInEmptyParentheses = false;
bool SpacesInConditionalStatement = false;
bool SpacesInCStyleCastParentheses = false;
bool SpacesInParentheses = false;
// For backward compatibility.
if (!IO.outputting()) {
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
@ -855,6 +876,12 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
IO.mapOptional("SpaceAfterControlStatementKeyword",
Style.SpaceBeforeParens);
IO.mapOptional("SpaceInEmptyParentheses", SpaceInEmptyParentheses);
IO.mapOptional("SpacesInConditionalStatement",
SpacesInConditionalStatement);
IO.mapOptional("SpacesInCStyleCastParentheses",
SpacesInCStyleCastParentheses);
IO.mapOptional("SpacesInParentheses", SpacesInParentheses);
IO.mapOptional("UseCRLF", UseCRLF);
}
@ -1045,19 +1072,15 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("SpaceBeforeSquareBrackets",
Style.SpaceBeforeSquareBrackets);
IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock);
IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
IO.mapOptional("SpacesBeforeTrailingComments",
Style.SpacesBeforeTrailingComments);
IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
IO.mapOptional("SpacesInConditionalStatement",
Style.SpacesInConditionalStatement);
IO.mapOptional("SpacesInContainerLiterals",
Style.SpacesInContainerLiterals);
IO.mapOptional("SpacesInCStyleCastParentheses",
Style.SpacesInCStyleCastParentheses);
IO.mapOptional("SpacesInLineCommentPrefix",
Style.SpacesInLineCommentPrefix);
IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
IO.mapOptional("SpacesInParens", Style.SpacesInParens);
IO.mapOptional("SpacesInParensOptions", Style.SpacesInParensOptions);
IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets);
IO.mapOptional("Standard", Style.Standard);
IO.mapOptional("StatementAttributeLikeMacros",
@ -1124,6 +1147,30 @@ template <> struct MappingTraits<FormatStyle> {
else if (UseCRLF)
Style.LineEnding = FormatStyle::LE_DeriveCRLF;
}
if (Style.SpacesInParens != FormatStyle::SIPO_Custom &&
(SpacesInParentheses || SpaceInEmptyParentheses ||
SpacesInConditionalStatement || SpacesInCStyleCastParentheses)) {
if (SpacesInParentheses) {
// set all options except InCStyleCasts and InEmptyParentheses
// to true for backward compatibility.
Style.SpacesInParensOptions.InConditionalStatements = true;
Style.SpacesInParensOptions.InCStyleCasts =
SpacesInCStyleCastParentheses;
Style.SpacesInParensOptions.InEmptyParentheses =
SpaceInEmptyParentheses;
Style.SpacesInParensOptions.Other = true;
} else {
Style.SpacesInParensOptions = {};
Style.SpacesInParensOptions.InConditionalStatements =
SpacesInConditionalStatement;
Style.SpacesInParensOptions.InCStyleCasts =
SpacesInCStyleCastParentheses;
Style.SpacesInParensOptions.InEmptyParentheses =
SpaceInEmptyParentheses;
}
Style.SpacesInParens = FormatStyle::SIPO_Custom;
}
}
};
@ -1328,6 +1375,14 @@ static void expandPresetsSpaceBeforeParens(FormatStyle &Expanded) {
}
}
static void expandPresetsSpacesInParens(FormatStyle &Expanded) {
if (Expanded.SpacesInParens == FormatStyle::SIPO_Custom)
return;
assert(Expanded.SpacesInParens == FormatStyle::SIPO_Never);
// Reset all flags
Expanded.SpacesInParensOptions = {};
}
FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
FormatStyle LLVMStyle;
LLVMStyle.InheritsParentConfig = false;
@ -1482,15 +1537,12 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SpaceBeforeCpp11BracedList = false;
LLVMStyle.SpaceBeforeSquareBrackets = false;
LLVMStyle.SpaceInEmptyBlock = false;
LLVMStyle.SpaceInEmptyParentheses = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never;
LLVMStyle.SpacesInContainerLiterals = true;
LLVMStyle.SpacesInCStyleCastParentheses = false;
LLVMStyle.SpacesInLineCommentPrefix = {/*Minimum=*/1, /*Maximum=*/-1u};
LLVMStyle.SpacesInParentheses = false;
LLVMStyle.SpacesInParens = FormatStyle::SIPO_Never;
LLVMStyle.SpacesInSquareBrackets = false;
LLVMStyle.SpacesInConditionalStatement = false;
LLVMStyle.Standard = FormatStyle::LS_Latest;
LLVMStyle.StatementAttributeLikeMacros.push_back("Q_EMIT");
LLVMStyle.StatementMacros.push_back("Q_UNUSED");
@ -1972,6 +2024,7 @@ std::string configurationAsText(const FormatStyle &Style) {
FormatStyle NonConstStyle = Style;
expandPresetsBraceWrapping(NonConstStyle);
expandPresetsSpaceBeforeParens(NonConstStyle);
expandPresetsSpacesInParens(NonConstStyle);
Output << NonConstStyle;
return Stream.str();
@ -3497,6 +3550,7 @@ reformat(const FormatStyle &Style, StringRef Code,
FormatStyle Expanded = Style;
expandPresetsBraceWrapping(Expanded);
expandPresetsSpaceBeforeParens(Expanded);
expandPresetsSpacesInParens(Expanded);
Expanded.InsertBraces = false;
Expanded.RemoveBracesLLVM = false;
Expanded.RemoveParentheses = FormatStyle::RPS_Leave;

View File

@ -3358,7 +3358,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
if (Current->is(TT_LineComment)) {
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
Current->SpacesRequiredBefore =
(Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1;
(Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
? 0
: 1;
} else if (Prev->is(TT_VerilogMultiLineListLParen)) {
Current->SpacesRequiredBefore = 0;
} else {
@ -3772,9 +3774,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
return Style.SpaceInEmptyParentheses;
return Style.SpacesInParensOptions.InEmptyParentheses;
}
if (Style.SpacesInConditionalStatement) {
if (Style.SpacesInParensOptions.InConditionalStatements) {
const FormatToken *LeftParen = nullptr;
if (Left.is(tok::l_paren))
LeftParen = &Left;
@ -3813,8 +3815,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) {
return (Right.is(TT_CastRParen) ||
(Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
? Style.SpacesInCStyleCastParentheses
: Style.SpacesInParentheses;
? Style.SpacesInParensOptions.InCStyleCasts
: Style.SpacesInParensOptions.Other;
}
if (Right.isOneOf(tok::semi, tok::comma))
return false;
@ -4040,7 +4042,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
(Right.is(tok::r_brace) && Right.MatchingParen &&
Right.MatchingParen->isNot(BK_Block))) {
return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true;
return Style.Cpp11BracedListStyle ? Style.SpacesInParensOptions.Other
: true;
}
if (Left.is(TT_BlockComment)) {
// No whitespace in x(/*foo=*/1), except for JavaScript.
@ -4702,7 +4705,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
!(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
tok::kw___super, TT_TemplateOpener,
TT_TemplateCloser)) ||
(Left.is(tok::l_paren) && Style.SpacesInParentheses);
(Left.is(tok::l_paren) && Style.SpacesInParensOptions.Other);
}
if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
return ShouldAddSpacesInAngles();

View File

@ -175,13 +175,9 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ReflowComments);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
CHECK_PARSE_BOOL(RemoveSemicolon);
CHECK_PARSE_BOOL(SpacesInParentheses);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpacesInConditionalStatement);
CHECK_PARSE_BOOL(SpaceInEmptyBlock);
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
CHECK_PARSE_BOOL(SpacesInContainerLiterals);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
CHECK_PARSE_BOOL(SpaceAfterLogicalNot);
@ -226,6 +222,10 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
}
#undef CHECK_PARSE_BOOL
@ -591,6 +591,30 @@ TEST(ConfigParseTest, ParsesConfiguration) {
SpaceBeforeParens,
FormatStyle::SBPO_ControlStatementsExceptControlMacros);
// For backward compatibility:
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
CHECK_PARSE("SpacesInParentheses: true", SpacesInParens,
FormatStyle::SIPO_Custom);
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
CHECK_PARSE("SpacesInParentheses: true", SpacesInParensOptions,
FormatStyle::SpacesInParensCustom(true, false, false, true));
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
CHECK_PARSE("SpacesInConditionalStatement: true", SpacesInParensOptions,
FormatStyle::SpacesInParensCustom(true, false, false, false));
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
CHECK_PARSE("SpacesInCStyleCastParentheses: true", SpacesInParensOptions,
FormatStyle::SpacesInParensCustom(false, true, false, false));
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
CHECK_PARSE("SpaceInEmptyParentheses: true", SpacesInParensOptions,
FormatStyle::SpacesInParensCustom(false, false, true, false));
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
Style.ColumnLimit = 123;
FormatStyle BaseStyle = getLLVMStyle();
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);

View File

@ -11031,14 +11031,16 @@ TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
verifyFormat("template <typename T> void operator=(T) && {}", AlignMiddle);
FormatStyle Spaces = getLLVMStyle();
Spaces.SpacesInCStyleCastParentheses = true;
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions = {};
Spaces.SpacesInParensOptions.InCStyleCasts = true;
verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
Spaces.SpacesInCStyleCastParentheses = false;
Spaces.SpacesInParentheses = true;
Spaces.SpacesInParensOptions.InCStyleCasts = false;
Spaces.SpacesInParensOptions.Other = true;
verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;",
Spaces);
@ -13674,7 +13676,8 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
FormatStyle SpaceBetweenBraces = getLLVMStyle();
SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always;
SpaceBetweenBraces.SpacesInParentheses = true;
SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
SpaceBetweenBraces.SpacesInParensOptions.Other = true;
SpaceBetweenBraces.SpacesInSquareBrackets = true;
verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
@ -13697,7 +13700,8 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"};",
format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces));
verifyFormat("vector< int > x{};", SpaceBetweenBraces);
SpaceBetweenBraces.SpaceInEmptyParentheses = true;
SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom;
SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true;
verifyFormat("vector< int > x{ };", SpaceBetweenBraces);
}
@ -16707,10 +16711,43 @@ TEST_F(FormatTest, SpaceAfterLogicalNot) {
verifyFormat("! ! x", Spaces);
}
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
TEST_F(FormatTest, ConfigurableSpacesInParens) {
FormatStyle Spaces = getLLVMStyle();
Spaces.SpacesInParentheses = true;
verifyFormat("do_something(::globalVar);", Spaces);
verifyFormat("call(x, y, z);", Spaces);
verifyFormat("call();", Spaces);
verifyFormat("std::function<void(int, int)> callback;", Spaces);
verifyFormat("void inFunction() { std::function<void(int, int)> fct; }",
Spaces);
verifyFormat("while ((bool)1)\n"
" continue;",
Spaces);
verifyFormat("for (;;)\n"
" continue;",
Spaces);
verifyFormat("if (true)\n"
" f();\n"
"else if (true)\n"
" f();",
Spaces);
verifyFormat("do {\n"
" do_something((int)i);\n"
"} while (something());",
Spaces);
verifyFormat("switch (x) {\n"
"default:\n"
" break;\n"
"}",
Spaces);
verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
verifyFormat("void f() __attribute__((asdf));", Spaces);
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions = {};
Spaces.SpacesInParensOptions.Other = true;
Spaces.SpacesInParensOptions.InConditionalStatements = true;
verifyFormat("do_something( ::globalVar );", Spaces);
verifyFormat("call( x, y, z );", Spaces);
verifyFormat("call();", Spaces);
@ -16737,9 +16774,13 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
" break;\n"
"}",
Spaces);
verifyFormat("SomeType *__attribute__( ( attr ) ) *a = NULL;", Spaces);
verifyFormat("void __attribute__( ( naked ) ) foo( int bar )", Spaces);
verifyFormat("void f() __attribute__( ( asdf ) );", Spaces);
Spaces.SpacesInParentheses = false;
Spaces.SpacesInCStyleCastParentheses = true;
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions = {};
Spaces.SpacesInParensOptions.InCStyleCasts = true;
verifyFormat("Type *A = ( Type * )P;", Spaces);
verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
verifyFormat("x = ( int32 )y;", Spaces);
@ -16750,9 +16791,10 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("#define x (( int )-1)", Spaces);
// Run the first set of tests again with:
Spaces.SpacesInParentheses = false;
Spaces.SpaceInEmptyParentheses = true;
Spaces.SpacesInCStyleCastParentheses = true;
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions = {};
Spaces.SpacesInParensOptions.InEmptyParentheses = true;
Spaces.SpacesInParensOptions.InCStyleCasts = true;
verifyFormat("call(x, y, z);", Spaces);
verifyFormat("call( );", Spaces);
verifyFormat("std::function<void(int, int)> callback;", Spaces);
@ -16776,6 +16818,9 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
" break;\n"
"}",
Spaces);
verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
verifyFormat("void f( ) __attribute__((asdf));", Spaces);
// Run the first set of tests again with:
Spaces.SpaceAfterCStyleCast = true;
@ -16808,9 +16853,12 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces);
verifyFormat("bool *y = ( bool * ) (x);", Spaces);
verifyFormat("throw ( int32 ) x;", Spaces);
verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
verifyFormat("void f( ) __attribute__((asdf));", Spaces);
// Run subset of tests again with:
Spaces.SpacesInCStyleCastParentheses = false;
Spaces.SpacesInParensOptions.InCStyleCasts = false;
Spaces.SpaceAfterCStyleCast = true;
verifyFormat("while ((bool) 1)\n"
" continue;",
@ -16833,6 +16881,9 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces);
verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces);
verifyFormat("throw (int32) x;", Spaces);
verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces);
verifyFormat("void __attribute__((naked)) foo(int bar)", Spaces);
verifyFormat("void f( ) __attribute__((asdf));", Spaces);
Spaces.ColumnLimit = 80;
Spaces.IndentWidth = 4;
@ -23757,11 +23808,11 @@ TEST_F(FormatTest, AtomicQualifier) {
verifyFormat("_Atomic(int*)* a;", Style);
verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style);
Style.SpacesInCStyleCastParentheses = true;
Style.SpacesInParentheses = false;
Style.SpacesInParens = FormatStyle::SIPO_Custom;
Style.SpacesInParensOptions.InCStyleCasts = true;
verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style);
Style.SpacesInCStyleCastParentheses = false;
Style.SpacesInParentheses = true;
Style.SpacesInParensOptions.InCStyleCasts = false;
Style.SpacesInParensOptions.Other = true;
verifyFormat("x = (_Atomic( uint64_t ))*a;", Style);
verifyFormat("x = (_Atomic( uint64_t ))&a;", Style);
}
@ -23824,7 +23875,8 @@ TEST_F(FormatTest, SpacesInConditionalStatement) {
FormatStyle Spaces = getLLVMStyle();
Spaces.IfMacros.clear();
Spaces.IfMacros.push_back("MYIF");
Spaces.SpacesInConditionalStatement = true;
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions.InConditionalStatements = true;
verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces);
verifyFormat("if ( !a )\n return;", Spaces);
verifyFormat("if ( a )\n return;", Spaces);

View File

@ -807,7 +807,8 @@ TEST_F(FormatTestVerilog, If) {
" if (x)\n"
" x = x;",
Style);
Style.SpacesInConditionalStatement = true;
Style.SpacesInParens = FormatStyle::SIPO_Custom;
Style.SpacesInParensOptions.InConditionalStatements = true;
verifyFormat("if ( x )\n"
" x = x;\n"
"else if ( x )\n"
@ -903,7 +904,8 @@ TEST_F(FormatTestVerilog, Loop) {
verifyFormat("repeat (x) begin\n"
"end");
auto Style = getDefaultStyle();
Style.SpacesInConditionalStatement = true;
Style.SpacesInParens = FormatStyle::SIPO_Custom;
Style.SpacesInParensOptions.InConditionalStatements = true;
verifyFormat("foreach ( x[x] )\n"
" x = x;",
Style);