Skip to content

Commit f350fe2

Browse files
committed
Java: Adjust BasicBlock-based qltests.
1 parent 1154614 commit f350fe2

File tree

10 files changed

+58
-9
lines changed

10 files changed

+58
-9
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Provides utilities for working with basic blocks in tests.
3+
*/
4+
overlay[local?]
5+
module;
6+
7+
import java
8+
import codeql.util.Boolean
9+
10+
private predicate entryOrExit(ControlFlowNode n) {
11+
n instanceof ControlFlow::EntryNode or
12+
n instanceof ControlFlow::AnnotatedExitNode or
13+
n instanceof ControlFlow::ExitNode
14+
}
15+
16+
/** Gets the first AST node in the basic block `bb`, if any. */
17+
ControlFlowNode getFirstAstNode(BasicBlock bb) { result = getFirstAstNode(bb, false) }
18+
19+
/**
20+
* Gets the first AST node in the basic block `bb`, if any. Otherwise, gets
21+
* the first synthetic node.
22+
*/
23+
ControlFlowNode getFirstAstNodeOrSynth(BasicBlock bb) { result = getFirstAstNode(bb, true) }
24+
25+
private ControlFlowNode getFirstAstNode(BasicBlock bb, Boolean allowSynthetic) {
26+
result =
27+
min(ControlFlowNode n, int i, int astOrder |
28+
bb.getNode(i) = n and
29+
if n.injects(_)
30+
then astOrder = 0
31+
else
32+
if entryOrExit(n)
33+
then astOrder = 1
34+
else (
35+
allowSynthetic = true and astOrder = 2
36+
)
37+
|
38+
n order by astOrder, i
39+
)
40+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java
22
import semmle.code.java.controlflow.Dominance
3+
import utils.test.BasicBlock
34

45
from BasicBlock b, BasicBlock b2
56
where b.strictlyDominates(b2)
6-
select b, b2
7+
select getFirstAstNode(b), getFirstAstNode(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java
2+
import utils.test.BasicBlock
23

34
from BasicBlock b, BasicBlock b2
45
where b.getASuccessor() = b2
5-
select b, b2
6+
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java
22
import semmle.code.java.controlflow.Dominance
3+
import utils.test.BasicBlock
34

45
from BasicBlock b, BasicBlock b2
56
where b.strictlyDominates(b2)
6-
select b, b2
7+
select getFirstAstNode(b), getFirstAstNode(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java
2+
import utils.test.BasicBlock
23

34
from BasicBlock b, BasicBlock b2
45
where b.getASuccessor() = b2
5-
select b, b2
6+
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java
22
import semmle.code.java.controlflow.Dominance
3+
import utils.test.BasicBlock
34

45
from BasicBlock b, BasicBlock b2
56
where b.strictlyDominates(b2)
6-
select b, b2
7+
select getFirstAstNode(b), getFirstAstNode(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import java
2+
import utils.test.BasicBlock
23

34
from BasicBlock b, BasicBlock b2
45
where b.getASuccessor() = b2
5-
select b, b2
6+
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import java
22
import semmle.code.java.controlflow.Guards
3+
import utils.test.BasicBlock
34

45
from ConditionBlock cb, boolean testIsTrue, BasicBlock controlled
56
where
67
cb.controls(controlled, testIsTrue) and
78
cb.getEnclosingCallable().getDeclaringType().hasName("Test")
8-
select cb.getCondition(), testIsTrue, controlled
9+
select cb.getCondition(), testIsTrue, getFirstAstNode(controlled)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import java
22
import semmle.code.java.controlflow.Guards
3+
import utils.test.BasicBlock
34

45
from Guard g, BasicBlock bb, GuardValue gv
56
where
67
g.valueControls(bb, gv) and
78
g.getEnclosingCallable().getDeclaringType().hasName("Logic") and
89
(exists(gv.asBooleanValue()) or gv.isThrowsException() or gv.getDualValue().isThrowsException())
9-
select g, gv, bb
10+
select g, gv, getFirstAstNode(bb)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import java
22
import semmle.code.java.controlflow.Guards
3+
import utils.test.BasicBlock
34

45
from Guard g, BasicBlock bb, GuardValue gv
56
where
67
g.valueControls(bb, gv) and
78
g.getEnclosingCallable().getDeclaringType().hasName("Preconditions") and
89
(gv.isThrowsException() or gv.getDualValue().isThrowsException())
9-
select g, gv, bb
10+
select g, gv, getFirstAstNode(bb)

0 commit comments

Comments
 (0)