Skip to content

Commit 8774b4a

Browse files
committed
C++: Add a few subclasses to 'EdgeKind'.
1 parent 381b65c commit 8774b4a

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

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

Lines changed: 41 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,47 @@ 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+
predicate isDefault() { none() }
127+
}
128+
129+
final class SwitchEdge = SwitchEdgeKindImpl;
130+
98131
/**
99132
* A "default" edge, representing the successor of a `Switch` instruction when
100133
* none of the case values matches the condition value.
101134
*/
102-
class DefaultEdge extends EdgeKindImpl, TDefaultEdge {
135+
class DefaultEdge extends SwitchEdgeKindImpl, TDefaultEdge {
103136
final override string toString() { result = "Default" }
137+
138+
final override predicate isDefault() { any() }
104139
}
105140

106141
/**
107142
* A "case" edge, representing the successor of a `Switch` instruction when the
108143
* the condition value matches a corresponding `case` label.
109144
*/
110-
class CaseEdge extends EdgeKindImpl, TCaseEdge {
145+
class CaseEdge extends SwitchEdgeKindImpl, TCaseEdge {
111146
string minValue;
112147
string maxValue;
113148

@@ -119,24 +154,9 @@ class CaseEdge extends EdgeKindImpl, TCaseEdge {
119154
else result = "Case[" + minValue + ".." + maxValue + "]"
120155
}
121156

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 }
157+
final override string getMinValue() { result = minValue }
126158

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-
}
159+
final override string getMaxValue() { result = maxValue }
140160
}
141161

142162
/**

0 commit comments

Comments
 (0)