Skip to content

Commit d05543e

Browse files
owencatstellar
authored andcommitted
[clang-format] Keep the space between not and a unary operator (llvm#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 llvm#125465 (cherry picked from commit f344838)
1 parent 81220e6 commit d05543e

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
@@ -5436,7 +5436,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
54365436
// handled.
54375437
if (Left.is(tok::amp) && Right.is(tok::r_square))
54385438
return Style.SpacesInSquareBrackets;
5439-
return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
5439+
if (Left.isNot(tok::exclaim))
5440+
return false;
5441+
if (Left.TokenText == "!")
5442+
return Style.SpaceAfterLogicalNot;
5443+
assert(Left.TokenText == "not");
5444+
return Right.isOneOf(tok::coloncolon, TT_UnaryOperator);
54405445
}
54415446

54425447
// 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)