Skip to content

Commit f00fd3e

Browse files
authored
Merge pull request #6901 from WalterBright/betterC-assert
fix Issue 17499 - with -betterC switch, call C's assert failure function rather than druntime's merged-on-behalf-of: Daniel Murphy <yebblies@gmail.com>
2 parents c10719d + d11482c commit f00fd3e

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

src/ddmd/backend/rtlsym.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ enum
169169
RTLSYM_TRACEARRAYSETLENGTHIT,
170170
RTLSYM_TRACEALLOCMEMORY,
171171

172+
RTLSYM_C_ASSERT,
173+
RTLSYM_C__ASSERT,
174+
RTLSYM_C__ASSERT_RTN,
175+
172176
RTLSYM_MAX
173177
}
174178

src/ddmd/backend/rtlsym.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ SYMBOL_MARS(TRACEARRAYAPPENDWD, FLfunc,FREGSAVED,"_d_arrayappendwdTrace", 0, t
193193
SYMBOL_MARS(TRACEARRAYSETLENGTHT, FLfunc,FREGSAVED,"_d_arraysetlengthTTrace", 0, t) \
194194
SYMBOL_MARS(TRACEARRAYSETLENGTHIT,FLfunc,FREGSAVED,"_d_arraysetlengthiTTrace", 0, t) \
195195
SYMBOL_MARS(TRACEALLOCMEMORY, FLfunc,FREGSAVED,"_d_allocmemoryTrace", 0, t) \
196+
SYMBOL_MARS(C_ASSERT, FLfunc,FREGSAVED,"_assert", SFLexit, t) \
197+
SYMBOL_MARS(C__ASSERT, FLfunc,FREGSAVED,"__assert", SFLexit, t) \
198+
SYMBOL_MARS(C__ASSERT_RTN, FLfunc,FREGSAVED,"__assert_rtn", SFLexit, t) \
196199

197200

198201
// Migrate to function interface to rtl symbols

src/ddmd/e2ir.d

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,12 @@ elem *toElem(Expression e, IRState *irs)
19601960
elem *e;
19611961
if (global.params.useAssert)
19621962
{
1963+
if (global.params.betterC)
1964+
{
1965+
result = callCAssert(irs, ae);
1966+
return;
1967+
}
1968+
19631969
e = toElem(ae.e1, irs);
19641970
Symbol *ts = null;
19651971
elem *einv = null;
@@ -5998,3 +6004,85 @@ void toTraceGC(IRState *irs, elem *e, Loc *loc)
59986004
e.EV.E2 = el_param(e.EV.E2, filelinefunction(irs, loc));
59996005
}
60006006
}
6007+
6008+
/****************************************
6009+
* Generate call to C's assert failure function.
6010+
* Params:
6011+
* irs = context
6012+
* e = expression to test
6013+
* Returns:
6014+
* generated call
6015+
*/
6016+
6017+
elem *callCAssert(IRState *irs, AssertExp ae)
6018+
{
6019+
//printf("callCAssert.toElem() %s\n", ae.toChars());
6020+
elem *econd = toElem(ae.e1, irs);
6021+
6022+
Module m = cast(Module)irs.blx._module;
6023+
const(char)* mname = m.srcfile.toChars();
6024+
6025+
//printf("filename = '%s'\n", ae.loc.filename);
6026+
//printf("module = '%s'\n", mname);
6027+
6028+
/* If the source file name has changed, probably due
6029+
* to a #line directive.
6030+
*/
6031+
elem *efilename;
6032+
if (ae.loc.filename && strcmp(ae.loc.filename, mname) != 0)
6033+
{
6034+
const(char)* id = ae.loc.filename;
6035+
size_t len = strlen(id);
6036+
Symbol *si = toStringSymbol(id, len, 1);
6037+
efilename = el_ptr(si);
6038+
}
6039+
else
6040+
{
6041+
efilename = toEfilenamePtr(m);
6042+
}
6043+
6044+
elem *emsg;
6045+
if (ae.msg)
6046+
{
6047+
// Assuming here that ae.msg generates a 0 terminated string
6048+
elem *e = toElemDtor(ae.msg, irs);
6049+
emsg = array_toPtr(Type.tvoid.arrayOf(), e);
6050+
}
6051+
else
6052+
{
6053+
// Generate a message out of the assert expression
6054+
const(char)* id = ae.e1.toChars();
6055+
const len = strlen(id);
6056+
Symbol *si = toStringSymbol(id, len, 1);
6057+
emsg = el_ptr(si);
6058+
}
6059+
6060+
auto eline = el_long(TYint, ae.loc.linnum);
6061+
6062+
elem *ea;
6063+
if (global.params.isOSX)
6064+
{
6065+
// __assert_rtn(func, file, line, msg);
6066+
const(char)* id = "";
6067+
FuncDeclaration fd = irs.getFunc();
6068+
if (fd)
6069+
id = fd.toPrettyChars();
6070+
const len = strlen(id);
6071+
Symbol *si = toStringSymbol(id, len, 1);
6072+
elem *efunc = el_ptr(si);
6073+
6074+
auto eassert = el_var(getRtlsym(RTLSYM_C__ASSERT_RTN));
6075+
ea = el_bin(OPcall, TYvoid, eassert, el_params(emsg, eline, efilename, efunc, null));
6076+
}
6077+
else
6078+
{
6079+
// [_]_assert(msg, file, line);
6080+
const rtlsym = (global.params.isWindows) ? RTLSYM_C_ASSERT : RTLSYM_C__ASSERT;
6081+
auto eassert = el_var(getRtlsym(rtlsym));
6082+
ea = el_bin(OPcall, TYvoid, eassert, el_params(eline, efilename, emsg, null));
6083+
}
6084+
auto e = el_bin(OPoror,TYvoid,econd,ea);
6085+
elem_setLoc(e,ae.loc);
6086+
return e;
6087+
}
6088+

test/runnable/cassert.d

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* REQUIRED_ARGS: -betterC
2+
PERMUTE_ARGS:
3+
*/
4+
5+
6+
void test(int ij)
7+
{
8+
assert(ij);
9+
#line 100 "anotherfile"
10+
assert(ij,"it is not zero");
11+
}
12+
13+
int main()
14+
{
15+
test(1);
16+
return 0;
17+
}

0 commit comments

Comments
 (0)