Skip to content

Commit

Permalink
Add proper support for -checkaction=halt
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed May 14, 2020
1 parent f70feb1 commit 3424b55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
34 changes: 20 additions & 14 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,19 +1684,25 @@ class ToElemVisitor : public Visitor {
// failed: call assert runtime function
p->scope() = IRScope(failedbb);

/* DMD Bugzilla 8360: If the condition is evaluated to true,
* msg is not evaluated at all. So should use toElemDtor()
* instead of toElem().
*/
DValue *const msg = e->msg ? toElemDtor(e->msg) : nullptr;
Module *const module = p->func()->decl->getModule();
if (global.params.checkAction == CHECKACTION_C) {
const auto cMsg =
msg ? DtoArrayPtr(msg) // assuming `msg` is null-terminated, like DMD
: DtoConstCString(e->e1->toChars());
DtoCAssert(module, e->e1->loc, cMsg);
if (global.params.checkAction == CHECKACTION_halt) {
p->ir->CreateCall(GET_INTRINSIC_DECL(trap), {});
p->ir->CreateUnreachable();
} else {
DtoAssert(module, e->loc, msg);
/* DMD Bugzilla 8360: If the condition is evaluated to true,
* msg is not evaluated at all. So should use toElemDtor()
* instead of toElem().
*/
DValue *msg = e->msg ? toElemDtor(e->msg) : nullptr;
Module *module = p->func()->decl->getModule();
if (global.params.checkAction == CHECKACTION_C) {
LLValue *cMsg =
msg ? DtoArrayPtr(
msg) // assuming `msg` is null-terminated, like DMD
: DtoConstCString(e->e1->toChars());
DtoCAssert(module, e->e1->loc, cMsg);
} else {
DtoAssert(module, e->loc, msg);
}
}

// passed:
Expand All @@ -1705,8 +1711,8 @@ class ToElemVisitor : public Visitor {
// class/struct invariants
if (global.params.useInvariants != CHECKENABLEon)
return;
if (condty->ty == Tclass) {
const auto sym = static_cast<TypeClass *>(condty)->sym;
if (auto tc = condty->isTypeClass()) {
const auto sym = tc->sym;
if (sym->isInterfaceDeclaration() || sym->isCPPclass())
return;

Expand Down
11 changes: 11 additions & 0 deletions tests/codegen/checkaction_halt.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %ldc -checkaction=halt -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// CHECK: define {{.*}}_D16checkaction_halt3foo
void foo(int x)
{
assert(x, "msg");

// CHECK: assertFailed:
// CHECK-NEXT: call void @llvm.trap()
// CHECK-NEXT: unreachable
}

0 comments on commit 3424b55

Please sign in to comment.