Skip to content

Commit 19018bd

Browse files
authored
feat(cfg): mid marker for for loops (#1610)
feat(cfg): mid marker for for loops
1 parent 285ffe4 commit 19018bd

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/control-flow/extract-cfg.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function cfgWhile(whileLoop: RWhileLoop<ParentInformation>, condition: ControlFl
221221

222222
function cfgFor(forLoop: RForLoop<ParentInformation>, variable: ControlFlowInformation, vector: ControlFlowInformation, body: ControlFlowInformation): ControlFlowInformation {
223223
const graph = variable.graph;
224-
graph.addVertex({ id: forLoop.info.id, type: identifyMayStatementType(forLoop), exit: [forLoop.info.id + '-exit'] });
224+
graph.addVertex({ id: forLoop.info.id, type: identifyMayStatementType(forLoop), exit: [forLoop.info.id + '-exit'], mid: [forLoop.info.id + '-head'] });
225225

226226
graph.merge(vector.graph);
227227
graph.merge(body.graph);
@@ -236,10 +236,13 @@ function cfgFor(forLoop: RForLoop<ParentInformation>, variable: ControlFlowInfor
236236
}
237237
}
238238

239+
graph.addVertex({ id: forLoop.info.id + '-head', type: CfgVertexType.MidMarker, root: forLoop.info.id, kind: 'head' });
240+
239241
for(const exit of variable.exitPoints) {
240-
for(const entry of body.entryPoints) {
241-
graph.addEdge(entry, exit, { label: CfgEdgeType.Cd, when: RTrue, caused: forLoop.info.id });
242-
}
242+
graph.addEdge(forLoop.info.id + '-head', exit, { label: CfgEdgeType.Fd });
243+
}
244+
for(const entry of body.entryPoints) {
245+
graph.addEdge(entry, forLoop.info.id + '-head', { label: CfgEdgeType.Cd, when: RTrue, caused: forLoop.info.id });
243246
}
244247

245248
for(const next of [...body.nexts, ...body.exitPoints]) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ describe('Control Flow Graph', withTreeSitter(parser => {
216216
.addEdge('10-exit', 9, { label: CfgEdgeType.Fd })
217217
.addEdge(11, '10-exit', { label: CfgEdgeType.Fd })
218218
});
219+
220+
assertCfg(parser, 'for (i in v) b', {
221+
entryPoints: [ '5' ],
222+
exitPoints: [ '5-exit' ],
223+
graph: new ControlFlowGraph()
224+
.addVertex({ id: '4-head', kind: 'head', type: CfgVertexType.MidMarker, root: 4 })
225+
}, {
226+
expectIsSubgraph: true
227+
});
219228
});
220229

221230
describe('function calls', () => {

0 commit comments

Comments
 (0)