[clang] [clang][Sema][NFC] Share member template instantiation lookup (PR #239450)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 18:02:04 PST 2026


https://github.com/ChenMiaoi created https://github.com/llvm/llvm-project/pull/239450

Use `RedeclarableTemplateDecl` to consolidate the identical `isInstantiationOf` implementations for class and function templates, leaving the other overloads unchanged.

I initially considered turning these helpers into a function template, but the declaration types use different accessors, such as `getInstantiatedFromMemberTemplate()` and `getInstantiatedFromMember()`.

I'd also like to discuss whether a function template would be preferable here, although that would likely require a broader change.

>From f5443971b392b42d8a595e4629690729e060b9ba Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Fri, 6 Nov 2026 09:59:40 +0800
Subject: [PATCH] [clang][Sema][NFC] Share member template instantiation lookup

Use `RedeclarableTemplateDecl` to consolidate the identical
`isInstantiationOf` implementations for class and function templates,
leaving the other overloads unchanged.

I initially considered turning these helpers into a function template, but
the declaration types use different accessors, such as
`getInstantiatedFromMemberTemplate()` and `getInstantiatedFromMember()`.

I'd also like to discuss whether a function template would be preferable
here, although that would likely require a broader change.
---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 65c011024ce802..2f5ae7db5e0ac4 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -7043,21 +7043,8 @@ Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
 
 // TODO: this could be templated if the various decl types used the
 // same method name.
-static bool isInstantiationOf(ClassTemplateDecl *Pattern,
-                              ClassTemplateDecl *Instance) {
-  Pattern = Pattern->getCanonicalDecl();
-
-  do {
-    Instance = Instance->getCanonicalDecl();
-    if (Pattern == Instance) return true;
-    Instance = Instance->getInstantiatedFromMemberTemplate();
-  } while (Instance);
-
-  return false;
-}
-
-static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
-                              FunctionTemplateDecl *Instance) {
+static bool isInstantiationOf(RedeclarableTemplateDecl *Pattern,
+                              RedeclarableTemplateDecl *Instance) {
   Pattern = Pattern->getCanonicalDecl();
 
   do {



More information about the cfe-commits mailing list