mirror of
https://github.com/ldc-developers/ldc.git
synced 2025-05-04 00:55:49 +03:00
ImportC: Account for StringExp typed as static array with length > string length
Fixes dmd-testsuite's compilable/test23066.c.
This commit is contained in:
parent
f31738b2d6
commit
14759a744e
5 changed files with 38 additions and 24 deletions
|
@ -235,7 +235,8 @@ LLGlobalVariable *IRState::getCachedStringLiteral(StringExp *se) {
|
|||
keyData.length);
|
||||
|
||||
return getCachedStringLiteralImpl(module, *cache, key, [se]() {
|
||||
return buildStringLiteralConstant(se, true);
|
||||
// null-terminate
|
||||
return buildStringLiteralConstant(se, se->numberOfCodeUnits() + 1);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1654,30 +1654,31 @@ llvm::Constant *DtoConstSymbolAddress(const Loc &loc, Declaration *decl) {
|
|||
llvm_unreachable("Taking constant address not implemented.");
|
||||
}
|
||||
|
||||
llvm::Constant *buildStringLiteralConstant(StringExp *se, bool zeroTerm) {
|
||||
if (se->sz == 1) {
|
||||
llvm::Constant *buildStringLiteralConstant(StringExp *se,
|
||||
uint64_t bufferLength) {
|
||||
const auto stringLength = se->numberOfCodeUnits();
|
||||
assert(bufferLength >= stringLength);
|
||||
|
||||
if (se->sz == 1 && bufferLength <= stringLength + 1) {
|
||||
const DString data = se->peekString();
|
||||
const bool nullTerminate = (bufferLength == stringLength + 1);
|
||||
return llvm::ConstantDataArray::getString(
|
||||
gIR->context(), {data.ptr, data.length}, zeroTerm);
|
||||
gIR->context(), {data.ptr, data.length}, nullTerminate);
|
||||
}
|
||||
|
||||
Type *dtype = se->type->toBasetype();
|
||||
Type *cty = dtype->nextOf()->toBasetype();
|
||||
|
||||
LLType *ct = DtoMemType(cty);
|
||||
auto len = se->numberOfCodeUnits();
|
||||
if (zeroTerm) {
|
||||
len += 1;
|
||||
}
|
||||
LLArrayType *at = LLArrayType::get(ct, len);
|
||||
LLArrayType *at = LLArrayType::get(ct, bufferLength);
|
||||
|
||||
std::vector<LLConstant *> vals;
|
||||
vals.reserve(len);
|
||||
for (size_t i = 0; i < se->numberOfCodeUnits(); ++i) {
|
||||
vals.reserve(bufferLength);
|
||||
for (uint64_t i = 0; i < stringLength; ++i) {
|
||||
vals.push_back(LLConstantInt::get(ct, se->getCodeUnit(i), false));
|
||||
}
|
||||
if (zeroTerm) {
|
||||
vals.push_back(LLConstantInt::get(ct, 0, false));
|
||||
const auto nullChar = LLConstantInt::get(ct, 0, false);
|
||||
for (uint64_t i = stringLength; i < bufferLength; ++i) {
|
||||
vals.push_back(nullChar);
|
||||
}
|
||||
return LLConstantArray::get(at, vals);
|
||||
}
|
||||
|
|
|
@ -232,7 +232,8 @@ LLConstant *toConstantArray(LLType *ct, LLArrayType *at, T *str, size_t len,
|
|||
return LLConstantArray::get(at, vals);
|
||||
}
|
||||
|
||||
llvm::Constant *buildStringLiteralConstant(StringExp *se, bool zeroTerm);
|
||||
llvm::Constant *buildStringLiteralConstant(StringExp *se,
|
||||
uint64_t bufferLength);
|
||||
|
||||
/// Returns true if the specified symbol is to be defined on declaration,
|
||||
/// primarily for -linkonce-templates.
|
||||
|
|
|
@ -165,8 +165,9 @@ public:
|
|||
Type *const t = e->type->toBasetype();
|
||||
|
||||
if (auto ts = t->isTypeSArray()) {
|
||||
bool zeroTerm = ts->dim->toInteger() == e->numberOfCodeUnits() + 1;
|
||||
result = buildStringLiteralConstant(e, zeroTerm);
|
||||
const auto arrayLength = ts->dim->toInteger();
|
||||
assert(arrayLength >= e->numberOfCodeUnits());
|
||||
result = buildStringLiteralConstant(e, arrayLength);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
24
gen/toir.cpp
24
gen/toir.cpp
|
@ -415,20 +415,30 @@ public:
|
|||
LOG_SCOPE;
|
||||
|
||||
Type *dtype = e->type->toBasetype();
|
||||
const auto stringLength = e->numberOfCodeUnits();
|
||||
|
||||
if (auto tsa = dtype->isTypeSArray()) {
|
||||
const auto arrayLength = tsa->dim->toInteger();
|
||||
assert(arrayLength >= stringLength);
|
||||
// ImportC: static array length may exceed string length incl. null
|
||||
// terminator - bypass string-literal cache and create a separate constant
|
||||
// with zero-initialized tail
|
||||
if (arrayLength > stringLength + 1) {
|
||||
auto constant = buildStringLiteralConstant(e, arrayLength);
|
||||
result = new DLValue(e->type, constant);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
llvm::GlobalVariable *gvar = p->getCachedStringLiteral(e);
|
||||
LLConstant *arrptr = DtoGEP(gvar->getValueType(), gvar, 0u, 0u);
|
||||
|
||||
if (dtype->ty == TY::Tarray) {
|
||||
LLConstant *clen =
|
||||
LLConstantInt::get(DtoSize_t(), e->numberOfCodeUnits(), false);
|
||||
LLConstant *clen = LLConstantInt::get(DtoSize_t(), stringLength, false);
|
||||
result = new DSliceValue(e->type, DtoConstSlice(clen, arrptr, dtype));
|
||||
} else if (dtype->ty == TY::Tsarray) {
|
||||
Type *cty = dtype->nextOf()->toBasetype();
|
||||
LLType *ct = DtoMemType(cty);
|
||||
LLType *dstType =
|
||||
getPtrToType(LLArrayType::get(ct, e->numberOfCodeUnits()));
|
||||
result = new DLValue(e->type, DtoBitCast(gvar, dstType));
|
||||
// array length matches string length with or without null terminator
|
||||
result = new DLValue(e->type, DtoBitCast(gvar, DtoPtrToType(dtype)));
|
||||
} else if (dtype->ty == TY::Tpointer) {
|
||||
result = new DImValue(e->type, DtoBitCast(arrptr, DtoType(dtype)));
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue