Skip to content

Commit 6bf3128

Browse files
committed
Use the ast edge label when building the ast node label
The C PrintAST library now includes the edge name in the AST Viewer tree.
1 parent 1886c0c commit 6bf3128

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

extensions/ql-vscode/src/contextual/astBuilder.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,18 @@ export default class AstBuilder {
4545
const parentToChildren = new Map<BqrsId, BqrsId[]>();
4646
const childToParent = new Map<BqrsId, BqrsId>();
4747
const astOrder = new Map<BqrsId, number>();
48+
const tupleTargetLabels = new Map<BqrsId, string>();
4849
const roots = [];
4950

5051
// Build up the parent-child relationships
5152
edgeTuples.tuples.forEach(tuple => {
52-
const [source, target, tupleType, orderValue] = tuple as [EntityValue, EntityValue, string, string];
53+
const [source, target, tupleType, value] = tuple as [EntityValue, EntityValue, string, string];
5354
const sourceId = source.id!;
5455
const targetId = target.id!;
5556

5657
switch (tupleType) {
5758
case 'semmle.order':
58-
astOrder.set(targetId, Number(orderValue));
59+
astOrder.set(targetId, Number(value));
5960
break;
6061

6162
case 'semmle.label': {
@@ -65,6 +66,11 @@ export default class AstBuilder {
6566
parentToChildren.set(sourceId, children = []);
6667
}
6768
children.push(targetId);
69+
70+
// ignore values that indicate a numeric order.
71+
if (!Number.isFinite(Number(value))) {
72+
tupleTargetLabels.set(targetId, value);
73+
}
6874
break;
6975
}
7076

@@ -84,9 +90,10 @@ export default class AstBuilder {
8490
break;
8591

8692
case 'semmle.label': {
93+
const label = [tupleTargetLabels.get(id), value ?? entity.label].filter(e => e).join(': ');
8794
const item = {
8895
id,
89-
label: value ?? entity.label,
96+
label,
9097
location: entity.url,
9198
fileLocation: fileRangeFromURI(entity.url, this.db),
9299
children: [] as ChildAstItem[],

extensions/ql-vscode/src/vscode-tests/no-workspace/contextual/astBuilder.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ describe('AstBuilder', () => {
6565
)).to.deep.eq(expectedRoots);
6666
});
6767

68+
it('should build the AST children', async () => {
69+
// just test one of the children to make sure that the structure is right
70+
const astBuilder = createAstBuilder();
71+
const roots = await astBuilder.getRoots();
72+
73+
expect(roots[0].children[1].parent).to.eq(roots[0]);
74+
// break the recursion
75+
(roots[0].children[1] as any).parent = undefined;
76+
(roots[0].children[1] as any).children = undefined;
77+
78+
const child = {
79+
children: undefined,
80+
fileLocation: undefined,
81+
id: 26367,
82+
label: 'body: [Block] { ... }',
83+
location: {
84+
endColumn: 1,
85+
endLine: 22,
86+
startColumn: 1,
87+
startLine: 20,
88+
uri: 'file:/opt/src/arch/sandbox/lib/interrupts.c'
89+
},
90+
order: 2,
91+
parent: undefined
92+
};
93+
94+
expect(roots[0].children[1]).to.deep.eq(child);
95+
});
96+
6897
it('should fail when graphProperties are not correct', async () => {
6998
overrides.graphProperties = {
7099
tuples: [

0 commit comments

Comments
 (0)