Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 75d69ef

Browse files
authored
Merge pull request #267 from smowton/smowton/feature/print-ast-label-package-node
PrintAst: Label File nodes' package-name children, and ensure that child comes before all declarations
2 parents b4e15fb + 454993f commit 75d69ef

File tree

6 files changed

+117
-76
lines changed

6 files changed

+117
-76
lines changed

ql/src/semmle/go/PrintAst.qll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,47 @@ class FileNode extends BaseAstNode {
194194
result = getSortOrder().toString()
195195
}
196196

197+
/**
198+
* Gets a child of this node, renumbering `packageNode`, our parent's
199+
* `oldPackageIndex`th child, as the first child and moving others accordingly.
200+
*/
201+
private BaseAstNode getChildPackageFirst(
202+
int childIndex, BaseAstNode packageNode, int oldPackageIndex
203+
) {
204+
super.getChild(oldPackageIndex) = packageNode and
205+
(
206+
childIndex = 0 and result = packageNode
207+
or
208+
result =
209+
rank[childIndex](BaseAstNode node, int i |
210+
node = super.getChild(i) and i != oldPackageIndex
211+
|
212+
node order by i
213+
)
214+
)
215+
}
216+
217+
/**
218+
* Gets a child of this node, moving the package-name expression to the front
219+
* of the list if one exists.
220+
*/
221+
override BaseAstNode getChild(int childIndex) {
222+
if exists(ast.getPackageNameExpr())
223+
then result = getChildPackageFirst(childIndex, TAstNode(ast.getPackageNameExpr()), _)
224+
else result = super.getChild(childIndex)
225+
}
226+
227+
/**
228+
* Gets the label for the edge from this node to the specified child. The package name
229+
* expression is named 'package'; others are numbered as per our parent's implementation
230+
* of this method.
231+
*/
232+
override string getChildEdgeLabel(int childIndex) {
233+
if getChild(childIndex) = TAstNode(ast.getPackageNameExpr())
234+
then result = "package"
235+
else result = super.getChildEdgeLabel(childIndex)
236+
}
237+
197238
/**
198239
* Gets the string representation of this File. Note explicitly using a relative path
199240
* like this rather than absolute as per default for the File class is a workaround for

ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@ go.mod:
44
# 3| 1: [GoModGoLine] go.mod go line
55
input.go:
66
# 0| [File] library-tests/semmle/go/PrintAst/input.go
7-
# 5| 0: [CommentGroup] comment group
7+
# 1| package: [Ident] main
8+
# 5| 1: [CommentGroup] comment group
89
# 5| 0: [SlashSlashComment] comment
9-
# 7| 1: [CommentGroup] comment group
10+
# 7| 2: [CommentGroup] comment group
1011
# 7| 0: [SlashSlashComment] comment
11-
# 9| 2: [DocComment] comment group
12+
# 9| 3: [DocComment] comment group
1213
# 9| 0: [SlashSlashComment] comment
13-
# 17| 3: [CommentGroup] comment group
14+
# 17| 4: [CommentGroup] comment group
1415
# 17| 0: [SlashSlashComment] comment
15-
# 45| 4: [DocComment] comment group
16+
# 45| 5: [DocComment] comment group
1617
# 45| 0: [SlashSlashComment] comment
17-
# 64| 5: [DocComment] comment group
18+
# 64| 6: [DocComment] comment group
1819
# 64| 0: [SlashSlashComment] comment
19-
# 74| 6: [DocComment] comment group
20+
# 74| 7: [DocComment] comment group
2021
# 74| 0: [SlashSlashComment] comment
21-
# 111| 7: [DocComment] comment group
22+
# 111| 8: [DocComment] comment group
2223
# 111| 0: [SlashSlashComment] comment
23-
# 127| 8: [DocComment] comment group
24+
# 127| 9: [DocComment] comment group
2425
# 127| 0: [SlashSlashComment] comment
25-
# 132| 9: [DocComment] comment group
26+
# 132| 10: [DocComment] comment group
2627
# 132| 0: [SlashSlashComment] comment
27-
# 3| 10: [ImportDecl] import declaration
28+
# 3| 11: [ImportDecl] import declaration
2829
# 3| 0: [ImportSpec] import specifier
2930
# 3| 0: [StringLit] "fmt"
30-
# 10| 11: [FuncDecl] function declaration
31+
# 10| 12: [FuncDecl] function declaration
3132
# 10| 0: [FunctionName, Ident] test5
3233
# 10| Type = func(bool)
3334
# 10| 1: [FuncTypeExpr] function type
@@ -152,7 +153,7 @@ input.go:
152153
# 40| 1: [BlockStmt] block statement
153154
# 41| 0: [GotoStmt] goto statement
154155
# 41| 0: [Ident, LabelName] outer
155-
# 46| 12: [FuncDecl] function declaration
156+
# 46| 13: [FuncDecl] function declaration
156157
# 46| 0: [FunctionName, Ident] test6
157158
# 46| Type = func(chan int, chan float32)
158159
# 46| 1: [FuncTypeExpr] function type
@@ -262,7 +263,7 @@ input.go:
262263
# 58| Value = [IntLit] 42
263264
# 61| 3: [SelectStmt] select statement
264265
# 61| 0: [BlockStmt] block statement
265-
# 65| 13: [FuncDecl] function declaration
266+
# 65| 14: [FuncDecl] function declaration
266267
# 65| 0: [FunctionName, Ident] test7
267268
# 65| Type = func(int) int
268269
# 65| 1: [FuncTypeExpr] function type
@@ -327,7 +328,7 @@ input.go:
327328
# 71| 0: [IntLit] 42
328329
# 71| Type = int
329330
# 71| Value = [IntLit] 42
330-
# 75| 14: [FuncDecl] function declaration
331+
# 75| 15: [FuncDecl] function declaration
331332
# 75| 0: [FunctionName, Ident] test8
332333
# 75| Type = func(int)
333334
# 75| 1: [FuncTypeExpr] function type
@@ -447,7 +448,7 @@ input.go:
447448
# 107| 0: [ConstantName, Ident] true
448449
# 107| Type = bool
449450
# 107| Value = [ConstantName, Ident] true
450-
# 112| 15: [FuncDecl] function declaration
451+
# 112| 16: [FuncDecl] function declaration
451452
# 112| 0: [FunctionName, Ident] test9
452453
# 112| Type = func(interface { })
453454
# 112| 1: [FuncTypeExpr] function type
@@ -518,7 +519,7 @@ input.go:
518519
# 123| 1: [ConstantName, Ident] false
519520
# 123| Type = bool
520521
# 123| Value = [ConstantName, Ident] false
521-
# 128| 16: [FuncDecl] function declaration
522+
# 128| 17: [FuncDecl] function declaration
522523
# 128| 0: [FunctionName, Ident] test10
523524
# 128| Type = func(func() )
524525
# 128| 1: [FuncTypeExpr] function type
@@ -533,7 +534,7 @@ input.go:
533534
# 129| Type = ()
534535
# 129| 0: [Ident, VariableName] f
535536
# 129| Type = func()
536-
# 133| 17: [FuncDecl] function declaration
537+
# 133| 18: [FuncDecl] function declaration
537538
# 133| 0: [FunctionName, Ident] test11
538539
# 133| Type = func([]int)
539540
# 133| 1: [FuncTypeExpr] function type
@@ -595,25 +596,25 @@ input.go:
595596
# 145| 0: [Ident, VariableName] xs
596597
# 145| Type = []int
597598
# 145| 1: [BlockStmt] block statement
598-
# 1| 18: [Ident] main
599599
other.go:
600600
# 0| [File] library-tests/semmle/go/PrintAst/other.go
601-
# 3| 0: [FuncDecl] function declaration
601+
# 1| package: [Ident] main
602+
# 3| 1: [FuncDecl] function declaration
602603
# 3| 0: [FunctionName, Ident] main
603604
# 3| Type = func()
604605
# 3| 1: [FuncTypeExpr] function type
605606
# 3| 2: [BlockStmt] block statement
606-
# 5| 1: [FuncDecl] function declaration
607+
# 5| 2: [FuncDecl] function declaration
607608
# 5| 0: [FunctionName, Ident] f
608609
# 5| Type = func()
609610
# 5| 1: [FuncTypeExpr] function type
610611
# 5| 2: [BlockStmt] block statement
611-
# 6| 2: [FuncDecl] function declaration
612+
# 6| 3: [FuncDecl] function declaration
612613
# 6| 0: [FunctionName, Ident] g
613614
# 6| Type = func()
614615
# 6| 1: [FuncTypeExpr] function type
615616
# 6| 2: [BlockStmt] block statement
616-
# 8| 3: [FuncDecl] function declaration
617+
# 8| 4: [FuncDecl] function declaration
617618
# 8| 0: [FunctionName, Ident] hasNested
618619
# 8| Type = func()
619620
# 8| 1: [FuncTypeExpr] function type
@@ -638,7 +639,7 @@ other.go:
638639
# 11| Type = int
639640
# 11| 0: [Ident, VariableName] myNested
640641
# 11| Type = func() int
641-
# 15| 4: [VarDecl] variable declaration
642+
# 15| 5: [VarDecl] variable declaration
642643
# 15| 0: [ValueSpec] value declaration specifier
643644
# 15| 0: [Ident, VariableName] x
644645
# 15| Type = int
@@ -647,4 +648,3 @@ other.go:
647648
# 15| 2: [IntLit] 0
648649
# 15| Type = int
649650
# 15| Value = [IntLit] 0
650-
# 1| 5: [Ident] main

ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ go.mod:
44
# 3| 1: [GoModGoLine] go.mod go line
55
input.go:
66
# 0| [File] library-tests/semmle/go/PrintAst/input.go
7-
# 3| 10: [ImportDecl] import declaration
7+
# 1| package: [Ident] main
8+
# 3| 1: [ImportDecl] import declaration
89
# 3| 0: [ImportSpec] import specifier
910
# 3| 0: [StringLit] "fmt"
10-
# 10| 11: [FuncDecl] function declaration
11+
# 10| 2: [FuncDecl] function declaration
1112
# 10| 0: [FunctionName, Ident] test5
1213
# 10| Type = func(bool)
1314
# 10| 1: [FuncTypeExpr] function type
@@ -132,7 +133,7 @@ input.go:
132133
# 40| 1: [BlockStmt] block statement
133134
# 41| 0: [GotoStmt] goto statement
134135
# 41| 0: [Ident, LabelName] outer
135-
# 46| 12: [FuncDecl] function declaration
136+
# 46| 3: [FuncDecl] function declaration
136137
# 46| 0: [FunctionName, Ident] test6
137138
# 46| Type = func(chan int, chan float32)
138139
# 46| 1: [FuncTypeExpr] function type
@@ -242,7 +243,7 @@ input.go:
242243
# 58| Value = [IntLit] 42
243244
# 61| 3: [SelectStmt] select statement
244245
# 61| 0: [BlockStmt] block statement
245-
# 65| 13: [FuncDecl] function declaration
246+
# 65| 4: [FuncDecl] function declaration
246247
# 65| 0: [FunctionName, Ident] test7
247248
# 65| Type = func(int) int
248249
# 65| 1: [FuncTypeExpr] function type
@@ -307,7 +308,7 @@ input.go:
307308
# 71| 0: [IntLit] 42
308309
# 71| Type = int
309310
# 71| Value = [IntLit] 42
310-
# 75| 14: [FuncDecl] function declaration
311+
# 75| 5: [FuncDecl] function declaration
311312
# 75| 0: [FunctionName, Ident] test8
312313
# 75| Type = func(int)
313314
# 75| 1: [FuncTypeExpr] function type
@@ -427,7 +428,7 @@ input.go:
427428
# 107| 0: [ConstantName, Ident] true
428429
# 107| Type = bool
429430
# 107| Value = [ConstantName, Ident] true
430-
# 112| 15: [FuncDecl] function declaration
431+
# 112| 6: [FuncDecl] function declaration
431432
# 112| 0: [FunctionName, Ident] test9
432433
# 112| Type = func(interface { })
433434
# 112| 1: [FuncTypeExpr] function type
@@ -498,7 +499,7 @@ input.go:
498499
# 123| 1: [ConstantName, Ident] false
499500
# 123| Type = bool
500501
# 123| Value = [ConstantName, Ident] false
501-
# 128| 16: [FuncDecl] function declaration
502+
# 128| 7: [FuncDecl] function declaration
502503
# 128| 0: [FunctionName, Ident] test10
503504
# 128| Type = func(func() )
504505
# 128| 1: [FuncTypeExpr] function type
@@ -513,7 +514,7 @@ input.go:
513514
# 129| Type = ()
514515
# 129| 0: [Ident, VariableName] f
515516
# 129| Type = func()
516-
# 133| 17: [FuncDecl] function declaration
517+
# 133| 8: [FuncDecl] function declaration
517518
# 133| 0: [FunctionName, Ident] test11
518519
# 133| Type = func([]int)
519520
# 133| 1: [FuncTypeExpr] function type
@@ -575,25 +576,25 @@ input.go:
575576
# 145| 0: [Ident, VariableName] xs
576577
# 145| Type = []int
577578
# 145| 1: [BlockStmt] block statement
578-
# 1| 18: [Ident] main
579579
other.go:
580580
# 0| [File] library-tests/semmle/go/PrintAst/other.go
581-
# 3| 0: [FuncDecl] function declaration
581+
# 1| package: [Ident] main
582+
# 3| 1: [FuncDecl] function declaration
582583
# 3| 0: [FunctionName, Ident] main
583584
# 3| Type = func()
584585
# 3| 1: [FuncTypeExpr] function type
585586
# 3| 2: [BlockStmt] block statement
586-
# 5| 1: [FuncDecl] function declaration
587+
# 5| 2: [FuncDecl] function declaration
587588
# 5| 0: [FunctionName, Ident] f
588589
# 5| Type = func()
589590
# 5| 1: [FuncTypeExpr] function type
590591
# 5| 2: [BlockStmt] block statement
591-
# 6| 2: [FuncDecl] function declaration
592+
# 6| 3: [FuncDecl] function declaration
592593
# 6| 0: [FunctionName, Ident] g
593594
# 6| Type = func()
594595
# 6| 1: [FuncTypeExpr] function type
595596
# 6| 2: [BlockStmt] block statement
596-
# 8| 3: [FuncDecl] function declaration
597+
# 8| 4: [FuncDecl] function declaration
597598
# 8| 0: [FunctionName, Ident] hasNested
598599
# 8| Type = func()
599600
# 8| 1: [FuncTypeExpr] function type
@@ -618,7 +619,7 @@ other.go:
618619
# 11| Type = int
619620
# 11| 0: [Ident, VariableName] myNested
620621
# 11| Type = func() int
621-
# 15| 4: [VarDecl] variable declaration
622+
# 15| 5: [VarDecl] variable declaration
622623
# 15| 0: [ValueSpec] value declaration specifier
623624
# 15| 0: [Ident, VariableName] x
624625
# 15| Type = int
@@ -627,4 +628,3 @@ other.go:
627628
# 15| 2: [IntLit] 0
628629
# 15| Type = int
629630
# 15| Value = [IntLit] 0
630-
# 1| 5: [Ident] main

ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ go.mod:
44
# 3| 1: [GoModGoLine] go.mod go line
55
input.go:
66
# 0| [File] library-tests/semmle/go/PrintAst/input.go
7-
# 5| 0: [CommentGroup] comment group
7+
# 1| package: [Ident] main
8+
# 5| 1: [CommentGroup] comment group
89
# 5| 0: [SlashSlashComment] comment
9-
# 7| 1: [CommentGroup] comment group
10+
# 7| 2: [CommentGroup] comment group
1011
# 7| 0: [SlashSlashComment] comment
11-
# 9| 2: [DocComment] comment group
12+
# 9| 3: [DocComment] comment group
1213
# 9| 0: [SlashSlashComment] comment
13-
# 17| 3: [CommentGroup] comment group
14+
# 17| 4: [CommentGroup] comment group
1415
# 17| 0: [SlashSlashComment] comment
15-
# 45| 4: [DocComment] comment group
16+
# 45| 5: [DocComment] comment group
1617
# 45| 0: [SlashSlashComment] comment
17-
# 64| 5: [DocComment] comment group
18+
# 64| 6: [DocComment] comment group
1819
# 64| 0: [SlashSlashComment] comment
19-
# 74| 6: [DocComment] comment group
20+
# 74| 7: [DocComment] comment group
2021
# 74| 0: [SlashSlashComment] comment
21-
# 111| 7: [DocComment] comment group
22+
# 111| 8: [DocComment] comment group
2223
# 111| 0: [SlashSlashComment] comment
23-
# 127| 8: [DocComment] comment group
24+
# 127| 9: [DocComment] comment group
2425
# 127| 0: [SlashSlashComment] comment
25-
# 132| 9: [DocComment] comment group
26+
# 132| 10: [DocComment] comment group
2627
# 132| 0: [SlashSlashComment] comment
27-
# 3| 10: [ImportDecl] import declaration
28+
# 3| 11: [ImportDecl] import declaration
2829
# 3| 0: [ImportSpec] import specifier
2930
# 3| 0: [StringLit] "fmt"
30-
# 1| 18: [Ident] main
3131
other.go:
3232
# 0| [File] library-tests/semmle/go/PrintAst/other.go
33-
# 8| 3: [FuncDecl] function declaration
33+
# 1| package: [Ident] main
34+
# 8| 1: [FuncDecl] function declaration
3435
# 8| 0: [FunctionName, Ident] hasNested
3536
# 8| Type = func()
3637
# 8| 1: [FuncTypeExpr] function type
@@ -55,7 +56,7 @@ other.go:
5556
# 11| Type = int
5657
# 11| 0: [Ident, VariableName] myNested
5758
# 11| Type = func() int
58-
# 15| 4: [VarDecl] variable declaration
59+
# 15| 2: [VarDecl] variable declaration
5960
# 15| 0: [ValueSpec] value declaration specifier
6061
# 15| 0: [Ident, VariableName] x
6162
# 15| Type = int
@@ -64,4 +65,3 @@ other.go:
6465
# 15| 2: [IntLit] 0
6566
# 15| Type = int
6667
# 15| Value = [IntLit] 0
67-
# 1| 5: [Ident] main

0 commit comments

Comments
 (0)