[clang] 2ac7a95 - [clang] Fix crash on __builtin_addressof of an overload set (#235313)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 08:50:32 PST 2026


Author: lsh4711
Date: 2026-11-03T16:50:26Z
New Revision: 2ac7a957446632f41a649ead3ede56cc31e4006d

URL: https://github.com/llvm/llvm-project/commit/2ac7a957446632f41a649ead3ede56cc31e4006d
DIFF: https://github.com/llvm/llvm-project/commit/2ac7a957446632f41a649ead3ede56cc31e4006d.diff

LOG: [clang] Fix crash on __builtin_addressof of an overload set (#235313)

Fixes #77928

```cpp
template <typename T> void f(int *) {}
void (*p)(int *) = __builtin_addressof(f<int>);
```

Clang crashes on this (an invalid `cast<OverloadExpr>`).
`BuiltinAddressof` leaves an overload set operand unresolved like `&`
does, but nothing resolves it later for the builtin.
This resolves the operand with `CheckPlaceholderExpr`, so it behaves
like `std::addressof`.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/Sema/SemaChecking.cpp
    clang/test/SemaCXX/builtins.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 458c7aa7b6a15..93eb99fa27346 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1170,6 +1170,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed a bug where Clang could ignore lambda constraints in return-type
   requirements.
 
+- Fixed a crash when the operand of `__builtin_addressof` is an overload set.
+  A function template specialization such as `f<int>` is now resolved like the
+  argument of `std::addressof`, and an unresolvable overload set is diagnosed.
+  (#GH77928)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 406f03a17f7eb..4b9c1d187dfeb 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -272,6 +272,15 @@ static bool BuiltinAddressof(Sema &S, CallExpr *TheCall) {
     return true;
 
   ExprResult Arg(TheCall->getArg(0));
+
+  // Unlike '&', nothing resolves the overload set later, so resolve it now.
+  // FIXME: Resolve the overload set from the target type, like '&' does.
+  if (Arg.get()->getType() == S.Context.OverloadTy) {
+    Arg = S.CheckPlaceholderExpr(Arg.get());
+    if (Arg.isInvalid())
+      return true;
+  }
+
   QualType ResultType = S.CheckAddressOfOperand(Arg, TheCall->getBeginLoc());
   if (ResultType.isNull())
     return true;

diff  --git a/clang/test/SemaCXX/builtins.cpp b/clang/test/SemaCXX/builtins.cpp
index f99bb87b9cbd4..c955d5077532f 100644
--- a/clang/test/SemaCXX/builtins.cpp
+++ b/clang/test/SemaCXX/builtins.cpp
@@ -48,6 +48,17 @@ namespace addressof {
   int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}}
 
   S *ptmp = __builtin_addressof(S{}); // expected-error {{taking the address of a temporary}} expected-warning {{temporary whose address is used as value of local variable 'ptmp' will be destroyed at the end of the full-expression}}
+
+  template <typename T> void tmpl(T) {}
+  static_assert(__builtin_addressof(tmpl<int>) == &tmpl<int>, "");
+  template <typename T> void dependent() {
+    void (*ptmpl)(int) = __builtin_addressof(tmpl<T>);
+  }
+  auto _ = (dependent<int>(), 0);
+
+  void ovl(int); // expected-note {{possible target for call}}
+  void ovl(long); // expected-note {{possible target for call}}
+  void (*povl)(int) = __builtin_addressof(ovl); // expected-error {{reference to overloaded function could not be resolved; did you mean to call it?}}
 }
 
 namespace function_start {


        


More information about the cfe-commits mailing list