mirror of
https://github.com/github/codeql.git
synced 2026-02-20 08:53:49 +01:00
AST: improve AST for special parameters
This commit is contained in:
@@ -122,7 +122,7 @@ class VariableAccess extends Expr {
|
||||
*
|
||||
* both `a` and `b` are write accesses belonging to the same assignment.
|
||||
*/
|
||||
predicate isExplicitWrite(AstNode assignment) { explicitWriteAccess(this, assignment) }
|
||||
predicate isExplicitWrite(AstNode assignment) { range.isExplicitWrite(assignment) }
|
||||
|
||||
/**
|
||||
* Holds if this access is a write access belonging to an implicit assignment.
|
||||
@@ -139,7 +139,7 @@ class VariableAccess extends Expr {
|
||||
* the access to `elements` in the parameter list is an implicit assignment,
|
||||
* as is the first access to `e`.
|
||||
*/
|
||||
predicate isImplicitWrite() { implicitWriteAccess(this) }
|
||||
predicate isImplicitWrite() { range.isImplicitWrite() }
|
||||
}
|
||||
|
||||
/** An access to a variable where the value is updated. */
|
||||
@@ -161,13 +161,13 @@ class VariableReadAccess extends VariableAccess {
|
||||
}
|
||||
|
||||
/** An access to a local variable. */
|
||||
class LocalVariableAccess extends VariableAccess, @token_identifier {
|
||||
class LocalVariableAccess extends VariableAccess, LocalVariableAccess::LocalVariableRange {
|
||||
final override LocalVariableAccess::Range range;
|
||||
|
||||
final override LocalVariable getVariable() { result = range.getVariable() }
|
||||
|
||||
final override string getAPrimaryQlClass() {
|
||||
not this instanceof SimpleParameter and result = "LocalVariableAccess"
|
||||
not this instanceof NamedParameter and result = "LocalVariableAccess"
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -111,6 +111,14 @@ module KeywordParameter {
|
||||
final override string toString() { result = this.getName() }
|
||||
|
||||
final override string getName() { result = generated.getName().getValue() }
|
||||
|
||||
final override Location getLocation() { result = generated.getName().getLocation() }
|
||||
|
||||
final override predicate child(string label, AstNode::Range child) {
|
||||
NamedParameter::Range.super.child(label, child)
|
||||
or
|
||||
label = "getDefaultValue" and child = getDefaultValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +135,14 @@ module OptionalParameter {
|
||||
final override string toString() { result = this.getName() }
|
||||
|
||||
final override string getName() { result = generated.getName().getValue() }
|
||||
|
||||
final override Location getLocation() { result = generated.getName().getLocation() }
|
||||
|
||||
final override predicate child(string label, AstNode::Range child) {
|
||||
NamedParameter::Range.super.child(label, child)
|
||||
or
|
||||
label = "getDefaultValue" and child = getDefaultValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -116,6 +116,23 @@ private predicate strictlyBefore(Location one, Location two) {
|
||||
one.getStartLine() = two.getStartLine() and one.getStartColumn() < two.getStartColumn()
|
||||
}
|
||||
|
||||
private Generated::AstNode getNodeForIdentifier(Generated::Identifier id) {
|
||||
exists(Generated::AstNode parent | parent = id.getParent() |
|
||||
if
|
||||
parent instanceof Generated::BlockParameter
|
||||
or
|
||||
parent instanceof Generated::SplatParameter
|
||||
or
|
||||
parent instanceof Generated::HashSplatParameter
|
||||
or
|
||||
parent instanceof Generated::KeywordParameter
|
||||
or
|
||||
parent instanceof Generated::OptionalParameter
|
||||
then result = parent
|
||||
else result = id
|
||||
)
|
||||
}
|
||||
|
||||
cached
|
||||
private module Cached {
|
||||
/** Gets the enclosing scope for `node`. */
|
||||
@@ -518,7 +535,7 @@ module LocalVariable {
|
||||
|
||||
final override VariableScope getDeclaringScope() { result = scope }
|
||||
|
||||
final VariableAccess getDefiningAccess() { result = i }
|
||||
final VariableAccess getDefiningAccess() { result = getNodeForIdentifier(i) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -577,28 +594,46 @@ module VariableAccess {
|
||||
abstract class Range extends Expr::Range {
|
||||
abstract Variable getVariable();
|
||||
|
||||
final override string toString() { result = this.getVariable().getName() }
|
||||
final predicate isExplicitWrite(AstNode assignment) {
|
||||
exists(Generated::Identifier i | this = getNodeForIdentifier(i) |
|
||||
explicitWriteAccess(i, assignment)
|
||||
)
|
||||
or
|
||||
not this = getNodeForIdentifier(_) and explicitWriteAccess(this, assignment)
|
||||
}
|
||||
|
||||
override predicate child(string label, AstNode::Range child) { none() }
|
||||
final predicate isImplicitWrite() {
|
||||
exists(Generated::Identifier i | this = getNodeForIdentifier(i) | implicitWriteAccess(i))
|
||||
or
|
||||
not this = getNodeForIdentifier(_) and implicitWriteAccess(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module LocalVariableAccess {
|
||||
class Range extends VariableAccess::Range, @token_identifier {
|
||||
override Generated::Identifier generated;
|
||||
class LocalVariableRange =
|
||||
@token_identifier or @splat_parameter or @keyword_parameter or @optional_parameter or
|
||||
@hash_splat_parameter or @block_parameter;
|
||||
|
||||
class Range extends VariableAccess::Range, LocalVariableRange {
|
||||
LocalVariable variable;
|
||||
|
||||
Range() {
|
||||
access(this, variable) and
|
||||
(
|
||||
explicitWriteAccess(this, _)
|
||||
or
|
||||
implicitWriteAccess(this)
|
||||
or
|
||||
vcall(this)
|
||||
exists(Generated::Identifier id |
|
||||
this = getNodeForIdentifier(id) and
|
||||
access(id, variable) and
|
||||
(
|
||||
explicitWriteAccess(id, _)
|
||||
or
|
||||
implicitWriteAccess(id)
|
||||
or
|
||||
vcall(id)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override string toString() { result = generated.(Generated::Identifier).getValue() }
|
||||
|
||||
final override LocalVariable getVariable() { result = variable }
|
||||
}
|
||||
}
|
||||
@@ -610,6 +645,8 @@ module GlobalVariableAccess {
|
||||
Range() { this.(Generated::GlobalVariable).getValue() = variable.getName() }
|
||||
|
||||
final override GlobalVariable getVariable() { result = variable }
|
||||
|
||||
override string toString() { result = generated.(Generated::GlobalVariable).getValue() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -620,6 +657,8 @@ module InstanceVariableAccess {
|
||||
Range() { instanceVariableAccess(this, variable) }
|
||||
|
||||
final override InstanceVariable getVariable() { result = variable }
|
||||
|
||||
override string toString() { result = generated.(Generated::InstanceVariable).getValue() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,5 +669,7 @@ module ClassVariableAccess {
|
||||
Range() { classVariableAccess(this, variable) }
|
||||
|
||||
final override ClassVariable getVariable() { result = variable }
|
||||
|
||||
override string toString() { result = generated.(Generated::ClassVariable).getValue() }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user