[clang] [Clang] Apply exclude_from_explicit_instantiation to dllimport/dllexport (PR #168171)

Tomohiro Kashiwada via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 20 03:45:02 PST 2026


================
@@ -19109,7 +19119,17 @@ bool Sema::DefineUsedVTables() {
         }
       }
 
-      if (IsExplicitInstantiationDeclaration)
+      if (IsExplicitInstantiationDeclaration &&
+          llvm::none_of(Class->decls(), [](Decl *decl) {
+            // If the class has a virtual member function declared with
+            // `__attribute__((exclude_from_explicit_instantiation))`, the
+            // explicit instantiation declaration shouldn't suppress emitting
+            // the vtable to ensure that the excluded member function is
+            // accessible through the vtable.
+            auto *Method = dyn_cast<CXXMethodDecl>(decl);
+            return Method && Method->isVirtual() &&
+                   Method->hasAttr<ExcludeFromExplicitInstantiationAttr>();
+          }))
----------------
kikairoya wrote:

> > Are there any cases to emit vtables, other than excluded constructors?
> 
> I'm not sure. I think they're emitted when used, which is typically by the constructor, but maybe there could be other cases.

I found the case. A virtual inheritance triggers to emit sub-tables.
https://godbolt.org/z/GqWKbYYjK

In that case, I feel odd if adding the attribute to `normal_fn` affects the vtable generator routine, even if the result is not changed. The assumption includes non-virtual functions looks too strong for me.

As this attribute is an extension, so I'd prefer to minimize the areas affected by the attribute.

https://github.com/llvm/llvm-project/pull/168171


More information about the cfe-commits mailing list