From 75ca73824403d9d998b12c30e87138abcd6d0223 Mon Sep 17 00:00:00 2001 From: Christian Banse <oxisto@aybaze.com> Date: Wed, 8 Mar 2023 20:35:05 +0100 Subject: [PATCH] add go resources --- .../src/test/resources/golang/declare.go | 46 +++++++++++++++++++ .../src/test/resources/golang/slices.go | 21 +++++++++ .../test/resources/golang/type_constraints.go | 8 ++++ 3 files changed, 75 insertions(+) create mode 100644 cpg-language-go/src/test/resources/golang/declare.go create mode 100644 cpg-language-go/src/test/resources/golang/slices.go create mode 100644 cpg-language-go/src/test/resources/golang/type_constraints.go diff --git a/cpg-language-go/src/test/resources/golang/declare.go b/cpg-language-go/src/test/resources/golang/declare.go new file mode 100644 index 00000000000..15da35ffa9a --- /dev/null +++ b/cpg-language-go/src/test/resources/golang/declare.go @@ -0,0 +1,46 @@ +package main + +import "fmt" + +func main() { + // Declaring multiple variables in a block with initializer. This is one + // GenDecl with two ValueSpec specs and one "Name" and (initializer) "Value" + // each. + // + // We translate this into one DeclarationStatement with two + // VariableDeclaration nodes + var ( + a int = 1 + b int = 2 + ) + + // Declaring multiple variables in a single line. This is one GenDecl with + // one ValueSpec spec which contains two "Names" and two "Values" which + // correspond to the respective initializer values. Note, that the number of + // values MUST match the number of names + // + // We translate this into one DeclarationStatement with two + // VariableDeclaration nodes + var c, d = 3, 4 + + // Short assignment using an assignment, where all variables were not + // defined before. This is an AssignStmt which has DEFINE as its token. + // + // We need to split this up into several nodes. First, we translate this + // into one (implicit) DeclarationStatement with two VariableDeclaration + // nodes. Afterwards we are parsing it as a regular assignment. + e, f := 5, 6 + + // Short assignment using an assignment, where one variable (f) was defined + // before in the local scope. This is an AssignStmt which has DEFINE as its + // token. From the AST we cannot differentiate this from the previous + // example and we need to do a (local) variable lookup here. + // + // Finally, We need to split this up into several nodes. First, we translate + // this into one (implicit) DeclarationStatement with one + // VariableDeclaration node. Afterwards we are parsing it as a regular + // assignment. + f, g := 7, 8 + + fmt.Printf("%d %d %d %d %d %d %d\n", a, b, c, d, e, f, g) +} diff --git a/cpg-language-go/src/test/resources/golang/slices.go b/cpg-language-go/src/test/resources/golang/slices.go new file mode 100644 index 00000000000..00563b29f9b --- /dev/null +++ b/cpg-language-go/src/test/resources/golang/slices.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func main() { + var a = []int{1,2,3} + + // [1] + var b = a[:1] + + // [2, 3] + var c = a[1:] + + // [1] + var d = a[0:1] + + // [1] + var e = a[0:1:1] + + fmt.Printf("%v %v %v %v %v", a, b, c, d, e) +} \ No newline at end of file diff --git a/cpg-language-go/src/test/resources/golang/type_constraints.go b/cpg-language-go/src/test/resources/golang/type_constraints.go new file mode 100644 index 00000000000..a060f41d9d6 --- /dev/null +++ b/cpg-language-go/src/test/resources/golang/type_constraints.go @@ -0,0 +1,8 @@ +package main + +type MyStruct[T any] {} +type MyInterface{} + +func main() { + _ := &MyStruct[MyInterface]{} +} \ No newline at end of file