Skip to content

Commit 74eb6b2

Browse files
authored
Merge pull request #10400 from hvitved/ruby/singleton-class-object-scope
Ruby: Adjust the scope of singleton class targets
2 parents f807b80 + 4247843 commit 74eb6b2

File tree

10 files changed

+641
-15
lines changed

10 files changed

+641
-15
lines changed

ruby/ql/lib/codeql/ruby/AST.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class AstNode extends TAstNode {
6060
final string getPrimaryQlClasses() { result = concat(this.getAPrimaryQlClass(), ",") }
6161

6262
/** Gets the enclosing module, if any. */
63-
ModuleBase getEnclosingModule() { result = getEnclosingModule(scopeOfInclSynth(this)) }
63+
final ModuleBase getEnclosingModule() { result = getEnclosingModule(scopeOfInclSynth(this)) }
6464

6565
/** Gets the enclosing method, if any. */
66-
MethodBase getEnclosingMethod() { result = getEnclosingMethod(scopeOfInclSynth(this)) }
66+
final MethodBase getEnclosingMethod() { result = getEnclosingMethod(scopeOfInclSynth(this)) }
6767

6868
/** Gets a textual representation of this node. */
6969
cached

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private AstNode specialParentOfInclSynth(AstNode n) {
128128
n =
129129
[
130130
result.(Namespace).getScopeExpr(), result.(ClassDeclaration).getSuperclassExpr(),
131-
result.(SingletonMethod).getObject()
131+
result.(SingletonMethod).getObject(), result.(SingletonClass).getValue()
132132
]
133133
}
134134

ruby/ql/test/library-tests/modules/ancestors.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ modules.rb:
155155
# 116| XX::YY
156156
#-----| super -> YY
157157

158-
# 120| Test::Foo1::Bar::Baz
158+
# 123| Test::Foo1::Bar::Baz
159159

160160
modules_rec.rb:
161161
# 1| B::A

ruby/ql/test/library-tests/modules/callgraph.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ getTarget
7979
| modules.rb:90:24:90:36 | call to prepend | calls.rb:93:5:93:20 | prepend |
8080
| modules.rb:96:3:96:14 | call to include | calls.rb:92:5:92:20 | include |
8181
| modules.rb:102:3:102:16 | call to prepend | calls.rb:93:5:93:20 | prepend |
82+
| modules.rb:126:6:126:11 | call to new | calls.rb:99:5:99:16 | new |
8283
| modules_rec.rb:8:3:8:11 | call to prepend | calls.rb:93:5:93:20 | prepend |
8384
| private.rb:2:3:3:5 | call to private | calls.rb:94:5:94:20 | private |
8485
| private.rb:10:3:10:19 | call to private | calls.rb:94:5:94:20 | private |

ruby/ql/test/library-tests/modules/methods.expected

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,107 @@ lookupMethod
272272
| private.rb:42:1:60:3 | F | private3 | private.rb:55:3:56:5 | private3 |
273273
| private.rb:42:1:60:3 | F | private4 | private.rb:58:3:59:5 | private4 |
274274
| private.rb:42:1:60:3 | F | public | private.rb:46:3:47:5 | public |
275+
enclosingMethod
276+
| calls.rb:2:5:2:14 | call to puts | calls.rb:1:1:3:3 | foo |
277+
| calls.rb:2:5:2:14 | self | calls.rb:1:1:3:3 | foo |
278+
| calls.rb:2:10:2:14 | "foo" | calls.rb:1:1:3:3 | foo |
279+
| calls.rb:2:11:2:13 | foo | calls.rb:1:1:3:3 | foo |
280+
| calls.rb:8:5:8:14 | call to puts | calls.rb:7:1:9:3 | bar |
281+
| calls.rb:8:5:8:14 | self | calls.rb:7:1:9:3 | bar |
282+
| calls.rb:8:10:8:14 | "bar" | calls.rb:7:1:9:3 | bar |
283+
| calls.rb:8:11:8:13 | bar | calls.rb:7:1:9:3 | bar |
284+
| calls.rb:38:9:38:18 | call to instance_m | calls.rb:37:5:43:7 | baz |
285+
| calls.rb:38:9:38:18 | self | calls.rb:37:5:43:7 | baz |
286+
| calls.rb:39:9:39:12 | self | calls.rb:37:5:43:7 | baz |
287+
| calls.rb:39:9:39:23 | call to instance_m | calls.rb:37:5:43:7 | baz |
288+
| calls.rb:41:9:41:19 | call to singleton_m | calls.rb:37:5:43:7 | baz |
289+
| calls.rb:41:9:41:19 | self | calls.rb:37:5:43:7 | baz |
290+
| calls.rb:42:9:42:12 | self | calls.rb:37:5:43:7 | baz |
291+
| calls.rb:42:9:42:24 | call to singleton_m | calls.rb:37:5:43:7 | baz |
292+
| calls.rb:53:9:53:13 | call to super | calls.rb:52:5:54:7 | baz |
293+
| calls.rb:62:18:62:18 | a | calls.rb:62:1:65:3 | optional_arg |
294+
| calls.rb:62:18:62:18 | a | calls.rb:62:1:65:3 | optional_arg |
295+
| calls.rb:62:22:62:22 | 4 | calls.rb:62:1:65:3 | optional_arg |
296+
| calls.rb:62:25:62:25 | b | calls.rb:62:1:65:3 | optional_arg |
297+
| calls.rb:62:25:62:25 | b | calls.rb:62:1:65:3 | optional_arg |
298+
| calls.rb:62:28:62:28 | 5 | calls.rb:62:1:65:3 | optional_arg |
299+
| calls.rb:63:5:63:5 | a | calls.rb:62:1:65:3 | optional_arg |
300+
| calls.rb:63:5:63:16 | call to bit_length | calls.rb:62:1:65:3 | optional_arg |
301+
| calls.rb:64:5:64:5 | b | calls.rb:62:1:65:3 | optional_arg |
302+
| calls.rb:64:5:64:16 | call to bit_length | calls.rb:62:1:65:3 | optional_arg |
303+
| calls.rb:68:5:68:11 | yield ... | calls.rb:67:1:69:3 | call_block |
304+
| calls.rb:68:11:68:11 | 1 | calls.rb:67:1:69:3 | call_block |
305+
| calls.rb:72:5:72:7 | var | calls.rb:71:1:75:3 | foo |
306+
| calls.rb:72:5:72:18 | ... = ... | calls.rb:71:1:75:3 | foo |
307+
| calls.rb:72:11:72:14 | Hash | calls.rb:71:1:75:3 | foo |
308+
| calls.rb:72:11:72:18 | call to new | calls.rb:71:1:75:3 | foo |
309+
| calls.rb:73:5:73:7 | var | calls.rb:71:1:75:3 | foo |
310+
| calls.rb:73:5:73:10 | ...[...] | calls.rb:71:1:75:3 | foo |
311+
| calls.rb:73:9:73:9 | 1 | calls.rb:71:1:75:3 | foo |
312+
| calls.rb:74:5:74:29 | call to call_block | calls.rb:71:1:75:3 | foo |
313+
| calls.rb:74:5:74:29 | self | calls.rb:71:1:75:3 | foo |
314+
| calls.rb:74:16:74:29 | { ... } | calls.rb:71:1:75:3 | foo |
315+
| calls.rb:74:19:74:19 | x | calls.rb:71:1:75:3 | foo |
316+
| calls.rb:74:19:74:19 | x | calls.rb:71:1:75:3 | foo |
317+
| calls.rb:74:22:74:24 | var | calls.rb:71:1:75:3 | foo |
318+
| calls.rb:74:22:74:27 | ...[...] | calls.rb:71:1:75:3 | foo |
319+
| calls.rb:74:26:74:26 | x | calls.rb:71:1:75:3 | foo |
320+
| calls.rb:110:15:110:19 | &body | calls.rb:110:3:116:5 | foreach |
321+
| calls.rb:110:16:110:19 | body | calls.rb:110:3:116:5 | foreach |
322+
| calls.rb:111:5:111:5 | x | calls.rb:110:3:116:5 | foreach |
323+
| calls.rb:111:5:111:9 | ... = ... | calls.rb:110:3:116:5 | foreach |
324+
| calls.rb:111:9:111:9 | 0 | calls.rb:110:3:116:5 | foreach |
325+
| calls.rb:112:5:115:7 | while ... | calls.rb:110:3:116:5 | foreach |
326+
| calls.rb:112:11:112:11 | x | calls.rb:110:3:116:5 | foreach |
327+
| calls.rb:112:11:112:25 | ... < ... | calls.rb:110:3:116:5 | foreach |
328+
| calls.rb:112:15:112:18 | self | calls.rb:110:3:116:5 | foreach |
329+
| calls.rb:112:15:112:25 | call to length | calls.rb:110:3:116:5 | foreach |
330+
| calls.rb:112:26:115:7 | do ... | calls.rb:110:3:116:5 | foreach |
331+
| calls.rb:113:9:113:24 | yield ... | calls.rb:110:3:116:5 | foreach |
332+
| calls.rb:113:15:113:15 | x | calls.rb:110:3:116:5 | foreach |
333+
| calls.rb:113:18:113:21 | self | calls.rb:110:3:116:5 | foreach |
334+
| calls.rb:113:18:113:24 | ...[...] | calls.rb:110:3:116:5 | foreach |
335+
| calls.rb:113:23:113:23 | x | calls.rb:110:3:116:5 | foreach |
336+
| calls.rb:114:9:114:9 | x | calls.rb:110:3:116:5 | foreach |
337+
| calls.rb:114:9:114:9 | x | calls.rb:110:3:116:5 | foreach |
338+
| calls.rb:114:9:114:14 | ... += ... | calls.rb:110:3:116:5 | foreach |
339+
| calls.rb:114:9:114:14 | ... = ... | calls.rb:110:3:116:5 | foreach |
340+
| calls.rb:114:11:114:12 | ... + ... | calls.rb:110:3:116:5 | foreach |
341+
| calls.rb:114:14:114:14 | 1 | calls.rb:110:3:116:5 | foreach |
342+
| calls.rb:120:5:120:20 | yield ... | calls.rb:119:1:121:3 | funny |
343+
| calls.rb:120:11:120:20 | "prefix: " | calls.rb:119:1:121:3 | funny |
344+
| calls.rb:120:12:120:19 | prefix: | calls.rb:119:1:121:3 | funny |
345+
| calls.rb:137:14:137:15 | &b | calls.rb:137:1:139:3 | indirect |
346+
| calls.rb:137:15:137:15 | b | calls.rb:137:1:139:3 | indirect |
347+
| calls.rb:138:5:138:17 | call to call_block | calls.rb:137:1:139:3 | indirect |
348+
| calls.rb:138:5:138:17 | self | calls.rb:137:1:139:3 | indirect |
349+
| calls.rb:138:16:138:17 | &... | calls.rb:137:1:139:3 | indirect |
350+
| calls.rb:138:17:138:17 | b | calls.rb:137:1:139:3 | indirect |
351+
| calls.rb:146:9:146:12 | self | calls.rb:145:5:147:7 | s_method |
352+
| calls.rb:146:9:146:17 | call to to_s | calls.rb:145:5:147:7 | s_method |
353+
| calls.rb:171:9:171:12 | self | calls.rb:170:5:172:7 | singleton_a |
354+
| calls.rb:171:9:171:24 | call to singleton_b | calls.rb:170:5:172:7 | singleton_a |
355+
| calls.rb:175:9:175:12 | self | calls.rb:174:5:176:7 | singleton_b |
356+
| calls.rb:175:9:175:24 | call to singleton_c | calls.rb:174:5:176:7 | singleton_b |
357+
| calls.rb:182:9:182:12 | self | calls.rb:181:5:183:7 | singleton_d |
358+
| calls.rb:182:9:182:24 | call to singleton_a | calls.rb:181:5:183:7 | singleton_d |
359+
| hello.rb:3:9:3:22 | return | hello.rb:2:5:4:7 | hello |
360+
| hello.rb:3:16:3:22 | "hello" | hello.rb:2:5:4:7 | hello |
361+
| hello.rb:3:17:3:21 | hello | hello.rb:2:5:4:7 | hello |
362+
| hello.rb:6:9:6:22 | return | hello.rb:5:5:7:7 | world |
363+
| hello.rb:6:16:6:22 | "world" | hello.rb:5:5:7:7 | world |
364+
| hello.rb:6:17:6:21 | world | hello.rb:5:5:7:7 | world |
365+
| hello.rb:14:9:14:20 | return | hello.rb:13:5:15:7 | message |
366+
| hello.rb:14:16:14:20 | call to hello | hello.rb:13:5:15:7 | message |
367+
| hello.rb:14:16:14:20 | self | hello.rb:13:5:15:7 | message |
368+
| hello.rb:20:9:20:40 | return | hello.rb:19:5:21:7 | message |
369+
| hello.rb:20:16:20:20 | call to super | hello.rb:19:5:21:7 | message |
370+
| hello.rb:20:16:20:26 | ... + ... | hello.rb:19:5:21:7 | message |
371+
| hello.rb:20:16:20:34 | ... + ... | hello.rb:19:5:21:7 | message |
372+
| hello.rb:20:16:20:40 | ... + ... | hello.rb:19:5:21:7 | message |
373+
| hello.rb:20:24:20:26 | " " | hello.rb:19:5:21:7 | message |
374+
| hello.rb:20:25:20:25 | | hello.rb:19:5:21:7 | message |
375+
| hello.rb:20:30:20:34 | call to world | hello.rb:19:5:21:7 | message |
376+
| hello.rb:20:30:20:34 | self | hello.rb:19:5:21:7 | message |
377+
| hello.rb:20:38:20:40 | "!" | hello.rb:19:5:21:7 | message |
378+
| hello.rb:20:39:20:39 | ! | hello.rb:19:5:21:7 | message |

ruby/ql/test/library-tests/modules/methods.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ query MethodBase getMethod(Module m, string name) {
66
}
77

88
query MethodBase lookupMethod(Module m, string name) { result = M::lookupMethod(m, name) }
9+
10+
query predicate enclosingMethod(AstNode n, MethodBase m) { m = n.getEnclosingMethod() }

0 commit comments

Comments
 (0)