From: Anders Carlsson Date: Sat, 29 Jan 2011 05:04:11 +0000 (+0000) Subject: When trying to get the most derived class, don't assume that we can ignore all casts... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=268ab8c6b92dc40c678d8458a335698fa34915c9;p=clang When trying to get the most derived class, don't assume that we can ignore all casts. We can only ignore derived-to-base and no-op casts. Fixes selfhost. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124528 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index fa6ac5469f..99c54f0456 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -54,7 +54,23 @@ RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, } static const CXXRecordDecl *getMostDerivedClassDecl(const Expr *Base) { - QualType DerivedType = Base->IgnoreParenCasts()->getType(); + const Expr *E = Base; + + while (true) { + E = E->IgnoreParens(); + if (const CastExpr *CE = dyn_cast(E)) { + if (CE->getCastKind() == CK_DerivedToBase || + CE->getCastKind() == CK_UncheckedDerivedToBase || + CE->getCastKind() == CK_NoOp) { + E = CE->getSubExpr(); + continue; + } + } + + break; + } + + QualType DerivedType = E->getType(); if (const PointerType *PTy = DerivedType->getAs()) DerivedType = PTy->getPointeeType(); diff --git a/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp b/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp index 08a94903d5..3de75ed3db 100644 --- a/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp +++ b/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp @@ -36,4 +36,16 @@ namespace Test3 { // CHECK: call i32 @_ZN5Test31A1fEv return b->f(); } + + // CHECK: define i32 @_ZN5Test31fERNS_1BE + int f(B &b) { + // CHECK: call i32 @_ZN5Test31A1fEv + return b.f(); + } + + // CHECK: define i32 @_ZN5Test31fEPv + int f(void *v) { + // CHECK: call i32 @_ZN5Test31A1fEv + return static_cast(v)->f(); + } }