[clang] [clang-format] Add space after a word token (PR #92741)

Robin Caloudis via cfe-commits cfe-commits at lists.llvm.org
Mon May 20 04:24:41 PDT 2024


https://github.com/robincaloudis created https://github.com/llvm/llvm-project/pull/92741

Closes https://github.com/llvm/llvm-project/issues/92688

>From 9e8c360029fb6789360ad4296e2f14098db76dd6 Mon Sep 17 00:00:00 2001
From: Robin Caloudis <robin.caloudis at gmx.de>
Date: Mon, 20 May 2024 13:21:32 +0200
Subject: [PATCH 1/2] Test binary after unary operator

---
 clang/unittests/Format/FormatTest.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6f57f10e12e88..ca0edd7b22630 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24545,6 +24545,13 @@ TEST_F(FormatTest, STLWhileNotDefineChed) {
                "#endif // while");
 }
 
+TEST_F(FormatTest, BinaryOperatorAfterUnaryOperator) {
+  verifyFormat("void test(void) {\n"
+               "  static void (*xor)(uint8_t *, size_t, uint8_t);\n"
+               "  xor = resolve_xor_x86();\n"
+               "}");
+}
+
 TEST_F(FormatTest, OperatorSpacing) {
   FormatStyle Style = getLLVMStyle();
   Style.PointerAlignment = FormatStyle::PAS_Right;

>From 76e17eee617ee4ec9fb2562579a38c60cce0f76a Mon Sep 17 00:00:00 2001
From: Robin Caloudis <robin.caloudis at gmx.de>
Date: Mon, 20 May 2024 13:21:54 +0200
Subject: [PATCH 2/2] Support binary after unary operator

---
 clang/lib/Format/TokenAnnotator.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7c4c76a91f2c5..7786b85e8a1fc 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5280,7 +5280,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     // handled.
     if (Left.is(tok::amp) && Right.is(tok::r_square))
       return Style.SpacesInSquareBrackets;
-    return Style.SpaceAfterLogicalNot && Left.is(tok::exclaim);
+    return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
+           Right.is(TT_BinaryOperator);
   }
 
   // If the next token is a binary operator or a selector name, we have



More information about the cfe-commits mailing list