[PATCH] D120140: [clang-format] Avoid inserting space after C++ casts.

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 18 09:25:22 PST 2022


curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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

This is a solution for standard C++ casts: const_cast, dynamic_cast, reinterpret_cast, static_cast.

A general approach handling all possible casts is not possible without semantic information.
Consider the code:

  static_cast<T>(*function_pointer_variable)(arguments);

vs.

  some_return_type<T> (*function_pointer_variable)(parameters);
  // Later used as:
  function_pointer_variable = &some_function;
  return function_pointer_variable(args);

In the latter case, it's not a cast but a variable declaration of a pointer to function.
Without knowing what `some_return_type<T>` is (and clang-format does not know it), it's hard to distinguish between the two cases. Theoretically, one could check whether "parameters" are types (not a cast) and "arguments" are value/expressions (a cast), but that might be inefficient (needs lots of lookahead).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120140

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
@@ -10565,6 +10565,13 @@
 
 TEST_F(FormatTest, FormatsCasts) {
   verifyFormat("Type *A = static_cast<Type *>(P);");
+  verifyFormat("static_cast<Type *>(P);");
+  verifyFormat("static_cast<Type &>(Fun)(Args);");
+  verifyFormat("static_cast<Type &>(*Fun)(Args);");
+  verifyFormat("a = static_cast<Type &>(*Fun)(Args);");
+  verifyFormat("const_cast<Type &>(*Fun)(Args);");
+  verifyFormat("dynamic_cast<Type &>(*Fun)(Args);");
+  verifyFormat("reinterpret_cast<Type &>(*Fun)(Args);");
   verifyFormat("Type *A = (Type *)P;");
   verifyFormat("Type *A = (vector<Type *, int *>)P;");
   verifyFormat("int a = (int)(2.0f);");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1734,8 +1734,11 @@
       else
         Current.setType(TT_LineComment);
     } else if (Current.is(tok::r_paren)) {
-      if (rParenEndsCast(Current))
+      if (rParenEndsCast(Current)) {
         Current.setType(TT_CastRParen);
+        assert(Current.MatchingParen);
+        Current.MatchingParen->setType(TT_Unknown);
+      }
       if (Current.MatchingParen && Current.Next &&
           !Current.Next->isBinaryOperator() &&
           !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
@@ -1938,8 +1941,20 @@
 
       // Certain other tokens right before the parentheses are also signals that
       // this cannot be a cast.
+      if (LeftOfParens->is(TT_TemplateCloser)) {
+        if (LeftOfParens->MatchingParen) {
+          auto *Prev = LeftOfParens->MatchingParen->getPreviousNonComment();
+          if (Prev &&
+              Prev->isOneOf(tok::kw_const_cast, tok::kw_dynamic_cast,
+                            tok::kw_reinterpret_cast, tok::kw_static_cast))
+            // FIXME: Maybe we should handle identifiers ending with "_cast",
+            // e.g. bit_cast?
+            return true;
+        }
+        return false;
+      }
       if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
-                                TT_TemplateCloser, tok::ellipsis))
+                                tok::ellipsis))
         return false;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120140.409959.patch
Type: text/x-patch
Size: 2413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220218/e6a26c87/attachment.bin>


More information about the cfe-commits mailing list