Skip to content
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
21 changes: 14 additions & 7 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,19 @@ static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,

Pointer U = Ptr.getBase();
Pointer C = Ptr;
while (!U.isRoot() && U.inUnion() && !U.isActive()) {
if (U.getField())
C = U;
while (!U.isRoot() && !U.isActive()) {
// A little arbitrary, but this is what the current interpreter does.
// See the AnonymousUnion test in test/AST/ByteCode/unions.cpp.
// GCC's output is more similar to what we would get without
// this condition.
if (U.getRecord() && U.getRecord()->isAnonymousUnion())
break;

C = U;
U = U.getBase();
}
assert(C.isField());

// Get the inactive field descriptor.
const FieldDecl *InactiveField = C.getField();
assert(InactiveField);

// Consider:
// union U {
// struct {
Expand All @@ -165,6 +167,11 @@ static bool CheckActive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
if (!U.getFieldDesc()->isUnion())
return true;

// Get the inactive field descriptor.
assert(!C.isActive());
const FieldDecl *InactiveField = C.getField();
assert(InactiveField);

// Find the active field of the union.
const Record *R = U.getRecord();
assert(R && R->isUnion() && "Not a union");
Expand Down
23 changes: 15 additions & 8 deletions clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,28 +437,35 @@ void Pointer::activate() const {
if (!getInlineDesc()->InUnion)
return;

getInlineDesc()->IsActive = true;
auto activate = [](Pointer &P) -> void {
P.getInlineDesc()->IsActive = true;
};
auto deactivate = [](Pointer &P) -> void {
P.getInlineDesc()->IsActive = false;
};

// Get the union, iterate over its fields and DEactivate all others.
// Unions might be nested etc., so find the topmost Pointer that's
// not in a union anymore.
Pointer UnionPtr = getBase();
while (!UnionPtr.getFieldDesc()->isUnion())
while (!UnionPtr.isRoot() && UnionPtr.inUnion())
UnionPtr = UnionPtr.getBase();

assert(UnionPtr.getFieldDesc()->isUnion());

const Record *UnionRecord = UnionPtr.getRecord();
for (const Record::Field &F : UnionRecord->fields()) {
Pointer FieldPtr = UnionPtr.atField(F.Offset);
if (FieldPtr == *this) {
} else {
FieldPtr.getInlineDesc()->IsActive = false;
deactivate(FieldPtr);
// FIXME: Recurse.
}
}

Pointer B = getBase();
while (!B.isRoot() && B.inUnion()) {
Pointer B = *this;
while (B != UnionPtr) {
activate(B);
// FIXME: Need to de-activate other fields of parent records.
B.getInlineDesc()->IsActive = true;
assert(B.isActive());
B = B.getBase();
}
}
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/AST/ByteCode/Pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,6 @@ class Pointer {
/// Returns the field information.
const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }

/// Checks if the object is a union.
bool isUnion() const;

/// Checks if the storage is extern.
bool isExtern() const {
if (isBlockPointer())
Expand Down
19 changes: 19 additions & 0 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,23 @@ namespace IFD {
}
static_assert(test());
}

namespace AnonymousUnion {
struct A {
int x;
union { int p, q; };
};
union B {
A a;
int bb;
};

constexpr B return_init_all() {
B b = {.bb = 1};
b.a.x = 2;
return b;
}
static_assert(return_init_all().a.p == 7); // both-error {{}} \
// both-note {{read of member 'p' of union with no active member}}
}
#endif