Skip to content

IRGen: Fix handling of empty fields in Clang-imported types. #15351

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
Mar 20, 2018
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: 15 additions & 6 deletions lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ namespace {
llvm::Constant *getConstantFieldOffset(IRGenModule &IGM,
VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
if (fieldInfo.getKind() == ElementLayout::Kind::Fixed) {
if (fieldInfo.getKind() == ElementLayout::Kind::Fixed
|| fieldInfo.getKind() == ElementLayout::Kind::Empty) {
return llvm::ConstantInt::get(IGM.SizeTy,
fieldInfo.getFixedByteOffset().getValue());
}
Expand Down Expand Up @@ -773,14 +774,22 @@ class ClangRecordLowering {
unsigned explosionEnd = NextExplosionIndex;

ElementLayout layout = ElementLayout::getIncomplete(fieldType);
layout.completeFixed(fieldType.isPOD(ResilienceExpansion::Maximal),
NextOffset, LLVMFields.size());
auto isEmpty = fieldType.isKnownEmpty(ResilienceExpansion::Maximal);
if (isEmpty)
layout.completeEmpty(fieldType.isPOD(ResilienceExpansion::Maximal),
NextOffset);
else
layout.completeFixed(fieldType.isPOD(ResilienceExpansion::Maximal),
NextOffset, LLVMFields.size());

FieldInfos.push_back(
ClangFieldInfo(swiftField, layout, explosionBegin, explosionEnd));
LLVMFields.push_back(fieldType.getStorageType());
NextOffset += fieldType.getFixedSize();
SpareBits.append(fieldType.getSpareBits());

if (!isEmpty) {
LLVMFields.push_back(fieldType.getStorageType());
NextOffset += fieldType.getFixedSize();
SpareBits.append(fieldType.getSpareBits());
}
}

/// Add padding to get up to the given offset.
Expand Down
4 changes: 4 additions & 0 deletions test/IRGen/Inputs/clang_empty_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct TrailingArray {
int size;
char buffer[0];
};
6 changes: 6 additions & 0 deletions test/IRGen/clang_empty_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-frontend -emit-ir -verify -import-objc-header %S/Inputs/clang_empty_type.h %s

public func projectTrailingArray(x: inout TrailingArray) {
x.size = 2
x.buffer = ()
}