@@ -70,6 +70,13 @@ class MemberAssignmentOperation extends FunctionCall {
7070 */
7171pragma [ noopt]
7272Variable getALoopCounter ( ForStmt fs ) {
73+ // ------------------------------------------------------------------------------------------------
74+ // NOTE: This is an updated version of ForStmt.getAnIterationVariable(), handling additional cases.
75+ // The use of pragma[noopt] is retained from the original code, as we haven't determined
76+ // whether it's still necessary across a broad range of databases. As a noopt predicate, it
77+ // includes a degree of duplication as the join order is defined based on the order of the
78+ // conditions.
79+ // ------------------------------------------------------------------------------------------------
7380 // check that it is assigned to, incremented or decremented in the update
7481 exists ( Expr updateOpRoot , Expr updateOp |
7582 updateOpRoot = fs .getUpdate ( ) and
@@ -106,6 +113,15 @@ Variable getALoopCounter(ForStmt fs) {
106113 )
107114 or
108115 updateOp = result .getAnAssignedValue ( )
116+ or
117+ // updateOp is an access whose address is taken in a non-const way
118+ exists ( FunctionCall fc , VariableAccess va |
119+ fc = updateOp and
120+ fc instanceof FunctionCall and
121+ fc .getAnArgument ( ) = va and
122+ va = result .getAnAccess ( ) and
123+ va .isAddressOfAccessNonConst ( )
124+ )
109125 ) and
110126 result instanceof Variable and
111127 // checked or used in the condition
@@ -260,7 +276,7 @@ predicate isLoopControlVarModifiedInLoopCondition(
260276 loopControlVariableAccess = forLoop .getCondition ( ) .getAChild + ( ) and
261277 (
262278 loopControlVariableAccess .isModified ( ) or
263- loopControlVariableAccess .isAddressOfAccess ( )
279+ loopControlVariableAccess .isAddressOfAccessNonConst ( )
264280 )
265281}
266282
@@ -277,7 +293,7 @@ predicate isLoopControlVarModifiedInLoopExpr(
277293 loopControlVariableAccess = forLoop .getUpdate ( ) .getAChild ( ) and
278294 (
279295 loopControlVariableAccess .isModified ( ) or
280- loopControlVariableAccess .isAddressOfAccess ( )
296+ loopControlVariableAccess .isAddressOfAccessNonConst ( )
281297 )
282298}
283299
0 commit comments