Skip to content

Commit 1ca6f99

Browse files
committed
Java: Adjust BasicBlock-based qltests.
1 parent 0fa7b5e commit 1ca6f99

10 files changed

Lines changed: 50 additions & 9 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
overlay[local?]
2+
module;
3+
4+
import java
5+
import codeql.util.Boolean
6+
7+
private predicate entryOrExit(ControlFlowNode n) {
8+
n instanceof ControlFlow::EntryNode or
9+
n instanceof ControlFlow::AnnotatedExitNode or
10+
n instanceof ControlFlow::ExitNode
11+
}
12+
13+
ControlFlowNode getFirstAstNode(BasicBlock bb) { result = getFirstAstNode(bb, false) }
14+
15+
ControlFlowNode getFirstAstNodeOrSynth(BasicBlock bb) { result = getFirstAstNode(bb, true) }
16+
17+
private ControlFlowNode getFirstAstNode(BasicBlock bb, Boolean allowSynthetic) {
18+
result =
19+
min(ControlFlowNode n, int i, int astOrder |
20+
bb.getNode(i) = n and
21+
if n.injects(_)
22+
then astOrder = 0
23+
else
24+
if entryOrExit(n)
25+
then astOrder = 1
26+
else (
27+
allowSynthetic = true and astOrder = 2
28+
)
29+
|
30+
n order by astOrder, i
31+
)
32+
}
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)