Skip to content

Commit

Permalink
Add options to saveTotalModelDebug (#10921)
Browse files Browse the repository at this point in the history
- Add options for stripping and obfuscation to saveTotalModelDebug for
  feature parity with saveTotalModel.
  • Loading branch information
perost committed Jul 3, 2023
1 parent a8e92e8 commit b9c7adf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,9 @@ function saveTotalModelDebug
and is meant to be used in cases where the normal saveTotalModel fails."
input String filename;
input TypeName className;
input Boolean stripAnnotations = false;
input Boolean stripComments = false;
input Boolean obfuscate = false;
output Boolean success;
external "builtin";
annotation(preferredView="text");
Expand Down
12 changes: 12 additions & 0 deletions OMCompiler/Compiler/FrontEnd/SCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4606,6 +4606,18 @@ algorithm
end match;
end isClassNamed;

public function isElementNamed
input SCode.Ident name;
input SCode.Element element;
output Boolean res;
algorithm
res := match element
case SCode.CLASS() then element.name == name;
case SCode.COMPONENT() then element.name == name;
else false;
end match;
end isElementNamed;

public function getElementComment
"Returns the comment of an element."
input SCode.Element inElement;
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,9 @@ function saveTotalModelDebug
and is meant to be used in cases where the normal saveTotalModel fails."
input String filename;
input TypeName className;
input Boolean stripAnnotations = false;
input Boolean stripComments = false;
input Boolean obfuscate = false;
output Boolean success;
external "builtin";
annotation(preferredView="text");
Expand Down
38 changes: 29 additions & 9 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1944,11 +1944,12 @@ algorithm
Values.BOOL(_), Values.BOOL(_), Values.BOOL(_)})
then Values.BOOL(false);

case ("saveTotalModelDebug",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath))})
case ("saveTotalModelDebug",{Values.STRING(filename),Values.CODE(Absyn.C_TYPENAME(classpath)),
Values.BOOL(b1), Values.BOOL(b2), Values.BOOL(b3)})
equation
Values.ENUM_LITERAL(index=access) = Interactive.checkAccessAnnotationAndEncryption(classpath, SymbolTable.getAbsyn());
if (access >= 9) then // i.e., Access.documentation
saveTotalModelDebug(filename, classpath);
saveTotalModelDebug(filename, classpath, b1, b2, b3);
b = true;
else
Error.addMessage(Error.SAVE_ENCRYPTED_CLASS_ERROR, {});
Expand Down Expand Up @@ -7500,20 +7501,39 @@ end saveTotalModel;
protected function saveTotalModelDebug
input String filename;
input Absyn.Path classPath;
input Boolean stripAnnotations;
input Boolean stripComments;
input Boolean obfuscate;
protected
SCode.Program prog;
String str, name_str, cls_str;
String str, name_str, cls_str, str1, str2, str3;
Absyn.Path cls_path = classPath;
Option<SCode.Comment> ocmt;
SCode.Comment cmt;
algorithm
runFrontEndLoadProgram(classPath);
runFrontEndLoadProgram(cls_path);
prog := SymbolTable.getSCode();
prog := TotalModelDebug.getTotalModel(prog, classPath);
prog := TotalModelDebug.getTotalModel(prog, cls_path);
prog := SCodeUtil.removeBuiltinsFromTopScope(prog);
prog := SCodeUtil.stripCommentsFromProgram(prog, stripAnnotations = false, stripComments = true);
ocmt := SCodeUtil.getElementComment(InteractiveUtil.getPathedSCodeElementInProgram(cls_path, prog));
cmt := if isSome(ocmt) then Util.getOption(ocmt) else SCode.noComment;

if stripAnnotations or stripComments then
prog := SCodeUtil.stripCommentsFromProgram(prog, stripAnnotations, stripComments);
end if;

if obfuscate then
(prog, cls_path, cmt, _) := Obfuscate.obfuscateProgram(prog, cls_path, cmt);
end if;

str := SCodeDump.programStr(prog, SCodeDump.defaultOptions);
name_str := AbsynUtil.pathLastIdent(classPath) + "_total";
cls_str := "\nmodel " + name_str + "\n extends " + AbsynUtil.pathString(classPath) + ";\nend " + name_str + ";\n";
System.writeFile(filename, str + cls_str);
str1 := AbsynUtil.pathLastIdent(cls_path) + "_total";
str2 := if stripComments then "" else SCodeDump.printCommentStr(cmt);
str2 := if stringEq(str2,"") then "" else (" " + str2);
str3 := if stripAnnotations then "" else SCodeDump.printAnnotationStr(cmt,SCodeDump.defaultOptions);
str3 := if stringEq(str3,"") then "" else (str3 + ";\n");
str1 := "\nmodel " + str1 + str2 + "\n extends " + AbsynUtil.pathString(cls_path) + ";\n" + str3 + "end " + str1 + ";\n";
System.writeFile(filename, str + str1);
end saveTotalModelDebug;

protected function getDymolaStateAnnotation
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/Script/InteractiveUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4707,5 +4707,20 @@ algorithm
end try;
end getPathedClassRestriction;

public function getPathedSCodeElementInProgram
input Absyn.Path path;
input SCode.Program program;
output SCode.Element element;
protected
String name;
algorithm
name := AbsynUtil.pathFirstIdent(path);
element := List.find(program, function SCodeUtil.isElementNamed(name = name));

if not AbsynUtil.pathIsIdent(path) then
element := getPathedSCodeElementInProgram(AbsynUtil.pathRest(path), SCodeUtil.getClassElements(element));
end if;
end getPathedSCodeElementInProgram;

annotation(__OpenModelica_Interface="backend");
end InteractiveUtil;

0 comments on commit b9c7adf

Please sign in to comment.