Skip to content

Commit

Permalink
Skip record field names with --obfuscate=protected (#11997)
Browse files Browse the repository at this point in the history
- Don't obfuscate record field names when `--obfuscate=protected` is
  used, since doing so causes the names to be inconsistent with the
  names in the record constructors/types.

Fixes #11994
  • Loading branch information
perost committed Feb 16, 2024
1 parent eec8462 commit 9f40725
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
21 changes: 16 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Original file line number Diff line number Diff line change
Expand Up @@ -744,21 +744,32 @@ public
function obfuscateCref
input output ComponentRef cref;
input ObfuscationMap obfuscationMap;
output Boolean insideRecord = false;
protected
Option<String> name;
ComponentRef rest_cref;
algorithm
() := match cref
case ComponentRef.CREF()
algorithm
name := UnorderedMap.get(cref.node, obfuscationMap);
if isSome(name) then
cref.node := InstNode.rename(Util.getOption(name), cref.node);
(rest_cref, insideRecord) := obfuscateCref(cref.restCref, obfuscationMap);
cref.restCref := rest_cref;
// Only obfuscate variables that do not belong to a record instance,
// record field names need to be kept to keep them consistent with the
// record constructors.
if not insideRecord then
name := UnorderedMap.get(cref.node, obfuscationMap);
if isSome(name) then
cref.node := InstNode.rename(Util.getOption(name), cref.node);
end if;
end if;
insideRecord := InstNode.isRecord(cref.node);
cref.subscripts := list(Subscript.mapShallowExp(s,
function obfuscateExp(obfuscationMap = obfuscationMap)) for s in cref.subscripts);
cref.restCref := obfuscateCref(cref.restCref, obfuscationMap);
then
();
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/interactive-API/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ MoveClass.mos \
Obfuscation1.mos \
Obfuscation2.mos \
Obfuscation3.mos \
Obfuscation5.mos \
ProtectedHandlingBug2917.mos \
ReadOnlyPkg.mos \
refactorGraphAnn1.mos \
Expand Down
46 changes: 46 additions & 0 deletions testsuite/openmodelica/interactive-API/Obfuscation5.mos
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// name: Obfuscation5
// keywords:
// status: correct
// cflags: -d=newInst
//

setCommandLineOptions("--obfuscate=protected");
loadString("
record R
Real x;
Real y;
end R;

model M
R r1;
protected
R r2;
equation
r1 = R(1, 2);
r2 = R(3, 4);
end M;
");

instantiateModel(M); getErrorString();

// Result:
// true
// true
// "function R \"Automatically generated record constructor for R\"
// input Real x;
// input Real y;
// output R res;
// end R;
//
// class M
// Real r1.x;
// Real r1.y;
// protected Real n1.x;
// protected Real n1.y;
// equation
// r1 = R(1.0, 2.0);
// n1 = R(3.0, 4.0);
// end M;
// "
// ""
// endResult

0 comments on commit 9f40725

Please sign in to comment.