Skip to content

Commit 352ee23

Browse files
owencallvmbot
authored andcommitted
[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 (cherry picked from commit f344838)
1 parent 656289f commit 352ee23

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5437,7 +5437,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
54375437
// handled.
54385438
if (Left.is(tok::amp) && Right.is(tok::r_square))
54395439
return Style.SpacesInSquareBrackets;
5440-
return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
5440+
if (Left.isNot(tok::exclaim))
5441+
return false;
5442+
if (Left.TokenText == "!")
5443+
return Style.SpaceAfterLogicalNot;
5444+
assert(Left.TokenText == "not");
5445+
return Right.isOneOf(tok::coloncolon, TT_UnaryOperator);
54415446
}
54425447

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

clang/unittests/Format/FormatTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25067,16 +25067,17 @@ TEST_F(FormatTest, AlternativeOperators) {
2506725067
verifyFormat("%:define ABC abc"); // #define ABC abc
2506825068
verifyFormat("%:%:"); // ##
2506925069

25070+
verifyFormat("return not ::f();");
25071+
verifyFormat("return not *foo;");
25072+
2507025073
verifyFormat("a = v(not;);\n"
25071-
"b = v(not+);\n"
2507225074
"c = v(not x);\n"
2507325075
"d = v(not 1);\n"
2507425076
"e = v(not 123.f);");
2507525077

2507625078
verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
2507725079
" V(and) \\\n"
2507825080
" V(not) \\\n"
25079-
" V(not!) \\\n"
2508025081
" V(other)",
2508125082
getLLVMStyleWithColumns(40));
2508225083
}

0 commit comments

Comments
 (0)