@@ -87,8 +87,11 @@ public void validate(Collection<CompilationUnit> toCheck) {
8787 if (runtimeNameIndex != null ) {
8888 runtimeNameIndex .clearSyntheticMarkers ();
8989 }
90+ trveWrapperFuncs .clear ();
91+ wrapperCalls .clear ();
9092 NamePreservation .clearSyntheticMarkers (prog );
9193 runtimeNameIndex = NamePreservation .indexGlobals (prog );
94+ recomputeTrvePreservation ();
9295
9396 lightValidation (toCheck );
9497
@@ -3265,19 +3268,11 @@ private void checkBannedFunctions(ExprFunctionCall e) {
32653268 return ;
32663269 } else if (e .getArgs ().get (1 ) instanceof ExprVarAccess ) {
32673270 // Check if this is a two line hook... thanks Bribe
3268- ExprVarAccess varAccess = (ExprVarAccess ) e .getArgs ().get (1 );
3269- @ Nullable FunctionImplementation nearestFunc = e .attrNearestFuncDef ();
3270- WStatements fbody = nearestFunc .getBody ();
3271- if (e .getParent () instanceof StmtReturn && fbody .size () <= 4 && fbody .get (fbody .size () - 2 ).structuralEquals (e .getParent ())) {
3272- WParameters params = nearestFunc .getParameters ();
3273- if (params .size () == 4 && ((TypeExprSimple ) params .get (0 ).getTyp ()).getTypeName ().equals ("trigger" )
3274- && ((TypeExprSimple ) params .get (1 ).getTyp ()).getTypeName ().equals ("string" )
3275- && ((TypeExprSimple ) params .get (2 ).getTyp ()).getTypeName ().equals ("limitop" )
3276- && ((TypeExprSimple ) params .get (3 ).getTyp ()).getTypeName ().equals ("real" )) {
3277- trveWrapperFuncs .add (nearestFunc .getName ());
3278- WLogger .info ("found wrapper: " + nearestFunc .getName ());
3279- return ;
3280- }
3271+ String wrapper = trveWrapperName (e );
3272+ if (wrapper != null ) {
3273+ trveWrapperFuncs .add (wrapper );
3274+ WLogger .info ("found wrapper: " + wrapper );
3275+ return ;
32813276 }
32823277 }
32833278 } else {
@@ -3318,6 +3313,66 @@ private void checkBannedFunctions(ExprFunctionCall e) {
33183313 }
33193314 }
33203315
3316+ private void recomputeTrvePreservation () {
3317+ prog .accept (new Element .DefaultVisitor () {
3318+ @ Override
3319+ public void visit (ExprFunctionCall call ) {
3320+ super .visit (call );
3321+ if (call .getFuncName ().equals ("TriggerRegisterVariableEvent" ) && call .getArgs ().size () > 1 ) {
3322+ if (call .getArgs ().get (1 ) instanceof ExprStringVal varName ) {
3323+ preserveVariableName (varName .getValS ());
3324+ } else if (call .getArgs ().get (1 ) instanceof ExprVarAccess ) {
3325+ String wrapper = trveWrapperName (call );
3326+ if (wrapper != null ) {
3327+ trveWrapperFuncs .add (wrapper );
3328+ }
3329+ }
3330+ }
3331+ }
3332+
3333+ });
3334+
3335+ // Repeat the cheap call pass so calls which precede their wrapper declaration are covered.
3336+ prog .accept (new Element .DefaultVisitor () {
3337+ @ Override
3338+ public void visit (ExprFunctionCall call ) {
3339+ super .visit (call );
3340+ if (trveWrapperFuncs .contains (call .getFuncName ())
3341+ && call .getArgs ().size () > 1
3342+ && call .getArgs ().get (1 ) instanceof ExprStringVal varName ) {
3343+ preserveVariableName (varName .getValS ());
3344+ }
3345+ }
3346+ });
3347+ }
3348+
3349+ private @ Nullable String trveWrapperName (ExprFunctionCall e ) {
3350+ @ Nullable FunctionImplementation nearestFunc = e .attrNearestFuncDef ();
3351+ if (nearestFunc == null ) {
3352+ return null ;
3353+ }
3354+ WStatements fbody = nearestFunc .getBody ();
3355+ if (!(e .getParent () instanceof StmtReturn )
3356+ || fbody .size () < 2
3357+ || fbody .size () > 4
3358+ || !fbody .get (fbody .size () - 2 ).structuralEquals (e .getParent ())) {
3359+ return null ;
3360+ }
3361+ WParameters params = nearestFunc .getParameters ();
3362+ if (params .size () != 4
3363+ || !(params .get (0 ).getTyp () instanceof TypeExprSimple triggerType )
3364+ || !(params .get (1 ).getTyp () instanceof TypeExprSimple stringType )
3365+ || !(params .get (2 ).getTyp () instanceof TypeExprSimple limitopType )
3366+ || !(params .get (3 ).getTyp () instanceof TypeExprSimple realType )
3367+ || !triggerType .getTypeName ().equals ("trigger" )
3368+ || !stringType .getTypeName ().equals ("string" )
3369+ || !limitopType .getTypeName ().equals ("limitop" )
3370+ || !realType .getTypeName ().equals ("real" )) {
3371+ return null ;
3372+ }
3373+ return nearestFunc .getName ();
3374+ }
3375+
33213376 private void preserveVariableName (String variableName ) {
33223377 runtimeNameIndex .preserve (variableName );
33233378 }
0 commit comments