[clang] [clang-format] adds a space after not inside macros (PR #78176)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 00:31:28 PST 2024


https://github.com/mydeveloperday updated https://github.com/llvm/llvm-project/pull/78176

>From 9d29ad06ff71b855a43f57b339990e41f206ac8d Mon Sep 17 00:00:00 2001
From: mydeveloperday <mydeveloperday at gmail.com>
Date: Mon, 15 Jan 2024 15:42:59 +0000
Subject: [PATCH 1/2] [clang-format] adds a space after not inside macros

No need to add an extract space if merging the characters
doesn't cause a problem, hence ) and ! as well as ( do not
require the extraneous space.
---
 clang/lib/Format/TokenAnnotator.cpp   | 2 +-
 clang/unittests/Format/FormatTest.cpp | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 227aa0b97af6ba4..e45271809e80854 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4842,7 +4842,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     return true;
   }
   if (Left.is(TT_UnaryOperator)) {
-    if (Right.isNot(tok::l_paren)) {
+    if (!Right.isOneOf(tok::r_paren, tok::l_paren, tok::exclaim)) {
       // 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.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8f115fb8cbf0fbe..54b8593fb98d424 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24159,6 +24159,8 @@ TEST_F(FormatTest, AlternativeOperators) {
 
   verifyFormat("int a compl(5);");
   verifyFormat("int a not(5);");
+  verifyFormat("v(not)");
+  verifyFormat("v(not!)");
 
   /* FIXME handle alternate tokens
    * https://en.cppreference.com/w/cpp/language/operator_alternative

>From ccea956d6a3ebf619520f1b5097d78bc71f8c065 Mon Sep 17 00:00:00 2001
From: mydeveloperday <mydeveloperday at gmail.com>
Date: Tue, 16 Jan 2024 08:30:14 +0000
Subject: [PATCH 2/2] [clang-format] adds a space after not inside macros

No need to add an extract space if merging the characters
doesn't cause a problem, hence ) and ! as well as ( do not
require the extraneous space.
---
 clang/lib/Format/TokenAnnotator.cpp   | 10 +++++-----
 clang/unittests/Format/FormatTest.cpp |  6 ++++++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e45271809e80854..0eec0195b1c63a9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4842,7 +4842,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     return true;
   }
   if (Left.is(TT_UnaryOperator)) {
-    if (!Right.isOneOf(tok::r_paren, tok::l_paren, tok::exclaim)) {
+    if (Right.isOneOf(tok::identifier, tok::numeric_constant)) {
       // 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.
@@ -4850,11 +4850,11 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
         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;
     }
+    // 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);
   }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 54b8593fb98d424..5a540672eca2a39 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24159,8 +24159,14 @@ TEST_F(FormatTest, AlternativeOperators) {
 
   verifyFormat("int a compl(5);");
   verifyFormat("int a not(5);");
+
   verifyFormat("v(not)");
   verifyFormat("v(not!)");
+  verifyFormat("Symbol(not, None)");
+  verifyFormat("Symbol(not!, None)");
+
+  verifyFormat("assert(!\"fail\")");
+  verifyFormat("assert(not\"fail\")");
 
   /* FIXME handle alternate tokens
    * https://en.cppreference.com/w/cpp/language/operator_alternative



More information about the cfe-commits mailing list