Skip to content

Commit b6cc6a3

Browse files
author
Robert Marsh
committed
C++: Fix BinaryLogicalOperators always being guards
1 parent f323fa1 commit b6cc6a3

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ class GuardCondition extends Expr {
1010
exists(IRGuardCondition ir | this = ir.getUnconvertedResultExpression())
1111
or
1212
// no binary operators in the IR
13-
exists(Instruction ir |
14-
this.(BinaryLogicalOperation).getAnOperand().getFullyConverted() = ir.getAST()
13+
exists(GuardCondition gc |
14+
this.(BinaryLogicalOperation).getAnOperand()= gc
1515
)
1616
or
1717
// the IR short-circuits if(!x)
1818
(
19+
// don't produce a guard condition for `y = !x` and other non-short-circuited cases
1920
not exists (Instruction inst | this.getFullyConverted() = inst.getAST()) and
2021
exists(IRGuardCondition ir | this.(NotExpr).getOperand() = ir.getAST())
2122
)

cpp/ql/test/library-tests/controlflow/guards-ir/ASTGuards.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
| test.c:138:9:138:9 | i |
2828
| test.c:146:7:146:8 | ! ... |
2929
| test.c:146:8:146:8 | x |
30+
| test.c:152:10:152:10 | x |
31+
| test.c:152:10:152:15 | ... && ... |
32+
| test.c:152:15:152:15 | y |
3033
| test.cpp:18:8:18:10 | call to get |
3134
| test.cpp:31:7:31:13 | ... == ... |
3235
| test.cpp:42:13:42:20 | call to getABool |

cpp/ql/test/library-tests/controlflow/guards-ir/ASTGuardsControl.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282
| test.c:138:9:138:9 | i | true | 138 | 139 |
8383
| test.c:146:7:146:8 | ! ... | true | 146 | 147 |
8484
| test.c:146:8:146:8 | x | false | 146 | 147 |
85+
| test.c:152:10:152:10 | x | true | 151 | 152 |
86+
| test.c:152:10:152:10 | x | true | 152 | 152 |
87+
| test.c:152:10:152:15 | ... && ... | true | 151 | 152 |
88+
| test.c:152:15:152:15 | y | true | 151 | 152 |
8589
| test.cpp:18:8:18:10 | call to get | true | 19 | 19 |
8690
| test.cpp:31:7:31:13 | ... == ... | false | 30 | 30 |
8791
| test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 |

cpp/ql/test/library-tests/controlflow/guards-ir/IRGuards.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
| test.c:137:7:137:7 | 0 |
2222
| test.c:138:9:138:9 | i |
2323
| test.c:146:8:146:8 | x |
24+
| test.c:152:10:152:10 | x |
25+
| test.c:152:15:152:15 | y |
2426
| test.cpp:18:8:18:12 | (bool)... |
2527
| test.cpp:31:7:31:13 | ... == ... |
2628
| test.cpp:42:13:42:20 | call to getABool |

cpp/ql/test/library-tests/controlflow/guards-ir/IRGuardsControl.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
| test.c:137:7:137:7 | 0 | true | 139 | 139 |
7979
| test.c:138:9:138:9 | i | true | 139 | 139 |
8080
| test.c:146:8:146:8 | x | false | 147 | 147 |
81+
| test.c:152:10:152:10 | x | true | 152 | 152 |
82+
| test.c:152:15:152:15 | y | true | 152 | 152 |
8183
| test.cpp:18:8:18:12 | (bool)... | true | 0 | 0 |
8284
| test.cpp:31:7:31:13 | ... == ... | false | 34 | 34 |
8385
| test.cpp:31:7:31:13 | ... == ... | true | 30 | 30 |

cpp/ql/test/library-tests/controlflow/guards-ir/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ void test5(int x) {
147147
test3();
148148
}
149149
}
150+
151+
void test6(int x, int y) {
152+
return x && y;
153+
}
154+

0 commit comments

Comments
 (0)