@@ -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+ 
0 commit comments