Skip to content

Go: Expose whether functions are variadic in their pp() output #17360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions go/ql/lib/semmle/go/Types.qll
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,19 @@ class SignatureType extends @signaturetype, CompositeType {
language[monotonicAggregates]
override string pp() {
result =
"func(" + concat(int i, Type tp | tp = this.getParameterType(i) | tp.pp(), ", " order by i) +
") " + concat(int i, Type tp | tp = this.getResultType(i) | tp.pp(), ", " order by i)
"func(" +
concat(int i, Type tp, string prefix |
if i = this.getNumParameter() - 1 and this.isVariadic()
then
tp = this.getParameterType(i).(SliceType).getElementType() and
prefix = "..."
else (
tp = this.getParameterType(i) and
prefix = ""
)
|
prefix + tp.pp(), ", " order by i
) + ") " + concat(int i, Type tp | tp = this.getResultType(i) | tp.pp(), ", " order by i)
}

override string toString() { result = "signature type" }
Expand Down
40 changes: 20 additions & 20 deletions go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ input.go:
# 20| 0: [CallExpr] call to Println
# 20| Type = (int, error)
# 20| 0: [FunctionName, SelectorExpr] selection of Println
# 20| Type = func([]interface { }) int, error
# 20| Type = func(...interface { }) int, error
# 20| 0: [Ident, PackageName] fmt
# 20| 1: [FunctionName, Ident] Println
# 20| Type = func([]interface { }) int, error
# 20| Type = func(...interface { }) int, error
# 20| 1: [StringLit] "Hi"
# 20| Type = string
# 20| Value = [StringLit] Hi
Expand Down Expand Up @@ -203,10 +203,10 @@ input.go:
# 52| 0: [CallExpr] call to Println
# 52| Type = (int, error)
# 52| 0: [FunctionName, SelectorExpr] selection of Println
# 52| Type = func([]interface { }) int, error
# 52| Type = func(...interface { }) int, error
# 52| 0: [Ident, PackageName] fmt
# 52| 1: [FunctionName, Ident] Println
# 52| Type = func([]interface { }) int, error
# 52| Type = func(...interface { }) int, error
# 52| 1: [StringLit] "Heard from ch1"
# 52| Type = string
# 52| Value = [StringLit] Heard from ch1
Expand All @@ -229,31 +229,31 @@ input.go:
# 54| 0: [CallExpr] call to Println
# 54| Type = (int, error)
# 54| 0: [FunctionName, SelectorExpr] selection of Println
# 54| Type = func([]interface { }) int, error
# 54| Type = func(...interface { }) int, error
# 54| 0: [Ident, PackageName] fmt
# 54| 1: [FunctionName, Ident] Println
# 54| Type = func([]interface { }) int, error
# 54| Type = func(...interface { }) int, error
# 54| 1: [Ident, VariableName] a
# 54| Type = [1]float32
# 55| 2: [ExprStmt] expression statement
# 55| 0: [CallExpr] call to Println
# 55| Type = (int, error)
# 55| 0: [FunctionName, SelectorExpr] selection of Println
# 55| Type = func([]interface { }) int, error
# 55| Type = func(...interface { }) int, error
# 55| 0: [Ident, PackageName] fmt
# 55| 1: [FunctionName, Ident] Println
# 55| Type = func([]interface { }) int, error
# 55| Type = func(...interface { }) int, error
# 55| 1: [Ident, VariableName] w
# 55| Type = bool
# 56| 2: [CommClause] comm clause
# 57| 0: [ExprStmt] expression statement
# 57| 0: [CallExpr] call to Println
# 57| Type = (int, error)
# 57| 0: [FunctionName, SelectorExpr] selection of Println
# 57| Type = func([]interface { }) int, error
# 57| Type = func(...interface { }) int, error
# 57| 0: [Ident, PackageName] fmt
# 57| 1: [FunctionName, Ident] Println
# 57| Type = func([]interface { }) int, error
# 57| Type = func(...interface { }) int, error
# 58| 3: [CommClause] comm clause
# 58| 0: [SendStmt] send statement
# 58| 0: [Ident, VariableName] ch1
Expand Down Expand Up @@ -297,10 +297,10 @@ input.go:
# 67| 0: [CallExpr] call to Println
# 67| Type = (int, error)
# 67| 0: [FunctionName, SelectorExpr] selection of Println
# 67| Type = func([]interface { }) int, error
# 67| Type = func(...interface { }) int, error
# 67| 0: [Ident, PackageName] fmt
# 67| 1: [FunctionName, Ident] Println
# 67| Type = func([]interface { }) int, error
# 67| Type = func(...interface { }) int, error
# 67| 1: [Ident, VariableName] x
# 67| Type = int
# 68| 2: [BlockStmt] block statement
Expand All @@ -316,10 +316,10 @@ input.go:
# 69| 0: [CallExpr] call to Println
# 69| Type = (int, error)
# 69| 0: [FunctionName, SelectorExpr] selection of Println
# 69| Type = func([]interface { }) int, error
# 69| Type = func(...interface { }) int, error
# 69| 0: [Ident, PackageName] fmt
# 69| 1: [FunctionName, Ident] Println
# 69| Type = func([]interface { }) int, error
# 69| Type = func(...interface { }) int, error
# 69| 1: [MinusExpr] -...
# 69| Type = int
# 69| 0: [Ident, VariableName] x
Expand Down Expand Up @@ -474,10 +474,10 @@ input.go:
# 115| 0: [CallExpr] call to Println
# 115| Type = (int, error)
# 115| 0: [FunctionName, SelectorExpr] selection of Println
# 115| Type = func([]interface { }) int, error
# 115| Type = func(...interface { }) int, error
# 115| 0: [Ident, PackageName] fmt
# 115| 1: [FunctionName, Ident] Println
# 115| Type = func([]interface { }) int, error
# 115| Type = func(...interface { }) int, error
# 115| 1: [Ident, VariableName] y
# 115| Type = interface { }
# 116| 1: [CaseClause] case clause
Expand Down Expand Up @@ -566,10 +566,10 @@ input.go:
# 138| 0: [CallExpr] call to Print
# 138| Type = (int, error)
# 138| 0: [FunctionName, SelectorExpr] selection of Print
# 138| Type = func([]interface { }) int, error
# 138| Type = func(...interface { }) int, error
# 138| 0: [Ident, PackageName] fmt
# 138| 1: [FunctionName, Ident] Print
# 138| Type = func([]interface { }) int, error
# 138| Type = func(...interface { }) int, error
# 138| 1: [Ident, VariableName] x
# 138| Type = int
# 141| 1: [RangeStmt] range statement
Expand All @@ -584,10 +584,10 @@ input.go:
# 142| 0: [CallExpr] call to Print
# 142| Type = (int, error)
# 142| 0: [FunctionName, SelectorExpr] selection of Print
# 142| Type = func([]interface { }) int, error
# 142| Type = func(...interface { }) int, error
# 142| 0: [Ident, PackageName] fmt
# 142| 1: [FunctionName, Ident] Print
# 142| Type = func([]interface { }) int, error
# 142| Type = func(...interface { }) int, error
# 142| 1: [Ident, VariableName] i
# 142| Type = int
# 142| 2: [Ident, VariableName] v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ input.go:
# 20| 0: [CallExpr] call to Println
# 20| Type = (int, error)
# 20| 0: [FunctionName, SelectorExpr] selection of Println
# 20| Type = func([]interface { }) int, error
# 20| Type = func(...interface { }) int, error
# 20| 0: [Ident, PackageName] fmt
# 20| 1: [FunctionName, Ident] Println
# 20| Type = func([]interface { }) int, error
# 20| Type = func(...interface { }) int, error
# 20| 1: [StringLit] "Hi"
# 20| Type = string
# 20| Value = [StringLit] Hi
Expand Down Expand Up @@ -183,10 +183,10 @@ input.go:
# 52| 0: [CallExpr] call to Println
# 52| Type = (int, error)
# 52| 0: [FunctionName, SelectorExpr] selection of Println
# 52| Type = func([]interface { }) int, error
# 52| Type = func(...interface { }) int, error
# 52| 0: [Ident, PackageName] fmt
# 52| 1: [FunctionName, Ident] Println
# 52| Type = func([]interface { }) int, error
# 52| Type = func(...interface { }) int, error
# 52| 1: [StringLit] "Heard from ch1"
# 52| Type = string
# 52| Value = [StringLit] Heard from ch1
Expand All @@ -209,31 +209,31 @@ input.go:
# 54| 0: [CallExpr] call to Println
# 54| Type = (int, error)
# 54| 0: [FunctionName, SelectorExpr] selection of Println
# 54| Type = func([]interface { }) int, error
# 54| Type = func(...interface { }) int, error
# 54| 0: [Ident, PackageName] fmt
# 54| 1: [FunctionName, Ident] Println
# 54| Type = func([]interface { }) int, error
# 54| Type = func(...interface { }) int, error
# 54| 1: [Ident, VariableName] a
# 54| Type = [1]float32
# 55| 2: [ExprStmt] expression statement
# 55| 0: [CallExpr] call to Println
# 55| Type = (int, error)
# 55| 0: [FunctionName, SelectorExpr] selection of Println
# 55| Type = func([]interface { }) int, error
# 55| Type = func(...interface { }) int, error
# 55| 0: [Ident, PackageName] fmt
# 55| 1: [FunctionName, Ident] Println
# 55| Type = func([]interface { }) int, error
# 55| Type = func(...interface { }) int, error
# 55| 1: [Ident, VariableName] w
# 55| Type = bool
# 56| 2: [CommClause] comm clause
# 57| 0: [ExprStmt] expression statement
# 57| 0: [CallExpr] call to Println
# 57| Type = (int, error)
# 57| 0: [FunctionName, SelectorExpr] selection of Println
# 57| Type = func([]interface { }) int, error
# 57| Type = func(...interface { }) int, error
# 57| 0: [Ident, PackageName] fmt
# 57| 1: [FunctionName, Ident] Println
# 57| Type = func([]interface { }) int, error
# 57| Type = func(...interface { }) int, error
# 58| 3: [CommClause] comm clause
# 58| 0: [SendStmt] send statement
# 58| 0: [Ident, VariableName] ch1
Expand Down Expand Up @@ -277,10 +277,10 @@ input.go:
# 67| 0: [CallExpr] call to Println
# 67| Type = (int, error)
# 67| 0: [FunctionName, SelectorExpr] selection of Println
# 67| Type = func([]interface { }) int, error
# 67| Type = func(...interface { }) int, error
# 67| 0: [Ident, PackageName] fmt
# 67| 1: [FunctionName, Ident] Println
# 67| Type = func([]interface { }) int, error
# 67| Type = func(...interface { }) int, error
# 67| 1: [Ident, VariableName] x
# 67| Type = int
# 68| 2: [BlockStmt] block statement
Expand All @@ -296,10 +296,10 @@ input.go:
# 69| 0: [CallExpr] call to Println
# 69| Type = (int, error)
# 69| 0: [FunctionName, SelectorExpr] selection of Println
# 69| Type = func([]interface { }) int, error
# 69| Type = func(...interface { }) int, error
# 69| 0: [Ident, PackageName] fmt
# 69| 1: [FunctionName, Ident] Println
# 69| Type = func([]interface { }) int, error
# 69| Type = func(...interface { }) int, error
# 69| 1: [MinusExpr] -...
# 69| Type = int
# 69| 0: [Ident, VariableName] x
Expand Down Expand Up @@ -454,10 +454,10 @@ input.go:
# 115| 0: [CallExpr] call to Println
# 115| Type = (int, error)
# 115| 0: [FunctionName, SelectorExpr] selection of Println
# 115| Type = func([]interface { }) int, error
# 115| Type = func(...interface { }) int, error
# 115| 0: [Ident, PackageName] fmt
# 115| 1: [FunctionName, Ident] Println
# 115| Type = func([]interface { }) int, error
# 115| Type = func(...interface { }) int, error
# 115| 1: [Ident, VariableName] y
# 115| Type = interface { }
# 116| 1: [CaseClause] case clause
Expand Down Expand Up @@ -546,10 +546,10 @@ input.go:
# 138| 0: [CallExpr] call to Print
# 138| Type = (int, error)
# 138| 0: [FunctionName, SelectorExpr] selection of Print
# 138| Type = func([]interface { }) int, error
# 138| Type = func(...interface { }) int, error
# 138| 0: [Ident, PackageName] fmt
# 138| 1: [FunctionName, Ident] Print
# 138| Type = func([]interface { }) int, error
# 138| Type = func(...interface { }) int, error
# 138| 1: [Ident, VariableName] x
# 138| Type = int
# 141| 1: [RangeStmt] range statement
Expand All @@ -564,10 +564,10 @@ input.go:
# 142| 0: [CallExpr] call to Print
# 142| Type = (int, error)
# 142| 0: [FunctionName, SelectorExpr] selection of Print
# 142| Type = func([]interface { }) int, error
# 142| Type = func(...interface { }) int, error
# 142| 0: [Ident, PackageName] fmt
# 142| 1: [FunctionName, Ident] Print
# 142| Type = func([]interface { }) int, error
# 142| Type = func(...interface { }) int, error
# 142| 1: [Ident, VariableName] i
# 142| Type = int
# 142| 2: [Ident, VariableName] v
Expand Down
Loading