Skip to content

Commit

Permalink
[janitor] Fix string prefix checks (#11537)
Browse files Browse the repository at this point in the history
Also clanup string util functions
  • Loading branch information
phannebohm committed Nov 8, 2023
1 parent bebc301 commit 43dfd75
Show file tree
Hide file tree
Showing 40 changed files with 125 additions and 145 deletions.
13 changes: 5 additions & 8 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -63,6 +63,7 @@ import List;
import MetaModelica.Dangerous;
import Mutable;
import SCodeUtil;
import StringUtil;
import System;
import Types;
import Util;
Expand Down Expand Up @@ -1441,13 +1442,11 @@ public function isRESVar
input BackendDAE.Var inVar;
output Boolean outBoolean;
algorithm
outBoolean := match (inVar.varName)
outBoolean := match inVar.varName
local
String s;
case(DAE.CREF_IDENT(ident=s))
then (stringLength(s) > 3 and substring(s, 1, 4) == "$res");
case(DAE.CREF_QUAL(ident=s))
then (stringLength(s) > 3 and substring(s, 1, 4) == "$res");
case DAE.CREF_IDENT(ident=s) then StringUtil.startsWith(s, "$res");
case DAE.CREF_QUAL(ident=s) then StringUtil.startsWith(s, "$res");
else false;
end match;
end isRESVar;
Expand Down Expand Up @@ -4445,9 +4444,7 @@ end calcAliasKey;

public function selfGeneratedVar
input DAE.ComponentRef inCref;
output Boolean b;
algorithm
b := substring(ComponentReference.crefStr(inCref), 1, 1) == "$";
output Boolean b = StringUtil.startsWith(ComponentReference.crefStr(inCref), "$");
end selfGeneratedVar;

public function varStateSelectPrioAlias "Helper function to calculateVarPriorities.
Expand Down
30 changes: 12 additions & 18 deletions OMCompiler/Compiler/BackEnd/CommonSubExpression.mo
Expand Up @@ -64,6 +64,7 @@ import HashTableExpToIndex;
import HpcOmTaskGraph;
import List;
import ResolveLoops;
import StringUtil;
import Types;

uniontype CSE_Equation
Expand Down Expand Up @@ -1645,32 +1646,25 @@ public function isCSECref
"Returns true if the cref is prefixed with '$cse'"
input DAE.ComponentRef cr;
output Boolean b;
protected
String s;
algorithm
b := matchcontinue(cr)
case(DAE.CREF_IDENT(ident=s))
then (stringLength(s) > 3 and substring(s, 1, 4) == "$cse");
case(DAE.CREF_QUAL(ident=s))
then (stringLength(s) > 3 and substring(s, 1, 4) == "$cse");
end matchcontinue;
b := match cr
local
String s;
case DAE.CREF_IDENT(ident=s) then StringUtil.startsWith(s, "$cse");
case DAE.CREF_QUAL(ident=s) then StringUtil.startsWith(s, "$cse");
else false;
end match;
end isCSECref;

public function isCSEExp
"Returns true if the exp is prefixed with '$cse'"
input DAE.Exp inExp;
output Boolean b;
protected
DAE.ComponentRef cr;
String s;
algorithm
try
cr := Expression.expCref(inExp);
DAE.CREF_IDENT(ident=s) := cr;
b := substring(s, 1, 4) == "$cse";
else
b := false;
end try;
b := match inExp
case DAE.CREF() then isCSECref(inExp.componentRef);
else false;
end match;
end isCSEExp;

public function cseBinary "authors: Jan Hagemann and Lennart Ochel (FH Bielefeld, Germany)
Expand Down
9 changes: 5 additions & 4 deletions OMCompiler/Compiler/BackEnd/Differentiate.mo
Expand Up @@ -73,6 +73,7 @@ protected import Flags;
protected import Inline;
protected import List;
protected import SCode;
protected import StringUtil;
protected import Util;
protected import SymbolicJacobian.DAE_CJ;

Expand Down Expand Up @@ -1328,11 +1329,11 @@ public function isSeedCref
input DAE.ComponentRef cr;
output Boolean b;
algorithm
b := matchcontinue(cr)
case(DAE.CREF_IDENT()) then (substring(cr.ident, 1, 4) == "Seed");
case(DAE.CREF_QUAL()) then isSeedCref(cr.componentRef);
b := match cr
case DAE.CREF_IDENT() then StringUtil.startsWith(cr.ident, "Seed");
case DAE.CREF_QUAL() then StringUtil.startsWith(cr.ident, "Seed");

This comment has been minimized.

Copy link
@hkiel

hkiel Nov 9, 2023

Member

@phannebohm Is the change from recursive call with isSeedCref(cr.componentRef) to direct evaluation of cr.ident wanted?

This comment has been minimized.

Copy link
@phannebohm

phannebohm Nov 9, 2023

Author Contributor

Thanks for reminding me. As I'm checking it now it should probably be recursive. I'm a bit confused since all tests passed, though.

This comment has been minimized.

Copy link
@phannebohm

phannebohm Nov 9, 2023

Author Contributor

Fixed in #11545

This comment has been minimized.

Copy link
@hkiel

hkiel Nov 9, 2023

Member

Maybe you can add a test so that it will not break again?

else false;
end matchcontinue;
end match;
end isSeedCref;

protected function differentiateCalls
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/BackEnd/Initialization.mo
Expand Up @@ -42,6 +42,7 @@ import BackendDAE;
import BackendDAEFunc;
import DAE;
import HashSet;
import StringUtil;
import Util;

protected
Expand Down Expand Up @@ -2423,7 +2424,7 @@ algorithm

if isFixed then
// Special case for initial state selection
if Util.stringStartsWith("$STATESET",ComponentReference.crefFirstIdent(cr)) and Flags.getConfigBool(Flags.INITIAL_STATE_SELECTION) then
if StringUtil.startsWith(ComponentReference.crefFirstIdent(cr), "$STATESET") and Flags.getConfigBool(Flags.INITIAL_STATE_SELECTION) then
stateSetSplit = Util.stringSplitAtChar(ComponentReference.crefFirstIdent(cr),".");
stateSetIdxString::stateSetSplit = stateSetSplit;
stateSetIdxString = substring(stateSetIdxString,10,stringLength(stateSetIdxString));
Expand Down
5 changes: 3 additions & 2 deletions OMCompiler/Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -73,6 +73,7 @@ import Graph;
import HashSet;
import IndexReduction;
import List;
import StringUtil;
import System;
import UnorderedMap;
import Util;
Expand Down Expand Up @@ -4435,9 +4436,9 @@ algorithm
local
DAE.ComponentRef cr;

case DAE.CREF_IDENT() guard(stringLength(cref.ident) > 4 and substring(cref.ident, 1, 5) == "$pDER") then (cref, true);
case DAE.CREF_IDENT() guard(StringUtil.startsWith(cref.ident, "$pDER")) then (cref, true);

case DAE.CREF_QUAL() guard(stringLength(cref.ident) > 4 and substring(cref.ident, 1, 5) == "$pDER") then (cref, true);
case DAE.CREF_QUAL() guard(StringUtil.startsWith(cref.ident, "$pDER")) then (cref, true);

case DAE.CREF_QUAL() algorithm
(cr, strip) := stripPartialDerWork(cref.componentRef);
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -59,6 +59,7 @@ import Flags;
import HashTable;
import List;
import MMath;
import StringUtil;
import Types;
import Util;
import MetaModelica.Dangerous.listReverseInPlace;
Expand Down Expand Up @@ -252,7 +253,7 @@ algorithm
subPartition := shared.partitionsInfo.subPartitions[idx];
solverMethod := BackendDump.optionString(getSubClockSolverOpt(subPartition.clock));
// check solverMethod
if stringLength(solverMethod) > 7 and substring(solverMethod, 1, 8) == "Explicit" then
if StringUtil.startsWith(solverMethod, "Explicit") then
if solverMethod <> "ExplicitEuler" then
Error.addMessage(Error.CLOCK_SOLVERMETHOD, {"ExplicitEuler", solverMethod});
solverMethod := "ExplicitEuler";
Expand Down
7 changes: 3 additions & 4 deletions OMCompiler/Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -55,6 +55,7 @@ protected import Flags;
protected import List;
protected import MetaModelica.Dangerous;
protected import Print;
protected import StringUtil;
protected import System;
protected import Types;
protected import Util;
Expand Down Expand Up @@ -1207,10 +1208,8 @@ algorithm
b := match(cr)
case(DAE.CREF_QUAL(ident=DAE.derivativeNamePrefix)) then false; // allow exception for derivate vars
case(DAE.CREF_QUAL(ident=DAE.previousNamePrefix)) then false; // allow exception for Clk-previous vars
case(DAE.CREF_IDENT(ident=s))
then (substring(s, 1, 1) == "$");
case(DAE.CREF_QUAL(ident=s))
then (substring(s, 1, 1) == "$");
case(DAE.CREF_IDENT(ident=s)) then StringUtil.startsWith(s, "$");
case(DAE.CREF_QUAL(ident=s)) then StringUtil.startsWith(s, "$");
else false;
end match;
end isInternalCref;
Expand Down
5 changes: 1 addition & 4 deletions OMCompiler/Compiler/FrontEnd/Inst.mo
Expand Up @@ -148,7 +148,6 @@ import Mutable;
import PrefixUtil;
import SCodeUtil;
import SCodeInstUtil;
import StringUtil;
import Static;
import Types;
import UnitParserExt;
Expand Down Expand Up @@ -5556,9 +5555,7 @@ protected function generateCachePath
protected
String name;
algorithm
name := StringUtil.stringAppend9(InstTypes.callingScopeStr(callScope), "$",
SCodeDump.restrString(SCodeUtil.getClassRestriction(cls)), "$",
generatePrefixStr(prefix), "$");
name := InstTypes.callingScopeStr(callScope) + "$" + SCodeDump.restrString(SCodeUtil.getClassRestriction(cls)) + "$" + generatePrefixStr(prefix) + "$";
cachePath := AbsynUtil.joinPaths(Absyn.IDENT(name), FGraph.getGraphName(env));
end generateCachePath;

Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/FrontEnd/Mod.mo
Expand Up @@ -2456,7 +2456,7 @@ algorithm
len1 = stringLength(i);
len2 = stringLength(idx);
// either one of them is a substring of the other
true = boolOr(0 == System.strncmp(i, idx, len1), 0 == System.strncmp(idx, i, len2));
true = (0 == System.strncmp(i, idx, intMin(len1, len2)));
then true;
else false;
end matchcontinue;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo
Expand Up @@ -562,7 +562,7 @@ public

function isFunctionAlias extends checkVar;
algorithm
b := Util.stringStartsWith(FUNCTION_STR, ComponentRef.firstName(getVarName(var_ptr)));
b := StringUtil.startsWith(ComponentRef.firstName(getVarName(var_ptr)), FUNCTION_STR);
end isFunctionAlias;

function createTimeVar
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBAdjacency.mo
Expand Up @@ -59,6 +59,7 @@ protected
import BackendUtil = NBBackendUtil;
import BuiltinSystem = System;
import Slice = NBSlice;
import StringUtil;

// SetBased Graph imports
import SBGraph.BipartiteIncidenceList;
Expand Down
Expand Up @@ -73,6 +73,7 @@ protected
import ClockIndexes;
import DoubleEnded;
import Slice = NBSlice;
import StringUtil;

public
function main extends Module.wrapper;
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/1_Main/NBMatching.mo
Expand Up @@ -63,6 +63,7 @@ protected
import BackendUtil = NBBackendUtil;
import Slice = NBSlice;
import NBSlice.IntLst;
import StringUtil;
public
// =======================================
// MATCHING
Expand Down
Expand Up @@ -56,6 +56,7 @@ protected
// util imports
import BackendUtil = NBBackendUtil;
import Slice = NBSlice;
import StringUtil;
import UnorderedSet;

public
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBBindings.mo
Expand Up @@ -43,6 +43,9 @@ protected
import BVariable = NBVariable;
import NBVariable.{VariablePointers, VarData};

// Util
import StringUtil;

public
function main extends Module.wrapper;
algorithm
Expand Down
4 changes: 4 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBDetectStates.mo
Expand Up @@ -60,6 +60,10 @@ protected
import Differentiate = NBDifferentiate;
import NBEquation.{Equation, EquationPointers, EqData, WhenEquationBody, WhenStatement};
import NBVariable.{VariablePointers, VarData};

// Util
import StringUtil;

// =========================================================================
// MAIN ROUTINE, PLEASE DO NOT CHANGE
// =========================================================================
Expand Down
Expand Up @@ -61,6 +61,7 @@ protected
import NBVariable.{VariablePointers, VarData};

// Util imports
import StringUtil;
import UnorderedMap;
public
function main
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBInline.mo
Expand Up @@ -61,6 +61,9 @@ protected
import Replacements = NBReplacements;
import NBVariable.{VariablePointers, VarData};

// Util
import StringUtil;

// =========================================================================
// MAIN ROUTINE, PLEASE DO NOT CHANGE
// =========================================================================
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/NBackEnd/Modules/NBModule.mo
Expand Up @@ -81,6 +81,7 @@ protected

// Util imports
import BuiltinSystem = System;
import StringUtil;

public
partial function wrapper
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/NBackEnd/Util/NBReplacements.mo
Expand Up @@ -67,6 +67,9 @@ protected
import StrongComponent = NBStrongComponent;
import NBVariable.{VarData, VariablePointers};

// Util
import StringUtil;

public
// =========================================================================
// COMPONENT REFERENCE REPLACEMENT
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -47,6 +47,7 @@ protected
import Prefixes = NFPrefixes;
import MetaModelica.Dangerous.*;
import JSON;
import StringUtil;
import Variable = NFVariable;

import ComponentRef = NFComponentRef;
Expand Down Expand Up @@ -1093,7 +1094,7 @@ public
function backendCref
"returns true if the cref was generated by the backend (starts with $)"
input ComponentRef cref;
output Boolean b = substring(firstName(cref), 1, 1) == "$";
output Boolean b = StringUtil.startsWith(firstName(cref), "$");
end backendCref;

function toAbsyn
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -51,6 +51,7 @@ protected
import NFComponentRef.ComponentRef;
import DAE.ElementSource;
import MetaModelica.Dangerous.listReverseInPlace;
import StringUtil;
import Util;
import Prefixes = NFPrefixes;
import NFPrefixes.Visibility;
Expand Down Expand Up @@ -910,7 +911,7 @@ public
case "unassignedMessage" then false;
case "Protection" then false;
case "Authorization" then false;
else not Util.stringStartsWith("__", mod.ident);
else not StringUtil.startsWith(mod.ident, "__");
end match;
end isAllowedAnnotation;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -65,6 +65,7 @@ import Sections = NFSections;
import NFOCConnectionGraph;
import Prefixes = NFPrefixes;
import RangeIterator = NFRangeIterator;
import StringUtil;
import Subscript = NFSubscript;
import Type = NFType;
import Util;
Expand Down Expand Up @@ -1277,15 +1278,14 @@ function addIterator_traverse
input list<Subscript> subscripts;
protected
String restString, prefixString = ComponentRef.toString(Prefix.prefix(prefix));
Integer prefixLength = stringLength(prefixString);
algorithm
exp := match exp
local
ComponentRef restCref;
case Expression.CREF(cref = ComponentRef.CREF(restCref = restCref))
algorithm
restString := ComponentRef.toString(restCref);
if prefixLength <= stringLength(restString) and prefixString == substring(restString, 1, prefixLength) then
if StringUtil.startsWith(restString, prefixString) then
exp.cref := ComponentRef.mergeSubscripts(subscripts, exp.cref, applyToScope = true);
end if;
then
Expand Down
7 changes: 3 additions & 4 deletions OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -54,6 +54,7 @@ import InstUtil;
import Class = NFClass;
import Component = NFComponent;
import Attributes = NFAttributes;
import StringUtil;
import Typing = NFTyping;
import TypeCheck = NFTypeCheck;
import Util;
Expand Down Expand Up @@ -2390,10 +2391,8 @@ protected
name := InstNode.name(component);

// Remove $in_ for OM input output arguments.
if stringGet(name, 1) == 36 /*$*/ then
if stringLength(name) > 4 and substring(name, 1, 4) == "$in_" then
name := substring(name, 5, stringLength(name));
end if;
if StringUtil.startsWith(name, "$in_") then
name := substring(name, 5, stringLength(name));
end if;

slot := SLOT(component, SlotType.GENERIC, default, NONE(), index, SlotEvalStatus.NOT_EVALUATED);
Expand Down

0 comments on commit 43dfd75

Please sign in to comment.