Skip to content

Commit 97c9534

Browse files
authored
[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)
Closes #149971
1 parent f79efa9 commit 97c9534

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6400,6 +6400,14 @@ the configuration (without a prefix: ``Auto``).
64006400
IF (...) vs. IF(...)
64016401
<conditional-body> <conditional-body>
64026402

6403+
* ``bool AfterNot`` If ``true``, put a space between alternative operator ``not`` and the
6404+
opening parenthesis.
6405+
6406+
.. code-block:: c++
6407+
6408+
true: false:
6409+
return not (a || b); vs. return not(a || b);
6410+
64036411
* ``bool AfterOverloadedOperator`` If ``true``, put a space between operator overloading and opening
64046412
parentheses.
64056413

clang/include/clang/Format/Format.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4704,6 +4704,13 @@ struct FormatStyle {
47044704
/// <conditional-body> <conditional-body>
47054705
/// \endcode
47064706
bool AfterIfMacros;
4707+
/// If ``true``, put a space between alternative operator ``not`` and the
4708+
/// opening parenthesis.
4709+
/// \code
4710+
/// true: false:
4711+
/// return not (a || b); vs. return not(a || b);
4712+
/// \endcode
4713+
bool AfterNot;
47074714
/// If ``true``, put a space between operator overloading and opening
47084715
/// parentheses.
47094716
/// \code
@@ -4752,9 +4759,9 @@ struct FormatStyle {
47524759
: AfterControlStatements(false), AfterForeachMacros(false),
47534760
AfterFunctionDeclarationName(false),
47544761
AfterFunctionDefinitionName(false), AfterIfMacros(false),
4755-
AfterOverloadedOperator(false), AfterPlacementOperator(true),
4756-
AfterRequiresInClause(false), AfterRequiresInExpression(false),
4757-
BeforeNonEmptyParentheses(false) {}
4762+
AfterNot(false), AfterOverloadedOperator(false),
4763+
AfterPlacementOperator(true), AfterRequiresInClause(false),
4764+
AfterRequiresInExpression(false), BeforeNonEmptyParentheses(false) {}
47584765

47594766
bool operator==(const SpaceBeforeParensCustom &Other) const {
47604767
return AfterControlStatements == Other.AfterControlStatements &&
@@ -4763,6 +4770,7 @@ struct FormatStyle {
47634770
Other.AfterFunctionDeclarationName &&
47644771
AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName &&
47654772
AfterIfMacros == Other.AfterIfMacros &&
4773+
AfterNot == Other.AfterNot &&
47664774
AfterOverloadedOperator == Other.AfterOverloadedOperator &&
47674775
AfterPlacementOperator == Other.AfterPlacementOperator &&
47684776
AfterRequiresInClause == Other.AfterRequiresInClause &&

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ template <> struct MappingTraits<FormatStyle::SpaceBeforeParensCustom> {
731731
IO.mapOptional("AfterFunctionDeclarationName",
732732
Spacing.AfterFunctionDeclarationName);
733733
IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros);
734+
IO.mapOptional("AfterNot", Spacing.AfterNot);
734735
IO.mapOptional("AfterOverloadedOperator", Spacing.AfterOverloadedOperator);
735736
IO.mapOptional("AfterPlacementOperator", Spacing.AfterPlacementOperator);
736737
IO.mapOptional("AfterRequiresInClause", Spacing.AfterRequiresInClause);

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5478,7 +5478,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
54785478
if (Left.TokenText == "!")
54795479
return Style.SpaceAfterLogicalNot;
54805480
assert(Left.TokenText == "not");
5481-
return Right.isOneOf(tok::coloncolon, TT_UnaryOperator);
5481+
return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
5482+
(Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
54825483
}
54835484

54845485
// If the next token is a binary operator or a selector name, we have

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
249249
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
250250
AfterFunctionDefinitionName);
251251
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
252+
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterNot);
252253
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
253254
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterPlacementOperator);
254255
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17674,6 +17674,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1767417674
verifyFormat("int x = int (y);", SomeSpace2);
1767517675
verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
1767617676

17677+
auto Style = getLLVMStyle();
17678+
Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17679+
EXPECT_FALSE(Style.SpaceBeforeParensOptions.AfterNot);
17680+
Style.SpaceBeforeParensOptions.AfterNot = true;
17681+
verifyFormat("return not (a || b);", Style);
17682+
1767717683
FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
1767817684
SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
1767917685
SpaceAfterOverloadedOperator.SpaceBeforeParensOptions

0 commit comments

Comments
 (0)