Skip to content

Commit 724d97e

Browse files
committed
C++: Make sign analysis aware of unsigned'ness and accept test changes.
1 parent cbd4662 commit 724d97e

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

cpp/ql/lib/experimental/semmle/code/cpp/semantic/SemanticExprSpecific.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ module SemanticExprConfig {
216216
TSsaInstruction(IR::Instruction instr) { instr.hasMemoryResult() } or
217217
TSsaOperand(IR::Operand op) { op.isDefinitionInexact() } or
218218
TSsaPointerArithmeticGuard(ValueNumber instr) {
219-
exists(Guard g, IR::Operand use | use = instr.getAUse() |
219+
exists(Guard g, IR::Operand use |
220+
use = instr.getAUse() and use.getIRType() instanceof IR::IRAddressType
221+
|
220222
g.comparesLt(use, _, _, _, _) or
221223
g.comparesLt(_, use, _, _, _) or
222224
g.comparesEq(use, _, _, _, _) or

cpp/ql/lib/experimental/semmle/code/cpp/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ module SignAnalysis<DeltaSig D, UtilSig<D> Utils> {
198198
}
199199
}
200200

201+
/** An expression of an unsigned type. */
202+
private class UnsignedExpr extends FlowSignExpr {
203+
UnsignedExpr() { Utils::getTrackedType(this) instanceof SemUnsignedIntegerType }
204+
205+
override Sign getSignRestriction() {
206+
result = TPos() or
207+
result = TZero()
208+
}
209+
}
210+
201211
pragma[nomagic]
202212
private predicate binaryExprOperands(SemBinaryExpr binary, SemExpr left, SemExpr right) {
203213
binary.getLeftOperand() = left and binary.getRightOperand() = right

cpp/ql/test/library-tests/ir/range-analysis/SimpleRangeAnalysis_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ int test_mult01(int a, int b) {
318318
int r = a*b; // -143 .. 253
319319
range(r);
320320
total += r;
321-
range(total); // $ range=">=... * ...+0"
321+
range(total); // $ MISSING: range=">=... * ...+0"
322322
}
323323
if (3 <= a && a <= 11 && -13 <= b && b <= 0) {
324324
range(a); // $ range=<=11 range=>=3
@@ -366,7 +366,7 @@ int test_mult02(int a, int b) {
366366
int r = a*b; // -143 .. 253
367367
range(r);
368368
total += r;
369-
range(total); // $ range=">=... * ...+0"
369+
range(total); // $ MISSING: range=">=... * ...+0"
370370
}
371371
if (0 <= a && a <= 11 && -13 <= b && b <= 0) {
372372
range(a); // $ range=<=11 range=>=0
@@ -461,7 +461,7 @@ int test_mult04(int a, int b) {
461461
int r = a*b; // -391 .. 221
462462
range(r);
463463
total += r;
464-
range(total); // $ range="<=... * ...+0"
464+
range(total); // $ MISSING: range="<=... * ...+0"
465465
}
466466
if (-17 <= a && a <= 0 && -13 <= b && b <= 0) {
467467
range(a); // $ range=<=0 range=>=-17
@@ -509,7 +509,7 @@ int test_mult05(int a, int b) {
509509
int r = a*b; // -391 .. 221
510510
range(r);
511511
total += r;
512-
range(total); // $ range="<=... * ...+0"
512+
range(total); // $ MISSING: range="<=... * ...+0"
513513
}
514514
if (-17 <= a && a <= -2 && -13 <= b && b <= 0) {
515515
range(a); // $ range=<=-2 range=>=-17

0 commit comments

Comments
 (0)