Skip to content

Commit 6077f45

Browse files
owencatru
authored andcommitted
[clang-format] Add AfterNot to SpaceBeforeParensOptions (#150367)
Closes #149971 (cherry picked from commit 97c9534)
1 parent 9554152 commit 6077f45

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
@@ -6390,6 +6390,14 @@ the configuration (without a prefix: ``Auto``).
63906390
IF (...) vs. IF(...)
63916391
<conditional-body> <conditional-body>
63926392

6393+
* ``bool AfterNot`` If ``true``, put a space between alternative operator ``not`` and the
6394+
opening parenthesis.
6395+
6396+
.. code-block:: c++
6397+
6398+
true: false:
6399+
return not (a || b); vs. return not(a || b);
6400+
63936401
* ``bool AfterOverloadedOperator`` If ``true``, put a space between operator overloading and opening
63946402
parentheses.
63956403

clang/include/clang/Format/Format.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,6 +4694,13 @@ struct FormatStyle {
46944694
/// <conditional-body> <conditional-body>
46954695
/// \endcode
46964696
bool AfterIfMacros;
4697+
/// If ``true``, put a space between alternative operator ``not`` and the
4698+
/// opening parenthesis.
4699+
/// \code
4700+
/// true: false:
4701+
/// return not (a || b); vs. return not(a || b);
4702+
/// \endcode
4703+
bool AfterNot;
46974704
/// If ``true``, put a space between operator overloading and opening
46984705
/// parentheses.
46994706
/// \code
@@ -4742,9 +4749,9 @@ struct FormatStyle {
47424749
: AfterControlStatements(false), AfterForeachMacros(false),
47434750
AfterFunctionDeclarationName(false),
47444751
AfterFunctionDefinitionName(false), AfterIfMacros(false),
4745-
AfterOverloadedOperator(false), AfterPlacementOperator(true),
4746-
AfterRequiresInClause(false), AfterRequiresInExpression(false),
4747-
BeforeNonEmptyParentheses(false) {}
4752+
AfterNot(false), AfterOverloadedOperator(false),
4753+
AfterPlacementOperator(true), AfterRequiresInClause(false),
4754+
AfterRequiresInExpression(false), BeforeNonEmptyParentheses(false) {}
47484755

47494756
bool operator==(const SpaceBeforeParensCustom &Other) const {
47504757
return AfterControlStatements == Other.AfterControlStatements &&
@@ -4753,6 +4760,7 @@ struct FormatStyle {
47534760
Other.AfterFunctionDeclarationName &&
47544761
AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName &&
47554762
AfterIfMacros == Other.AfterIfMacros &&
4763+
AfterNot == Other.AfterNot &&
47564764
AfterOverloadedOperator == Other.AfterOverloadedOperator &&
47574765
AfterPlacementOperator == Other.AfterPlacementOperator &&
47584766
AfterRequiresInClause == Other.AfterRequiresInClause &&

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ template <> struct MappingTraits<FormatStyle::SpaceBeforeParensCustom> {
727727
IO.mapOptional("AfterFunctionDeclarationName",
728728
Spacing.AfterFunctionDeclarationName);
729729
IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros);
730+
IO.mapOptional("AfterNot", Spacing.AfterNot);
730731
IO.mapOptional("AfterOverloadedOperator", Spacing.AfterOverloadedOperator);
731732
IO.mapOptional("AfterPlacementOperator", Spacing.AfterPlacementOperator);
732733
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
@@ -17652,6 +17652,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1765217652
verifyFormat("int x = int (y);", SomeSpace2);
1765317653
verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
1765417654

17655+
auto Style = getLLVMStyle();
17656+
Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
17657+
EXPECT_FALSE(Style.SpaceBeforeParensOptions.AfterNot);
17658+
Style.SpaceBeforeParensOptions.AfterNot = true;
17659+
verifyFormat("return not (a || b);", Style);
17660+
1765517661
FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
1765617662
SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
1765717663
SpaceAfterOverloadedOperator.SpaceBeforeParensOptions

0 commit comments

Comments
 (0)