Skip to content

Commit 8c20de2

Browse files
committed
feat: respect return in cfg construction
1 parent e19be9f commit 8c20de2

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

src/control-flow/cfg-dead-code.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ class CfgConditionalDeadCodeRemoval extends SemanticCfgGuidedVisitor {
127127
this.handleWithCondition(data);
128128
}
129129

130-
protected onReturnCall(data: { call: DataflowGraphVertexFunctionCall }): void {
131-
this.cachedStatements.set(data.call.id, true);
132-
}
133-
134130
protected onStopCall(data: { call: DataflowGraphVertexFunctionCall }): void {
135131
this.cachedStatements.set(data.call.id, true);
136132
}

src/control-flow/extract-cfg.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ function cfgFunctionCall(call: RFunctionCall<ParentInformation>, name: ControlFl
415415
graph.addEdge(callId + '-exit', exit, { label: CfgEdgeType.Fd });
416416
}
417417

418+
if(call.named && call.functionName.content === 'return') {
419+
info.returns.push(callId + '-exit');
420+
info.exitPoints.length = 0;
421+
}
422+
418423
// should not contain any breaks, nexts, or returns, (except for the body if something like 'break()')
419424
return info;
420425
}

test/functionality/control-flow/control-flow-graph.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ describe('Control Flow Graph', withTreeSitter(parser => {
114114
.addEdge('7-exit', '6-exit', { label: CfgEdgeType.Fd })
115115
.addEdge('8-exit', '7-exit', { label: CfgEdgeType.Fd })
116116
});
117+
118+
assertCfg(parser, 'f <- function() if (u) return(42) else return(1)', {
119+
entryPoints: [ '4' ],
120+
exitPoints: [ '4-exit' ],
121+
graph: new ControlFlowGraph()
122+
.addVertex({ id: 5, type: CfgVertexType.Statement, end: ['5-exit'] }, false)
123+
.addVertex({ id: 10, type: CfgVertexType.Statement, end: ['10-exit'] }, false)
124+
.addEdge('14-exit', '5-exit', { label: CfgEdgeType.Fd })
125+
.addEdge('14-exit', '10-exit', { label: CfgEdgeType.Fd })
126+
}, { simplificationPasses: ['analyze-dead-code'], expectIsSubgraph: true });
127+
117128
});
118129

119130
describe('loops', () => {

test/functionality/control-flow/dead-code/cfg-dead-code.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Control Flow Graph', withTreeSitter(parser => {
2121
expectIsSubgraph: true,
2222
simplificationPasses: ['analyze-dead-code'],
2323
/** we break unreachable edges for this test, the whole point is for not all of them being reachable */
24-
excludeProperties: ['entry-reaches-all', 'exit-reaches-all'],
24+
excludeProperties: ['entry-reaches-all', 'exit-reaches-all', 'single-entry-and-exit'],
2525
testIds: ids,
2626
additionalAsserts: (cfg, ast) => {
2727
for(const [n, i] of [...reachableFromStart.map(n => [n, false] as const), ...unreachableFromStart.map(n => [n, true] as const)]) {

0 commit comments

Comments
 (0)