[clang] Fix a UsingTemplate regression after 3e78fa860235431323aaf08c8fa922d75a7cfffa
authorHaojian Wu <hokein.wu@gmail.com>
Thu, 16 Mar 2023 08:42:41 +0000 (09:42 +0100)
committerHaojian Wu <hokein.wu@gmail.com>
Thu, 16 Mar 2023 11:59:26 +0000 (12:59 +0100)
TemplateName::getAsTemplateDecl() returns the underlying TemplateDecl
for a UsingTemplate kind template name. We should respect that in the
Profile method otherwise we might desugar the template name unexpectedly
(e.g. for template argument deduction with deduciton guides).

Differential Revision: https://reviews.llvm.org/D146202

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
clang/lib/AST/TemplateName.cpp
clang/test/AST/ast-dump-using-template.cpp

index 7dcbf47..68b6b21 100644 (file)
@@ -171,6 +171,13 @@ TEST(WalkAST, TemplateNames) {
       namespace ns {template <typename> struct S {}; }
       using ns::$explicit^S;)cpp",
            "^S<int> x;");
+  testWalk(R"cpp(
+      namespace ns {
+        template <typename T> struct S { S(T);};
+        template <typename T> S(T t) -> S<T>;
+      }
+      using ns::$explicit^S;)cpp",
+      "^S x(123);");
   testWalk("template<typename> struct $explicit^S {};",
            R"cpp(
       template <template <typename> typename> struct X {};
index 76f507b..2f0e418 100644 (file)
@@ -282,7 +282,9 @@ bool TemplateName::containsUnexpandedParameterPack() const {
 }
 
 void TemplateName::Profile(llvm::FoldingSetNodeID &ID) {
-  if (auto *TD = getAsTemplateDecl())
+  if (const auto* USD = getAsUsingShadowDecl())
+    ID.AddPointer(USD->getCanonicalDecl());
+  else if (const auto *TD = getAsTemplateDecl())
     ID.AddPointer(TD->getCanonicalDecl());
   else
     ID.AddPointer(Storage.getOpaqueValue());
index da18f04..de3ce27 100644 (file)
@@ -9,8 +9,11 @@ template<typename T> class S {
  public:
    S(T);
 };
+template<typename T> struct S2 { S2(T); };
+template <typename T> S2(T t) -> S2<T>;
 }
 using ns::S;
+using ns::S2;
 
 // TemplateName in TemplateSpecializationType.
 template<typename T>
@@ -36,3 +39,10 @@ using C = decltype(DeducedTemplateSpecializationT);
 // CHECK-NEXT:  |-DeclRefExpr {{.*}}
 // CHECK-NEXT:  `-ElaboratedType {{.*}} 'S<int>' sugar
 // CHECK-NEXT:    `-DeducedTemplateSpecializationType {{.*}} 'ns::S<int>' sugar using
+
+S2 DeducedTemplateSpecializationT2(123);
+using D = decltype(DeducedTemplateSpecializationT2);
+// CHECK:      DecltypeType {{.*}}
+// CHECK-NEXT:  |-DeclRefExpr {{.*}}
+// CHECK-NEXT:  `-ElaboratedType {{.*}} 'S2<int>' sugar
+// CHECK-NEXT:    `-DeducedTemplateSpecializationType {{.*}} 'S2<int>' sugar using