Skip to content

Commit 002a395

Browse files
authored
Flat-switch improvements
Flat-Switch fixes
2 parents 32f16d8 + 1a67ec4 commit 002a395

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
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;

src/control-flow/cfg-go.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ export class CFGBuilder {
159159
switchHeadNode: string,
160160
) {
161161
let fallthrough: string | null = null;
162-
let previous: string | null = null;
163-
if (!this.flatSwitch && cases[0]?.conditionEntry) {
164-
this.addEdge(switchHeadNode, cases[0].conditionEntry);
165-
}
162+
let previous: string | null = switchHeadNode;
166163
cases.forEach((thisCase) => {
167164
if (this.flatSwitch) {
168165
if (thisCase.consequenceEntry) {
169166
this.addEdge(switchHeadNode, thisCase.consequenceEntry);
170167
if (fallthrough) {
171168
this.addEdge(fallthrough, thisCase.consequenceEntry);
172169
}
170+
if (thisCase.isDefault) {
171+
previous = null;
172+
}
173173
}
174174
} else {
175175
if (fallthrough && thisCase.consequenceEntry) {
@@ -203,7 +203,6 @@ export class CFGBuilder {
203203
});
204204
// Connect the last node to the merge node.
205205
// No need to handle `fallthrough` here as it is not allowed for the last case.
206-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
207206
if (previous) {
208207
this.addEdge(previous, mergeNode, "alternative");
209208
}

src/control-flow/cfg-python.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,12 @@ export class CFGBuilder {
409409
this.addEdge(patternNode, consequenceBlock.entry, "consequence");
410410
if (consequenceBlock?.exit)
411411
this.addEdge(consequenceBlock.exit, mergeNode, "regular");
412-
if (previous) this.addEdge(previous, patternNode, "alternative");
413-
previous = patternNode;
412+
if (this.flatSwitch) {
413+
this.addEdge(previous, patternNode, "regular");
414+
} else {
415+
if (previous) this.addEdge(previous, patternNode, "alternative");
416+
previous = patternNode;
417+
}
414418
}
415419

416420
return blockHandler.update({ entry: subjectBlock.entry, exit: mergeNode });

0 commit comments

Comments
 (0)