Skip to content

Commit cd35cfa

Browse files
authored
Merge pull request #33 from tmr232/bugfix-issue32
bugfix issue32
2 parents a5133f4 + 846616c commit cd35cfa

File tree

10 files changed

+659
-527
lines changed

10 files changed

+659
-527
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
2222

2323
- Adding a new language now requires less wiring code, as many language declarations were merged.
2424

25+
### Fixed
26+
27+
- In C and C++, `if-else` statements without curly braces no longer break the CFG builder (#32)
28+
2529
## [0.0.8] - 2024-10-10
2630

2731
### Added

src/components/Demo.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
{#if showSegmentation}
228228
<div class="segmentation">
229229
<CodeSegmentation
230-
languageCode={languageCode[selection.language]}
230+
code={languageCode[selection.language]}
231231
language={selection.language}
232232
{simplify}
233233
/>

src/components/Graph.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
colorList: ColorList,
6363
) {
6464
const { trim, simplify, verbose, flatSwitch, highlight } = options;
65-
console.log(code);
6665
tree = parsers[language].parse(code);
6766
const functionSyntax = getFirstFunction(tree, language);
6867
if (!functionSyntax) {

src/components/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export function getFirstFunction(
5555
): Parser.SyntaxNode | null {
5656
let functionNode: Parser.SyntaxNode | null = null;
5757
const cursor = tree.walk();
58-
console.log(tree.rootNode.toString());
5958
const visitNode = () => {
6059
if (functionNodeTypes[language].includes(cursor.nodeType)) {
6160
functionNode = cursor.currentNode;

src/control-flow/cfg-c.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function processIfStatement(
180180
const queryString = `
181181
(if_statement
182182
condition: (_ ")" @closing-paren) @cond
183-
consequence: (_ "}"? @closing-brace) @then
183+
consequence: (_) @then
184184
alternative: (
185185
else_clause ([
186186
(if_statement) @else-if
@@ -213,7 +213,7 @@ function processIfStatement(
213213
}
214214
for (const [prevIf, thisIf] of pairwise(allIfs)) {
215215
ctx.link.offsetToSyntax(
216-
prevIf.requireSyntax("closing-brace"),
216+
prevIf.requireSyntax("then"),
217217
thisIf.requireSyntax("if"),
218218
);
219219
}
@@ -258,10 +258,7 @@ function processIfStatement(
258258
const lastMatch = last(allIfs) as Match;
259259
const elseSyntax = lastMatch.requireSyntax("else");
260260
ctx.link.syntaxToNode(elseSyntax, elseBlock.entry);
261-
ctx.link.offsetToSyntax(
262-
lastMatch.requireSyntax("closing-brace"),
263-
elseSyntax,
264-
);
261+
ctx.link.offsetToSyntax(lastMatch.requireSyntax("then"), elseSyntax);
265262

266263
if (previous && elseBlock.entry) {
267264
ctx.builder.addEdge(previous, elseBlock.entry, "alternative");

src/control-flow/generic-cfg-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface Dispatch {
2424
many(statements: Parser.SyntaxNode[]): BasicBlock;
2525
}
2626
interface Link {
27-
syntaxToNode: InstanceType<typeof NodeMapper>["linkSytaxToNode"];
27+
syntaxToNode: InstanceType<typeof NodeMapper>["linkSyntaxToNode"];
2828
offsetToSyntax: InstanceType<typeof NodeMapper>["linkOffsetToSyntax"];
2929
}
3030
export interface Context {
@@ -78,7 +78,7 @@ export class GenericCFGBuilder {
7878
"START",
7979
functionNode.startIndex,
8080
);
81-
this.nodeMapper.linkSytaxToNode(functionNode, startNode);
81+
this.nodeMapper.linkSyntaxToNode(functionNode, startNode);
8282
const bodySyntax = functionNode.childForFieldName("body");
8383
if (bodySyntax) {
8484
const blockHandler = new BlockHandler();
@@ -135,7 +135,7 @@ export class GenericCFGBuilder {
135135
many: this.dispatchMany.bind(this),
136136
},
137137
link: {
138-
syntaxToNode: this.nodeMapper.linkSytaxToNode.bind(this.nodeMapper),
138+
syntaxToNode: this.nodeMapper.linkSyntaxToNode.bind(this.nodeMapper),
139139
offsetToSyntax: this.nodeMapper.linkOffsetToSyntax.bind(
140140
this.nodeMapper,
141141
),

src/control-flow/node-mapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class NodeMapper {
3939
private ranges: { start: number; stop: number; value: Parser.SyntaxNode }[] =
4040
[];
4141

42-
public linkSytaxToNode(syntax: Parser.SyntaxNode, node: string) {
42+
public linkSyntaxToNode(syntax: Parser.SyntaxNode, node: string) {
4343
this.syntaxToNode.set(syntax, node);
4444

4545
this.ranges.push({

0 commit comments

Comments
 (0)