llvm-project/clang/test/CodeGenCXX/class-layout.cpp
Michael Buch 4497ec293a
[clang][CGRecordLayout] Remove dependency on isZeroSize (#96422)
This is a follow-up from the conversation starting at
https://github.com/llvm/llvm-project/pull/93809#issuecomment-2173729801

The root problem that motivated the change are external AST sources that
compute `ASTRecordLayout`s themselves instead of letting Clang compute
them from the AST. One such example is LLDB using DWARF to get the
definitive offsets and sizes of C++ structures. Such layouts should be
considered correct (modulo buggy DWARF), but various assertions and
lowering logic around the `CGRecordLayoutBuilder` relies on the AST
having `[[no_unique_address]]` attached to them. This is a
layout-altering attribute which is not encoded in DWARF. This causes us
LLDB to trip over the various LLVM<->Clang layout consistency checks.
There has been precedent for avoiding such layout-altering attributes
from affecting lowering with externally-provided layouts (e.g., packed
structs).

This patch proposes to replace the `isZeroSize` checks in
`CGRecordLayoutBuilder` (which roughly means "empty field with
[[no_unique_address]]") with checks for
`CodeGen::isEmptyField`/`CodeGen::isEmptyRecord`.

**Details**
The main strategy here was to change the `isZeroSize` check in
`CGRecordLowering::accumulateFields` and
`CGRecordLowering::accumulateBases` to use the `isEmptyXXX` APIs
instead, preventing empty fields from being added to the `Members` and
`Bases` structures. The rest of the changes fall out from here, to
prevent lookups into these structures (for field numbers or base
indices) from failing.

Added `isEmptyRecordForLayout` and `isEmptyFieldForLayout` (open to
better naming suggestions). The main difference to the existing
`isEmptyRecord`/`isEmptyField` APIs, is that the `isEmptyXXXForLayout`
counterparts don't have special treatment for `unnamed bitfields`/arrays
and also treat fields of empty types as if they had
`[[no_unique_address]]` (i.e., just like the `AsIfNoUniqueAddr` in
`isEmptyField` does).
2024-07-16 04:59:51 +01:00

103 lines
2.3 KiB
C++

// RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
// An extra byte should be allocated for an empty class.
namespace Test1 {
// CHECK: %"struct.Test1::A" = type { i8 }
struct A { } a;
}
namespace Test2 {
// No need to add tail padding here.
// CHECK: %"struct.Test2::A" = type { ptr, i32 }
struct A { void *a; int b; } a;
}
namespace Test3 {
// C should have a vtable pointer.
// CHECK: %"struct.Test3::A" = type <{ ptr, i32, [4 x i8] }>
struct A { virtual void f(); int a; } a;
}
namespace Test4 {
// Test from PR5589.
// CHECK: %"struct.Test4::B" = type { %"struct.Test4::A", i16, double }
// CHECK: %"struct.Test4::A" = type { i32, i8, float }
struct A {
int a;
char c;
float b;
};
struct B : public A {
short d;
double e;
} b;
}
namespace Test5 {
struct A {
virtual void f();
char a;
};
// CHECK: %"struct.Test5::B" = type { %"struct.Test5::A.base", i8, i8, [5 x i8] }
struct B : A {
char b : 1;
char c;
} b;
}
// PR10912: don't crash
namespace Test6 {
template <typename T> class A {
// If T is complete, IR-gen will want to translate it recursively
// when translating T*.
T *foo;
};
class B;
// This causes IR-gen to have an incomplete translation of A<B>
// sitting around.
A<B> *a;
class C {};
class B : public C {
// This forces Sema to instantiate A<B>, which triggers a callback
// to IR-gen. Because of the previous, incomplete translation,
// IR-gen actually cares, and it immediately tries to complete
// A<B>'s IR type. That, in turn, causes the translation of B*.
// B isn't complete yet, but it has a definition, and if we try to
// compute a record layout for that definition then we'll really
// regret it later.
A<B> a;
};
// The derived class E and empty base class C are required to
// provoke the original assertion.
class E : public B {};
E *e;
}
// Make sure this doesn't crash. (It's okay if we start rejecting it at some
// point.)
namespace Test7 {
#pragma pack (1)
class A {};
// CHECK: %"class.Test7::B" = type <{ ptr, i8 }>
class B {
virtual ~B();
A a;
};
B test(B b) { return b; }
#pragma pack ()
}
// Shouldn't crash.
namespace Test8 {
struct A {};
struct D { int a; };
struct B : virtual D, A { };
struct C : B, A { void f() {} };
C c;
}