[PATCH] D139416: [clang-format] Don't require deduction guides to be templates

Emilia Dreamer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 6 05:24:04 PST 2022


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

The C++ standard doesn't require that class template deduction guides
be templates themselves, but previously `isDeductionGuide` would assert
for the existence of a template closer or requires clause closer before
the deduction guide declaration.

This patch simply removes that check. Because of this, a test which
asserted that `x()->x<1>;` *isn't* a deduction guide failed, as it is
now formatted as a deduction guide. However, as @JohelEGP demonstrated,
it is [possible to make that a viable deduction guide][1].

Thus, that test has been removed, but other tests related to
non-template class template deduction guides have been added.

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

[1]: https://compiler-explorer.com/z/Wx3K6d5K9


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139416

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
@@ -8001,12 +8001,14 @@
   verifyFormat("template <class T> x() -> x<1>;");
   verifyFormat("template <class T> explicit x(T &) -> x<1>;");
 
+  verifyFormat("A(const char *) -> A<string &>;");
+  verifyFormat("A() -> A<int>;");
+
   // Ensure not deduction guides.
   verifyFormat("c()->f<int>();");
   verifyFormat("x()->foo<1>;");
   verifyFormat("x = p->foo<3>();");
   verifyFormat("x()->x<1>();");
-  verifyFormat("x()->x<1>;");
 }
 
 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1804,21 +1804,8 @@
         FormatToken *LeadingIdentifier =
             Current.Previous->MatchingParen->Previous;
 
-        // Differentiate a deduction guide by seeing the
-        // > of the template prior to the leading identifier.
-        if (LeadingIdentifier) {
-          FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
-          // Skip back past explicit decoration
-          if (PriorLeadingIdentifier &&
-              PriorLeadingIdentifier->is(tok::kw_explicit)) {
-            PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
-          }
-
-          return PriorLeadingIdentifier &&
-                 (PriorLeadingIdentifier->is(TT_TemplateCloser) ||
-                  PriorLeadingIdentifier->ClosesRequiresClause) &&
-                 LeadingIdentifier->TokenText == Current.Next->TokenText;
-        }
+        return LeadingIdentifier &&
+               LeadingIdentifier->TokenText == Current.Next->TokenText;
       }
     }
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139416.480442.patch
Type: text/x-patch
Size: 1903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221206/fa94ab4b/attachment-0001.bin>


More information about the cfe-commits mailing list