Skip to content

Commit f805e26

Browse files
committed
JS: Eliminate some redundant type checks
1 parent 18a1946 commit f805e26

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

javascript/ql/lib/semmle/javascript/ApiGraphs.qll

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,25 @@ module API {
12531253

12541254
private import semmle.javascript.dataflow.TypeTracking
12551255

1256+
/**
1257+
* A version of `DataFlow::SourceNode` without a charpred, to avoid redundant type checks that the
1258+
* optimizer is not yet able to remove. Should only be used in cases where we know only `SourceNode` values
1259+
* will appear.
1260+
*/
1261+
private class RawSourceNode extends DataFlow::Node {
1262+
predicate flowsTo(DataFlow::Node node) { this.(DataFlow::SourceNode).flowsTo(node) }
1263+
1264+
predicate flowsToExpr(Expr expr) { this.(DataFlow::SourceNode).flowsToExpr(expr) }
1265+
1266+
DataFlow::PropRead getAPropertyRead(string prop) {
1267+
result = this.(DataFlow::SourceNode).getAPropertyRead(prop)
1268+
}
1269+
1270+
DataFlow::PropWrite getAPropertyWrite() {
1271+
result = this.(DataFlow::SourceNode).getAPropertyWrite()
1272+
}
1273+
}
1274+
12561275
/**
12571276
* Gets a data-flow node to which `nd`, which is a use of an API-graph node, flows.
12581277
*
@@ -1266,9 +1285,8 @@ module API {
12661285
* - `prop`: if non-empty, the flow is only guaranteed to preserve the value of this property,
12671286
* and not necessarily the entire object.
12681287
*/
1269-
private DataFlow::SourceNode trackUseNode(
1270-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop,
1271-
DataFlow::TypeTracker t
1288+
private RawSourceNode trackUseNode(
1289+
RawSourceNode nd, boolean promisified, int boundArgs, string prop, DataFlow::TypeTracker t
12721290
) {
12731291
t.start() and
12741292
use(_, nd) and
@@ -1347,8 +1365,8 @@ module API {
13471365
)
13481366
}
13491367

1350-
private DataFlow::SourceNode trackUseNode(
1351-
DataFlow::SourceNode nd, boolean promisified, int boundArgs, string prop
1368+
private RawSourceNode trackUseNode(
1369+
RawSourceNode nd, boolean promisified, int boundArgs, string prop
13521370
) {
13531371
result = trackUseNode(nd, promisified, boundArgs, prop, DataFlow::TypeTracker::end())
13541372
}
@@ -1357,9 +1375,7 @@ module API {
13571375
* Gets a node that is inter-procedurally reachable from `nd`, which is a use of some node.
13581376
*/
13591377
cached
1360-
DataFlow::SourceNode trackUseNode(DataFlow::SourceNode nd) {
1361-
result = trackUseNode(nd, false, 0, "")
1362-
}
1378+
RawSourceNode trackUseNode(RawSourceNode nd) { result = trackUseNode(nd, false, 0, "") }
13631379

13641380
private DataFlow::SourceNode trackDefNode(DataFlow::Node nd, DataFlow::TypeBackTracker t) {
13651381
t.start() and

0 commit comments

Comments
 (0)