Author: Owen Pan Date: 2025-04-09T17:52:12-07:00 New Revision: f34483838937b1a01ee11ee22bdd6e13c81e9fff
URL: https://github.com/llvm/llvm-project/commit/f34483838937b1a01ee11ee22bdd6e13c81e9fff DIFF: https://github.com/llvm/llvm-project/commit/f34483838937b1a01ee11ee22bdd6e13c81e9fff.diff LOG: [clang-format] Keep the space between `not` and a unary operator (#135035) Also keep the space between `not` and `::`. Based on the [documentation](https://releases.llvm.org/20.1.0/tools/clang/docs/ClangFormatStyleOptions.html#spaceafterlogicalnot), it can be argued that SpaceAfterLogicalNot doesn't cover the alternative operator `not`. Closes #125465 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index a3aa69a1c17b4..46cd009da281e 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -5472,7 +5472,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, // handled. if (Left.is(tok::amp) && Right.is(tok::r_square)) return Style.SpacesInSquareBrackets; - return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim); + if (Left.isNot(tok::exclaim)) + return false; + if (Left.TokenText == "!") + return Style.SpaceAfterLogicalNot; + assert(Left.TokenText == "not"); + return Right.isOneOf(tok::coloncolon, TT_UnaryOperator); } // If the next token is a binary operator or a selector name, we have diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 69c9ee1d1dcb2..f0e67c604cc4b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -25393,8 +25393,10 @@ TEST_F(FormatTest, AlternativeOperators) { verifyFormat("%:define ABC abc"); // #define ABC abc verifyFormat("%:%:"); // ## + verifyFormat("return not ::f();"); + verifyFormat("return not *foo;"); + verifyFormat("a = v(not;);\n" - "b = v(not+);\n" "c = v(not x);\n" "d = v(not 1);\n" "e = v(not 123.f);"); @@ -25402,7 +25404,6 @@ TEST_F(FormatTest, AlternativeOperators) { verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n" " V(and) \\\n" " V(not) \\\n" - " V(not!) \\\n" " V(other)", getLLVMStyleWithColumns(40)); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits