[cfe-dev] C++ operator overload miscompilation

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 8 11:27:06 PDT 2017


On 8 June 2017 at 03:22, Adam Nowacki via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> For C++ code:
>
> struct A {
>     operator int();
> };
> bool f() {
>     bool operator==(const A &, const A &);
>     return A() == A();
> }
>
> clang emits calls to A::operator int() and then performs the == comparison
> on ints - which is incorrect, only a call to operator==(const A &, const A
> &) should be emitted.


Yes, this is a bug. Looks like we're not correctly putting the operator==
function into IDNS_NonMemberOperator. We have this code in two places in
Sema:

   if (Function->isOverloadedOperator() && !DC->isRecord() &&
      PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
    PrincipalDecl->setNonMemberOperator();

... which is wrong -- the above declaration is in IDNS_LocalExtern, not
IDNS_Ordinary. If you're interested in putting together a patch for this,
the fix might be as simple as changing the IDNS_Ordinary in these two cases
to IDNS_Ordinary | IDNS_LocalExtern (you'll need to make sure that doesn't
make the operator visible to operator lookups *outside* f(), though).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170608/d3442722/attachment.html>


More information about the cfe-dev mailing list