This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by george.burgess.iv on Feb 28 2016, 6:27 PM.

Details

Summary

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).

Diff Detail

Event Timeline

george.burgess.iv retitled this revision from to [Sema] Teach SemaCast to allow reinterpret_casts of overloaded functions with only one addressable candidate.
george.burgess.iv updated this object.
george.burgess.iv added a reviewer: rsmith.
george.burgess.iv added a subscriber: cfe-commits.
rsmith accepted this revision.Mar 18 2016, 11:25 AM
rsmith edited edge metadata.

OK, though I'm hoping either there aren't many more of these cases or we can treat them more generally somehow.

lib/Sema/SemaCast.cpp
1771–1782

Do we reach a DiagnoseUseOfDecl somewhere in here?

This revision is now accepted and ready to land.Mar 18 2016, 11:25 AM
george.burgess.iv marked an inline comment as done.Mar 19 2016, 2:40 PM
george.burgess.iv added inline comments.
lib/Sema/SemaCast.cpp
1771–1782

Good catch.

On a slightly related side note, it seems that ResolveAndFixSingleFunctionTemplateSpecialization doesn't check for member access (though it does call DiagnoseUseOfDecl). Will commit a fix for that separately. :)

This revision was automatically updated to reflect the committed changes.