Skip to content

Commit 06de5e7

Browse files
committed
Python: Add NeverReachable test
This looks for nodes annotated with `t.never` in the test that are reachable in the CFG. This should not happen (it messes with various queries, e.g. the "mixed returns" query), but the test shows that in a few particular cases (involving the `match` statement where all cases contain `return`s), we _do_ have reachable nodes that shouldn't be.
1 parent 91f74ce commit 06de5e7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test_match.py:159:13:159:23 | BinaryExpr | Node annotated with t.never is reachable in $@ | test_match.py:151:1:151:42 | Function test_match_exhaustive_return_first | test_match_exhaustive_return_first |
2+
| test_match.py:172:13:172:23 | BinaryExpr | Node annotated with t.never is reachable in $@ | test_match.py:164:1:164:45 | Function test_match_exhaustive_return_wildcard | test_match_exhaustive_return_wildcard |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Checks that expressions annotated with `t.never` either have no CFG
3+
* node, or if they do, that the node is not reachable from its scope's
4+
* entry (including within the same basic block).
5+
*/
6+
7+
import python
8+
import TimerUtils
9+
10+
from NeverTimerAnnotation ann
11+
where
12+
exists(ControlFlowNode n, Scope s |
13+
n.getNode() = ann.getExpr() and
14+
s = n.getScope() and
15+
(
16+
// Reachable via inter-block path (includes same block)
17+
s.getEntryNode().getBasicBlock().reaches(n.getBasicBlock())
18+
or
19+
// In same block as entry but at a later index
20+
exists(BasicBlock bb, int i, int j |
21+
bb.getNode(i) = s.getEntryNode() and bb.getNode(j) = n and i < j
22+
)
23+
)
24+
)
25+
select ann,
26+
"Node annotated with t.never is reachable in $@",
27+
ann.getTestFunction(), ann.getTestFunction().getName()

0 commit comments

Comments
 (0)