Skip to content

Commit dc0de91

Browse files
committed
Add GlobalVariable
1 parent 1ada9fe commit dc0de91

File tree

6 files changed

+77
-10
lines changed

6 files changed

+77
-10
lines changed

ql/src/codeql_ruby/ast/Variable.qll

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class LocalVariable extends Variable {
5959
final override LocalVariableAccess getAnAccess() { result.getVariable() = this }
6060
}
6161

62+
/** A global variable. */
63+
class GlobalVariable extends Variable {
64+
override GlobalVariable::Range range;
65+
66+
final override GlobalVariableAccess getAnAccess() { result.getVariable() = this }
67+
}
68+
6269
/** An access to a variable. */
6370
class VariableAccess extends Expr {
6471
override VariableAccess::Range range;
@@ -70,11 +77,21 @@ class VariableAccess extends Expr {
7077
}
7178

7279
/** An access to a local variable. */
73-
class LocalVariableAccess extends VariableAccess {
80+
class LocalVariableAccess extends VariableAccess, @token_identifier {
7481
final override LocalVariableAccess::Range range;
7582

7683
/** Gets the variable this identifier refers to. */
7784
final override LocalVariable getVariable() { result = range.getVariable() }
7885
// TODO uncomment this and fix the params test
7986
//final override string getAPrimaryQlClass() { result = "LocalVariableAccess" }
8087
}
88+
89+
/** An access to a local variable. */
90+
class GlobalVariableAccess extends VariableAccess, @token_global_variable {
91+
final override GlobalVariableAccess::Range range;
92+
93+
/** Gets the variable this identifier refers to. */
94+
final override GlobalVariable getVariable() { result = range.getVariable() }
95+
96+
final override string getAPrimaryQlClass() { result = "GlobalVariableAccess" }
97+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ cached
101101
private module Cached {
102102
cached
103103
newtype TScope =
104+
TGlobalScope() or
104105
TTopLevelScope(Generated::Program node) or
105106
TModuleScope(Generated::Module node) or
106107
TClassScope(AstNode cls) {
@@ -110,6 +111,9 @@ private module Cached {
110111

111112
cached
112113
newtype TVariable =
114+
TGlobalVariable(TGlobalScope scope, string name) {
115+
name = any(Generated::GlobalVariable var).getValue()
116+
} or
113117
TLocalVariable(VariableScope scope, string name, Generated::Identifier i) {
114118
scopeDefinesParameterVariable(scope, name, i)
115119
or
@@ -153,6 +157,14 @@ module VariableScope {
153157
}
154158
}
155159

160+
module GlobalScope {
161+
class Range extends VariableScope::Range, TGlobalScope {
162+
override string toString() { result = "global scope" }
163+
164+
override AstNode getScopeElement() { none() }
165+
}
166+
}
167+
156168
module TopLevelScope {
157169
class Range extends VariableScope::Range, TTopLevelScope {
158170
override string toString() { result = "top-level scope" }
@@ -226,6 +238,21 @@ module LocalVariable {
226238
}
227239
}
228240

241+
module GlobalVariable {
242+
class Range extends Variable::Range {
243+
private VariableScope scope;
244+
private string name;
245+
246+
Range() { this = TGlobalVariable(scope, name) }
247+
248+
final override string getName() { result = name }
249+
250+
final override Location getLocation() { none() }
251+
252+
final override VariableScope getDeclaringScope() { result = scope }
253+
}
254+
}
255+
229256
module VariableAccess {
230257
abstract class Range extends Expr::Range {
231258
abstract Variable getVariable();
@@ -242,3 +269,13 @@ module LocalVariableAccess {
242269
final override LocalVariable getVariable() { result = variable }
243270
}
244271
}
272+
273+
module GlobalVariableAccess {
274+
class Range extends VariableAccess::Range, @token_global_variable {
275+
GlobalVariable variable;
276+
277+
Range() { this.(Generated::GlobalVariable).getValue() = variable.getName() }
278+
279+
final override GlobalVariable getVariable() { result = variable }
280+
}
281+
}

ql/test/library-tests/variables/scopes.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ def a ; "x" end
1515
puts b # new local variable
1616
puts c # new local variable
1717
puts d # new local variable
18-
end
18+
end
19+
20+
# new global variable
21+
$global = 42
22+
23+
# use of a pre-defined global variable
24+
script = $0

ql/test/library-tests/variables/varaccess.expected

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,20 @@
7676
| scopes.rb:2:14:2:14 | x | scopes.rb:2:14:2:14 | x | scopes.rb:2:9:6:3 | block scope |
7777
| scopes.rb:4:4:4:4 | a | scopes.rb:4:4:4:4 | a | scopes.rb:2:9:6:3 | block scope |
7878
| scopes.rb:5:9:5:9 | a | scopes.rb:4:4:4:4 | a | scopes.rb:2:9:6:3 | block scope |
79-
| scopes.rb:7:1:7:1 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
80-
| scopes.rb:8:6:8:6 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
79+
| scopes.rb:7:1:7:1 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
80+
| scopes.rb:8:6:8:6 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
8181
| scopes.rb:9:14:9:14 | x | scopes.rb:9:14:9:14 | x | scopes.rb:9:9:18:3 | block scope |
82-
| scopes.rb:10:9:10:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
83-
| scopes.rb:11:4:11:4 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
84-
| scopes.rb:12:9:12:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
85-
| scopes.rb:13:4:13:4 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
82+
| scopes.rb:10:9:10:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
83+
| scopes.rb:11:4:11:4 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
84+
| scopes.rb:12:9:12:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
85+
| scopes.rb:13:4:13:4 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
8686
| scopes.rb:13:7:13:7 | b | scopes.rb:13:7:13:7 | b | scopes.rb:9:9:18:3 | block scope |
8787
| scopes.rb:13:11:13:11 | c | scopes.rb:13:11:13:11 | c | scopes.rb:9:9:18:3 | block scope |
8888
| scopes.rb:13:14:13:14 | d | scopes.rb:13:14:13:14 | d | scopes.rb:9:9:18:3 | block scope |
89-
| scopes.rb:14:9:14:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:18:3 | top-level scope |
89+
| scopes.rb:14:9:14:9 | a | scopes.rb:7:1:7:1 | a | scopes.rb:1:1:24:12 | top-level scope |
9090
| scopes.rb:15:9:15:9 | b | scopes.rb:13:7:13:7 | b | scopes.rb:9:9:18:3 | block scope |
9191
| scopes.rb:16:9:16:9 | c | scopes.rb:13:11:13:11 | c | scopes.rb:9:9:18:3 | block scope |
9292
| scopes.rb:17:9:17:9 | d | scopes.rb:13:14:13:14 | d | scopes.rb:9:9:18:3 | block scope |
93+
| scopes.rb:21:1:21:7 | $global | file://:0:0:0:0 | $global | file://:0:0:0:0 | global scope |
94+
| scopes.rb:24:1:24:6 | script | scopes.rb:24:1:24:6 | script | scopes.rb:1:1:24:12 | top-level scope |
95+
| scopes.rb:24:10:24:11 | $0 | file://:0:0:0:0 | $0 | file://:0:0:0:0 | global scope |

ql/test/library-tests/variables/variable.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| file://:0:0:0:0 | $0 |
2+
| file://:0:0:0:0 | $global |
13
| nested_scopes.rb:5:3:5:3 | a |
24
| nested_scopes.rb:7:5:7:5 | a |
35
| nested_scopes.rb:9:7:9:7 | a |
@@ -40,3 +42,4 @@
4042
| scopes.rb:13:7:13:7 | b |
4143
| scopes.rb:13:11:13:11 | c |
4244
| scopes.rb:13:14:13:14 | d |
45+
| scopes.rb:24:1:24:6 | script |

ql/test/library-tests/variables/varscopes.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| file://:0:0:0:0 | global scope |
12
| nested_scopes.rb:1:1:3:3 | method scope |
23
| nested_scopes.rb:1:1:42:1 | top-level scope |
34
| nested_scopes.rb:4:1:39:3 | class scope |
@@ -25,6 +26,6 @@
2526
| parameters.rb:49:1:51:3 | method scope |
2627
| parameters.rb:54:9:57:3 | block scope |
2728
| scopes.rb:1:1:1:15 | method scope |
28-
| scopes.rb:1:1:18:3 | top-level scope |
29+
| scopes.rb:1:1:24:12 | top-level scope |
2930
| scopes.rb:2:9:6:3 | block scope |
3031
| scopes.rb:9:9:18:3 | block scope |

0 commit comments

Comments
 (0)