[PATCH] D120374: [clang-format] Do not insert space after new/delete keywords in C function declarations

Luis Penagos via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 22 20:13:42 PST 2022


penagos created this revision.
penagos added reviewers: curdeius, MyDeveloperDay, HazardyKnusperkeks.
penagos updated this revision to Diff 410700.
penagos added a comment.
penagos published this revision for review.
penagos added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The windows build failure appears to be unrelated to this change. I found the need to include `!Left.Previous` in the conditional so as to account for the existing unittest in the form of:

  new (expr)

To not wind up formatting this case as:

  new(expr)

Though it's still unclear to me whether or not it'd make more sense to continue to pursue a change to introduce `C` as a language. Thoughts?


Fixes https://github.com/llvm/llvm-project/issues/46915.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120374

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -9919,6 +9919,11 @@
   verifyFormat("void operator new(void *foo) ATTRIB;");
   verifyFormat("void operator delete[](void *foo) ATTRIB;");
   verifyFormat("void operator delete(void *ptr) noexcept;");
+
+  EXPECT_EQ("void new(link p);\n"
+            "void delete(link p);\n",
+            format("void new (link p);\n"
+                   "void delete(link p);\n"));
 }
 
 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3299,11 +3299,15 @@
       if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
         return Style.SpaceBeforeParensOptions.AfterControlStatements ||
                spaceRequiredBeforeParens(Right);
-      if (Left.isOneOf(tok::kw_new, tok::kw_delete) ||
-          (Left.is(tok::r_square) && Left.MatchingParen &&
-           Left.MatchingParen->Previous &&
-           Left.MatchingParen->Previous->is(tok::kw_delete)))
-        return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
+      if (Left.isOneOf(tok::kw_new, tok::kw_delete))
+        return ((!Line.MightBeFunctionDecl || !Left.Previous) &&
+                Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
+               spaceRequiredBeforeParens(Right);
+
+      if (Left.is(tok::r_square) && Left.MatchingParen &&
+          Left.MatchingParen->Previous &&
+          Left.MatchingParen->Previous->is(tok::kw_delete))
+        return (Style.SpaceBeforeParens != FormatStyle::SBPO_Never) ||
                spaceRequiredBeforeParens(Right);
     }
     if (Line.Type != LT_PreprocessorDirective &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120374.410700.patch
Type: text/x-patch
Size: 1916 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220223/6de5e153/attachment.bin>


More information about the cfe-commits mailing list