Skip to content

[Clang][CodeGen] Fix CanSkipVTablePointerInitialization for dynamic classes with a trivial anonymous union #84651

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
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
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ FieldHasTrivialDestructorBody(ASTContext &Context,

// The destructor for an implicit anonymous union member is never invoked.
if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
return false;
return true;

return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
}
Expand Down
62 changes: 62 additions & 0 deletions clang/test/CodeGenCXX/skip-vtable-pointer-initialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,65 @@ struct C : virtual B {
C::~C() {}

}

namespace Test10 {

// Check that we don't initialize the vtable pointer in A::~A(), since the class has an anonymous union which
// never has its destructor invoked.
struct A {
virtual void f();
~A();

union
{
int i;
unsigned u;
};
};

// CHECK-LABEL: define{{.*}} void @_ZN6Test101AD2Ev
// CHECK-NOT: store ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN6Test101AE, i32 0, inrange i32 0, i32 2), ptr
A::~A() {
}

}

namespace Test11 {

// Check that we don't initialize the vtable pointer in A::~A(), even if the base class has a non trivial destructor.
struct Field {
~Field();
};

struct A : public Field {
virtual void f();
~A();
};

// CHECK-LABEL: define{{.*}} void @_ZN6Test111AD2Ev
// CHECK-NOT: store ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN6Test111AE, i32 0, inrange i32 0, i32 2), ptr
A::~A() {
}

}

namespace Test12 {

// Check that we don't initialize the vtable pointer in A::~A(), since the class has an anonymous struct with trivial fields.
struct A {
virtual void f();
~A();

struct
{
int i;
unsigned u;
};
};

// CHECK-LABEL: define{{.*}} void @_ZN6Test121AD2Ev
// CHECK-NOT: store ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN6Test121AE, i32 0, inrange i32 0, i32 2), ptr
A::~A() {
}

}