Skip to content

Commit 590e93d

Browse files
authored
Merge pull request #17360 from smowton/smowton/admin/go-function-pretty-printer
Go: Expose whether functions are variadic in their pp() output
2 parents 4dbb15d + d673d24 commit 590e93d

File tree

3 files changed

+53
-42
lines changed

3 files changed

+53
-42
lines changed

go/ql/lib/semmle/go/Types.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,19 @@ class SignatureType extends @signaturetype, CompositeType {
922922
language[monotonicAggregates]
923923
override string pp() {
924924
result =
925-
"func(" + concat(int i, Type tp | tp = this.getParameterType(i) | tp.pp(), ", " order by i) +
926-
") " + concat(int i, Type tp | tp = this.getResultType(i) | tp.pp(), ", " order by i)
925+
"func(" +
926+
concat(int i, Type tp, string prefix |
927+
if i = this.getNumParameter() - 1 and this.isVariadic()
928+
then
929+
tp = this.getParameterType(i).(SliceType).getElementType() and
930+
prefix = "..."
931+
else (
932+
tp = this.getParameterType(i) and
933+
prefix = ""
934+
)
935+
|
936+
prefix + tp.pp(), ", " order by i
937+
) + ") " + concat(int i, Type tp | tp = this.getResultType(i) | tp.pp(), ", " order by i)
927938
}
928939

929940
override string toString() { result = "signature type" }

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ input.go:
5353
# 20| 0: [CallExpr] call to Println
5454
# 20| Type = (int, error)
5555
# 20| 0: [FunctionName, SelectorExpr] selection of Println
56-
# 20| Type = func([]interface { }) int, error
56+
# 20| Type = func(...interface { }) int, error
5757
# 20| 0: [Ident, PackageName] fmt
5858
# 20| 1: [FunctionName, Ident] Println
59-
# 20| Type = func([]interface { }) int, error
59+
# 20| Type = func(...interface { }) int, error
6060
# 20| 1: [StringLit] "Hi"
6161
# 20| Type = string
6262
# 20| Value = [StringLit] Hi
@@ -203,10 +203,10 @@ input.go:
203203
# 52| 0: [CallExpr] call to Println
204204
# 52| Type = (int, error)
205205
# 52| 0: [FunctionName, SelectorExpr] selection of Println
206-
# 52| Type = func([]interface { }) int, error
206+
# 52| Type = func(...interface { }) int, error
207207
# 52| 0: [Ident, PackageName] fmt
208208
# 52| 1: [FunctionName, Ident] Println
209-
# 52| Type = func([]interface { }) int, error
209+
# 52| Type = func(...interface { }) int, error
210210
# 52| 1: [StringLit] "Heard from ch1"
211211
# 52| Type = string
212212
# 52| Value = [StringLit] Heard from ch1
@@ -229,31 +229,31 @@ input.go:
229229
# 54| 0: [CallExpr] call to Println
230230
# 54| Type = (int, error)
231231
# 54| 0: [FunctionName, SelectorExpr] selection of Println
232-
# 54| Type = func([]interface { }) int, error
232+
# 54| Type = func(...interface { }) int, error
233233
# 54| 0: [Ident, PackageName] fmt
234234
# 54| 1: [FunctionName, Ident] Println
235-
# 54| Type = func([]interface { }) int, error
235+
# 54| Type = func(...interface { }) int, error
236236
# 54| 1: [Ident, VariableName] a
237237
# 54| Type = [1]float32
238238
# 55| 2: [ExprStmt] expression statement
239239
# 55| 0: [CallExpr] call to Println
240240
# 55| Type = (int, error)
241241
# 55| 0: [FunctionName, SelectorExpr] selection of Println
242-
# 55| Type = func([]interface { }) int, error
242+
# 55| Type = func(...interface { }) int, error
243243
# 55| 0: [Ident, PackageName] fmt
244244
# 55| 1: [FunctionName, Ident] Println
245-
# 55| Type = func([]interface { }) int, error
245+
# 55| Type = func(...interface { }) int, error
246246
# 55| 1: [Ident, VariableName] w
247247
# 55| Type = bool
248248
# 56| 2: [CommClause] comm clause
249249
# 57| 0: [ExprStmt] expression statement
250250
# 57| 0: [CallExpr] call to Println
251251
# 57| Type = (int, error)
252252
# 57| 0: [FunctionName, SelectorExpr] selection of Println
253-
# 57| Type = func([]interface { }) int, error
253+
# 57| Type = func(...interface { }) int, error
254254
# 57| 0: [Ident, PackageName] fmt
255255
# 57| 1: [FunctionName, Ident] Println
256-
# 57| Type = func([]interface { }) int, error
256+
# 57| Type = func(...interface { }) int, error
257257
# 58| 3: [CommClause] comm clause
258258
# 58| 0: [SendStmt] send statement
259259
# 58| 0: [Ident, VariableName] ch1
@@ -297,10 +297,10 @@ input.go:
297297
# 67| 0: [CallExpr] call to Println
298298
# 67| Type = (int, error)
299299
# 67| 0: [FunctionName, SelectorExpr] selection of Println
300-
# 67| Type = func([]interface { }) int, error
300+
# 67| Type = func(...interface { }) int, error
301301
# 67| 0: [Ident, PackageName] fmt
302302
# 67| 1: [FunctionName, Ident] Println
303-
# 67| Type = func([]interface { }) int, error
303+
# 67| Type = func(...interface { }) int, error
304304
# 67| 1: [Ident, VariableName] x
305305
# 67| Type = int
306306
# 68| 2: [BlockStmt] block statement
@@ -316,10 +316,10 @@ input.go:
316316
# 69| 0: [CallExpr] call to Println
317317
# 69| Type = (int, error)
318318
# 69| 0: [FunctionName, SelectorExpr] selection of Println
319-
# 69| Type = func([]interface { }) int, error
319+
# 69| Type = func(...interface { }) int, error
320320
# 69| 0: [Ident, PackageName] fmt
321321
# 69| 1: [FunctionName, Ident] Println
322-
# 69| Type = func([]interface { }) int, error
322+
# 69| Type = func(...interface { }) int, error
323323
# 69| 1: [MinusExpr] -...
324324
# 69| Type = int
325325
# 69| 0: [Ident, VariableName] x
@@ -474,10 +474,10 @@ input.go:
474474
# 115| 0: [CallExpr] call to Println
475475
# 115| Type = (int, error)
476476
# 115| 0: [FunctionName, SelectorExpr] selection of Println
477-
# 115| Type = func([]interface { }) int, error
477+
# 115| Type = func(...interface { }) int, error
478478
# 115| 0: [Ident, PackageName] fmt
479479
# 115| 1: [FunctionName, Ident] Println
480-
# 115| Type = func([]interface { }) int, error
480+
# 115| Type = func(...interface { }) int, error
481481
# 115| 1: [Ident, VariableName] y
482482
# 115| Type = interface { }
483483
# 116| 1: [CaseClause] case clause
@@ -566,10 +566,10 @@ input.go:
566566
# 138| 0: [CallExpr] call to Print
567567
# 138| Type = (int, error)
568568
# 138| 0: [FunctionName, SelectorExpr] selection of Print
569-
# 138| Type = func([]interface { }) int, error
569+
# 138| Type = func(...interface { }) int, error
570570
# 138| 0: [Ident, PackageName] fmt
571571
# 138| 1: [FunctionName, Ident] Print
572-
# 138| Type = func([]interface { }) int, error
572+
# 138| Type = func(...interface { }) int, error
573573
# 138| 1: [Ident, VariableName] x
574574
# 138| Type = int
575575
# 141| 1: [RangeStmt] range statement
@@ -584,10 +584,10 @@ input.go:
584584
# 142| 0: [CallExpr] call to Print
585585
# 142| Type = (int, error)
586586
# 142| 0: [FunctionName, SelectorExpr] selection of Print
587-
# 142| Type = func([]interface { }) int, error
587+
# 142| Type = func(...interface { }) int, error
588588
# 142| 0: [Ident, PackageName] fmt
589589
# 142| 1: [FunctionName, Ident] Print
590-
# 142| Type = func([]interface { }) int, error
590+
# 142| Type = func(...interface { }) int, error
591591
# 142| 1: [Ident, VariableName] i
592592
# 142| Type = int
593593
# 142| 2: [Ident, VariableName] v

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ input.go:
3333
# 20| 0: [CallExpr] call to Println
3434
# 20| Type = (int, error)
3535
# 20| 0: [FunctionName, SelectorExpr] selection of Println
36-
# 20| Type = func([]interface { }) int, error
36+
# 20| Type = func(...interface { }) int, error
3737
# 20| 0: [Ident, PackageName] fmt
3838
# 20| 1: [FunctionName, Ident] Println
39-
# 20| Type = func([]interface { }) int, error
39+
# 20| Type = func(...interface { }) int, error
4040
# 20| 1: [StringLit] "Hi"
4141
# 20| Type = string
4242
# 20| Value = [StringLit] Hi
@@ -183,10 +183,10 @@ input.go:
183183
# 52| 0: [CallExpr] call to Println
184184
# 52| Type = (int, error)
185185
# 52| 0: [FunctionName, SelectorExpr] selection of Println
186-
# 52| Type = func([]interface { }) int, error
186+
# 52| Type = func(...interface { }) int, error
187187
# 52| 0: [Ident, PackageName] fmt
188188
# 52| 1: [FunctionName, Ident] Println
189-
# 52| Type = func([]interface { }) int, error
189+
# 52| Type = func(...interface { }) int, error
190190
# 52| 1: [StringLit] "Heard from ch1"
191191
# 52| Type = string
192192
# 52| Value = [StringLit] Heard from ch1
@@ -209,31 +209,31 @@ input.go:
209209
# 54| 0: [CallExpr] call to Println
210210
# 54| Type = (int, error)
211211
# 54| 0: [FunctionName, SelectorExpr] selection of Println
212-
# 54| Type = func([]interface { }) int, error
212+
# 54| Type = func(...interface { }) int, error
213213
# 54| 0: [Ident, PackageName] fmt
214214
# 54| 1: [FunctionName, Ident] Println
215-
# 54| Type = func([]interface { }) int, error
215+
# 54| Type = func(...interface { }) int, error
216216
# 54| 1: [Ident, VariableName] a
217217
# 54| Type = [1]float32
218218
# 55| 2: [ExprStmt] expression statement
219219
# 55| 0: [CallExpr] call to Println
220220
# 55| Type = (int, error)
221221
# 55| 0: [FunctionName, SelectorExpr] selection of Println
222-
# 55| Type = func([]interface { }) int, error
222+
# 55| Type = func(...interface { }) int, error
223223
# 55| 0: [Ident, PackageName] fmt
224224
# 55| 1: [FunctionName, Ident] Println
225-
# 55| Type = func([]interface { }) int, error
225+
# 55| Type = func(...interface { }) int, error
226226
# 55| 1: [Ident, VariableName] w
227227
# 55| Type = bool
228228
# 56| 2: [CommClause] comm clause
229229
# 57| 0: [ExprStmt] expression statement
230230
# 57| 0: [CallExpr] call to Println
231231
# 57| Type = (int, error)
232232
# 57| 0: [FunctionName, SelectorExpr] selection of Println
233-
# 57| Type = func([]interface { }) int, error
233+
# 57| Type = func(...interface { }) int, error
234234
# 57| 0: [Ident, PackageName] fmt
235235
# 57| 1: [FunctionName, Ident] Println
236-
# 57| Type = func([]interface { }) int, error
236+
# 57| Type = func(...interface { }) int, error
237237
# 58| 3: [CommClause] comm clause
238238
# 58| 0: [SendStmt] send statement
239239
# 58| 0: [Ident, VariableName] ch1
@@ -277,10 +277,10 @@ input.go:
277277
# 67| 0: [CallExpr] call to Println
278278
# 67| Type = (int, error)
279279
# 67| 0: [FunctionName, SelectorExpr] selection of Println
280-
# 67| Type = func([]interface { }) int, error
280+
# 67| Type = func(...interface { }) int, error
281281
# 67| 0: [Ident, PackageName] fmt
282282
# 67| 1: [FunctionName, Ident] Println
283-
# 67| Type = func([]interface { }) int, error
283+
# 67| Type = func(...interface { }) int, error
284284
# 67| 1: [Ident, VariableName] x
285285
# 67| Type = int
286286
# 68| 2: [BlockStmt] block statement
@@ -296,10 +296,10 @@ input.go:
296296
# 69| 0: [CallExpr] call to Println
297297
# 69| Type = (int, error)
298298
# 69| 0: [FunctionName, SelectorExpr] selection of Println
299-
# 69| Type = func([]interface { }) int, error
299+
# 69| Type = func(...interface { }) int, error
300300
# 69| 0: [Ident, PackageName] fmt
301301
# 69| 1: [FunctionName, Ident] Println
302-
# 69| Type = func([]interface { }) int, error
302+
# 69| Type = func(...interface { }) int, error
303303
# 69| 1: [MinusExpr] -...
304304
# 69| Type = int
305305
# 69| 0: [Ident, VariableName] x
@@ -454,10 +454,10 @@ input.go:
454454
# 115| 0: [CallExpr] call to Println
455455
# 115| Type = (int, error)
456456
# 115| 0: [FunctionName, SelectorExpr] selection of Println
457-
# 115| Type = func([]interface { }) int, error
457+
# 115| Type = func(...interface { }) int, error
458458
# 115| 0: [Ident, PackageName] fmt
459459
# 115| 1: [FunctionName, Ident] Println
460-
# 115| Type = func([]interface { }) int, error
460+
# 115| Type = func(...interface { }) int, error
461461
# 115| 1: [Ident, VariableName] y
462462
# 115| Type = interface { }
463463
# 116| 1: [CaseClause] case clause
@@ -546,10 +546,10 @@ input.go:
546546
# 138| 0: [CallExpr] call to Print
547547
# 138| Type = (int, error)
548548
# 138| 0: [FunctionName, SelectorExpr] selection of Print
549-
# 138| Type = func([]interface { }) int, error
549+
# 138| Type = func(...interface { }) int, error
550550
# 138| 0: [Ident, PackageName] fmt
551551
# 138| 1: [FunctionName, Ident] Print
552-
# 138| Type = func([]interface { }) int, error
552+
# 138| Type = func(...interface { }) int, error
553553
# 138| 1: [Ident, VariableName] x
554554
# 138| Type = int
555555
# 141| 1: [RangeStmt] range statement
@@ -564,10 +564,10 @@ input.go:
564564
# 142| 0: [CallExpr] call to Print
565565
# 142| Type = (int, error)
566566
# 142| 0: [FunctionName, SelectorExpr] selection of Print
567-
# 142| Type = func([]interface { }) int, error
567+
# 142| Type = func(...interface { }) int, error
568568
# 142| 0: [Ident, PackageName] fmt
569569
# 142| 1: [FunctionName, Ident] Print
570-
# 142| Type = func([]interface { }) int, error
570+
# 142| Type = func(...interface { }) int, error
571571
# 142| 1: [Ident, VariableName] i
572572
# 142| Type = int
573573
# 142| 2: [Ident, VariableName] v

0 commit comments

Comments
 (0)