Skip to content

Commit f344838

Browse files
authored
[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
1 parent 2795abb commit f344838

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
@@ -5472,7 +5472,12 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
54725472
// handled.
54735473
if (Left.is(tok::amp) && Right.is(tok::r_square))
54745474
return Style.SpacesInSquareBrackets;
5475-
return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
5475+
if (Left.isNot(tok::exclaim))
5476+
return false;
5477+
if (Left.TokenText == "!")
5478+
return Style.SpaceAfterLogicalNot;
5479+
assert(Left.TokenText == "not");
5480+
return Right.isOneOf(tok::coloncolon, TT_UnaryOperator);
54765481
}
54775482

54785483
// 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
@@ -25393,16 +25393,17 @@ TEST_F(FormatTest, AlternativeOperators) {
2539325393
verifyFormat("%:define ABC abc"); // #define ABC abc
2539425394
verifyFormat("%:%:"); // ##
2539525395

25396+
verifyFormat("return not ::f();");
25397+
verifyFormat("return not *foo;");
25398+
2539625399
verifyFormat("a = v(not;);\n"
25397-
"b = v(not+);\n"
2539825400
"c = v(not x);\n"
2539925401
"d = v(not 1);\n"
2540025402
"e = v(not 123.f);");
2540125403

2540225404
verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
2540325405
" V(and) \\\n"
2540425406
" V(not) \\\n"
25405-
" V(not!) \\\n"
2540625407
" V(other)",
2540725408
getLLVMStyleWithColumns(40));
2540825409
}

0 commit comments

Comments
 (0)