Skip to content

Commit 01173bf

Browse files
committed
Cfg: Fold getTryInit into indexed getBody.
1 parent 3cbc8f0 commit 01173bf

3 files changed

Lines changed: 24 additions & 32 deletions

File tree

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ module Ast implements AstSig<Location> {
203203
final private class FinalTryStmt = CS::TryStmt;
204204

205205
class TryStmt extends FinalTryStmt {
206-
Stmt getBody() { result = this.getBlock() }
206+
Stmt getBody(int index) { index = 0 and result = this.getBlock() }
207207

208208
CatchClause getCatch(int index) { result = this.getCatchClause(index) }
209209

java/ql/lib/semmle/code/java/ControlFlowGraph.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,18 @@ private module Ast implements AstSig<Location> {
117117
final private class FinalTryStmt = J::TryStmt;
118118

119119
class TryStmt extends FinalTryStmt {
120-
Stmt getBody() { result = super.getBlock() }
120+
Stmt getBody(int index) {
121+
result = super.getResource(index)
122+
or
123+
index = count(super.getAResource()) and
124+
result = super.getBlock()
125+
}
121126

122127
CatchClause getCatch(int index) { result = super.getCatchClause(index) }
123128

124129
Stmt getFinally() { result = super.getFinally() }
125130
}
126131

127-
AstNode getTryInit(TryStmt try, int index) { result = try.getResource(index) }
128-
129132
final private class FinalCatchClause = J::CatchClause;
130133

131134
class CatchClause extends FinalCatchClause {

shared/controlflow/codeql/controlflow/ControlFlowGraph.qll

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,12 @@ signature module AstSig<LocationSig Location> {
185185

186186
/** A `try` statement with `catch` and/or `finally` clauses. */
187187
class TryStmt extends Stmt {
188-
/** Gets the body of this `try` statement. */
189-
Stmt getBody();
188+
/**
189+
* Gets the body of this `try` statement at the specified (zero-based)
190+
* position `index`. In some languages, there is only ever a single body
191+
* (with `index` 0).
192+
*/
193+
Stmt getBody(int index);
190194

191195
/**
192196
* Gets the `catch` clause at the specified (zero-based) position `index`
@@ -198,15 +202,6 @@ signature module AstSig<LocationSig Location> {
198202
Stmt getFinally();
199203
}
200204

201-
/**
202-
* Gets the initializer of this `try` statement at the specified (zero-based)
203-
* position `index`, if any.
204-
*
205-
* An example of this are resource declarations in Java's try-with-resources
206-
* statement.
207-
*/
208-
default AstNode getTryInit(TryStmt try, int index) { none() }
209-
210205
/**
211206
* Gets the `else` block of this `try` statement, if any.
212207
*
@@ -699,7 +694,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
699694
or
700695
exists(TryStmt trystmt |
701696
trystmt = n and
702-
cannotTerminateNormally(trystmt.getBody()) and
697+
cannotTerminateNormally(trystmt.getBody(_)) and
703698
forall(CatchClause catch | trystmt.getCatch(_) = catch |
704699
cannotTerminateNormally(catch.getBody())
705700
)
@@ -1256,11 +1251,7 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
12561251
)
12571252
)
12581253
or
1259-
exists(TryStmt trystmt |
1260-
ast = getTryInit(trystmt, _)
1261-
or
1262-
ast = trystmt.getBody()
1263-
|
1254+
exists(TryStmt trystmt | ast = trystmt.getBody(_) |
12641255
c.getSuccessorType() instanceof ExceptionSuccessor and
12651256
(
12661257
n.isBefore(trystmt.getCatch(0))
@@ -1635,16 +1626,11 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
16351626
or
16361627
exists(TryStmt trystmt |
16371628
n1.isBefore(trystmt) and
1638-
(
1639-
n2.isBefore(getTryInit(trystmt, 0))
1640-
or
1641-
not exists(getTryInit(trystmt, _)) and n2.isBefore(trystmt.getBody())
1642-
)
1629+
n2.isBefore(trystmt.getBody(0))
16431630
or
1644-
exists(int i | n1.isAfter(getTryInit(trystmt, i)) |
1645-
n2.isBefore(getTryInit(trystmt, i + 1))
1646-
or
1647-
not exists(getTryInit(trystmt, i + 1)) and n2.isBefore(trystmt.getBody())
1631+
exists(int i |
1632+
n1.isAfter(trystmt.getBody(i)) and
1633+
n2.isBefore(trystmt.getBody(i + 1))
16481634
)
16491635
or
16501636
exists(PreControlFlowNode beforeElse, PreControlFlowNode beforeFinally |
@@ -1659,8 +1645,11 @@ module Make0<LocationSig Location, AstSig<Location> Ast> {
16591645
not exists(trystmt.getFinally()) and beforeFinally.isAfter(trystmt)
16601646
)
16611647
|
1662-
n1.isAfter(trystmt.getBody()) and
1663-
n2 = beforeElse
1648+
exists(int i |
1649+
n1.isAfter(trystmt.getBody(i)) and
1650+
not exists(trystmt.getBody(i + 1)) and
1651+
n2 = beforeElse
1652+
)
16641653
or
16651654
n1.isAfter(getTryElse(trystmt)) and
16661655
n2 = beforeFinally

0 commit comments

Comments
 (0)