Skip to content

Commit 315dc4b

Browse files
authored
[clang-format] Add a space after a word token only if required (#90161)
Fixes #78166.
1 parent e04df69 commit 315dc4b

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,10 +4834,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
48344834
Right.is(TT_TemplateOpener)) {
48354835
return true;
48364836
}
4837-
if (Left.is(tok::identifier) && Right.is(tok::numeric_constant) &&
4838-
Right.TokenText[0] == '.') {
4839-
return false;
4840-
}
4837+
if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
4838+
return Right.TokenText[0] != '.';
48414839
} else if (Style.isProto()) {
48424840
if (Right.is(tok::period) &&
48434841
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
@@ -5266,21 +5264,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
52665264
return true;
52675265
}
52685266
if (Left.is(TT_UnaryOperator)) {
5269-
if (Right.isNot(tok::l_paren)) {
5270-
// The alternative operators for ~ and ! are "compl" and "not".
5271-
// If they are used instead, we do not want to combine them with
5272-
// the token to the right, unless that is a left paren.
5273-
if (Left.is(tok::exclaim) && Left.TokenText == "not")
5274-
return true;
5275-
if (Left.is(tok::tilde) && Left.TokenText == "compl")
5276-
return true;
5277-
// Lambda captures allow for a lone &, so "&]" needs to be properly
5278-
// handled.
5279-
if (Left.is(tok::amp) && Right.is(tok::r_square))
5280-
return Style.SpacesInSquareBrackets;
5281-
}
5282-
return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
5283-
Right.is(TT_BinaryOperator);
5267+
// Lambda captures allow for a lone &, so "&]" needs to be properly
5268+
// handled.
5269+
if (Left.is(tok::amp) && Right.is(tok::r_square))
5270+
return Style.SpacesInSquareBrackets;
5271+
return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
52845272
}
52855273

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

clang/unittests/Format/FormatTest.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24507,16 +24507,25 @@ TEST_F(FormatTest, AlternativeOperators) {
2450724507
verifyFormat("int a compl(5);");
2450824508
verifyFormat("int a not(5);");
2450924509

24510-
/* FIXME handle alternate tokens
24511-
* https://en.cppreference.com/w/cpp/language/operator_alternative
24512-
// alternative tokens
24513-
verifyFormat("compl foo();"); // ~foo();
24514-
verifyFormat("foo() <%%>;"); // foo();
24515-
verifyFormat("void foo() <%%>;"); // void foo(){}
24516-
verifyFormat("int a <:1:>;"); // int a[1];[
24510+
verifyFormat("compl foo();"); // ~foo();
24511+
verifyFormat("foo() <%%>"); // foo() {}
24512+
verifyFormat("void foo() <%%>"); // void foo() {}
24513+
verifyFormat("int a<:1:>;"); // int a[1];
2451724514
verifyFormat("%:define ABC abc"); // #define ABC abc
2451824515
verifyFormat("%:%:"); // ##
24519-
*/
24516+
24517+
verifyFormat("a = v(not;);\n"
24518+
"b = v(not+);\n"
24519+
"c = v(not x);\n"
24520+
"d = v(not 1);\n"
24521+
"e = v(not 123.f);");
24522+
24523+
verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
24524+
" V(and) \\\n"
24525+
" V(not) \\\n"
24526+
" V(not!) \\\n"
24527+
" V(other)",
24528+
getLLVMStyleWithColumns(40));
2452024529
}
2452124530

2452224531
TEST_F(FormatTest, STLWhileNotDefineChed) {

0 commit comments

Comments
 (0)