mirror of
https://github.com/github/codeql.git
synced 2026-03-30 20:28:15 +02:00
Java: Adjust BasicBlock-based qltests.
This commit is contained in:
40
java/ql/lib/utils/test/BasicBlock.qll
Normal file
40
java/ql/lib/utils/test/BasicBlock.qll
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Provides utilities for working with basic blocks in tests.
|
||||
*/
|
||||
overlay[local?]
|
||||
module;
|
||||
|
||||
import java
|
||||
import codeql.util.Boolean
|
||||
|
||||
private predicate entryOrExit(ControlFlowNode n) {
|
||||
n instanceof ControlFlow::EntryNode or
|
||||
n instanceof ControlFlow::AnnotatedExitNode or
|
||||
n instanceof ControlFlow::ExitNode
|
||||
}
|
||||
|
||||
/** Gets the first AST node in the basic block `bb`, if any. */
|
||||
ControlFlowNode getFirstAstNode(BasicBlock bb) { result = getFirstAstNode(bb, false) }
|
||||
|
||||
/**
|
||||
* Gets the first AST node in the basic block `bb`, if any. Otherwise, gets
|
||||
* the first synthetic node.
|
||||
*/
|
||||
ControlFlowNode getFirstAstNodeOrSynth(BasicBlock bb) { result = getFirstAstNode(bb, true) }
|
||||
|
||||
private ControlFlowNode getFirstAstNode(BasicBlock bb, Boolean allowSynthetic) {
|
||||
result =
|
||||
min(ControlFlowNode n, int i, int astOrder |
|
||||
bb.getNode(i) = n and
|
||||
if n.injects(_)
|
||||
then astOrder = 0
|
||||
else
|
||||
if entryOrExit(n)
|
||||
then astOrder = 1
|
||||
else (
|
||||
allowSynthetic = true and astOrder = 2
|
||||
)
|
||||
|
|
||||
n order by astOrder, i
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Dominance
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.strictlyDominates(b2)
|
||||
select b, b2
|
||||
select getFirstAstNode(b), getFirstAstNode(b2)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import java
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.getASuccessor() = b2
|
||||
select b, b2
|
||||
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Dominance
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.strictlyDominates(b2)
|
||||
select b, b2
|
||||
select getFirstAstNode(b), getFirstAstNode(b2)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import java
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.getASuccessor() = b2
|
||||
select b, b2
|
||||
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Dominance
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.strictlyDominates(b2)
|
||||
select b, b2
|
||||
select getFirstAstNode(b), getFirstAstNode(b2)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import java
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from BasicBlock b, BasicBlock b2
|
||||
where b.getASuccessor() = b2
|
||||
select b, b2
|
||||
select getFirstAstNodeOrSynth(b), getFirstAstNodeOrSynth(b2)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Guards
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from ConditionBlock cb, boolean testIsTrue, BasicBlock controlled
|
||||
where
|
||||
cb.controls(controlled, testIsTrue) and
|
||||
cb.getEnclosingCallable().getDeclaringType().hasName("Test")
|
||||
select cb.getCondition(), testIsTrue, controlled
|
||||
select cb.getCondition(), testIsTrue, getFirstAstNode(controlled)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Guards
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from Guard g, BasicBlock bb, GuardValue gv
|
||||
where
|
||||
g.valueControls(bb, gv) and
|
||||
g.getEnclosingCallable().getDeclaringType().hasName("Logic") and
|
||||
(exists(gv.asBooleanValue()) or gv.isThrowsException() or gv.getDualValue().isThrowsException())
|
||||
select g, gv, bb
|
||||
select g, gv, getFirstAstNode(bb)
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import java
|
||||
import semmle.code.java.controlflow.Guards
|
||||
import utils.test.BasicBlock
|
||||
|
||||
from Guard g, BasicBlock bb, GuardValue gv
|
||||
where
|
||||
g.valueControls(bb, gv) and
|
||||
g.getEnclosingCallable().getDeclaringType().hasName("Preconditions") and
|
||||
(gv.isThrowsException() or gv.getDualValue().isThrowsException())
|
||||
select g, gv, bb
|
||||
select g, gv, getFirstAstNode(bb)
|
||||
|
||||
Reference in New Issue
Block a user