diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp index 2a92258b8469..a7b8f659786f 100644 --- a/clang/lib/CodeGen/CGClass.cpp +++ b/clang/lib/CodeGen/CGClass.cpp @@ -180,8 +180,17 @@ CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, llvm::Value *VirtualOffset = 0; - if (VBase) - VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase); + if (VBase) { + if (Derived->hasAttr()) { + VirtualOffset = 0; + + const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); + + uint64_t VBaseOffset = Layout.getVBaseClassOffsetInBits(VBase); + NonVirtualOffset += VBaseOffset / 8; + } else + VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase); + } // Apply the offsets. Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset, diff --git a/clang/test/CodeGenCXX/derived-to-virtual-base-class-calls-final.cpp b/clang/test/CodeGenCXX/derived-to-virtual-base-class-calls-final.cpp new file mode 100644 index 000000000000..e332f4020001 --- /dev/null +++ b/clang/test/CodeGenCXX/derived-to-virtual-base-class-calls-final.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s + +struct A { int i; }; +struct B { int j; }; +struct C : A, B { int k; }; + +struct D final : virtual C { + D(); + virtual void f(); +}; + +// CHECK: define %struct.B* @_Z1fR1D +B &f(D &d) { + // CHECK-NOT: load i8** + return d; +}