[PATCH] D17701: [Sema] Teach SemaCast to allow reinterpret_casts of overloaded functions with only one addressable candidate

George Burgess IV via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 28 18:27:11 PST 2016


george.burgess.iv created this revision.
george.burgess.iv added a reviewer: rsmith.
george.burgess.iv added a subscriber: cfe-commits.

Given the following declarations for `foo`:

```
void foo(int a); 
void foo(int a) __attribute__((enable_if(a, "")));
```

...The only way to `reinterpret_cast` `foo` is to insert two casts:

```
auto fnptr = reinterpret_cast<void (*)(void *)>((void (*)(int))foo);
```

...Which isn't quite ideal. This patch teaches clang to check to see if an overload set has only one candidate that can have its address taken. If this is the case, then we'll use it automatically.

(As a side note: we use a custom loop rather than modifying `AddressOfFunctionResolver` because we don't need to do any kind of ranking, and there seem to be some cases where `AddressOfFunctionResolver` will disqualify candidates, which we don't really want here. I can fold this logic into `AddressOfFunctionResolver` if we want to potentially have less code duplication, but I feel this is straightforward enough that it can stand on its own, rather than adding complexity to `AddressOfFunctionResolver`).

http://reviews.llvm.org/D17701

Files:
  include/clang/Sema/Sema.h
  lib/Sema/SemaCast.cpp
  lib/Sema/SemaOverload.cpp
  test/SemaCXX/enable_if.cpp
  test/SemaCXX/unaddressable-functions.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17701.49334.patch
Type: text/x-patch
Size: 11249 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160229/9114e69b/attachment-0001.bin>


More information about the cfe-commits mailing list