[clang] Enable unique-object-duplication warning for windows (PR #143537)

Devon Loehr via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 11 11:20:41 PDT 2025


================
@@ -13518,8 +13518,29 @@ bool Sema::GloballyUniqueObjectMightBeAccidentallyDuplicated(
 
   // If the object isn't hidden, the dynamic linker will prevent duplication.
   clang::LinkageInfo Lnk = Target->getLinkageAndVisibility();
-  if (Lnk.getVisibility() != HiddenVisibility)
+
+  // The target is "hidden" (from the dynamic linker) if:
+  // 1. On posix, it has hidden visibility, or
+  // 2. On windows, it has no import/export annotation
+  if (Context.getTargetInfo().shouldDLLImportComdatSymbols()) {
+    if (Target->hasAttr<DLLExportAttr>() || Target->hasAttr<DLLImportAttr>())
+      return false;
+
+    // If the variable isn't directly annotated, check to see if it's a member
+    // of an annotated class.
+    const VarDecl *VD = dyn_cast_if_present<VarDecl>(Target);
----------------
DKLoehr wrote:

Hmm, right now we're running the warning in `CheckCompleteVariableDeclaration`, which feels like the right place to me. It seems like the attribute gets propagated in `CheckCompletedCXXClass`, which would have to happen later, so I'm not sure there's a good way to move the warning check later. We could defer to the end of the TU, but that seems more complicated then looking upwards one level. It is annoying that we can't borrow the propagation logic, though.

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


More information about the cfe-commits mailing list