Skip to content

Commit 7b832c3

Browse files
committed
Ruby
1 parent fac23d3 commit 7b832c3

7 files changed

Lines changed: 218 additions & 114 deletions

File tree

ruby/ql/lib/codeql/ruby/ast/Parameter.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class BlockParameter extends NamedParameter, TBlockParameter {
134134
final override string getName() { result = g.getName().getValue() }
135135

136136
final override LocalVariable getVariable() {
137-
result = TLocalVariableReal(_, _, g.getName()) or
137+
result.(LocalVariableReal).getDefiningNode() = g.getName() or
138138
result = TLocalVariableSynth(this, 0)
139139
}
140140

@@ -164,7 +164,7 @@ class HashSplatParameter extends NamedParameter, THashSplatParameter {
164164
final override string getAPrimaryQlClass() { result = "HashSplatParameter" }
165165

166166
final override LocalVariable getVariable() {
167-
result = TLocalVariableReal(_, _, g.getName()) or
167+
result.(LocalVariableReal).getDefiningNode() = g.getName() or
168168
result = TLocalVariableSynth(this, 0)
169169
}
170170

@@ -212,7 +212,9 @@ class KeywordParameter extends NamedParameter, TKeywordParameter {
212212

213213
final override string getAPrimaryQlClass() { result = "KeywordParameter" }
214214

215-
final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
215+
final override LocalVariable getVariable() {
216+
result.(LocalVariableReal).getDefiningNode() = g.getName()
217+
}
216218

217219
/**
218220
* Gets the default value, i.e. the value assigned to the parameter when one
@@ -262,7 +264,9 @@ class OptionalParameter extends NamedParameter, TOptionalParameter {
262264
*/
263265
final Expr getDefaultValue() { toGenerated(result) = g.getValue() }
264266

265-
final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
267+
final override LocalVariable getVariable() {
268+
result.(LocalVariableReal).getDefiningNode() = g.getName()
269+
}
266270

267271
final override string toString() { result = this.getName() }
268272

@@ -293,7 +297,7 @@ class SplatParameter extends NamedParameter, TSplatParameter {
293297
final override string getAPrimaryQlClass() { result = "SplatParameter" }
294298

295299
final override LocalVariable getVariable() {
296-
result = TLocalVariableReal(_, _, g.getName()) or
300+
result.(LocalVariableReal).getDefiningNode() = g.getName() or
297301
result = TLocalVariableSynth(this, 0)
298302
}
299303

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SimpleParameterRealImpl extends SimpleParameterImpl, TSimpleParameterReal
3333

3434
SimpleParameterRealImpl() { this = TSimpleParameterReal(g) }
3535

36-
override LocalVariable getVariableImpl() { result = TLocalVariableReal(_, _, g) }
36+
override LocalVariable getVariableImpl() { result.(LocalVariableReal).getDefiningNode() = g }
3737

3838
override string getNameImpl() { result = g.getValue() }
3939
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private Ruby::AstNode specialParentOf(Ruby::AstNode n) {
118118
]
119119
}
120120

121-
private Ruby::AstNode parentOf(Ruby::AstNode n) {
121+
Ruby::AstNode parentOf(Ruby::AstNode n) {
122122
n = getHereDocBody(result)
123123
or
124124
result = specialParentOf(n).getParent()

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,12 @@ private predicate hasLocation(AstNode n, Location l) {
296296
private module ImplicitSelfSynthesis {
297297
pragma[nomagic]
298298
private predicate identifierMethodCallSelfSynthesis(AstNode mc, int i, Child child) {
299-
child = SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(mc)).getEnclosingSelfScope()))) and
300-
mc = TIdentifierMethodCall(_) and
301-
i = 0
299+
exists(SelfVariableImpl self |
300+
self.getDeclaringScopeImpl() = scopeOf(toGenerated(mc)).getEnclosingSelfScope() and
301+
child = SynthChild(SelfKind(self)) and
302+
mc = TIdentifierMethodCall(_) and
303+
i = 0
304+
)
302305
}
303306

304307
private class IdentifierMethodCallSelfSynthesis extends Synthesis {
@@ -309,13 +312,14 @@ private module ImplicitSelfSynthesis {
309312

310313
pragma[nomagic]
311314
private predicate regularMethodCallSelfSynthesis(TRegularMethodCall mc, int i, Child child) {
312-
exists(Ruby::AstNode g |
315+
exists(Ruby::AstNode g, SelfVariableImpl self |
313316
mc = TRegularMethodCall(g) and
314317
// If there's no explicit receiver, then the receiver is implicitly `self`.
315-
not exists(g.(Ruby::Call).getReceiver())
316-
) and
317-
child = SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(mc)).getEnclosingSelfScope()))) and
318-
i = 0
318+
not exists(g.(Ruby::Call).getReceiver()) and
319+
self.getDeclaringScopeImpl() = scopeOf(toGenerated(mc)).getEnclosingSelfScope() and
320+
child = SynthChild(SelfKind(self)) and
321+
i = 0
322+
)
319323
}
320324

321325
private class RegularMethodCallSelfSynthesis extends Synthesis {
@@ -338,9 +342,10 @@ private module ImplicitSelfSynthesis {
338342
*/
339343
pragma[nomagic]
340344
private SelfKind getSelfKind(InstanceVariableAccess var) {
341-
exists(Ruby::AstNode owner |
345+
exists(Ruby::AstNode owner, SelfVariableImpl self |
346+
self.getDeclaringScopeImpl() = scopeOf(owner).getEnclosingSelfScope() and
342347
owner = toGenerated(instanceVarAccessSynthParentStar(var)) and
343-
result = SelfKind(TSelfVariable(scopeOf(owner).getEnclosingSelfScope()))
348+
result = SelfKind(self)
344349
)
345350
}
346351

@@ -1556,20 +1561,20 @@ private module ForLoopDesugar {
15561561
* { a: a }
15571562
* ```
15581563
*/
1559-
private module ImplicitHashValueSynthesis {
1560-
private Ruby::AstNode keyWithoutValue(AstNode parent, int i) {
1564+
module ImplicitHashValueSynthesis {
1565+
Ruby::AstNode keyWithoutValue(Ruby::AstNode parent, int i) {
15611566
exists(Ruby::KeywordPattern pair |
15621567
result = pair.getKey() and
1563-
result = toGenerated(parent.(HashPattern).getKey(i)) and
1568+
result = parent.(Ruby::HashPattern).getChild(i).(Ruby::KeywordPattern).getKey() and
15641569
not exists(pair.getValue())
15651570
)
15661571
or
1567-
exists(Ruby::Pair pair |
1568-
i = 0 and
1569-
result = pair.getKey() and
1570-
pair = toGenerated(parent) and
1571-
not exists(pair.getValue())
1572-
)
1572+
parent =
1573+
any(Ruby::Pair pair |
1574+
i = 0 and
1575+
result = pair.getKey() and
1576+
not exists(pair.getValue())
1577+
)
15731578
}
15741579

15751580
private string keyName(Ruby::AstNode key) {
@@ -1579,7 +1584,7 @@ private module ImplicitHashValueSynthesis {
15791584

15801585
private class ImplicitHashValueSynthesis extends Synthesis {
15811586
final override predicate child(AstNode parent, int i, Child child) {
1582-
exists(Ruby::AstNode key | key = keyWithoutValue(parent, i) |
1587+
exists(Ruby::AstNode key | key = keyWithoutValue(toGenerated(parent), i) |
15831588
exists(TVariableReal variable |
15841589
access(key, variable) and
15851590
child = SynthChild(LocalVariableAccessRealKind(variable))
@@ -1606,7 +1611,7 @@ private module ImplicitHashValueSynthesis {
16061611
}
16071612

16081613
final override predicate location(AstNode n, Location l) {
1609-
exists(AstNode p, int i | l = keyWithoutValue(p, i).getLocation() |
1614+
exists(AstNode p, int i | l = keyWithoutValue(toGenerated(p), i).getLocation() |
16101615
n = p.(HashPattern).getValue(i)
16111616
or
16121617
i = 0 and n = p.(Pair).getValue()

0 commit comments

Comments
 (0)