Skip to content

Commit 48d071a

Browse files
committed
Fix flat-switch in C
In flat-switch mode switches without a default case add an alternative edge from the head to the merge.
1 parent aef9445 commit 48d071a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/control-flow/cfg-c.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,18 @@ export class CFGBuilder {
161161
switchHeadNode: string,
162162
) {
163163
let fallthrough: string | null = null;
164-
let previous: string | null = null;
165-
if (!this.flatSwitch && cases[0]?.conditionEntry) {
166-
this.addEdge(switchHeadNode, cases[0].conditionEntry);
167-
}
164+
let previous: string | null = switchHeadNode;
168165
cases.forEach((thisCase) => {
169166
if (this.flatSwitch) {
170167
if (thisCase.consequenceEntry) {
171168
this.addEdge(switchHeadNode, thisCase.consequenceEntry);
172169
if (fallthrough) {
173170
this.addEdge(fallthrough, thisCase.consequenceEntry);
174171
}
172+
if (thisCase.isDefault) {
173+
// If we have any default node - then we don't connect the head to the merge node.
174+
previous = null;
175+
}
175176
}
176177
} else {
177178
if (fallthrough && thisCase.consequenceEntry) {
@@ -205,7 +206,6 @@ export class CFGBuilder {
205206
});
206207
// Connect the last node to the merge node.
207208
// No need to handle `fallthrough` here as it is not allowed for the last case.
208-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
209209
if (previous) {
210210
this.addEdge(previous, mergeNode, "alternative");
211211
}
@@ -224,7 +224,7 @@ export class CFGBuilder {
224224
switchSyntax.namedChildren[1].namedChildren
225225
.filter((child) => caseTypes.includes(child.type))
226226
.forEach((caseSyntax) => {
227-
const isDefault = caseSyntax.type === "default_case";
227+
const isDefault = !caseSyntax.childForFieldName("value");
228228

229229
const consequence = caseSyntax.namedChildren.slice(isDefault ? 0 : 1);
230230
const hasFallthrough = true;

0 commit comments

Comments
 (0)