Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions ruby/ql/lib/codeql/ruby/ast/Parameter.qll
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class BlockParameter extends NamedParameter, TBlockParameter {
final override string getName() { result = g.getName().getValue() }

final override LocalVariable getVariable() {
result = TLocalVariableReal(_, _, g.getName()) or
result.(LocalVariableReal).getDefiningNode() = g.getName() or
result = TLocalVariableSynth(this, 0)
}

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

final override LocalVariable getVariable() {
result = TLocalVariableReal(_, _, g.getName()) or
result.(LocalVariableReal).getDefiningNode() = g.getName() or
result = TLocalVariableSynth(this, 0)
}

Expand Down Expand Up @@ -212,7 +212,9 @@ class KeywordParameter extends NamedParameter, TKeywordParameter {

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

final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
final override LocalVariable getVariable() {
result.(LocalVariableReal).getDefiningNode() = g.getName()
}

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

final override LocalVariable getVariable() { result = TLocalVariableReal(_, _, g.getName()) }
final override LocalVariable getVariable() {
result.(LocalVariableReal).getDefiningNode() = g.getName()
}

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

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

final override LocalVariable getVariable() {
result = TLocalVariableReal(_, _, g.getName()) or
result.(LocalVariableReal).getDefiningNode() = g.getName() or
result = TLocalVariableSynth(this, 0)
}

Expand Down
2 changes: 1 addition & 1 deletion ruby/ql/lib/codeql/ruby/ast/internal/Parameter.qll
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SimpleParameterRealImpl extends SimpleParameterImpl, TSimpleParameterReal

SimpleParameterRealImpl() { this = TSimpleParameterReal(g) }

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

override string getNameImpl() { result = g.getValue() }
}
Expand Down
2 changes: 1 addition & 1 deletion ruby/ql/lib/codeql/ruby/ast/internal/Scope.qll
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private Ruby::AstNode specialParentOf(Ruby::AstNode n) {
]
}

private Ruby::AstNode parentOf(Ruby::AstNode n) {
Ruby::AstNode parentOf(Ruby::AstNode n) {
n = getHereDocBody(result)
or
result = specialParentOf(n).getParent()
Expand Down
47 changes: 26 additions & 21 deletions ruby/ql/lib/codeql/ruby/ast/internal/Synthesis.qll
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,12 @@ private predicate hasLocation(AstNode n, Location l) {
private module ImplicitSelfSynthesis {
pragma[nomagic]
private predicate identifierMethodCallSelfSynthesis(AstNode mc, int i, Child child) {
child = SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(mc)).getEnclosingSelfScope()))) and
mc = TIdentifierMethodCall(_) and
i = 0
exists(SelfVariableImpl self |
self.getDeclaringScopeImpl() = scopeOf(toGenerated(mc)).getEnclosingSelfScope() and
child = SynthChild(SelfKind(self)) and
mc = TIdentifierMethodCall(_) and
i = 0
)
}

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

pragma[nomagic]
private predicate regularMethodCallSelfSynthesis(TRegularMethodCall mc, int i, Child child) {
exists(Ruby::AstNode g |
exists(Ruby::AstNode g, SelfVariableImpl self |
mc = TRegularMethodCall(g) and
// If there's no explicit receiver, then the receiver is implicitly `self`.
not exists(g.(Ruby::Call).getReceiver())
) and
child = SynthChild(SelfKind(TSelfVariable(scopeOf(toGenerated(mc)).getEnclosingSelfScope()))) and
i = 0
not exists(g.(Ruby::Call).getReceiver()) and
self.getDeclaringScopeImpl() = scopeOf(toGenerated(mc)).getEnclosingSelfScope() and
child = SynthChild(SelfKind(self)) and
i = 0
)
}

private class RegularMethodCallSelfSynthesis extends Synthesis {
Expand All @@ -338,9 +342,10 @@ private module ImplicitSelfSynthesis {
*/
pragma[nomagic]
private SelfKind getSelfKind(InstanceVariableAccess var) {
exists(Ruby::AstNode owner |
exists(Ruby::AstNode owner, SelfVariableImpl self |
self.getDeclaringScopeImpl() = scopeOf(owner).getEnclosingSelfScope() and
owner = toGenerated(instanceVarAccessSynthParentStar(var)) and
result = SelfKind(TSelfVariable(scopeOf(owner).getEnclosingSelfScope()))
result = SelfKind(self)
)
}

Expand Down Expand Up @@ -1556,20 +1561,20 @@ private module ForLoopDesugar {
* { a: a }
* ```
*/
private module ImplicitHashValueSynthesis {
private Ruby::AstNode keyWithoutValue(AstNode parent, int i) {
module ImplicitHashValueSynthesis {
Ruby::AstNode keyWithoutValue(Ruby::AstNode parent, int i) {
exists(Ruby::KeywordPattern pair |
result = pair.getKey() and
result = toGenerated(parent.(HashPattern).getKey(i)) and
result = parent.(Ruby::HashPattern).getChild(i).(Ruby::KeywordPattern).getKey() and
not exists(pair.getValue())
)
or
exists(Ruby::Pair pair |
i = 0 and
result = pair.getKey() and
pair = toGenerated(parent) and
not exists(pair.getValue())
)
parent =
any(Ruby::Pair pair |
i = 0 and
result = pair.getKey() and
not exists(pair.getValue())
)
}

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

private class ImplicitHashValueSynthesis extends Synthesis {
final override predicate child(AstNode parent, int i, Child child) {
exists(Ruby::AstNode key | key = keyWithoutValue(parent, i) |
exists(Ruby::AstNode key | key = keyWithoutValue(toGenerated(parent), i) |
exists(TVariableReal variable |
access(key, variable) and
child = SynthChild(LocalVariableAccessRealKind(variable))
Expand All @@ -1606,7 +1611,7 @@ private module ImplicitHashValueSynthesis {
}

final override predicate location(AstNode n, Location l) {
exists(AstNode p, int i | l = keyWithoutValue(p, i).getLocation() |
exists(AstNode p, int i | l = keyWithoutValue(toGenerated(p), i).getLocation() |
n = p.(HashPattern).getValue(i)
or
i = 0 and n = p.(Pair).getValue()
Expand Down
Loading
Loading