Skip to content

Commit

Permalink
C++: Infer 'b1 = true' and 'b2 = true' from 'b1 && b2 = true', and in…
Browse files Browse the repository at this point in the history
…fer 'b1 = false' and 'b2 = false' from 'b1 || b2 = false'.
  • Loading branch information
MathiasVP committed Jan 17, 2025
1 parent eadccf2 commit 5c494c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,18 @@ private module Cached {
)
or
unary_compares_eq(test.(BuiltinExpectCallValueNumber).getCondition(), op, k, areEqual, value)
or
exists(BinaryLogicalOperation logical, Expr operand, boolean b |
test.getAnInstruction().getUnconvertedResultExpression() = logical and
op.getDef().getUnconvertedResultExpression() = operand and
logical.impliesValue(operand, b, value.(BooleanValue).getValue())
|
k = 1 and
areEqual = b
or
k = 0 and
areEqual = b.booleanNot()
)
}

/** Rearrange various simple comparisons into `left == right + k` form. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,16 +563,20 @@
| 182 | ... >= ... == 0 when ... >= ... is false |
| 182 | ... >= ... == 1 when ... && ... is true |
| 182 | ... >= ... == 1 when ... >= ... is true |
| 182 | b1 != 0 when ! ... is false |
| 182 | b1 != 0 when ... && ... is true |
| 182 | b1 != 0 when b1 is true |
| 182 | b1 != 1 when b1 is false |
| 182 | b1 == 0 when b1 is false |
| 182 | b1 == 1 when ! ... is false |
| 182 | b1 == 1 when ... && ... is true |
| 182 | b1 == 1 when b1 is true |
| 182 | b2 != 0 when ! ... is false |
| 182 | b2 != 0 when ... && ... is true |
| 182 | b2 != 0 when b2 is true |
| 182 | b2 != 1 when b2 is false |
| 182 | b2 == 0 when b2 is false |
| 182 | b2 == 1 when ! ... is false |
| 182 | b2 == 1 when ... && ... is true |
| 182 | b2 == 1 when b2 is true |
| 182 | foo < 1.0+0 when ... && ... is true |
Expand Down Expand Up @@ -610,14 +614,18 @@
| 193 | ... \|\| ... == 1 when ! ... is false |
| 193 | ... \|\| ... == 1 when ... \|\| ... is true |
| 193 | b1 != 0 when b1 is true |
| 193 | b1 != 1 when ! ... is true |
| 193 | b1 != 1 when ... \|\| ... is false |
| 193 | b1 != 1 when b1 is false |
| 193 | b1 == 0 when ! ... is true |
| 193 | b1 == 0 when ... \|\| ... is false |
| 193 | b1 == 0 when b1 is false |
| 193 | b1 == 1 when b1 is true |
| 193 | b2 != 0 when b2 is true |
| 193 | b2 != 1 when ! ... is true |
| 193 | b2 != 1 when ... \|\| ... is false |
| 193 | b2 != 1 when b2 is false |
| 193 | b2 == 0 when ! ... is true |
| 193 | b2 == 0 when ... \|\| ... is false |
| 193 | b2 == 0 when b2 is false |
| 193 | b2 == 1 when b2 is true |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,14 @@ unary
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | != | 1 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | == | 0 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:6:182:16 | ! ... | == | 1 | 182 | 184 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:9 | b1 | != | 0 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:9 | b1 | == | 1 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | != | 0 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | != | 1 | 182 | 184 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | == | 0 | 182 | 184 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:8:182:15 | ... && ... | == | 1 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:14:182:15 | b2 | != | 0 | 185 | 188 |
| test.cpp:182:6:182:16 | ! ... | test.cpp:182:14:182:15 | b2 | == | 1 | 185 | 188 |
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | != | 0 | 181 | 182 |
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | != | 0 | 182 | 182 |
| test.cpp:182:8:182:9 | b1 | test.cpp:182:8:182:9 | b1 | == | 1 | 181 | 182 |
Expand Down Expand Up @@ -674,10 +678,14 @@ unary
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | != | 1 | 197 | 199 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | == | 0 | 197 | 199 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:6:193:16 | ! ... | == | 1 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:9 | b1 | != | 1 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:9 | b1 | == | 0 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | != | 0 | 197 | 199 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | != | 1 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | == | 0 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:8:193:15 | ... \|\| ... | == | 1 | 197 | 199 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:14:193:15 | b2 | != | 1 | 193 | 196 |
| test.cpp:193:6:193:16 | ! ... | test.cpp:193:14:193:15 | b2 | == | 0 | 193 | 196 |
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | != | 1 | 192 | 193 |
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | != | 1 | 193 | 193 |
| test.cpp:193:8:193:9 | b1 | test.cpp:193:8:193:9 | b1 | == | 0 | 192 | 193 |
Expand Down

0 comments on commit 5c494c3

Please sign in to comment.