Skip to content

Commit bf4c445

Browse files
committed
Fixed handling of except nodes with no finally
1 parent 311b3b2 commit bf4c445

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/control-flow/cfg-python.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ export class CFGBuilder {
224224
const exceptSyntaxMany = this.getSyntaxMany(match, "except-body");
225225
const elseSyntax = this.getSyntax(match, "else-body");
226226
const finallySyntax = this.getSyntax(match, "finally-body");
227+
228+
const mergeNode = this.addNode("MERGE", "merge try-complex");
229+
227230
return this.withCluster("try-complex", (tryComplexCluster) => {
228231
const bodyBlock = this.withCluster("try", () =>
229232
getBlock(bodySyntax),
@@ -281,29 +284,37 @@ export class CFGBuilder {
281284
return finallyBlock;
282285
});
283286

284-
let complexExit: string | null = bodyBlock.exit;
287+
// This is the exit we get to if we don't have an exception
288+
let happyExit: string | null = bodyBlock.exit;
285289

286290
// Connect the body to the `else` block
287291
if (bodyBlock.exit && elseBlock?.entry) {
288292
this.addEdge(bodyBlock.exit, elseBlock.entry);
289-
complexExit = elseBlock.exit;
293+
happyExit = elseBlock.exit;
290294
}
291295

292296
if (finallyBlock?.entry) {
293297
// Connect `try` to `finally`
294298
const toFinally = elseBlock?.exit ?? bodyBlock.exit;
295299
if (toFinally) this.addEdge(toFinally, finallyBlock.entry);
296-
complexExit = finallyBlock.exit;
300+
happyExit = finallyBlock.exit;
297301
// Connect `except` to `finally`
298302
exceptBlocks.forEach((exceptBlock) => {
299303
if (exceptBlock.exit)
300304
this.addEdge(exceptBlock.exit, finallyBlock.entry as string);
301305
});
306+
} else {
307+
// We need to connect the `except` blocks to the merge node
308+
exceptBlocks.forEach((exceptBlock) => {
309+
if (exceptBlock.exit) this.addEdge(exceptBlock.exit, mergeNode);
310+
});
302311
}
303312

313+
if (happyExit) this.addEdge(happyExit, mergeNode);
314+
304315
return blockHandler.update({
305316
entry: bodyBlock.entry,
306-
exit: complexExit,
317+
exit: mergeNode,
307318
});
308319
});
309320
}

0 commit comments

Comments
 (0)