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

Commit f8d141f

Browse files
committed
PrintAst: Sort root File nodes by relative path.
This should make graphtext output deterministic, rather than depending on the order the results interpretation step happens to see the nodes.
1 parent c30d198 commit f8d141f

File tree

5 files changed

+164
-151
lines changed

5 files changed

+164
-151
lines changed

ql/src/semmle/go/PrintAst.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ class ExprNode extends BaseAstNode {
181181
class FileNode extends BaseAstNode {
182182
override File ast;
183183

184+
private string getRelativePath() { result = ast.getRelativePath() }
185+
186+
private int getSortOrder() {
187+
rank[result](FileNode fn | any() | fn order by fn.getRelativePath()) = this
188+
}
189+
190+
override string getProperty(string key) {
191+
result = super.getProperty(key)
192+
or
193+
key = "semmle.order" and
194+
result = getSortOrder().toString()
195+
}
196+
184197
/**
185198
* Gets the string representation of this File. Note explicitly using a relative path
186199
* 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: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
1-
other.go:
2-
# 0| [File] library-tests/semmle/go/PrintAst/other.go
3-
# 3| 0: [FuncDecl] function declaration
4-
# 3| 0: [FunctionName, Ident] main
5-
# 3| Type = func()
6-
# 3| 1: [FuncTypeExpr] function type
7-
# 3| 2: [BlockStmt] block statement
8-
# 5| 1: [FuncDecl] function declaration
9-
# 5| 0: [FunctionName, Ident] f
10-
# 5| Type = func()
11-
# 5| 1: [FuncTypeExpr] function type
12-
# 5| 2: [BlockStmt] block statement
13-
# 6| 2: [FuncDecl] function declaration
14-
# 6| 0: [FunctionName, Ident] g
15-
# 6| Type = func()
16-
# 6| 1: [FuncTypeExpr] function type
17-
# 6| 2: [BlockStmt] block statement
18-
# 8| 3: [FuncDecl] function declaration
19-
# 8| 0: [FunctionName, Ident] hasNested
20-
# 8| Type = func()
21-
# 8| 1: [FuncTypeExpr] function type
22-
# 8| 2: [BlockStmt] block statement
23-
# 10| 0: [DefineStmt] ... := ...
24-
# 10| 0: [Ident, VariableName] myNested
25-
# 10| Type = func() int
26-
# 10| 1: [FuncLit] function literal
27-
# 10| Type = func() int
28-
# 10| 0: [FuncTypeExpr] function type
29-
# 10| Type = func() int
30-
# 10| 0: [ResultVariableDecl] result variable declaration
31-
# 10| 0: [Ident, TypeName] int
32-
# 10| Type = int
33-
# 10| 1: [BlockStmt] block statement
34-
# 10| 0: [ReturnStmt] return statement
35-
# 10| 0: [IntLit] 1
36-
# 10| Type = int
37-
# 10| Value = [IntLit] 1
38-
# 11| 1: [ExprStmt] expression statement
39-
# 11| 0: [CallExpr] call to myNested
40-
# 11| Type = int
41-
# 11| 0: [Ident, VariableName] myNested
42-
# 11| Type = func() int
43-
# 15| 4: [VarDecl] variable declaration
44-
# 15| 0: [ValueSpec] value declaration specifier
45-
# 15| 0: [Ident, VariableName] x
46-
# 15| Type = int
47-
# 15| 1: [Ident, TypeName] int
48-
# 15| Type = int
49-
# 15| 2: [IntLit] 0
50-
# 15| Type = int
51-
# 15| Value = [IntLit] 0
52-
# 1| 5: [Ident] main
531
go.mod:
542
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
553
# 1| 0: [GoModModuleLine] go.mod module line
@@ -648,3 +596,55 @@ input.go:
648596
# 145| Type = []int
649597
# 145| 1: [BlockStmt] block statement
650598
# 1| 18: [Ident] main
599+
other.go:
600+
# 0| [File] library-tests/semmle/go/PrintAst/other.go
601+
# 3| 0: [FuncDecl] function declaration
602+
# 3| 0: [FunctionName, Ident] main
603+
# 3| Type = func()
604+
# 3| 1: [FuncTypeExpr] function type
605+
# 3| 2: [BlockStmt] block statement
606+
# 5| 1: [FuncDecl] function declaration
607+
# 5| 0: [FunctionName, Ident] f
608+
# 5| Type = func()
609+
# 5| 1: [FuncTypeExpr] function type
610+
# 5| 2: [BlockStmt] block statement
611+
# 6| 2: [FuncDecl] function declaration
612+
# 6| 0: [FunctionName, Ident] g
613+
# 6| Type = func()
614+
# 6| 1: [FuncTypeExpr] function type
615+
# 6| 2: [BlockStmt] block statement
616+
# 8| 3: [FuncDecl] function declaration
617+
# 8| 0: [FunctionName, Ident] hasNested
618+
# 8| Type = func()
619+
# 8| 1: [FuncTypeExpr] function type
620+
# 8| 2: [BlockStmt] block statement
621+
# 10| 0: [DefineStmt] ... := ...
622+
# 10| 0: [Ident, VariableName] myNested
623+
# 10| Type = func() int
624+
# 10| 1: [FuncLit] function literal
625+
# 10| Type = func() int
626+
# 10| 0: [FuncTypeExpr] function type
627+
# 10| Type = func() int
628+
# 10| 0: [ResultVariableDecl] result variable declaration
629+
# 10| 0: [Ident, TypeName] int
630+
# 10| Type = int
631+
# 10| 1: [BlockStmt] block statement
632+
# 10| 0: [ReturnStmt] return statement
633+
# 10| 0: [IntLit] 1
634+
# 10| Type = int
635+
# 10| Value = [IntLit] 1
636+
# 11| 1: [ExprStmt] expression statement
637+
# 11| 0: [CallExpr] call to myNested
638+
# 11| Type = int
639+
# 11| 0: [Ident, VariableName] myNested
640+
# 11| Type = func() int
641+
# 15| 4: [VarDecl] variable declaration
642+
# 15| 0: [ValueSpec] value declaration specifier
643+
# 15| 0: [Ident, VariableName] x
644+
# 15| Type = int
645+
# 15| 1: [Ident, TypeName] int
646+
# 15| Type = int
647+
# 15| 2: [IntLit] 0
648+
# 15| Type = int
649+
# 15| Value = [IntLit] 0
650+
# 1| 5: [Ident] main

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

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,3 @@
1-
other.go:
2-
# 0| [File] library-tests/semmle/go/PrintAst/other.go
3-
# 3| 0: [FuncDecl] function declaration
4-
# 3| 0: [FunctionName, Ident] main
5-
# 3| Type = func()
6-
# 3| 1: [FuncTypeExpr] function type
7-
# 3| 2: [BlockStmt] block statement
8-
# 5| 1: [FuncDecl] function declaration
9-
# 5| 0: [FunctionName, Ident] f
10-
# 5| Type = func()
11-
# 5| 1: [FuncTypeExpr] function type
12-
# 5| 2: [BlockStmt] block statement
13-
# 6| 2: [FuncDecl] function declaration
14-
# 6| 0: [FunctionName, Ident] g
15-
# 6| Type = func()
16-
# 6| 1: [FuncTypeExpr] function type
17-
# 6| 2: [BlockStmt] block statement
18-
# 8| 3: [FuncDecl] function declaration
19-
# 8| 0: [FunctionName, Ident] hasNested
20-
# 8| Type = func()
21-
# 8| 1: [FuncTypeExpr] function type
22-
# 8| 2: [BlockStmt] block statement
23-
# 10| 0: [DefineStmt] ... := ...
24-
# 10| 0: [Ident, VariableName] myNested
25-
# 10| Type = func() int
26-
# 10| 1: [FuncLit] function literal
27-
# 10| Type = func() int
28-
# 10| 0: [FuncTypeExpr] function type
29-
# 10| Type = func() int
30-
# 10| 0: [ResultVariableDecl] result variable declaration
31-
# 10| 0: [Ident, TypeName] int
32-
# 10| Type = int
33-
# 10| 1: [BlockStmt] block statement
34-
# 10| 0: [ReturnStmt] return statement
35-
# 10| 0: [IntLit] 1
36-
# 10| Type = int
37-
# 10| Value = [IntLit] 1
38-
# 11| 1: [ExprStmt] expression statement
39-
# 11| 0: [CallExpr] call to myNested
40-
# 11| Type = int
41-
# 11| 0: [Ident, VariableName] myNested
42-
# 11| Type = func() int
43-
# 15| 4: [VarDecl] variable declaration
44-
# 15| 0: [ValueSpec] value declaration specifier
45-
# 15| 0: [Ident, VariableName] x
46-
# 15| Type = int
47-
# 15| 1: [Ident, TypeName] int
48-
# 15| Type = int
49-
# 15| 2: [IntLit] 0
50-
# 15| Type = int
51-
# 15| Value = [IntLit] 0
52-
# 1| 5: [Ident] main
531
go.mod:
542
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
553
# 1| 0: [GoModModuleLine] go.mod module line
@@ -628,3 +576,55 @@ input.go:
628576
# 145| Type = []int
629577
# 145| 1: [BlockStmt] block statement
630578
# 1| 18: [Ident] main
579+
other.go:
580+
# 0| [File] library-tests/semmle/go/PrintAst/other.go
581+
# 3| 0: [FuncDecl] function declaration
582+
# 3| 0: [FunctionName, Ident] main
583+
# 3| Type = func()
584+
# 3| 1: [FuncTypeExpr] function type
585+
# 3| 2: [BlockStmt] block statement
586+
# 5| 1: [FuncDecl] function declaration
587+
# 5| 0: [FunctionName, Ident] f
588+
# 5| Type = func()
589+
# 5| 1: [FuncTypeExpr] function type
590+
# 5| 2: [BlockStmt] block statement
591+
# 6| 2: [FuncDecl] function declaration
592+
# 6| 0: [FunctionName, Ident] g
593+
# 6| Type = func()
594+
# 6| 1: [FuncTypeExpr] function type
595+
# 6| 2: [BlockStmt] block statement
596+
# 8| 3: [FuncDecl] function declaration
597+
# 8| 0: [FunctionName, Ident] hasNested
598+
# 8| Type = func()
599+
# 8| 1: [FuncTypeExpr] function type
600+
# 8| 2: [BlockStmt] block statement
601+
# 10| 0: [DefineStmt] ... := ...
602+
# 10| 0: [Ident, VariableName] myNested
603+
# 10| Type = func() int
604+
# 10| 1: [FuncLit] function literal
605+
# 10| Type = func() int
606+
# 10| 0: [FuncTypeExpr] function type
607+
# 10| Type = func() int
608+
# 10| 0: [ResultVariableDecl] result variable declaration
609+
# 10| 0: [Ident, TypeName] int
610+
# 10| Type = int
611+
# 10| 1: [BlockStmt] block statement
612+
# 10| 0: [ReturnStmt] return statement
613+
# 10| 0: [IntLit] 1
614+
# 10| Type = int
615+
# 10| Value = [IntLit] 1
616+
# 11| 1: [ExprStmt] expression statement
617+
# 11| 0: [CallExpr] call to myNested
618+
# 11| Type = int
619+
# 11| 0: [Ident, VariableName] myNested
620+
# 11| Type = func() int
621+
# 15| 4: [VarDecl] variable declaration
622+
# 15| 0: [ValueSpec] value declaration specifier
623+
# 15| 0: [Ident, VariableName] x
624+
# 15| Type = int
625+
# 15| 1: [Ident, TypeName] int
626+
# 15| Type = int
627+
# 15| 2: [IntLit] 0
628+
# 15| Type = int
629+
# 15| Value = [IntLit] 0
630+
# 1| 5: [Ident] main
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
go.mod:
2+
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
3+
# 1| 0: [GoModModuleLine] go.mod module line
4+
# 3| 1: [GoModGoLine] go.mod go line
5+
input.go:
6+
# 0| [File] library-tests/semmle/go/PrintAst/input.go
7+
# 5| 0: [CommentGroup] comment group
8+
# 5| 0: [SlashSlashComment] comment
9+
# 7| 1: [CommentGroup] comment group
10+
# 7| 0: [SlashSlashComment] comment
11+
# 9| 2: [DocComment] comment group
12+
# 9| 0: [SlashSlashComment] comment
13+
# 17| 3: [CommentGroup] comment group
14+
# 17| 0: [SlashSlashComment] comment
15+
# 45| 4: [DocComment] comment group
16+
# 45| 0: [SlashSlashComment] comment
17+
# 64| 5: [DocComment] comment group
18+
# 64| 0: [SlashSlashComment] comment
19+
# 74| 6: [DocComment] comment group
20+
# 74| 0: [SlashSlashComment] comment
21+
# 111| 7: [DocComment] comment group
22+
# 111| 0: [SlashSlashComment] comment
23+
# 127| 8: [DocComment] comment group
24+
# 127| 0: [SlashSlashComment] comment
25+
# 132| 9: [DocComment] comment group
26+
# 132| 0: [SlashSlashComment] comment
27+
# 3| 10: [ImportDecl] import declaration
28+
# 3| 0: [ImportSpec] import specifier
29+
# 3| 0: [StringLit] "fmt"
30+
# 1| 18: [Ident] main
131
other.go:
232
# 0| [File] library-tests/semmle/go/PrintAst/other.go
333
# 8| 3: [FuncDecl] function declaration
@@ -35,33 +65,3 @@ other.go:
3565
# 15| Type = int
3666
# 15| Value = [IntLit] 0
3767
# 1| 5: [Ident] main
38-
go.mod:
39-
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
40-
# 1| 0: [GoModModuleLine] go.mod module line
41-
# 3| 1: [GoModGoLine] go.mod go line
42-
input.go:
43-
# 0| [File] library-tests/semmle/go/PrintAst/input.go
44-
# 5| 0: [CommentGroup] comment group
45-
# 5| 0: [SlashSlashComment] comment
46-
# 7| 1: [CommentGroup] comment group
47-
# 7| 0: [SlashSlashComment] comment
48-
# 9| 2: [DocComment] comment group
49-
# 9| 0: [SlashSlashComment] comment
50-
# 17| 3: [CommentGroup] comment group
51-
# 17| 0: [SlashSlashComment] comment
52-
# 45| 4: [DocComment] comment group
53-
# 45| 0: [SlashSlashComment] comment
54-
# 64| 5: [DocComment] comment group
55-
# 64| 0: [SlashSlashComment] comment
56-
# 74| 6: [DocComment] comment group
57-
# 74| 0: [SlashSlashComment] comment
58-
# 111| 7: [DocComment] comment group
59-
# 111| 0: [SlashSlashComment] comment
60-
# 127| 8: [DocComment] comment group
61-
# 127| 0: [SlashSlashComment] comment
62-
# 132| 9: [DocComment] comment group
63-
# 132| 0: [SlashSlashComment] comment
64-
# 3| 10: [ImportDecl] import declaration
65-
# 3| 0: [ImportSpec] import specifier
66-
# 3| 0: [StringLit] "fmt"
67-
# 1| 18: [Ident] main

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
other.go:
2-
# 0| [File] library-tests/semmle/go/PrintAst/other.go
3-
# 6| 2: [FuncDecl] function declaration
4-
# 6| 0: [FunctionName, Ident] g
5-
# 6| Type = func()
6-
# 6| 1: [FuncTypeExpr] function type
7-
# 6| 2: [BlockStmt] block statement
8-
# 15| 4: [VarDecl] variable declaration
9-
# 15| 0: [ValueSpec] value declaration specifier
10-
# 15| 0: [Ident, VariableName] x
11-
# 15| Type = int
12-
# 15| 1: [Ident, TypeName] int
13-
# 15| Type = int
14-
# 15| 2: [IntLit] 0
15-
# 15| Type = int
16-
# 15| Value = [IntLit] 0
17-
# 1| 5: [Ident] main
181
go.mod:
192
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
203
# 1| 0: [GoModModuleLine] go.mod module line
@@ -45,3 +28,20 @@ input.go:
4528
# 3| 0: [ImportSpec] import specifier
4629
# 3| 0: [StringLit] "fmt"
4730
# 1| 18: [Ident] main
31+
other.go:
32+
# 0| [File] library-tests/semmle/go/PrintAst/other.go
33+
# 6| 2: [FuncDecl] function declaration
34+
# 6| 0: [FunctionName, Ident] g
35+
# 6| Type = func()
36+
# 6| 1: [FuncTypeExpr] function type
37+
# 6| 2: [BlockStmt] block statement
38+
# 15| 4: [VarDecl] variable declaration
39+
# 15| 0: [ValueSpec] value declaration specifier
40+
# 15| 0: [Ident, VariableName] x
41+
# 15| Type = int
42+
# 15| 1: [Ident, TypeName] int
43+
# 15| Type = int
44+
# 15| 2: [IntLit] 0
45+
# 15| Type = int
46+
# 15| Value = [IntLit] 0
47+
# 1| 5: [Ident] main

0 commit comments

Comments
 (0)