Skip to content

Commit

Permalink
Go: Add test for AliasType extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg committed Sep 4, 2024
1 parent 8c50b64 commit 745a9fb
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/ql/test/library-tests/semmle/go/Types/Aliases.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
entities
| aliases.go | aliases.go:3:6:3:13 | aliasesX | 1 | file://:0:0:0:0 | int |
| aliases.go | aliases.go:4:6:4:13 | aliasesY | 1 | file://:0:0:0:0 | int |
| aliases.go | aliases.go:6:6:6:14 | aliasesS1 | 1 | file://:0:0:0:0 | struct type |
| aliases.go | aliases.go:6:26:6:26 | x | 3 | file://:0:0:0:0 | int |
| aliases.go | aliases.go:8:6:8:14 | aliasesS2 | 1 | file://:0:0:0:0 | struct type |
| aliases.go | aliases.go:8:26:8:26 | x | 3 | file://:0:0:0:0 | int |
| aliases.go | aliases.go:10:6:10:6 | F | 1 | file://:0:0:0:0 | signature type |
| aliases.go | aliases.go:10:8:10:11 | Afs1 | 1 | file://:0:0:0:0 | struct type |
| aliases.go | aliases.go:14:6:14:6 | G | 1 | file://:0:0:0:0 | signature type |
| aliases.go | aliases.go:14:8:14:11 | Afs2 | 1 | file://:0:0:0:0 | struct type |
| aliases.go | aliases.go:19:6:19:7 | S3 | 1 | aliases.go:19:6:19:7 | S3 |
| aliases.go | aliases.go:19:17:19:17 | x | 3 | file://:0:0:0:0 | int |
| aliases.go | aliases.go:22:6:22:6 | T | 1 | aliases.go:19:6:19:7 | S3 |
| aliases.go | aliases.go:25:6:25:6 | H | 1 | file://:0:0:0:0 | signature type |
| aliases.go | aliases.go:25:8:25:11 | Afs3 | 1 | aliases.go:19:6:19:7 | S3 |
#select
| F | func(struct { x int }) int |
| G | func(struct { x int }) int |
| H | func(S3) int |
23 changes: 23 additions & 0 deletions go/ql/test/library-tests/semmle/go/Types/Aliases.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import go

int countDecls(Entity e) {
result = count(Ident decl | decl = e.getDeclaration())
}

query predicate entities(string fp, Entity e, int c, Type ty) {
c = countDecls(e) and
ty = e.getType() and
exists(DbLocation loc |
loc = e.getDeclaration().getLocation() and
fp = loc.getFile().getBaseName() and
fp = "aliases.go"
)
}

from string fp, FuncDecl decl, SignatureType sig
where
decl.hasLocationInfo(fp, _, _, _, _) and
decl.getName() = ["F", "G", "H"] and
sig = decl.getType() and
fp.matches("%aliases.go%")
select decl.getName(), sig.pp()
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:6:26:6:26 | x | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
| aliases.go:8:26:8:26 | x | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
| aliases.go:19:17:19:17 | x | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
| cyclic.go:4:3:4:3 | s | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
| cyclic.go:8:3:8:3 | u | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
| cyclic.go:9:2:9:2 | f | package github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:6:26:6:26 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.S3 | x |
| aliases.go:8:26:8:26 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.S3 | x |
| aliases.go:19:17:19:17 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.S3 | x |
| cyclic.go:4:3:4:3 | s | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.s | s |
| cyclic.go:8:3:8:3 | u | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.t | u |
| cyclic.go:8:3:8:3 | u | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.u | u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:6:26:6:26 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | S3 | x |
| aliases.go:8:26:8:26 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | S3 | x |
| aliases.go:19:17:19:17 | x | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | S3 | x |
| cyclic.go:4:3:4:3 | s | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | s | s |
| cyclic.go:8:3:8:3 | u | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | t | u |
| cyclic.go:8:3:8:3 | u | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types | u | u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| aliases.go:19:6:19:7 | S3 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.S3 |
| cyclic.go:3:6:3:6 | s | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.s |
| cyclic.go:7:6:7:6 | t | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.t |
| cyclic.go:12:6:12:6 | u | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:10:1:12:1 | function declaration | 1 |
| aliases.go:14:1:16:1 | function declaration | 1 |
| aliases.go:25:1:27:1 | function declaration | 1 |
| depth.go:22:1:25:1 | function declaration | 0 |
| generic.go:70:1:72:1 | function declaration | 1 |
| generic.go:74:1:80:1 | function declaration | 1 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:10:1:12:1 | function declaration | 1 |
| aliases.go:14:1:16:1 | function declaration | 1 |
| aliases.go:25:1:27:1 | function declaration | 1 |
| depth.go:22:1:25:1 | function declaration | 0 |
| generic.go:70:1:72:1 | function declaration | 1 |
| generic.go:74:1:80:1 | function declaration | 0 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:19:6:19:7 | S3 | aliases.go:6:18:6:37 | struct type | x | int |
| aliases.go:19:6:19:7 | S3 | aliases.go:8:18:8:37 | struct type | x | int |
| aliases.go:19:6:19:7 | S3 | aliases.go:19:9:19:23 | struct type | x | int |
| cyclic.go:3:6:3:6 | s | cyclic.go:3:8:5:1 | struct type | s | * s |
| cyclic.go:7:6:7:6 | t | cyclic.go:7:8:10:1 | struct type | f | int |
| cyclic.go:7:6:7:6 | t | cyclic.go:7:8:10:1 | struct type | t | t |
Expand Down
1 change: 1 addition & 0 deletions go/ql/test/library-tests/semmle/go/Types/Types.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| aliases.go:19:6:19:7 | S3 | S3 |
| cyclic.go:3:6:3:6 | s | s |
| cyclic.go:7:6:7:6 | t | t |
| cyclic.go:12:6:12:6 | u | u |
Expand Down
27 changes: 27 additions & 0 deletions go/ql/test/library-tests/semmle/go/Types/aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

type aliasesX = int
type aliasesY = int

type aliasesS1 = struct{ x aliasesX }

type aliasesS2 = struct{ x aliasesY }

func F(Afs1 aliasesS1) int {
return G(Afs1) + Afs1.x
}

func G(Afs2 aliasesS2) int {
return Afs2.x
}

// This is a named type, not an alias
type S3 struct{ x int }

// This is a type alias
type T = S3

// We expect `Afs3` to be of type `S3` here, not `struct{ x int }`
func H(Afs3 T) int {
return Afs3.x
}

0 comments on commit 745a9fb

Please sign in to comment.