Skip to content

Commit 7b67add

Browse files
committed
Added C switch
1 parent 4d70a36 commit 7b67add

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

src/control-flow/cfg-c.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ export class CFGBuilder {
122122
return this.processWhileStatement(node);
123123
case "do_statement":
124124
return this.processDoStatement(node);
125-
case "expression_switch_statement":
126-
case "type_switch_statement":
127-
case "select_statement":
125+
case "switch_statement":
128126
return this.processSwitchlike(node);
129127
case "return_statement": {
130128
const returnNode = this.addNode("RETURN", node.text);
@@ -211,42 +209,49 @@ export class CFGBuilder {
211209
if (previous) {
212210
this.addEdge(previous, mergeNode, "alternative");
213211
}
212+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
213+
if (fallthrough) {
214+
this.addEdge(fallthrough, mergeNode, "regular");
215+
}
214216
}
215217

216218
private collectCases(
217219
switchSyntax: Parser.SyntaxNode,
218220
blockHandler: BlockHandler,
219221
): Case[] {
220222
const cases: Case[] = [];
221-
const caseTypes = [
222-
"default_case",
223-
"communication_case",
224-
"type_case",
225-
"expression_case",
226-
];
227-
switchSyntax.namedChildren
223+
const caseTypes = ["case_statement"];
224+
switchSyntax.namedChildren[1].namedChildren
228225
.filter((child) => caseTypes.includes(child.type))
229226
.forEach((caseSyntax) => {
230227
const isDefault = caseSyntax.type === "default_case";
231228

232229
const consequence = caseSyntax.namedChildren.slice(isDefault ? 0 : 1);
233-
const hasFallthrough = consequence
234-
.map((node) => node.type)
235-
.includes("fallthrough_statement");
230+
const hasFallthrough = true;
236231

237232
const conditionNode = this.addNode(
238233
"CASE_CONDITION",
239234
isDefault ? "default" : (caseSyntax.firstNamedChild?.text ?? ""),
240235
);
241-
const consequenceNode = blockHandler.update(
236+
// let consequenceBlock;
237+
// if (consequence.length) {
238+
// consequenceBlock = blockHandler.update(
239+
// this.processStatements(consequence),
240+
// );
241+
// } else {
242+
// const consequenceNode = this.addNode("EMPTY", "empty case body");
243+
// consequenceBlock = { entry: consequenceNode, exit: consequenceNode };
244+
// }
245+
246+
const consequenceBlock = blockHandler.update(
242247
this.processStatements(consequence),
243248
);
244249

245250
cases.push({
246251
conditionEntry: conditionNode,
247252
conditionExit: conditionNode,
248-
consequenceEntry: consequenceNode.entry,
249-
consequenceExit: consequenceNode.exit,
253+
consequenceEntry: consequenceBlock.entry,
254+
consequenceExit: consequenceBlock.exit,
250255
alternativeExit: conditionNode,
251256
hasFallthrough,
252257
isDefault,

src/control-flow/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function graphToDot(cfg: CFG, verbose: boolean = false): string {
1616

1717
// const label = `${graph.getNodeAttribute(node, "line") || ""}`;
1818
// label = `${levels.get(node)}`;
19-
label = `${graph.getNodeAttribute(node, "markers")}`;
19+
// label = `${graph.getNodeAttribute(node, "markers")}`;
2020

2121
let shape = "box";
2222
let fillColor = "lightgray";

src/frontend/src/lib/TestGraph.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@
8383
if (trim) cfg = trimFor(cfg);
8484
if (simplify) cfg = simplifyCFG(cfg, mergeNodeAttrs);
8585
86+
dot = graphToDot(cfg, verbose);
87+
ast = formatAST(functionSyntax.toString());
88+
8689
const testResults = runTest(record);
8790
visibleTestResults = JSON.stringify(testResults);
8891
failed = !!testResults.filter((result) => result.failure).length;
89-
console.log(failed);
90-
dot = graphToDot(cfg, verbose);
91-
ast = formatAST(functionSyntax.toString());
9292
9393
return graphviz.dot(dot);
9494
}

src/test/sample.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,45 @@ exits: 1
6868
void doWhile() {
6969
do {
7070
} while (x());
71+
}
72+
73+
/*
74+
nodes: 4,
75+
exits: 1
76+
*/
77+
void gotoA() {
78+
label:
79+
if (x) {
80+
goto label;
81+
}
82+
}
83+
84+
/*
85+
nodes: 7,
86+
exits: 1,
87+
reaches: [["1","3"]]
88+
*/
89+
void switch_1() {
90+
switch (x) {
91+
case 1:
92+
// CFG: 1
93+
"include me!";
94+
case 2:
95+
case 3:
96+
// CFG: 3
97+
"Include me!";
98+
}
99+
}
100+
101+
/*
102+
nodes: 7,
103+
exits: 1
104+
*/
105+
void switch_2() {
106+
switch (x) {
107+
case 1:
108+
break;
109+
case 2:
110+
case 3:
111+
}
71112
}

0 commit comments

Comments
 (0)