Skip to content

Commit 63a2670

Browse files
C++: Don't have ReachableBlock extends IRBlock
1 parent fda8605 commit 63a2670

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import Instruction
33
import semmle.code.cpp.ir.implementation.EdgeKind
44
private import Cached
55

6-
class IRBlock extends TIRBlock {
6+
/**
7+
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
8+
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
9+
* sequence.
10+
*
11+
* This class does not contain any members that query the predecessor or successor edges of the
12+
* block. This allows different classes that extend `IRBlockBase` to expose different subsets of
13+
* edges (e.g. ignoring unreachable edges).
14+
*
15+
* Most consumers should use the class `IRBlock`.
16+
*/
17+
class IRBlockBase extends TIRBlock {
718
final string toString() {
819
result = getFirstInstruction(this).toString()
920
}
@@ -59,7 +70,14 @@ class IRBlock extends TIRBlock {
5970
final Function getFunction() {
6071
result = getFirstInstruction(this).getFunction()
6172
}
73+
}
6274

75+
/**
76+
* A basic block with additional information about its predecessor and successor edges. Each edge
77+
* corresponds to the control flow between the last instruction of one block and the first
78+
* instruction of another block.
79+
*/
80+
class IRBlock extends IRBlockBase {
6381
final IRBlock getASuccessor() {
6482
blockSuccessor(this, result)
6583
}

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/IRBlock.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import Instruction
33
import semmle.code.cpp.ir.implementation.EdgeKind
44
private import Cached
55

6-
class IRBlock extends TIRBlock {
6+
/**
7+
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
8+
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
9+
* sequence.
10+
*
11+
* This class does not contain any members that query the predecessor or successor edges of the
12+
* block. This allows different classes that extend `IRBlockBase` to expose different subsets of
13+
* edges (e.g. ignoring unreachable edges).
14+
*
15+
* Most consumers should use the class `IRBlock`.
16+
*/
17+
class IRBlockBase extends TIRBlock {
718
final string toString() {
819
result = getFirstInstruction(this).toString()
920
}
@@ -59,7 +70,14 @@ class IRBlock extends TIRBlock {
5970
final Function getFunction() {
6071
result = getFirstInstruction(this).getFunction()
6172
}
73+
}
6274

75+
/**
76+
* A basic block with additional information about its predecessor and successor edges. Each edge
77+
* corresponds to the control flow between the last instruction of one block and the first
78+
* instruction of another block.
79+
*/
80+
class IRBlock extends IRBlockBase {
6381
final IRBlock getASuccessor() {
6482
blockSuccessor(this, result)
6583
}

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/reachability/ReachableBlock.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ predicate isInfeasibleInstructionSuccessor(Instruction instr, EdgeKind kind) {
1313
)
1414
}
1515

16-
predicate isInfeasibleEdge(IRBlock block, EdgeKind kind) {
16+
predicate isInfeasibleEdge(IRBlockBase block, EdgeKind kind) {
1717
isInfeasibleInstructionSuccessor(block.getLastInstruction(), kind)
1818
}
1919

20-
IRBlock getAFeasiblePredecessorBlock(IRBlock successor) {
20+
private IRBlock getAFeasiblePredecessorBlock(IRBlock successor) {
2121
exists(EdgeKind kind |
2222
result.getSuccessor(kind) = successor and
2323
not isInfeasibleEdge(result, kind)
2424
)
2525
}
2626

27-
predicate isBlockReachable(IRBlock block) {
27+
private predicate isBlockReachable(IRBlock block) {
2828
exists(FunctionIR f |
2929
getAFeasiblePredecessorBlock*(block) = f.getEntryBlock()
3030
)
3131
}
3232

33-
predicate isInstructionReachable(Instruction instr) {
34-
isBlockReachable(instr.getBlock())
35-
}
36-
37-
class ReachableBlock extends IRBlock {
33+
/**
34+
* An IR block that is reachable from the entry block of the function, considering only feasible
35+
* edges.
36+
*/
37+
class ReachableBlock extends IRBlockBase {
3838
ReachableBlock() {
3939
isBlockReachable(this)
4040
}
@@ -48,6 +48,9 @@ class ReachableBlock extends IRBlock {
4848
}
4949
}
5050

51+
/**
52+
* An instruction that is contained in a reachable block.
53+
*/
5154
class ReachableInstruction extends Instruction {
5255
ReachableInstruction() {
5356
this.getBlock() instanceof ReachableBlock

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ import Instruction
33
import semmle.code.cpp.ir.implementation.EdgeKind
44
private import Cached
55

6-
class IRBlock extends TIRBlock {
6+
/**
7+
* A basic block in the IR. A basic block consists of a sequence of `Instructions` with the only
8+
* incoming edges at the beginning of the sequence and the only outgoing edges at the end of the
9+
* sequence.
10+
*
11+
* This class does not contain any members that query the predecessor or successor edges of the
12+
* block. This allows different classes that extend `IRBlockBase` to expose different subsets of
13+
* edges (e.g. ignoring unreachable edges).
14+
*
15+
* Most consumers should use the class `IRBlock`.
16+
*/
17+
class IRBlockBase extends TIRBlock {
718
final string toString() {
819
result = getFirstInstruction(this).toString()
920
}
@@ -59,7 +70,14 @@ class IRBlock extends TIRBlock {
5970
final Function getFunction() {
6071
result = getFirstInstruction(this).getFunction()
6172
}
73+
}
6274

75+
/**
76+
* A basic block with additional information about its predecessor and successor edges. Each edge
77+
* corresponds to the control flow between the last instruction of one block and the first
78+
* instruction of another block.
79+
*/
80+
class IRBlock extends IRBlockBase {
6381
final IRBlock getASuccessor() {
6482
blockSuccessor(this, result)
6583
}

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/reachability/ReachableBlock.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ predicate isInfeasibleInstructionSuccessor(Instruction instr, EdgeKind kind) {
1313
)
1414
}
1515

16-
predicate isInfeasibleEdge(IRBlock block, EdgeKind kind) {
16+
predicate isInfeasibleEdge(IRBlockBase block, EdgeKind kind) {
1717
isInfeasibleInstructionSuccessor(block.getLastInstruction(), kind)
1818
}
1919

20-
IRBlock getAFeasiblePredecessorBlock(IRBlock successor) {
20+
private IRBlock getAFeasiblePredecessorBlock(IRBlock successor) {
2121
exists(EdgeKind kind |
2222
result.getSuccessor(kind) = successor and
2323
not isInfeasibleEdge(result, kind)
2424
)
2525
}
2626

27-
predicate isBlockReachable(IRBlock block) {
27+
private predicate isBlockReachable(IRBlock block) {
2828
exists(FunctionIR f |
2929
getAFeasiblePredecessorBlock*(block) = f.getEntryBlock()
3030
)
3131
}
3232

33-
predicate isInstructionReachable(Instruction instr) {
34-
isBlockReachable(instr.getBlock())
35-
}
36-
37-
class ReachableBlock extends IRBlock {
33+
/**
34+
* An IR block that is reachable from the entry block of the function, considering only feasible
35+
* edges.
36+
*/
37+
class ReachableBlock extends IRBlockBase {
3838
ReachableBlock() {
3939
isBlockReachable(this)
4040
}
@@ -48,6 +48,9 @@ class ReachableBlock extends IRBlock {
4848
}
4949
}
5050

51+
/**
52+
* An instruction that is contained in a reachable block.
53+
*/
5154
class ReachableInstruction extends Instruction {
5255
ReachableInstruction() {
5356
this.getBlock() instanceof ReachableBlock

0 commit comments

Comments
 (0)