Skip to content

[ClangImporter] Fix handling of bitfields in unions #14412

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
4 changes: 3 additions & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3177,11 +3177,13 @@ namespace {

// Bitfields are imported as computed properties with Clang-generated
// accessors.
bool isBitField = false;
if (auto field = dyn_cast<clang::FieldDecl>(nd)) {
if (field->isBitField()) {
// We can't represent this struct completely in SIL anymore,
// but we're still able to define a memberwise initializer.
hasUnreferenceableStorage = true;
isBitField = true;

makeBitFieldAccessors(Impl,
const_cast<clang::RecordDecl *>(decl),
Expand All @@ -3195,7 +3197,7 @@ namespace {
// Indirect fields are created as computed property accessible the
// fields on the anonymous field from which they are injected.
makeIndirectFieldAccessors(Impl, ind, members, result, VD);
} else if (decl->isUnion()) {
} else if (decl->isUnion() && !isBitField) {
// Union fields should only be available indirectly via a computed
// property. Since the union is made of all of the fields at once,
// this is a trivial accessor that casts self to the correct
Expand Down
62 changes: 62 additions & 0 deletions test/Interpreter/Inputs/unions-and-bitfields.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdint.h>
#include <string.h>

union PlainUnion {
uint32_t whole;
unsigned char first;
};

struct PlainBitfield {
uint32_t offset;
uint32_t first: 8;
uint32_t : 0;
};
_Static_assert(sizeof(struct PlainBitfield) == sizeof(uint64_t),
"must fit in 64 bits");

struct PlainIndirect {
uint32_t offset;
struct {
uint32_t whole;
};
};

union BitfieldUnion {
uint32_t whole;
uint32_t first: 8;
};

struct BitfieldIndirect {
uint32_t offset;
struct {
uint32_t first: 8;
uint32_t : 0;
};
};

struct UnionIndirect {
uint32_t offset;
union {
uint32_t whole;
unsigned char first;
};
};

struct BitfieldUnionIndirect {
uint32_t offset;
union {
uint32_t whole;
uint32_t first: 8;
};
};

void populate(void *memory) {
const uint32_t value = 0x11223344;
memcpy(memory, &value, sizeof(value));
}

void populateAtOffset(void *memory) {
const uint32_t value = 0x11223344;
memcpy((char *)memory + sizeof(uint32_t), &value, sizeof(value));
}

60 changes: 60 additions & 0 deletions test/Interpreter/unions-and-bitfields.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// RUN: %target-build-swift %s -import-objc-header %S/Inputs/unions-and-bitfields.h -disable-bridging-pch -o %t
// RUN: %target-run %t
// REQUIRES: executable_test

// The -disable-bridging-pch above isn't actually relevant to the test; however,
// precompiled headers don't play nice with the way we include the platform
// module map on non-Apple platforms. See
// https://bugs.llvm.org/show_bug.cgi?id=36245.

import StdlibUnittest

var suite = TestSuite("UnionsAndBitfields")

suite.test("PlainUnion") {
var x = PlainUnion()
populate(&x)
expectEqual(0x11223344, x.whole)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

suite.test("PlainBitfield") {
var x = PlainBitfield()
populateAtOffset(&x)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

suite.test("PlainIndirect") {
var x = PlainIndirect()
populateAtOffset(&x)
expectEqual(0x11223344, x.whole)
}

suite.test("BitfieldUnion") {
var x = BitfieldUnion()
populate(&x)
expectEqual(0x11223344, x.whole)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

suite.test("BitfieldIndirect") {
var x = BitfieldIndirect()
populateAtOffset(&x)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

suite.test("UnionIndirect") {
var x = UnionIndirect()
populateAtOffset(&x)
expectEqual(0x11223344, x.whole)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

suite.test("BitfieldUnionIndirect") {
var x = BitfieldUnionIndirect()
populateAtOffset(&x)
expectEqual(0x11223344, x.whole)
expectTrue(x.first == 0x11 || x.first == 0x44)
}

runAllTests()