Skip to content

Commit 5c494c3

Browse files
committed
C++: Infer 'b1 = true' and 'b2 = true' from 'b1 && b2 = true', and infer 'b1 = false' and 'b2 = false' from 'b1 || b2 = false'.
1 parent eadccf2 commit 5c494c3

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,18 @@ private module Cached {
994994
)
995995
or
996996
unary_compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), op, k, areEqual, value)
997+
or
998+
exists(BinaryLogicalOperation logical, Expr operand, boolean b |
999+
test.getAnInstruction().getUnconvertedResultExpression() = logical and
1000+
op.getDef().getUnconvertedResultExpression() = operand and
1001+
logical.impliesValue(operand, b, value.(BooleanValue).getValue())
1002+
|
1003+
k = 1 and
1004+
areEqual = b
1005+
or
1006+
k = 0 and
1007+
areEqual = b.booleanNot()
1008+
)
9971009
}
9981010

9991011
/** Rearrange various simple comparisons into `left == right + k` form. */

cpp/ql/test/library-tests/controlflow/guards/GuardsCompare.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,20 @@
563563
| 182 | ... >= ... == 0 when ... >= ... is false |
564564
| 182 | ... >= ... == 1 when ... && ... is true |
565565
| 182 | ... >= ... == 1 when ... >= ... is true |
566+
| 182 | b1 != 0 when ! ... is false |
566567
| 182 | b1 != 0 when ... && ... is true |
567568
| 182 | b1 != 0 when b1 is true |
568569
| 182 | b1 != 1 when b1 is false |
569570
| 182 | b1 == 0 when b1 is false |
571+
| 182 | b1 == 1 when ! ... is false |
570572
| 182 | b1 == 1 when ... && ... is true |
571573
| 182 | b1 == 1 when b1 is true |
574+
| 182 | b2 != 0 when ! ... is false |
572575
| 182 | b2 != 0 when ... && ... is true |
573576
| 182 | b2 != 0 when b2 is true |
574577
| 182 | b2 != 1 when b2 is false |
575578
| 182 | b2 == 0 when b2 is false |
579+
| 182 | b2 == 1 when ! ... is false |
576580
| 182 | b2 == 1 when ... && ... is true |
577581
| 182 | b2 == 1 when b2 is true |
578582
| 182 | foo < 1.0+0 when ... && ... is true |
@@ -610,14 +614,18 @@
610614
| 193 | ... \|\| ... == 1 when ! ... is false |
611615
| 193 | ... \|\| ... == 1 when ... \|\| ... is true |
612616
| 193 | b1 != 0 when b1 is true |
617+
| 193 | b1 != 1 when ! ... is true |
613618
| 193 | b1 != 1 when ... \|\| ... is false |
614619
| 193 | b1 != 1 when b1 is false |
620+
| 193 | b1 == 0 when ! ... is true |
615621
| 193 | b1 == 0 when ... \|\| ... is false |
616622
| 193 | b1 == 0 when b1 is false |
617623
| 193 | b1 == 1 when b1 is true |
618624
| 193 | b2 != 0 when b2 is true |
625+
| 193 | b2 != 1 when ! ... is true |
619626
| 193 | b2 != 1 when ... \|\| ... is false |
620627
| 193 | b2 != 1 when b2 is false |
628+
| 193 | b2 == 0 when ! ... is true |
621629
| 193 | b2 == 0 when ... \|\| ... is false |
622630
| 193 | b2 == 0 when b2 is false |
623631
| 193 | b2 == 1 when b2 is true |

cpp/ql/test/library-tests/controlflow/guards/GuardsEnsure.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,14 @@ unary
640640
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | != | 1 | 185 | 188 |
641641
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | == | 0 | 185 | 188 |
642642
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | == | 1 | 182 | 184 |
643+
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:9 | b1 | != | 0 | 185 | 188 |
644+
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:9 | b1 | == | 1 | 185 | 188 |
643645
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | != | 0 | 185 | 188 |
644646
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | != | 1 | 182 | 184 |
645647
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | == | 0 | 182 | 184 |
646648
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | == | 1 | 185 | 188 |
649+
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:14:182:15 | b2 | != | 0 | 185 | 188 |
650+
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:14:182:15 | b2 | == | 1 | 185 | 188 |
647651
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | != | 0 | 181 | 182 |
648652
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | != | 0 | 182 | 182 |
649653
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | == | 1 | 181 | 182 |
@@ -674,10 +678,14 @@ unary
674678
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | != | 1 | 197 | 199 |
675679
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | == | 0 | 197 | 199 |
676680
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | == | 1 | 193 | 196 |
681+
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:9 | b1 | != | 1 | 193 | 196 |
682+
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:9 | b1 | == | 0 | 193 | 196 |
677683
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | != | 0 | 197 | 199 |
678684
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | != | 1 | 193 | 196 |
679685
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | == | 0 | 193 | 196 |
680686
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | == | 1 | 197 | 199 |
687+
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:14:193:15 | b2 | != | 1 | 193 | 196 |
688+
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:14:193:15 | b2 | == | 0 | 193 | 196 |
681689
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | != | 1 | 192 | 193 |
682690
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | != | 1 | 193 | 193 |
683691
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | == | 0 | 192 | 193 |

0 commit comments

Comments
 (0)