[clang] [clang-format] Add AfterNot to SpaceBeforeParensOptions (PR #150367)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 23 21:07:13 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Closes #<!-- -->149971
---
Full diff: https://github.com/llvm/llvm-project/pull/150367.diff
6 Files Affected:
- (modified) clang/docs/ClangFormatStyleOptions.rst (+8)
- (modified) clang/include/clang/Format/Format.h (+11-3)
- (modified) clang/lib/Format/Format.cpp (+1)
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2-1)
- (modified) clang/unittests/Format/ConfigParseTest.cpp (+1)
- (modified) clang/unittests/Format/FormatTest.cpp (+6)
``````````diff
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index d39ee49b432e5..7f970f6713824 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -6400,6 +6400,14 @@ the configuration (without a prefix: ``Auto``).
IF (...) vs. IF(...)
<conditional-body> <conditional-body>
+ * ``bool AfterNot`` If ``true``, put a space between alternative operator ``not`` and the
+ opening parenthesis.
+
+ .. code-block:: c++
+
+ true: false:
+ return not (a || b); vs. return not(a || b);
+
* ``bool AfterOverloadedOperator`` If ``true``, put a space between operator overloading and opening
parentheses.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 7677604484f52..a54ab19b7bae6 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -4704,6 +4704,13 @@ struct FormatStyle {
/// <conditional-body> <conditional-body>
/// \endcode
bool AfterIfMacros;
+ /// If ``true``, put a space between alternative operator ``not`` and the
+ /// opening parenthesis.
+ /// \code
+ /// true: false:
+ /// return not (a || b); vs. return not(a || b);
+ /// \endcode
+ bool AfterNot;
/// If ``true``, put a space between operator overloading and opening
/// parentheses.
/// \code
@@ -4752,9 +4759,9 @@ struct FormatStyle {
: AfterControlStatements(false), AfterForeachMacros(false),
AfterFunctionDeclarationName(false),
AfterFunctionDefinitionName(false), AfterIfMacros(false),
- AfterOverloadedOperator(false), AfterPlacementOperator(true),
- AfterRequiresInClause(false), AfterRequiresInExpression(false),
- BeforeNonEmptyParentheses(false) {}
+ AfterNot(false), AfterOverloadedOperator(false),
+ AfterPlacementOperator(true), AfterRequiresInClause(false),
+ AfterRequiresInExpression(false), BeforeNonEmptyParentheses(false) {}
bool operator==(const SpaceBeforeParensCustom &Other) const {
return AfterControlStatements == Other.AfterControlStatements &&
@@ -4763,6 +4770,7 @@ struct FormatStyle {
Other.AfterFunctionDeclarationName &&
AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName &&
AfterIfMacros == Other.AfterIfMacros &&
+ AfterNot == Other.AfterNot &&
AfterOverloadedOperator == Other.AfterOverloadedOperator &&
AfterPlacementOperator == Other.AfterPlacementOperator &&
AfterRequiresInClause == Other.AfterRequiresInClause &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 62feb3db0ed5e..f247af6718437 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -731,6 +731,7 @@ template <> struct MappingTraits<FormatStyle::SpaceBeforeParensCustom> {
IO.mapOptional("AfterFunctionDeclarationName",
Spacing.AfterFunctionDeclarationName);
IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros);
+ IO.mapOptional("AfterNot", Spacing.AfterNot);
IO.mapOptional("AfterOverloadedOperator", Spacing.AfterOverloadedOperator);
IO.mapOptional("AfterPlacementOperator", Spacing.AfterPlacementOperator);
IO.mapOptional("AfterRequiresInClause", Spacing.AfterRequiresInClause);
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 581bfbab0972d..d28d2fd375fb3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5478,7 +5478,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
if (Left.TokenText == "!")
return Style.SpaceAfterLogicalNot;
assert(Left.TokenText == "not");
- return Right.isOneOf(tok::coloncolon, TT_UnaryOperator);
+ return Right.isOneOf(tok::coloncolon, TT_UnaryOperator) ||
+ (Right.is(tok::l_paren) && Style.SpaceBeforeParensOptions.AfterNot);
}
// If the next token is a binary operator or a selector name, we have
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 65d8b36c677bd..9de3cca71630d 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -249,6 +249,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions,
AfterFunctionDefinitionName);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
+ CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterNot);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterPlacementOperator);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index dbf6950446ef0..dc55068b5f5c8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17664,6 +17664,12 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("int x = int (y);", SomeSpace2);
verifyFormat("auto lambda = []() { return 0; };", SomeSpace2);
+ auto Style = getLLVMStyle();
+ Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
+ EXPECT_FALSE(Style.SpaceBeforeParensOptions.AfterNot);
+ Style.SpaceBeforeParensOptions.AfterNot = true;
+ verifyFormat("return not (a || b);", Style);
+
FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle();
SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
SpaceAfterOverloadedOperator.SpaceBeforeParensOptions
``````````
</details>
https://github.com/llvm/llvm-project/pull/150367
More information about the cfe-commits
mailing list