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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 00:22:15 PST 2024


owenca wrote:

This should work:
```
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4402,9 +4402,17 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
   if (Left.Finalized)
     return Right.hasWhitespaceBefore();
 
-  // Never ever merge two words.
-  if (Keywords.isWordLike(Right) && Keywords.isWordLike(Left))
-    return true;
+  if (Style.isVerilog()) {
+    if (Keywords.isWordLike(Left) && Keywords.isWordLike(Right))
+      return true;
+  } else {
+    auto IsWordOrNumber = [](const auto &Tok) {
+      return Tok.Tok.getIdentifierInfo() || Tok.is(tok::numeric_constant);
+    };
+    // Never ever merge two words/numbers.
+    if (IsWordOrNumber(Left) && IsWordOrNumber(Right))
+      return true;
+  }
 
   // Leave a space between * and /* to avoid C4138 `comment end` found outside
   // of comment.
@@ -4842,21 +4850,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
```

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


More information about the cfe-commits mailing list