[PATCH] D147318: [clang-format] Don't format typename template parameters as expression

Emilia Dreamer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 1 06:48:09 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG50acd67018a5: [clang-format] Don't format typename template parameters as expression (authored by rymiel).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147318/new/

https://reviews.llvm.org/D147318

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


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -261,6 +261,15 @@
   Tokens = annotate("template <typename T, bool B = C && D> struct S {};");
   ASSERT_EQ(Tokens.size(), 18u) << Tokens;
   EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);
+
+  Tokens = annotate("template <typename T, typename U = T&&> struct S {};");
+  ASSERT_EQ(Tokens.size(), 17u) << Tokens;
+  EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_PointerOrReference);
+
+  Tokens = annotate("template <typename T = int (*)(int)> struct S {};");
+  ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen);
+  EXPECT_TOKEN(Tokens[7], tok::star, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1723,10 +1723,13 @@
           return false;
         }
 
-        // This is the default value of a non-template type parameter, so treat
-        // it as an expression.
-        if (Contexts.back().ContextKind == tok::less)
-          return true;
+        // This is the default value of a template parameter, determine if it's
+        // type or non-type.
+        if (Contexts.back().ContextKind == tok::less) {
+          assert(Current.Previous->Previous);
+          return !Current.Previous->Previous->isOneOf(tok::kw_typename,
+                                                      tok::kw_class);
+        }
 
         Tok = Tok->MatchingParen;
         if (!Tok)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147318.510222.patch
Type: text/x-patch
Size: 1797 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230401/8406b00a/attachment-0001.bin>


More information about the cfe-commits mailing list