Skip to content

Commit 19b8e0d

Browse files
committed
C++: Add a few subclasses to 'EdgeKind'.
1 parent be8195a commit 19b8e0d

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

cpp/ql/lib/semmle/code/cpp/ir/implementation/EdgeKind.qll

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,26 @@ class GotoEdge extends EdgeKindImpl, TGotoEdge {
5252
final override string toString() { result = "Goto" }
5353
}
5454

55+
/**
56+
* A "true" or "false" edge representing a successor of a conditional branch.
57+
*/
58+
abstract private class BooleanEdgeKindImpl extends EdgeKindImpl { }
59+
60+
final class BooleanEdge = BooleanEdgeKindImpl;
61+
5562
/**
5663
* A "true" edge, representing the successor of a conditional branch when the
5764
* condition is non-zero.
5865
*/
59-
class TrueEdge extends EdgeKindImpl, TTrueEdge {
66+
class TrueEdge extends BooleanEdgeKindImpl, TTrueEdge {
6067
final override string toString() { result = "True" }
6168
}
6269

6370
/**
6471
* A "false" edge, representing the successor of a conditional branch when the
6572
* condition is zero.
6673
*/
67-
class FalseEdge extends EdgeKindImpl, TFalseEdge {
74+
class FalseEdge extends BooleanEdgeKindImpl, TFalseEdge {
6875
final override string toString() { result = "False" }
6976
}
7077

@@ -95,19 +102,48 @@ class SehExceptionEdge extends ExceptionEdgeImpl, TSehExceptionEdge {
95102
final override string toString() { result = "SEH Exception" }
96103
}
97104

105+
/**
106+
* An edge from a `Switch` instruction to one of the cases, or to the default
107+
* branch.
108+
*/
109+
abstract private class SwitchEdgeKindImpl extends EdgeKindImpl {
110+
/**
111+
* Gets the smallest value of the switch expression for which control will flow along this edge.
112+
*/
113+
string getMinValue() { none() }
114+
115+
/**
116+
* Gets the largest value of the switch expression for which control will flow along this edge.
117+
*/
118+
string getMaxValue() { none() }
119+
120+
/**
121+
* Gets the unique value of the switch expression for which control will
122+
* flow along this edge, if any.
123+
*/
124+
final string getValue() { result = unique( | | [this.getMinValue(), this.getMaxValue()]) }
125+
126+
/** Holds if this edge is the default edge. */
127+
predicate isDefault() { none() }
128+
}
129+
130+
final class SwitchEdge = SwitchEdgeKindImpl;
131+
98132
/**
99133
* A "default" edge, representing the successor of a `Switch` instruction when
100134
* none of the case values matches the condition value.
101135
*/
102-
class DefaultEdge extends EdgeKindImpl, TDefaultEdge {
136+
class DefaultEdge extends SwitchEdgeKindImpl, TDefaultEdge {
103137
final override string toString() { result = "Default" }
138+
139+
final override predicate isDefault() { any() }
104140
}
105141

106142
/**
107143
* A "case" edge, representing the successor of a `Switch` instruction when the
108144
* the condition value matches a corresponding `case` label.
109145
*/
110-
class CaseEdge extends EdgeKindImpl, TCaseEdge {
146+
class CaseEdge extends SwitchEdgeKindImpl, TCaseEdge {
111147
string minValue;
112148
string maxValue;
113149

@@ -119,24 +155,9 @@ class CaseEdge extends EdgeKindImpl, TCaseEdge {
119155
else result = "Case[" + minValue + ".." + maxValue + "]"
120156
}
121157

122-
/**
123-
* Gets the smallest value of the switch expression for which control will flow along this edge.
124-
*/
125-
final string getMinValue() { result = minValue }
158+
final override string getMinValue() { result = minValue }
126159

127-
/**
128-
* Gets the largest value of the switch expression for which control will flow along this edge.
129-
*/
130-
final string getMaxValue() { result = maxValue }
131-
132-
/**
133-
* Gets the unique value of the switch expression for which control will
134-
* flow along this edge, if any.
135-
*/
136-
final string getValue() {
137-
minValue = maxValue and
138-
result = minValue
139-
}
160+
final override string getMaxValue() { result = maxValue }
140161
}
141162

142163
/**

0 commit comments

Comments
 (0)