Skip to content

Commit

Permalink
- Added printing of Absyn.Info when assertions happen in the simulati…
Browse files Browse the repository at this point in the history
…on runtime, i.e.

  + e.g. [StringTest.mo:8:1-14:56:writable] Simulation call assert() at time 0.11 ...


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7808 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 28, 2011
1 parent 21e74d6 commit 098890e
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 44 deletions.
53 changes: 27 additions & 26 deletions Compiler/Template/Unparsing.mo
Expand Up @@ -16,8 +16,9 @@ public import ExpressionDump;
public import RTOpts;
public import Settings;
public import Patternm;
public import Error;

protected function lm_16
protected function lm_17
input Tpl.Text in_txt;
input SCode.Program in_items;

Expand All @@ -38,16 +39,16 @@ algorithm
i_cl :: rest )
equation
txt = classExternalHeader(txt, i_cl, "");
txt = lm_16(txt, rest);
txt = lm_17(txt, rest);
then txt;

case ( txt,
_ :: rest )
equation
txt = lm_16(txt, rest);
txt = lm_17(txt, rest);
then txt;
end matchcontinue;
end lm_16;
end lm_17;

public function programExternalHeader
input Tpl.Text txt;
Expand All @@ -61,7 +62,7 @@ algorithm
"extern \"C\" {\n",
"#endif\n"
}, true));
out_txt := lm_16(out_txt, a_program);
out_txt := lm_17(out_txt, a_program);
out_txt := Tpl.softNewLine(out_txt);
out_txt := Tpl.writeTok(out_txt, Tpl.ST_STRING_LIST({
"#ifdef __cplusplus\n",
Expand All @@ -71,7 +72,7 @@ algorithm
}, true));
end programExternalHeader;

protected function lm_18
protected function lm_19
input Tpl.Text in_txt;
input list<SCode.Element> in_items;
input SCode.Ident in_a_c_name;
Expand All @@ -96,19 +97,19 @@ algorithm
a_c_name )
equation
txt = elementExternalHeader(txt, i_elt, a_c_name);
txt = lm_18(txt, rest, a_c_name);
txt = lm_19(txt, rest, a_c_name);
then txt;

case ( txt,
_ :: rest,
a_c_name )
equation
txt = lm_18(txt, rest, a_c_name);
txt = lm_19(txt, rest, a_c_name);
then txt;
end matchcontinue;
end lm_18;
end lm_19;

protected function fun_19
protected function fun_20
input Tpl.Text in_txt;
input SCode.Class in_a_cl;

Expand All @@ -124,14 +125,14 @@ algorithm
case ( txt,
SCode.CLASS(classDef = SCode.PARTS(elementLst = i_p_elementLst), name = i_c_name) )
equation
txt = lm_18(txt, i_p_elementLst, i_c_name);
txt = lm_19(txt, i_p_elementLst, i_c_name);
then txt;

case ( txt,
_ )
then txt;
end matchcontinue;
end fun_19;
end fun_20;

public function classExternalHeader
input Tpl.Text txt;
Expand All @@ -140,7 +141,7 @@ public function classExternalHeader

output Tpl.Text out_txt;
algorithm
out_txt := fun_19(txt, a_cl);
out_txt := fun_20(txt, a_cl);
end classExternalHeader;

public function pathString
Expand Down Expand Up @@ -182,7 +183,7 @@ algorithm
end matchcontinue;
end pathString;

protected function lm_22
protected function lm_23
input Tpl.Text in_txt;
input list<SCode.Element> in_items;

Expand All @@ -204,18 +205,18 @@ algorithm
equation
txt = Tpl.writeStr(txt, i_component);
txt = Tpl.nextIter(txt);
txt = lm_22(txt, rest);
txt = lm_23(txt, rest);
then txt;

case ( txt,
_ :: rest )
equation
txt = lm_22(txt, rest);
txt = lm_23(txt, rest);
then txt;
end matchcontinue;
end lm_22;
end lm_23;

protected function lm_23
protected function lm_24
input Tpl.Text in_txt;
input list<SCode.Element> in_items;

Expand All @@ -239,18 +240,18 @@ algorithm
txt = Tpl.writeStr(txt, i_component);
txt = Tpl.writeTok(txt, Tpl.ST_STRING("\""));
txt = Tpl.nextIter(txt);
txt = lm_23(txt, rest);
txt = lm_24(txt, rest);
then txt;

case ( txt,
_ :: rest )
equation
txt = lm_23(txt, rest);
txt = lm_24(txt, rest);
then txt;
end matchcontinue;
end lm_23;
end lm_24;

protected function fun_24
protected function fun_25
input Tpl.Text in_txt;
input list<SCode.Element> in_a_p_elementLst;
input Tpl.Text in_a_fields;
Expand Down Expand Up @@ -321,7 +322,7 @@ algorithm
txt = Tpl.writeTok(txt, Tpl.ST_NEW_LINE());
then txt;
end matchcontinue;
end fun_24;
end fun_25;

public function elementExternalHeader
input Tpl.Text in_txt;
Expand Down Expand Up @@ -356,10 +357,10 @@ algorithm
a_pack )
equation
l_fields = Tpl.pushIter(Tpl.emptyTxt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_STRING(",")), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
l_fields = lm_22(l_fields, i_p_elementLst);
l_fields = lm_23(l_fields, i_p_elementLst);
l_fields = Tpl.popIter(l_fields);
l_fieldsStr = Tpl.pushIter(Tpl.emptyTxt, Tpl.ITER_OPTIONS(0, NONE(), SOME(Tpl.ST_STRING(",")), 0, 0, Tpl.ST_NEW_LINE(), 0, Tpl.ST_NEW_LINE()));
l_fieldsStr = lm_23(l_fieldsStr, i_p_elementLst);
l_fieldsStr = lm_24(l_fieldsStr, i_p_elementLst);
l_fieldsStr = Tpl.popIter(l_fieldsStr);
l_omcname = Tpl.writeStr(Tpl.emptyTxt, a_pack);
l_omcname = Tpl.writeTok(l_omcname, Tpl.ST_STRING("_"));
Expand Down Expand Up @@ -434,7 +435,7 @@ algorithm
txt = Tpl.writeTok(txt, Tpl.ST_STRING(" "));
txt = Tpl.writeText(txt, l_ctor);
txt = Tpl.softNewLine(txt);
txt = fun_24(txt, i_p_elementLst, l_fields, l_omcname, l_ctor, l_fullname);
txt = fun_25(txt, i_p_elementLst, l_fields, l_omcname, l_ctor, l_fullname);
then txt;

case ( txt,
Expand Down
29 changes: 18 additions & 11 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -353,7 +353,7 @@ template globalDataVarInfoArray(String _name, list<SimVar> items)
case items then
<<
const struct omc_varInfo <%_name%>[<%listLength(items)%>] = {
<%items |> var as SIMVAR(info=info as INFO(__)) => '{"<%crefStr(var.name)%>","<%var.comment%>",{"<%info.fileName%>",<%info.lineNumberStart%>,<%info.columnNumberStart%>,<%info.lineNumberEnd%>,<%info.columnNumberEnd%>}}'; separator=",\n"%>
<%items |> var as SIMVAR(info=info as INFO(__)) => '{"<%crefStr(var.name)%>","<%var.comment%>",{<%infoArgs(info)%>}}'; separator=",\n"%>
};
<%items |> var as SIMVAR(info=info as INFO(__)) hasindex i0 => '#define <%cref(var.name)%>__varInfo <%_name%>[<%i0%>]'; separator="\n"%>
>>
Expand Down Expand Up @@ -1486,8 +1486,8 @@ case TERMINATE(__) then
<<
<%preExp%> MODELICA_TERMINATE(<%msgVar%>);
>>
case ASSERT(__) then
assertCommon(condition, message, contextSimulationDiscrete, &varDecls)
case ASSERT(source=SOURCE(info=info)) then
assertCommon(condition, message, contextSimulationDiscrete, &varDecls, info)
end functionWhenReinitStatement;


Expand Down Expand Up @@ -1540,8 +1540,8 @@ template functionWhenReinitStatementThen(list<WhenOperator> reinits, Text &varDe
<%preExp%>
MODELICA_TERMINATE(<%msgVar%>);
>>
case ASSERT(__) then
assertCommon(condition, message, contextSimulationDiscrete, &varDecls)
case ASSERT(source=SOURCE(info=info)) then
assertCommon(condition, message, contextSimulationDiscrete, &varDecls, info)
;separator="\n")
<<
<%body%>
Expand Down Expand Up @@ -2631,7 +2631,7 @@ template functionsFile(String filePrefix,
<<
#include "<%filePrefix%>.h"
#include <algorithm>
#define MODELICA_ASSERT(msg) { fprintf(stderr,"Modelica Assert: %s!\n", msg); }
#define MODELICA_ASSERT(info,msg) { printInfo(stderr,info); fprintf(stderr,"Modelica Assert: %s!\n", msg); }
#define MODELICA_TERMINATE(msg) { fprintf(stderr,"Modelica Terminate: %s!\n", msg); fflush(stderr); }

extern "C" {
Expand Down Expand Up @@ -4293,8 +4293,8 @@ template algStmtAssert(DAE.Statement stmt, Context context, Text &varDecls /*BUF
"Generates an assert algorithm statement."
::=
match stmt
case STMT_ASSERT(__) then
assertCommon(cond, msg, context, &varDecls)
case STMT_ASSERT(source=SOURCE(info=info)) then
assertCommon(cond, msg, context, &varDecls, info)
end algStmtAssert;

template algStmtTerminate(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
Expand Down Expand Up @@ -5180,7 +5180,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
//relation = DAE.LBINARY(e1,DAE.GREATEREQ(ET_REAL()),DAE.RCONST(0))
//string = DAE.SCONST('Model error: Argument of sqrt should >= 0')
//let retPre = assertCommon(relation,s, context, &varDecls)
let retPre = assertCommon(createAssertforSqrt(e1),createDAEString("Model error: Argument of sqrt should >= 0"), context, &varDecls)
let retPre = assertCommon(createAssertforSqrt(e1),createDAEString("Model error: Argument of sqrt should be >= 0"), context, &varDecls, dummyInfo)
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>' ;separator=", ")
let funName = '<%underscorePath(path)%>'
let retType = '<%funName%>_rettype'
Expand Down Expand Up @@ -6314,7 +6314,13 @@ template patternMatch(Pattern pat, Text rhs, Text onPatternFail, Text &varDecls,
else 'UNKNOWN_PATTERN /* rhs: <%rhs%> */<%\n%>'
end patternMatch;

template assertCommon(Exp condition, Exp message, Context context, Text &varDecls)
template infoArgs(Info info)
::=
match info
case INFO(__) then '"<%fileName%>",<%lineNumberStart%>,<%columnNumberStart%>,<%lineNumberEnd%>,<%columnNumberEnd%>,<%isReadOnly%>'
end infoArgs;

template assertCommon(Exp condition, Exp message, Context context, Text &varDecls, Info info)
::=
let &preExpCond = buffer ""
let &preExpMsg = buffer ""
Expand All @@ -6324,7 +6330,8 @@ template assertCommon(Exp condition, Exp message, Context context, Text &varDecl
<%preExpCond%>
if (!<%condVar%>) {
<%preExpMsg%>
MODELICA_ASSERT(<%msgVar%>);
omc_fileInfo info = {<%infoArgs(info)%>};
MODELICA_ASSERT(info, <%msgVar%>);
}
>>
end assertCommon;
Expand Down
16 changes: 15 additions & 1 deletion Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -588,13 +588,15 @@ package Absyn
uniontype Info
record INFO
String fileName;
Boolean isReadOnly;
Integer lineNumberStart;
Integer columnNumberStart;
Integer lineNumberEnd;
Integer columnNumberEnd;
end INFO;
end Info;


constant Info dummyInfo;
end Absyn;


Expand Down Expand Up @@ -1124,6 +1126,11 @@ package DAE
end MATCH;
end MatchType;

uniontype ElementSource
record SOURCE
Absyn.Info info;
end SOURCE;
end ElementSource;
end DAE;


Expand Down Expand Up @@ -1704,4 +1711,11 @@ package Patternm
end getValueCtor;
end Patternm;

package Error
function infoStr
input Absyn.Info info;
output String str;
end infoStr;
end Error;

end SimCodeTV;
4 changes: 2 additions & 2 deletions c_runtime/Makefile.common
Expand Up @@ -15,10 +15,10 @@ ddasrt.o dlinpk.o enorm.o hybrd1.o nelmead.o qform.o r1updt.o \
biglag.o ddassl.o dogleg.o fdjac1.o hybrj.o newuoa.o qrfac.o trsapp.o \
daux.o dlamch.o dpmpar.o hybrd.o lsame.o newuob.o r1mpyq.o update.o division.o\
java_interface.o meta_modelica.o meta_modelica_builtin.o meta_modelica_real.o \
meta_modelica_string_lit.o rtclock.o ModelicaUtilities.o $(EXTRA_OBJS)
meta_modelica_string_lit.o rtclock.o ModelicaUtilities.o simulation_varinfo.o \
$(EXTRA_OBJS)

SIMOBJS = $(FOBJS) simulation_runtime.o simulation_init.o simulation_input.o simulation_events.o linearize.o \
simulation_varinfo.o \
solver_dasrt.o solver_main.o simulation_result_plt.o simulation_result_csv.o simulation_result_bin.o simulation_result_mat.o simulation_delay.o tables.o options.o dgesv_aux.o $(EXTRA_SIMOBJS)

HFILES = \
Expand Down
1 change: 1 addition & 0 deletions c_runtime/modelica.h
Expand Up @@ -81,6 +81,7 @@ typedef modelica_integer size_integer_array_rettype;
#include "meta_modelica_builtin.h"
#include "meta_modelica_real.h"
#include "matrix.h"
#include "simulation_varinfo.h"


/* math functions (-lm)*/
Expand Down
2 changes: 2 additions & 0 deletions c_runtime/simulation_events.cpp
Expand Up @@ -891,6 +891,8 @@ change(modelica_boolean& var)
void
checkTermination()
{
if (terminationAssert || terminationTerminate)
printInfo(stdout, TermInfo);
if (terminationAssert)
{
if (warningLevelAssert)
Expand Down
1 change: 1 addition & 0 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -68,6 +68,7 @@ int terminationAssert = 0;
char* terminateMessage = 0;
int warningLevelAssert = 0;
string TermMsg;
omc_fileInfo TermInfo = omc_dummyFileInfo;
int measure_time_flag = 0;

int sim_verbose; // Flag for logging
Expand Down
5 changes: 3 additions & 2 deletions c_runtime/simulation_runtime.h
Expand Up @@ -86,6 +86,7 @@ extern int terminationTerminate; // Becomes non-zero when user terminates simula
extern int terminationAssert; // Becomes non-zero when model call assert simulation.
extern int warningLevelAssert; // Becomes non-zero when model call assert with warning level.
extern string TermMsg; // message for termination.
extern omc_fileInfo TermInfo; // message for termination.


/* Flags for controlling logging to stdout */
Expand Down Expand Up @@ -370,10 +371,10 @@ int callSolver(int, char**, string, string, double, double, double, long, double

double newTime(double t, double step,double stop);

#define MODELICA_ASSERT(msg) { terminationAssert = 1; TermMsg = msg; }
#define MODELICA_ASSERT(info,msg) { terminationAssert = 1; TermMsg = msg; TermInfo = info;}

#define MODELICA_TERMINATE(msg) { modelTermination=1; \
terminationTerminate = 1; TermMsg = msg; }
terminationTerminate = 1; TermMsg = msg; TermInfo = omc_dummyFileInfo; }

#define initial() localData->init

Expand Down
7 changes: 7 additions & 0 deletions c_runtime/simulation_varinfo.c
Expand Up @@ -31,6 +31,8 @@

#include "simulation_varinfo.h"

const omc_fileInfo omc_dummyFileInfo = {"",-1,-1,-1,-1,1};

void printErrorEqSyst(equationSystemError err, struct omc_equationInfo eq, double var)
{
switch (err) {
Expand All @@ -50,3 +52,8 @@ void printErrorEqSyst(equationSystemError err, struct omc_equationInfo eq, doubl
printf("Unknown equation system error: %d %s %g\n", err, eq.name, var);
}
}

void printInfo(FILE *stream, omc_fileInfo info)
{
fprintf(stream, "[%s:%d:%d-%d:%d:%s] ", info.filename, info.lineStart, info.colStart, info.lineEnd, info.colEnd, info.readonly ? "readonly" : "writable");
}

0 comments on commit 098890e

Please sign in to comment.