Skip to content

Commit 6f73aa5

Browse files
committed
C++: Convert IRGuards to use final abstract classes.
1 parent 20dfbdc commit 6f73aa5

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,15 @@ private class LogicalNotValueNumber extends ValueNumber {
142142
/**
143143
* A Boolean condition in the AST that guards one or more basic blocks. This includes
144144
* operands of logical operators but not switch statements.
145-
*
146-
* For performance reasons conditions inside static local initializers or
147-
* global initializers are not considered `GuardCondition`s.
148145
*/
149-
cached
150-
class GuardCondition extends Expr {
151-
cached
152-
GuardCondition() {
153-
exists(IRGuardCondition ir | this = ir.getUnconvertedResultExpression())
154-
or
155-
// no binary operators in the IR
156-
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
157-
}
158-
146+
abstract private class GuardConditionImpl extends Expr {
159147
/**
160148
* Holds if this condition controls `controlled`, meaning that `controlled` is only
161149
* entered if the value of this condition is `v`.
162150
*
163151
* For details on what "controls" mean, see the QLDoc for `controls`.
164152
*/
165-
cached
166-
predicate valueControls(BasicBlock controlled, AbstractValue v) { none() }
153+
abstract predicate valueControls(BasicBlock controlled, AbstractValue v);
167154

168155
/**
169156
* Holds if this condition controls `controlled`, meaning that `controlled` is only
@@ -197,61 +184,58 @@ class GuardCondition extends Expr {
197184
}
198185

199186
/** Holds if (determined by this guard) `left < right + k` evaluates to `isLessThan` if this expression evaluates to `testIsTrue`. */
200-
cached
201-
predicate comparesLt(Expr left, Expr right, int k, boolean isLessThan, boolean testIsTrue) {
202-
none()
203-
}
187+
abstract predicate comparesLt(Expr left, Expr right, int k, boolean isLessThan, boolean testIsTrue);
204188

205189
/**
206190
* Holds if (determined by this guard) `left < right + k` must be `isLessThan` in `block`.
207191
* If `isLessThan = false` then this implies `left >= right + k`.
208192
*/
209193
cached
210-
predicate ensuresLt(Expr left, Expr right, int k, BasicBlock block, boolean isLessThan) { none() }
194+
abstract predicate ensuresLt(Expr left, Expr right, int k, BasicBlock block, boolean isLessThan);
211195

212196
/**
213197
* Holds if (determined by this guard) `e < k` evaluates to `isLessThan` if
214198
* this expression evaluates to `value`.
215199
*/
216200
cached
217-
predicate comparesLt(Expr e, int k, boolean isLessThan, AbstractValue value) { none() }
201+
abstract predicate comparesLt(Expr e, int k, boolean isLessThan, AbstractValue value);
218202

219203
/**
220204
* Holds if (determined by this guard) `e < k` must be `isLessThan` in `block`.
221205
* If `isLessThan = false` then this implies `e >= k`.
222206
*/
223207
cached
224-
predicate ensuresLt(Expr e, int k, BasicBlock block, boolean isLessThan) { none() }
208+
abstract predicate ensuresLt(Expr e, int k, BasicBlock block, boolean isLessThan);
225209

226210
/** Holds if (determined by this guard) `left == right + k` evaluates to `areEqual` if this expression evaluates to `testIsTrue`. */
227211
cached
228-
predicate comparesEq(Expr left, Expr right, int k, boolean areEqual, boolean testIsTrue) {
229-
none()
230-
}
212+
abstract predicate comparesEq(Expr left, Expr right, int k, boolean areEqual, boolean testIsTrue);
231213

232214
/**
233215
* Holds if (determined by this guard) `left == right + k` must be `areEqual` in `block`.
234216
* If `areEqual = false` then this implies `left != right + k`.
235217
*/
236218
cached
237-
predicate ensuresEq(Expr left, Expr right, int k, BasicBlock block, boolean areEqual) { none() }
219+
abstract predicate ensuresEq(Expr left, Expr right, int k, BasicBlock block, boolean areEqual);
238220

239221
/** Holds if (determined by this guard) `e == k` evaluates to `areEqual` if this expression evaluates to `value`. */
240222
cached
241-
predicate comparesEq(Expr e, int k, boolean areEqual, AbstractValue value) { none() }
223+
abstract predicate comparesEq(Expr e, int k, boolean areEqual, AbstractValue value);
242224

243225
/**
244226
* Holds if (determined by this guard) `e == k` must be `areEqual` in `block`.
245227
* If `areEqual = false` then this implies `e != k`.
246228
*/
247229
cached
248-
predicate ensuresEq(Expr e, int k, BasicBlock block, boolean areEqual) { none() }
230+
abstract predicate ensuresEq(Expr e, int k, BasicBlock block, boolean areEqual);
249231
}
250232

233+
final class GuardCondition = GuardConditionImpl;
234+
251235
/**
252236
* A binary logical operator in the AST that guards one or more basic blocks.
253237
*/
254-
private class GuardConditionFromBinaryLogicalOperator extends GuardCondition {
238+
private class GuardConditionFromBinaryLogicalOperator extends GuardConditionImpl {
255239
GuardConditionFromBinaryLogicalOperator() {
256240
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
257241
}
@@ -329,7 +313,7 @@ private class GuardConditionFromBinaryLogicalOperator extends GuardCondition {
329313
* A Boolean condition in the AST that guards one or more basic blocks and has a corresponding IR
330314
* instruction.
331315
*/
332-
private class GuardConditionFromIR extends GuardCondition {
316+
private class GuardConditionFromIR extends GuardConditionImpl {
333317
IRGuardCondition ir;
334318

335319
GuardConditionFromIR() { this = ir.getUnconvertedResultExpression() }

0 commit comments

Comments
 (0)