Skip to content

Commit c33c3a1

Browse files
committed
Address comments
1 parent 6a7e3bf commit c33c3a1

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

ql/src/codeql_ruby/ast/Variable.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class VariableScope extends TScope {
2828
result = this.getAVariable() and
2929
result.getName() = name
3030
}
31+
32+
/** Gets the scope in which this scope is nested, if any. */
33+
VariableScope getOuterScope() { result = enclosingScope(this.getScopeElement()) }
3134
}
3235

3336
/** A variable declared in a scope. */

ql/src/codeql_ruby/ast/internal/Variable.qll

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@ private predicate instanceVariableAccess(
1414
) {
1515
name = var.getValue() and
1616
scope = enclosingModuleOrClass(var) and
17-
if exists(enclosingMethod(var)) then instance = true else instance = false
17+
if hasEnclosingMethod(var) then instance = true else instance = false
1818
}
1919

2020
private predicate classVariableAccess(Generated::ClassVariable var, string name, VariableScope scope) {
2121
name = var.getValue() and
2222
scope = enclosingModuleOrClass(var)
2323
}
2424

25-
private Callable enclosingMethod(Generated::AstNode node) {
26-
parentCallableScope*(enclosingScope(node)) = TCallableScope(result) and
27-
(
28-
result instanceof Method or
29-
result instanceof SingletonMethod
25+
predicate hasEnclosingMethod(Generated::AstNode node) {
26+
exists(Callable method | parentCallableScope*(enclosingScope(node)) = TCallableScope(method) |
27+
method instanceof Method or
28+
method instanceof SingletonMethod
3029
)
3130
}
3231

@@ -36,13 +35,13 @@ private TCallableScope parentCallableScope(TCallableScope scope) {
3635
not c instanceof Method and
3736
not c instanceof SingletonMethod
3837
|
39-
result = outerScope(scope)
38+
result = scope.(VariableScope).getOuterScope()
4039
)
4140
}
4241

4342
private VariableScope parentScope(VariableScope scope) {
4443
not scope instanceof ModuleOrClassScope and
45-
result = outerScope(scope)
44+
result = scope.getOuterScope()
4645
}
4746

4847
private ModuleOrClassScope enclosingModuleOrClass(Generated::AstNode node) {
@@ -96,10 +95,6 @@ private predicate strictlyBefore(Location one, Location two) {
9695
one.getStartLine() = two.getStartLine() and one.getStartColumn() < two.getStartColumn()
9796
}
9897

99-
private VariableScope outerScope(VariableScope scope) {
100-
result = enclosingScope(scope.getScopeElement())
101-
}
102-
10398
/** A scope that may capture outer local variables. */
10499
private class CapturingScope extends VariableScope {
105100
CapturingScope() {
@@ -112,9 +107,6 @@ private class CapturingScope extends VariableScope {
112107
)
113108
}
114109

115-
/** Gets the scope in which this scope is nested, if any. */
116-
VariableScope getOuterScope() { result = outerScope(this) }
117-
118110
/** Holds if this scope inherits `name` from an outer scope `outer`. */
119111
predicate inherits(string name, VariableScope outer) {
120112
not scopeDefinesParameterVariable(this, name, _) and
@@ -365,6 +357,22 @@ private module Cached {
365357
predicate isCapturedAccess(LocalVariableAccess::Range access) {
366358
access.getVariable().getDeclaringScope() != enclosingScope(access)
367359
}
360+
361+
cached
362+
predicate instanceVariableAccess(Generated::InstanceVariable var, InstanceVariable v) {
363+
exists(string name, VariableScope scope, boolean instance |
364+
v = TInstanceVariable(scope, name, instance, _) and
365+
instanceVariableAccess(var, name, scope, instance)
366+
)
367+
}
368+
369+
cached
370+
predicate classVariableAccess(Generated::ClassVariable var, ClassVariable variable) {
371+
exists(VariableScope scope, string name |
372+
variable = TClassVariable(scope, name, _) and
373+
classVariableAccess(var, name, scope)
374+
)
375+
}
368376
}
369377

370378
import Cached
@@ -551,12 +559,7 @@ module InstanceVariableAccess {
551559
class Range extends VariableAccess::Range, @token_instance_variable {
552560
InstanceVariable variable;
553561

554-
Range() {
555-
exists(boolean instance, VariableScope scope, string name |
556-
variable = TInstanceVariable(scope, name, instance, _) and
557-
instanceVariableAccess(this, name, scope, instance)
558-
)
559-
}
562+
Range() { instanceVariableAccess(this, variable) }
560563

561564
final override InstanceVariable getVariable() { result = variable }
562565
}
@@ -566,12 +569,7 @@ module ClassVariableAccess {
566569
class Range extends VariableAccess::Range, @token_class_variable {
567570
ClassVariable variable;
568571

569-
Range() {
570-
exists(VariableScope scope, string name |
571-
variable = TClassVariable(scope, name, _) and
572-
classVariableAccess(this, name, scope)
573-
)
574-
}
572+
Range() { classVariableAccess(this, variable) }
575573

576574
final override ClassVariable getVariable() { result = variable }
577575
}

0 commit comments

Comments
 (0)