Skip to content

[clang][Interp] Properly adjust instance pointer in virtual calls #102800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/lib/AST/Interp/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2633,9 +2633,10 @@ inline bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
ThisPtr.getFieldDesc()->getType()->getAsCXXRecordDecl();
if (Func->getParentDecl()->isDerivedFrom(ThisFieldDecl)) {
// If the function we call is further DOWN the hierarchy than the
// FieldDesc of our pointer, just get the DeclDesc instead, which
// is the furthest we might go up in the hierarchy.
ThisPtr = ThisPtr.getDeclPtr();
// FieldDesc of our pointer, just go up the hierarchy of this field
// the furthest we can go.
while (ThisPtr.isBaseClass())
ThisPtr = ThisPtr.getBase();
}
}

Expand Down
24 changes: 24 additions & 0 deletions clang/test/AST/Interp/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,3 +1572,27 @@ namespace ctorOverrider {
constexpr Covariant1 cb;
}
#endif

#if __cplusplus >= 202002L
namespace VirtDtor {
struct X { char *p; constexpr ~X() { *p++ = 'X'; } };
struct Y : X { int y; virtual constexpr ~Y() { *p++ = 'Y'; } };
struct Z : Y { int z; constexpr ~Z() override { *p++ = 'Z'; } };

union VU {
constexpr VU() : z() {}
constexpr ~VU() {}
Z z;
};

constexpr char virt_dtor(int mode, const char *expected) {
char buff[4] = {};
VU vu;
vu.z.p = buff;

((Y&)vu.z).~Y();
return true;
}
static_assert(virt_dtor(0, "ZYX"));
}
#endif
Loading