Skip to content

Commit d6b0772

Browse files
committed
Ruby: Improve performance of instanceVariableSelfSynthesis
1 parent d86983b commit d6b0772

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/Synthesis.qll

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,29 @@ private module ImplicitSelfSynthesis {
211211
}
212212
}
213213

214-
/**
215-
* Gets the "owner" of a node. For real nodes this is the node itself, for synthetic nodes
216-
* this is the closest parent that is a real node.
217-
*/
218-
private TAstNodeReal owner(AstNode node) { result = synthParent*(node) }
214+
pragma[nomagic]
215+
private AstNode instanceVarAccessSynthParentStar(InstanceVariableAccess var) {
216+
result = var
217+
or
218+
instanceVarAccessSynthParentStar(var) = getSynthChild(result, _)
219+
}
219220

220221
/**
221-
* Gets the parent of a synthetic node
222+
* Gets the `SelfKind` for instance variable access `var`. This is based on the
223+
* "owner" of `var`; for real nodes this is the node itself, for synthetic nodes
224+
* this is the closest parent that is a real node.
222225
*/
223-
private AstNode synthParent(AstNode node) { node = getSynthChild(result, _) }
226+
pragma[nomagic]
227+
private SelfKind getSelfKind(InstanceVariableAccess var) {
228+
exists(Ruby::AstNode owner |
229+
owner = toGenerated(instanceVarAccessSynthParentStar(var)) and
230+
result = SelfKind(TSelfVariable(scopeOf(owner).getEnclosingSelfScope()))
231+
)
232+
}
224233

225234
pragma[nomagic]
226235
private predicate instanceVariableSelfSynthesis(InstanceVariableAccess var, int i, Child child) {
227-
child =
228-
SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(owner(var))).getEnclosingSelfScope()))) and
236+
child = SynthChild(getSelfKind(var)) and
229237
i = 0
230238
}
231239

0 commit comments

Comments
 (0)