Skip to content

Commit

Permalink
Merge pull request #17794 from owen-mc/go/ast-viewer-typeparamdecl
Browse files Browse the repository at this point in the history
Go: Fix type param declarations in AST viewer
  • Loading branch information
owen-mc authored Oct 18, 2024
2 parents b0376d5 + bacf448 commit f3abe54
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The AST viewer now shows type parameter declarations in the correct place in the AST.
2 changes: 2 additions & 0 deletions go/ql/lib/semmle/go/AST.qll
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class AstNode extends @node, Locatable {
kind = "commentgroup" and result = this.(File).getCommentGroup(i)
or
kind = "comment" and result = this.(CommentGroup).getComment(i)
or
kind = "typeparamdecl" and result = this.(TypeParamDeclParent).getTypeParameterDecl(i)
}

/**
Expand Down
34 changes: 34 additions & 0 deletions go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ other.go:
# 11| Type = int
# 11| 0: [Ident, VariableName] myNested
# 11| Type = func() int
# 8| 3: [TypeParamDecl] type parameter declaration
# 8| 0: [Ident, TypeName] int
# 8| Type = int
# 8| 1: [Ident, TypeName] U
# 8| Type = U
# 15| 5: [VarDecl] variable declaration
# 15| 0: [ValueSpec] value declaration specifier
# 15| 0: [Ident, VariableName] x
Expand All @@ -648,3 +653,32 @@ other.go:
# 15| 2: [IntLit] 0
# 15| Type = int
# 15| Value = [IntLit] 0
# 17| 6: [TypeDecl] type declaration
# 17| 0: [TypeSpec] type declaration specifier
# 17| 0: [Ident, TypeName] myType
# 17| Type = myType
# 17| 1: [ArrayTypeExpr] array type
# 17| Type = []T
# 17| 0: [Ident, TypeName] T
# 17| Type = T
# 17| 2: [TypeParamDecl] type parameter declaration
# 17| 0: [TypeSetLiteralExpr] type set literal
# 17| Type = ~string
# 17| 0: [Ident, TypeName] string
# 17| Type = string
# 17| 1: [Ident, TypeName] T
# 17| Type = T
# 19| 7: [MethodDecl] function declaration
# 19| 0: [FunctionName, Ident] f
# 19| Type = func()
# 19| 1: [FuncTypeExpr] function type
# 19| 2: [ReceiverDecl] receiver declaration
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
# 19| Type = myType
# 19| 0: [Ident, TypeName] myType
# 19| Type = myType
# 19| 1: [Ident, TypeName] U
# 19| Type = U
# 19| 1: [Ident, VariableName] m
# 19| Type = myType
# 19| 3: [BlockStmt] block statement
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ other.go:
# 11| Type = int
# 11| 0: [Ident, VariableName] myNested
# 11| Type = func() int
# 8| 3: [TypeParamDecl] type parameter declaration
# 8| 0: [Ident, TypeName] int
# 8| Type = int
# 8| 1: [Ident, TypeName] U
# 8| Type = U
# 15| 5: [VarDecl] variable declaration
# 15| 0: [ValueSpec] value declaration specifier
# 15| 0: [Ident, VariableName] x
Expand All @@ -628,3 +633,32 @@ other.go:
# 15| 2: [IntLit] 0
# 15| Type = int
# 15| Value = [IntLit] 0
# 17| 6: [TypeDecl] type declaration
# 17| 0: [TypeSpec] type declaration specifier
# 17| 0: [Ident, TypeName] myType
# 17| Type = myType
# 17| 1: [ArrayTypeExpr] array type
# 17| Type = []T
# 17| 0: [Ident, TypeName] T
# 17| Type = T
# 17| 2: [TypeParamDecl] type parameter declaration
# 17| 0: [TypeSetLiteralExpr] type set literal
# 17| Type = ~string
# 17| 0: [Ident, TypeName] string
# 17| Type = string
# 17| 1: [Ident, TypeName] T
# 17| Type = T
# 19| 7: [MethodDecl] function declaration
# 19| 0: [FunctionName, Ident] f
# 19| Type = func()
# 19| 1: [FuncTypeExpr] function type
# 19| 2: [ReceiverDecl] receiver declaration
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
# 19| Type = myType
# 19| 0: [Ident, TypeName] myType
# 19| Type = myType
# 19| 1: [Ident, TypeName] U
# 19| Type = U
# 19| 1: [Ident, VariableName] m
# 19| Type = myType
# 19| 3: [BlockStmt] block statement
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ other.go:
# 11| Type = int
# 11| 0: [Ident, VariableName] myNested
# 11| Type = func() int
# 8| 3: [TypeParamDecl] type parameter declaration
# 8| 0: [Ident, TypeName] int
# 8| Type = int
# 8| 1: [Ident, TypeName] U
# 8| Type = U
# 15| 2: [VarDecl] variable declaration
# 15| 0: [ValueSpec] value declaration specifier
# 15| 0: [Ident, VariableName] x
Expand All @@ -65,3 +70,18 @@ other.go:
# 15| 2: [IntLit] 0
# 15| Type = int
# 15| Value = [IntLit] 0
# 17| 3: [TypeDecl] type declaration
# 17| 0: [TypeSpec] type declaration specifier
# 17| 0: [Ident, TypeName] myType
# 17| Type = myType
# 17| 1: [ArrayTypeExpr] array type
# 17| Type = []T
# 17| 0: [Ident, TypeName] T
# 17| Type = T
# 17| 2: [TypeParamDecl] type parameter declaration
# 17| 0: [TypeSetLiteralExpr] type set literal
# 17| Type = ~string
# 17| 0: [Ident, TypeName] string
# 17| Type = string
# 17| 1: [Ident, TypeName] T
# 17| Type = T
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ other.go:
# 11| Type = int
# 11| 0: [Ident, VariableName] myNested
# 11| Type = func() int
# 8| 3: [TypeParamDecl] type parameter declaration
# 8| 0: [Ident, TypeName] int
# 8| Type = int
# 8| 1: [Ident, TypeName] U
# 8| Type = U
# 15| 5: [VarDecl] variable declaration
# 15| 0: [ValueSpec] value declaration specifier
# 15| 0: [Ident, VariableName] x
Expand All @@ -50,3 +55,32 @@ other.go:
# 15| 2: [IntLit] 0
# 15| Type = int
# 15| Value = [IntLit] 0
# 17| 6: [TypeDecl] type declaration
# 17| 0: [TypeSpec] type declaration specifier
# 17| 0: [Ident, TypeName] myType
# 17| Type = myType
# 17| 1: [ArrayTypeExpr] array type
# 17| Type = []T
# 17| 0: [Ident, TypeName] T
# 17| Type = T
# 17| 2: [TypeParamDecl] type parameter declaration
# 17| 0: [TypeSetLiteralExpr] type set literal
# 17| Type = ~string
# 17| 0: [Ident, TypeName] string
# 17| Type = string
# 17| 1: [Ident, TypeName] T
# 17| Type = T
# 19| 7: [MethodDecl] function declaration
# 19| 0: [FunctionName, Ident] f
# 19| Type = func()
# 19| 1: [FuncTypeExpr] function type
# 19| 2: [ReceiverDecl] receiver declaration
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
# 19| Type = myType
# 19| 0: [Ident, TypeName] myType
# 19| Type = myType
# 19| 1: [Ident, TypeName] U
# 19| Type = U
# 19| 1: [Ident, VariableName] m
# 19| Type = myType
# 19| 3: [BlockStmt] block statement
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
other.go:
# 8| [TypeParamDecl] type parameter declaration
# 8| 0: [Ident, TypeName] int
# 8| Type = int
# 8| 1: [Ident, TypeName] U
# 8| Type = U
go.mod:
# 0| [GoModFile] go.mod
# 1| 0: [GoModModuleLine] go.mod module line
Expand Down Expand Up @@ -45,3 +51,18 @@ other.go:
# 15| 2: [IntLit] 0
# 15| Type = int
# 15| Value = [IntLit] 0
# 17| 3: [TypeDecl] type declaration
# 17| 0: [TypeSpec] type declaration specifier
# 17| 0: [Ident, TypeName] myType
# 17| Type = myType
# 17| 1: [ArrayTypeExpr] array type
# 17| Type = []T
# 17| 0: [Ident, TypeName] T
# 17| Type = T
# 17| 2: [TypeParamDecl] type parameter declaration
# 17| 0: [TypeSetLiteralExpr] type set literal
# 17| Type = ~string
# 17| 0: [Ident, TypeName] string
# 17| Type = string
# 17| 1: [Ident, TypeName] T
# 17| Type = T
3 changes: 1 addition & 2 deletions go/ql/test/library-tests/semmle/go/PrintAst/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module codeql-go-tests/printast

go 1.14

go 1.18
6 changes: 5 additions & 1 deletion go/ql/test/library-tests/semmle/go/PrintAst/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ func main() {}
func f() {}
func g() {}

func hasNested() {
func hasNested[U int]() {

myNested := func() int { return 1 }
myNested()

}

var x int = 0

type myType[T ~string] []T

func (m myType[U]) f() {}

0 comments on commit f3abe54

Please sign in to comment.