Skip to content

Commit 05900cd

Browse files
committed
C++: Rename 'Ssa' to 'SsaInternals' and move definitions from 'SSaImplSpecific' to 'SsaInternals'. Now we can avoid cyclic imports.
1 parent 675e284 commit 05900cd

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private import semmle.code.cpp.ir.IR
1111
private import semmle.code.cpp.controlflow.IRGuards
1212
private import semmle.code.cpp.models.interfaces.DataFlow
1313
private import DataFlowPrivate
14-
private import Ssa as Ssa
14+
private import SsaInternals as Ssa
1515

1616
cached
1717
private module Cached {
Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
private import semmle.code.cpp.ir.IR
2-
private import DataFlowUtil
3-
private import DataFlowPrivate
4-
private import DataFlowImplCommon as DataFlowImplCommon
5-
private import Ssa as Ssa
2+
private import SsaInternals as Ssa
63

74
class BasicBlock = IRBlock;
85

6+
class SourceVariable = Ssa::SourceVariable;
7+
98
BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) }
109

1110
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }
@@ -14,51 +13,6 @@ class ExitBasicBlock extends IRBlock {
1413
ExitBasicBlock() { this.getLastInstruction() instanceof ExitFunctionInstruction }
1514
}
1615

17-
private newtype TSourceVariable =
18-
TSourceIRVariable(IRVariable var) or
19-
TSourceIRVariableIndirection(InitializeIndirectionInstruction init)
20-
21-
abstract class SourceVariable extends TSourceVariable {
22-
IRVariable var;
23-
24-
IRVariable getIRVariable() { result = var }
25-
26-
abstract string toString();
27-
28-
predicate isIndirection() { none() }
29-
}
30-
31-
class SourceIRVariable extends SourceVariable, TSourceIRVariable {
32-
SourceIRVariable() { this = TSourceIRVariable(var) }
33-
34-
override string toString() { result = this.getIRVariable().toString() }
35-
}
16+
predicate variableWrite = Ssa::variableWrite/4;
3617

37-
class SourceIRVariableIndirection extends SourceVariable, TSourceIRVariableIndirection {
38-
InitializeIndirectionInstruction init;
39-
40-
SourceIRVariableIndirection() {
41-
this = TSourceIRVariableIndirection(init) and var = init.getIRVariable()
42-
}
43-
44-
override string toString() { result = "*" + this.getIRVariable().toString() }
45-
46-
override predicate isIndirection() { any() }
47-
}
48-
49-
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
50-
DataFlowImplCommon::forceCachingInSameStage() and
51-
exists(Ssa::Def def |
52-
def.hasRankInBlock(bb, i) and
53-
v = def.getSourceVariable() and
54-
(if def.isCertain() then certain = true else certain = false)
55-
)
56-
}
57-
58-
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
59-
exists(Ssa::Use use |
60-
use.hasRankInBlock(bb, i) and
61-
v = use.getSourceVariable() and
62-
certain = true
63-
)
64-
}
18+
predicate variableRead = Ssa::variableRead/4;

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/Ssa.qll renamed to cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
import SsaImplCommon
2-
import SsaImplSpecific
32
private import cpp as Cpp
43
private import semmle.code.cpp.ir.IR
54
private import DataFlowUtil
6-
private import DataFlowPrivate
5+
private import DataFlowImplCommon as DataFlowImplCommon
76
private import semmle.code.cpp.models.interfaces.Allocation as Alloc
87
private import semmle.code.cpp.models.interfaces.DataFlow as DataFlow
98

9+
private module SourceVariables {
10+
private newtype TSourceVariable =
11+
TSourceIRVariable(IRVariable var) or
12+
TSourceIRVariableIndirection(InitializeIndirectionInstruction init)
13+
14+
abstract class SourceVariable extends TSourceVariable {
15+
IRVariable var;
16+
17+
IRVariable getIRVariable() { result = var }
18+
19+
abstract string toString();
20+
21+
predicate isIndirection() { none() }
22+
}
23+
24+
private class SourceIRVariable extends SourceVariable, TSourceIRVariable {
25+
SourceIRVariable() { this = TSourceIRVariable(var) }
26+
27+
override string toString() { result = this.getIRVariable().toString() }
28+
}
29+
30+
private class SourceIRVariableIndirection extends SourceVariable, TSourceIRVariableIndirection {
31+
InitializeIndirectionInstruction init;
32+
33+
SourceIRVariableIndirection() {
34+
this = TSourceIRVariableIndirection(init) and var = init.getIRVariable()
35+
}
36+
37+
override string toString() { result = "*" + this.getIRVariable().toString() }
38+
39+
override predicate isIndirection() { any() }
40+
}
41+
}
42+
43+
import SourceVariables
44+
1045
cached
1146
private newtype TDefOrUse =
1247
TExplicitDef(Instruction store) { explicitWrite(_, store, _) } or
@@ -509,3 +544,28 @@ private module Cached {
509544
}
510545

511546
import Cached
547+
548+
/**
549+
* Holds if the `i`'th write in block `bb` writes to the variable `v`.
550+
* `certain` is `true` if the write is guaranteed to overwrite the entire variable.
551+
*/
552+
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
553+
DataFlowImplCommon::forceCachingInSameStage() and
554+
exists(Def def |
555+
def.hasRankInBlock(bb, i) and
556+
v = def.getSourceVariable() and
557+
(if def.isCertain() then certain = true else certain = false)
558+
)
559+
}
560+
561+
/**
562+
* Holds if the `i`'th read in block `bb` reads to the variable `v`.
563+
* `certain` is `true` if the read is guaranteed. For C++, this is always the case.
564+
*/
565+
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
566+
exists(Use use |
567+
use.hasRankInBlock(bb, i) and
568+
v = use.getSourceVariable() and
569+
certain = true
570+
)
571+
}

0 commit comments

Comments
 (0)