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

Commit c30d198

Browse files
committed
Switch to using top-level function declarations to filter PrintAst
This means it's no longer possible to ask for the AST of a function literal, but this is hopefully a niche use-case that we can add if and when there is demand.
1 parent 09990f9 commit c30d198

13 files changed

+211
-51
lines changed

ql/src/printAst.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ external string selectedSourceFile();
2020
* Hook to customize the functions printed by this query.
2121
*/
2222
class Cfg extends PrintAstConfiguration {
23-
override predicate shouldPrintFunction(FuncDef func) { shouldPrintFile(func.getFile()) }
23+
override predicate shouldPrintFunction(FuncDecl func) { shouldPrintFile(func.getFile()) }
2424

2525
override predicate shouldPrintFile(File file) { file = getEncodedFile(selectedSourceFile()) }
2626

ql/src/semmle/go/PrintAst.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import PrintAst
1212
* Hook to customize the functions printed by this query.
1313
*/
1414
class Cfg extends PrintAstConfiguration {
15-
override predicate shouldPrintFunction(FuncDef func) { any() }
15+
override predicate shouldPrintFunction(FuncDecl func) { any() }
1616

1717
override predicate shouldPrintFile(File file) { any() }
1818

ql/src/semmle/go/PrintAst.qll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PrintAstConfiguration extends string {
2121
* Holds if the AST for `func` should be printed. By default, holds for all
2222
* functions.
2323
*/
24-
predicate shouldPrintFunction(FuncDef func) { any() }
24+
predicate shouldPrintFunction(FuncDecl func) { any() }
2525

2626
/**
2727
* Holds if the AST for `file` should be printed. By default, holds for all
@@ -48,10 +48,7 @@ private predicate shouldPrintComments(File file) {
4848
exists(PrintAstConfiguration config | config.shouldPrintComments(file))
4949
}
5050

51-
private FuncDef getEnclosingFunction(AstNode n) {
52-
result = n or
53-
result = n.getEnclosingFunction()
54-
}
51+
private FuncDecl getEnclosingFunctionDecl(AstNode n) { result = n.getParent*() }
5552

5653
/**
5754
* An AST node that should be printed.
@@ -60,7 +57,7 @@ private newtype TPrintAstNode =
6057
TAstNode(AstNode ast) {
6158
shouldPrintFile(ast.getFile()) and
6259
// Do print ast nodes without an enclosing function, e.g. file headers, that are not otherwise excluded
63-
forall(FuncDef f | f = getEnclosingFunction(ast) | shouldPrintFunction(f)) and
60+
forall(FuncDecl f | f = getEnclosingFunctionDecl(ast) | shouldPrintFunction(f)) and
6461
(
6562
shouldPrintComments(ast.getFile())
6663
or

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,41 @@ other.go:
1515
# 6| Type = func()
1616
# 6| 1: [FuncTypeExpr] function type
1717
# 6| 2: [BlockStmt] block statement
18-
# 8| 3: [VarDecl] variable declaration
19-
# 8| 0: [ValueSpec] value declaration specifier
20-
# 8| 0: [Ident, VariableName] x
21-
# 8| Type = int
22-
# 8| 1: [Ident, TypeName] int
23-
# 8| Type = int
24-
# 8| 2: [IntLit] 0
25-
# 8| Type = int
26-
# 8| Value = [IntLit] 0
27-
# 1| 4: [Ident] main
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
2853
go.mod:
2954
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
3055
# 1| 0: [GoModModuleLine] go.mod module line

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,41 @@ other.go:
1515
# 6| Type = func()
1616
# 6| 1: [FuncTypeExpr] function type
1717
# 6| 2: [BlockStmt] block statement
18-
# 8| 3: [VarDecl] variable declaration
19-
# 8| 0: [ValueSpec] value declaration specifier
20-
# 8| 0: [Ident, VariableName] x
21-
# 8| Type = int
22-
# 8| 1: [Ident, TypeName] int
23-
# 8| Type = int
24-
# 8| 2: [IntLit] 0
25-
# 8| Type = int
26-
# 8| Value = [IntLit] 0
27-
# 1| 4: [Ident] main
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
2853
go.mod:
2954
# 0| [GoModFile] library-tests/semmle/go/PrintAst/go.mod
3055
# 1| 0: [GoModModuleLine] go.mod module line

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import go
66
import semmle.go.PrintAst
77

88
class Cfg extends PrintAstConfiguration {
9-
override predicate shouldPrintFunction(FuncDef func) { any() }
9+
override predicate shouldPrintFunction(FuncDecl func) { any() }
1010

1111
override predicate shouldPrintFile(File file) { any() }
1212

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
other.go:
2+
# 0| [File] library-tests/semmle/go/PrintAst/other.go
3+
# 8| 3: [FuncDecl] function declaration
4+
# 8| 0: [FunctionName, Ident] hasNested
5+
# 8| Type = func()
6+
# 8| 1: [FuncTypeExpr] function type
7+
# 8| 2: [BlockStmt] block statement
8+
# 10| 0: [DefineStmt] ... := ...
9+
# 10| 0: [Ident, VariableName] myNested
10+
# 10| Type = func() int
11+
# 10| 1: [FuncLit] function literal
12+
# 10| Type = func() int
13+
# 10| 0: [FuncTypeExpr] function type
14+
# 10| Type = func() int
15+
# 10| 0: [ResultVariableDecl] result variable declaration
16+
# 10| 0: [Ident, TypeName] int
17+
# 10| Type = int
18+
# 10| 1: [BlockStmt] block statement
19+
# 10| 0: [ReturnStmt] return statement
20+
# 10| 0: [IntLit] 1
21+
# 10| Type = int
22+
# 10| Value = [IntLit] 1
23+
# 11| 1: [ExprStmt] expression statement
24+
# 11| 0: [CallExpr] call to myNested
25+
# 11| Type = int
26+
# 11| 0: [Ident, VariableName] myNested
27+
# 11| Type = func() int
28+
# 15| 4: [VarDecl] variable declaration
29+
# 15| 0: [ValueSpec] value declaration specifier
30+
# 15| 0: [Ident, VariableName] x
31+
# 15| Type = int
32+
# 15| 1: [Ident, TypeName] int
33+
# 15| Type = int
34+
# 15| 2: [IntLit] 0
35+
# 15| Type = int
36+
# 15| Value = [IntLit] 0
37+
# 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
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @kind graph
3+
*/
4+
5+
import go
6+
import semmle.go.PrintAst
7+
8+
class Cfg extends PrintAstConfiguration {
9+
override predicate shouldPrintFunction(FuncDecl func) { func.getName() = "hasNested" }
10+
11+
override predicate shouldPrintFile(File file) { any() }
12+
13+
override predicate shouldPrintComments(File file) { any() }
14+
}

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,38 @@ other.go:
1515
# 6| Type = func()
1616
# 6| 1: [FuncTypeExpr] function type
1717
# 6| 2: [BlockStmt] block statement
18-
# 8| 3: [VarDecl] variable declaration
19-
# 8| 0: [ValueSpec] value declaration specifier
20-
# 8| 0: [Ident, VariableName] x
21-
# 8| Type = int
22-
# 8| 1: [Ident, TypeName] int
23-
# 8| Type = int
24-
# 8| 2: [IntLit] 0
25-
# 8| Type = int
26-
# 8| Value = [IntLit] 0
27-
# 1| 4: [Ident] main
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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import go
66
import semmle.go.PrintAst
77

88
class Cfg extends PrintAstConfiguration {
9-
override predicate shouldPrintFunction(FuncDef func) { any() }
9+
override predicate shouldPrintFunction(FuncDecl func) { any() }
1010

1111
override predicate shouldPrintFile(File file) { file.getBaseName() = "other.go" }
1212
}

0 commit comments

Comments
 (0)