Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Check named arguments of API calls like simulate.
Browse files Browse the repository at this point in the history
- Check that the named arguments in StaticScript.getSimultionArguments
  actually exist, which is used by e.g. simulate and buildModel.

Belonging to [master]:
  - #2663
  - OpenModelica/OpenModelica-testsuite#1035
  • Loading branch information
perost authored and OpenModelica-Hudson committed Sep 17, 2018
1 parent 95e1e12 commit 92b7fbb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Compiler/Script/CevalScriptBackend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ algorithm
ErrorExt.setCheckpoint("getSimulationOptions");
simOpt = GlobalScript.SIMULATION_OPTIONS(DAE.RCONST(startTime),DAE.RCONST(stopTime),DAE.ICONST(numberOfIntervals),DAE.RCONST(0.0),DAE.RCONST(tolerance),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""),DAE.SCONST(""));
ErrorExt.rollBack("getSimulationOptions");
(_, _::startTimeExp::stopTimeExp::intervalExp::toleranceExp::_) = StaticScript.getSimulationArguments(FCore.emptyCache(), FGraph.empty(), {Absyn.CREF(cr_1)},{},false,Prefix.NOPRE(),Absyn.dummyInfo,SOME(simOpt));
(_, _::startTimeExp::stopTimeExp::intervalExp::toleranceExp::_) = StaticScript.getSimulationArguments(FCore.emptyCache(), FGraph.empty(), {Absyn.CREF(cr_1)},{},false,Prefix.NOPRE(), "getSimulationOptions", Absyn.dummyInfo,SOME(simOpt));
startTime = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,startTimeExp,Values.REAL(startTime)));
stopTime = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,stopTimeExp,Values.REAL(stopTime)));
tolerance = ValuesUtil.valueReal(Util.makeValueOrDefault(Ceval.cevalSimple,toleranceExp,Values.REAL(tolerance)));
Expand Down
46 changes: 39 additions & 7 deletions Compiler/Script/StaticScript.mo
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public function getSimulationArguments
input list<Absyn.NamedArg> inAbsynNamedArgLst;
input Boolean inImplInst;
input Prefix.Prefix inPrefix;
input String callName;
input SourceInfo inInfo;
input Option<GlobalScript.SimulationOptions> defaultOption;
output FCore.Cache outCache;
Expand All @@ -170,6 +171,7 @@ algorithm
// fill in defaults
case (cache,env,{crexp},args,impl,pre,info,_)
equation
checkSimulationArguments(args, callName, info);
exp = Static.elabCodeExp(crexp,cache,env,DAE.C_TYPENAME(),info);
// We need to force eval in order to get the correct prefix
(cache,v) = Ceval.ceval(cache,env,exp,true,Absyn.MSG(info),0);
Expand Down Expand Up @@ -238,6 +240,36 @@ algorithm
end match;
end getSimulationArguments;

constant list<String> VALID_SIMULATE_ARGS = {
"startTime",
"stopTime",
"numberOfIntervals",
"stepSize",
"tolerance",
"method",
"fileNamePrefix",
"options",
"outputFormat",
"variableFilter",
"cflags",
"simflags"
};

function checkSimulationArguments
input list<Absyn.NamedArg> args;
input String callName;
input SourceInfo info;
protected
list<String> valid_names;
algorithm
for arg in args loop
if not listMember(arg.argName, VALID_SIMULATE_ARGS) then
Error.addSourceMessage(Error.NO_SUCH_PARAMETER, {callName, arg.argName}, info);
fail();
end if;
end for;
end checkSimulationArguments;

public function elabCallInteractive "This function elaborates the functions defined in the interactive environment.
Since some of these functions are meta-functions, they can not be described in the type
system, and is thus given the the type T_UNKNOWN"
Expand Down Expand Up @@ -323,7 +355,7 @@ algorithm

case (cache,env,Absyn.CREF_IDENT(name = "translateModel"),{Absyn.CREF()},args,_,_,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "translateModel", info, NONE());
then
(cache,Expression.makePureBuiltinCall("translateModel",simulationArgs,DAE.T_STRING_DEFAULT),DAE.PROP(DAE.T_STRING_DEFAULT,DAE.C_VAR()));

Expand Down Expand Up @@ -383,42 +415,42 @@ algorithm

case (cache,env,Absyn.CREF_IDENT(name = "buildModel"),{Absyn.CREF()},args,_,_,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "buildModel", info, NONE());
then
(cache,Expression.makePureBuiltinCall("buildModel",simulationArgs,DAE.T_UNKNOWN_DEFAULT),
DAE.PROP(DAE.T_ARRAY(DAE.T_STRING_DEFAULT,{DAE.DIM_INTEGER(2)}),DAE.C_VAR()));

case (cache,env,Absyn.CREF_IDENT(name = "buildModelBeast"),{Absyn.CREF()},args,_,_,_)
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "buildModelBeast", info, NONE());
then
(cache,Expression.makePureBuiltinCall("buildModelBeast",simulationArgs,DAE.T_UNKNOWN_DEFAULT),
DAE.PROP(DAE.T_ARRAY(DAE.T_STRING_DEFAULT,{DAE.DIM_INTEGER(2)}),DAE.C_VAR()));

case (cache,env,Absyn.CREF_IDENT(name = "simulate"),{Absyn.CREF()},args,_,_,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "simulate", info, NONE());
recordtype = CevalScriptBackend.getSimulationResultType();
then
(cache,Expression.makePureBuiltinCall("simulate",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()));

case (cache,env,Absyn.CREF_IDENT(name = "simulation"),{Absyn.CREF()},args,_,_,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "simulation", info, NONE());
recordtype = CevalScriptBackend.getDrModelicaSimulationResultType();
then
(cache,Expression.makePureBuiltinCall("simulation",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()));

case (cache,env,Absyn.CREF_IDENT(name = "linearize"),{Absyn.CREF()},args,_,_,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "linearize", info, NONE());
recordtype = CevalScriptBackend.getSimulationResultType();
then
(cache,Expression.makePureBuiltinCall("linearize",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()));

case (cache,env,Absyn.CREF_IDENT(name = "optimize"),{Absyn.CREF()},args,_,_,_) /* Fill in rest of defaults here */
equation
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, info, NONE());
(cache, simulationArgs) = getSimulationArguments(cache, env, inExps, args, inImplInst, inPrefix, "optimize", info, NONE());
recordtype = CevalScriptBackend.getSimulationResultType();
then
(cache,Expression.makePureBuiltinCall("optimize",simulationArgs,DAE.T_UNKNOWN_DEFAULT),DAE.PROP(recordtype,DAE.C_VAR()));
Expand Down

0 comments on commit 92b7fbb

Please sign in to comment.