Skip to content

Commit bc80c9b

Browse files
committed
Ruby: Move SSA consistency queries into shared SSA library
1 parent 62730e7 commit bc80c9b

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
import ruby
21
import codeql.ruby.dataflow.SSA
3-
import codeql.ruby.controlflow.ControlFlowGraph
2+
import codeql.ruby.dataflow.internal.SsaImplCommon::Consistency
43

5-
query predicate nonUniqueDef(CfgNode read, Ssa::Definition def) {
6-
read = def.getARead() and
7-
exists(Ssa::Definition other | read = other.getARead() and other != def)
8-
}
9-
10-
query predicate readWithoutDef(LocalVariableReadAccess read) {
11-
exists(CfgNode node |
12-
node = read.getAControlFlowNode() and
13-
not node = any(Ssa::Definition def).getARead()
14-
)
15-
}
16-
17-
query predicate deadDef(Ssa::Definition def, LocalVariable v) {
18-
v = def.getSourceVariable() and
19-
not v.isCaptured() and
20-
not exists(def.getARead()) and
21-
not def = any(Ssa::PhiNode phi).getAnInput()
4+
class MyRelevantDefinition extends RelevantDefinition, Ssa::Definition {
5+
override predicate hasLocationInfo(
6+
string filepath, int startline, int startcolumn, int endline, int endcolumn
7+
) {
8+
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
9+
}
2210
}

ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImplCommon.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,28 @@ class UncertainWriteDefinition extends WriteDefinition {
634634
)
635635
}
636636
}
637+
638+
/** Provides a set of consistency queries. */
639+
module Consistency {
640+
abstract class RelevantDefinition extends Definition {
641+
abstract predicate hasLocationInfo(
642+
string filepath, int startline, int startcolumn, int endline, int endcolumn
643+
);
644+
}
645+
646+
query predicate nonUniqueDef(RelevantDefinition def, SourceVariable v, BasicBlock bb, int i) {
647+
ssaDefReachesRead(v, def, bb, i) and
648+
not exists(unique(Definition def0 | ssaDefReachesRead(_, def0, bb, i)))
649+
}
650+
651+
query predicate readWithoutDef(SourceVariable v, BasicBlock bb, int i) {
652+
variableRead(bb, i, v, _) and
653+
not ssaDefReachesRead(_, _, bb, i)
654+
}
655+
656+
query predicate deadDef(RelevantDefinition def, SourceVariable v) {
657+
v = def.getSourceVariable() and
658+
not ssaDefReachesRead(_, def, _, _) and
659+
not phiHasInputFromBlock(_, def, _)
660+
}
661+
}

0 commit comments

Comments
 (0)