[clang] [Clang][Sema] Accept weak reference after declaration (PR #228809)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 17:33:50 PST 2026


================
@@ -8866,19 +8865,28 @@ NamedDecl *Sema::DeclClonePragmaWeak(NamedDecl *ND, const IdentifierInfo *II,
 void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, const WeakInfo &W) {
   if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
     IdentifierInfo *NDId = ND->getIdentifier();
+    // An existing alias of this name, from __attribute__((alias)) or an
+    // earlier #pragma weak, takes precedence.
+    if (NamedDecl *Prev = LookupSingleName(TUScope, W.getAlias(),
+                                           W.getLocation(), LookupOrdinaryName))
+      if (llvm::any_of(Prev->redecls(),
+                       [](const Decl *D) { return D->hasAttr<AliasAttr>(); }))
+        return;
----------------
efriedma-quic wrote:

If someone writes:

```
#pragma weak g = f1
#pragma weak g = f2
```

We probably want to diagnose that.

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


More information about the cfe-commits mailing list