Skip to content

Commit 4961bbc

Browse files
committed
Changed logic for function name resolution
1 parent 92dcb0f commit 4961bbc

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

packages/cli/src/languagePlugins/c/headerResolver/index.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ describe("CHeaderResolver", () => {
77
const cFilesMap = getCFilesMap();
88
const resolver = new CHeaderResolver();
99
const burgers = path.join(cFilesFolder, "burgers.h");
10+
const crashcases = path.join(cFilesFolder, "crashcases.h");
1011
const file = cFilesMap.get(burgers);
1112
if (!file) {
1213
throw new Error(`File not found: ${burgers}`);
1314
}
15+
const ccfile = cFilesMap.get(crashcases);
16+
if (!ccfile) {
17+
throw new Error(`File not found: ${crashcases}`);
18+
}
1419
const exportedSymbols = resolver.resolveSymbols(file);
1520

1621
test("should resolve symbols in C header files", () => {
@@ -153,4 +158,40 @@ describe("CHeaderResolver", () => {
153158
expect(drink.identifierNode.type).toBe("type_identifier");
154159
expect(drink.filepath).toBe(burgers);
155160
});
161+
162+
test("Crash Cases", () => {
163+
const ccexportedSymbols = resolver.resolveSymbols(ccfile);
164+
expect(ccexportedSymbols).toBeDefined();
165+
166+
const sprite = ccexportedSymbols.find((s) => s.name === "Sprite");
167+
expect(sprite).toBeDefined();
168+
expect(sprite.type).toBe("struct");
169+
expect(sprite.specifiers).toEqual([]);
170+
expect(sprite.qualifiers).toEqual([]);
171+
expect(sprite.node.type).toBe("struct_specifier");
172+
expect(sprite.identifierNode.type).toBe("type_identifier");
173+
expect(sprite.filepath).toBe(crashcases);
174+
175+
const placeholderfunction = ccexportedSymbols.find(
176+
(s) => s.name === "PlaceholderFunction",
177+
);
178+
expect(placeholderfunction).toBeDefined();
179+
expect(placeholderfunction.type).toBe("function");
180+
expect(placeholderfunction.specifiers).toEqual([]);
181+
expect(placeholderfunction.qualifiers).toEqual([]);
182+
expect(placeholderfunction.node.type).toBe("declaration");
183+
expect(placeholderfunction.identifierNode.type).toBe("identifier");
184+
expect(placeholderfunction.filepath).toBe(crashcases);
185+
186+
const gmtfwa = ccexportedSymbols.find(
187+
(s) => s.name === "gMovementTypeFuncs_WanderAround",
188+
);
189+
expect(gmtfwa).toBeDefined();
190+
expect(gmtfwa.type).toBe("variable");
191+
expect(gmtfwa.specifiers).toEqual([]);
192+
expect(gmtfwa.qualifiers).toEqual([]);
193+
expect(gmtfwa.node.type).toBe("declaration");
194+
expect(gmtfwa.identifierNode.type).toBe("identifier");
195+
expect(gmtfwa.filepath).toBe(crashcases);
196+
});
156197
});

packages/cli/src/languagePlugins/c/headerResolver/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,22 @@ export class CHeaderResolver {
5050
const qualifiers = capture.node.children
5151
.filter((child) => child.type === "type_qualifier")
5252
.map((child) => child.text);
53-
// Check if the node is a function or variable declaration
54-
const type =
55-
capture.node.descendantsOfType("function_declarator").length !== 0
56-
? "function"
57-
: "variable";
5853
let currentNode = capture.node;
5954
// Traverse the tree to find the identifier node
6055
// This is a workaround for the fact that the identifier node is not always the first child
6156
// (e.g. in pointers or arrays)
6257
while (
58+
!currentNode.childForFieldName("declarator") ||
6359
currentNode.childForFieldName("declarator").type !== "identifier"
6460
) {
65-
currentNode = currentNode.childForFieldName("declarator");
61+
if (!currentNode.childForFieldName("declarator")) {
62+
currentNode = currentNode.firstNamedChild;
63+
} else {
64+
currentNode = currentNode.childForFieldName("declarator");
65+
}
6666
}
67+
const type =
68+
currentNode.type === "function_declarator" ? "function" : "variable";
6769
const idNode = currentNode.childForFieldName("declarator");
6870
exportedSymbols.push({
6971
name: idNode.text,

packages/cli/src/languagePlugins/c/symbolRegistry/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,14 @@ export class CSymbolRegistry {
130130
// Traverse the tree to find the identifier node
131131
// cf. header resolver.
132132
while (
133+
!currentNode.childForFieldName("declarator") ||
133134
currentNode.childForFieldName("declarator").type !== "identifier"
134135
) {
135-
currentNode = currentNode.childForFieldName("declarator");
136+
if (!currentNode.childForFieldName("declarator")) {
137+
currentNode = currentNode.firstNamedChild;
138+
} else {
139+
currentNode = currentNode.childForFieldName("declarator");
140+
}
136141
}
137142
const name = currentNode.childForFieldName("declarator").text;
138143
let foundInRegistry = false;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct ObjectEvent {
2+
int id;
3+
};
4+
5+
struct Sprite {
6+
int x;
7+
int y;
8+
};
9+
10+
int PlaceholderFunction(struct ObjectEvent *oe, struct Sprite *s);
11+
12+
int (*const gMovementTypeFuncs_WanderAround[])(struct ObjectEvent *, struct Sprite *) = {
13+
PlaceholderFunction,
14+
PlaceholderFunction,
15+
PlaceholderFunction,
16+
PlaceholderFunction,
17+
PlaceholderFunction,
18+
PlaceholderFunction,
19+
PlaceholderFunction,
20+
};

0 commit comments

Comments
 (0)