Skip to content

Commit d117c52

Browse files
committed
JS: Use the LHS as the location for SsaExplicitDefinition
1 parent 4a687a1 commit d117c52

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

javascript/ql/lib/semmle/javascript/SSA.qll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ private module Internal {
108108
*/
109109
cached
110110
newtype TSsaDefinition =
111-
TExplicitDef(ReachableBasicBlock bb, int i, VarDef d, SsaSourceVariable v) {
112-
bb.defAt(i, v, d) and
111+
TExplicitDef(ReachableBasicBlock bb, int i, VarDef d, SsaSourceVariable v, VarRef lhs) {
112+
bb.defAt(i, v, d, lhs) and
113113
(
114114
liveAfterDef(bb, i, v) or
115115
v.isCaptured()
@@ -509,19 +509,22 @@ class SsaDefinition extends TSsaDefinition {
509509
*/
510510
class SsaExplicitDefinition extends SsaDefinition, TExplicitDef {
511511
override predicate definesAt(ReachableBasicBlock bb, int i, SsaSourceVariable v) {
512-
this = TExplicitDef(bb, i, _, v)
512+
this = TExplicitDef(bb, i, _, v, _)
513513
}
514514

515515
/** This SSA definition corresponds to the definition of `v` at `def`. */
516-
predicate defines(VarDef def, SsaSourceVariable v) { this = TExplicitDef(_, _, def, v) }
516+
predicate defines(VarDef def, SsaSourceVariable v) { this = TExplicitDef(_, _, def, v, _) }
517517

518518
/** Gets the variable definition wrapped by this SSA definition. */
519-
VarDef getDef() { this = TExplicitDef(_, _, result, _) }
519+
VarDef getDef() { this = TExplicitDef(_, _, result, _, _) }
520+
521+
/** Gets the variable reference appearing on the left-hand side of this assignment. */
522+
VarRef getLhs() { this = TExplicitDef(_, _, _, _, result) }
520523

521524
/** Gets the basic block to which this definition belongs. */
522525
override ReachableBasicBlock getBasicBlock() { this.definesAt(result, _, _) }
523526

524-
override SsaSourceVariable getSourceVariable() { this = TExplicitDef(_, _, _, result) }
527+
override SsaSourceVariable getSourceVariable() { this = TExplicitDef(_, _, _, result, _) }
525528

526529
override VarDef getAContributingVarDef() { result = this.getDef() }
527530

@@ -533,6 +536,8 @@ class SsaExplicitDefinition extends SsaDefinition, TExplicitDef {
533536

534537
override string prettyPrintDef() { result = this.getDef().toString() }
535538

539+
override Location getLocation() { result = this.getLhs().getLocation() }
540+
536541
/**
537542
* Gets the data flow node representing the incoming value assigned at this definition,
538543
* if any.

javascript/ql/lib/semmle/javascript/internal/BasicBlockInternal.qll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ private module Cached {
6767
}
6868

6969
cached
70-
predicate defAt(BasicBlock bb, int i, Variable v, VarDef d) {
71-
exists(VarRef lhs |
70+
predicate defAt(BasicBlock bb, int i, Variable v, VarDef d, VarRef lhs) {
71+
(
7272
lhs = d.getTarget().(BindingPattern).getABindingVarRef() and
7373
v = lhs.getVariable()
74-
|
74+
) and
75+
(
7576
lhs = d.getTarget() and
7677
bbIndex(bb, d, i)
7778
or
@@ -148,7 +149,10 @@ module Public {
148149
predicate useAt(int i, Variable v, VarUse u) { useAt(this, i, v, u) }
149150

150151
/** Holds if this basic block defines variable `v` in its `i`th node `d`. */
151-
predicate defAt(int i, Variable v, VarDef d) { defAt(this, i, v, d) }
152+
predicate defAt(int i, Variable v, VarDef d) { defAt(this, i, v, d, _) }
153+
154+
/** Holds if this basic block defines variable `v` in its `i`th node `d`, and `lhs` is the corresponding variable reference. */
155+
predicate defAt(int i, Variable v, VarDef d, VarRef lhs) { defAt(this, i, v, d, lhs) }
152156

153157
/**
154158
* Holds if `v` is live at entry to this basic block and `u` is a use of `v`

0 commit comments

Comments
 (0)