aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2024-04-26 19:54:36 -0700
committerGitHub <noreply@github.com>2024-04-26 19:54:36 -0700
commit315dc4bbc730a3c672967c27587088cfe9752fe6 (patch)
tree31eece26c3bbe717966f5228f86c8bb542204d2a
parente04df693bf5b38099ef1d7ab8e6ce6a1469597e2 (diff)
downloadllvm-315dc4bbc730a3c672967c27587088cfe9752fe6.zip
llvm-315dc4bbc730a3c672967c27587088cfe9752fe6.tar.gz
llvm-315dc4bbc730a3c672967c27587088cfe9752fe6.tar.bz2
[clang-format] Add a space after a word token only if required (#90161)
Fixes #78166.
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp26
-rw-r--r--clang/unittests/Format/FormatTest.cpp25
2 files changed, 24 insertions, 27 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index cdfb425..63629fa 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4834,10 +4834,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
Right.is(TT_TemplateOpener)) {
return true;
}
- if (Left.is(tok::identifier) && Right.is(tok::numeric_constant) &&
- Right.TokenText[0] == '.') {
- return false;
- }
+ if (Left.Tok.getIdentifierInfo() && Right.is(tok::numeric_constant))
+ return Right.TokenText[0] != '.';
} else if (Style.isProto()) {
if (Right.is(tok::period) &&
Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
@@ -5266,21 +5264,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return true;
}
if (Left.is(TT_UnaryOperator)) {
- if (Right.isNot(tok::l_paren)) {
- // The alternative operators for ~ and ! are "compl" and "not".
- // If they are used instead, we do not want to combine them with
- // the token to the right, unless that is a left paren.
- if (Left.is(tok::exclaim) && Left.TokenText == "not")
- return true;
- if (Left.is(tok::tilde) && Left.TokenText == "compl")
- return true;
- // Lambda captures allow for a lone &, so "&]" needs to be properly
- // handled.
- if (Left.is(tok::amp) && Right.is(tok::r_square))
- return Style.SpacesInSquareBrackets;
- }
- return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
- Right.is(TT_BinaryOperator);
+ // Lambda captures allow for a lone &, so "&]" needs to be properly
+ // handled.
+ if (Left.is(tok::amp) && Right.is(tok::r_square))
+ return Style.SpacesInSquareBrackets;
+ return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
}
// 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 bc61b9c..8ecc118 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24507,16 +24507,25 @@ TEST_F(FormatTest, AlternativeOperators) {
verifyFormat("int a compl(5);");
verifyFormat("int a not(5);");
- /* FIXME handle alternate tokens
- * https://en.cppreference.com/w/cpp/language/operator_alternative
- // alternative tokens
- verifyFormat("compl foo();"); // ~foo();
- verifyFormat("foo() <%%>;"); // foo();
- verifyFormat("void foo() <%%>;"); // void foo(){}
- verifyFormat("int a <:1:>;"); // int a[1];[
+ verifyFormat("compl foo();"); // ~foo();
+ verifyFormat("foo() <%%>"); // foo() {}
+ verifyFormat("void foo() <%%>"); // void foo() {}
+ verifyFormat("int a<:1:>;"); // int a[1];
verifyFormat("%:define ABC abc"); // #define ABC abc
verifyFormat("%:%:"); // ##
- */
+
+ verifyFormat("a = v(not;);\n"
+ "b = v(not+);\n"
+ "c = v(not x);\n"
+ "d = v(not 1);\n"
+ "e = v(not 123.f);");
+
+ verifyNoChange("#define ASSEMBLER_INSTRUCTION_LIST(V) \\\n"
+ " V(and) \\\n"
+ " V(not) \\\n"
+ " V(not!) \\\n"
+ " V(other)",
+ getLLVMStyleWithColumns(40));
}
TEST_F(FormatTest, STLWhileNotDefineChed) {