Skip to content

Commit

Permalink
Add test for aliasing interface method definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
smowton committed Sep 4, 2024
1 parent 6ed0a37 commit cbd129f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions go/ql/test/library-tests/semmle/go/aliases/MethodDefs/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package aliases

type IntAlias = int

type S1 = struct{ x int }
type S2 = struct{ x IntAlias }

type I1 = interface{ F(int) }
type I2 = interface{ F(IntAlias) }
type I3 = interface{ F(S1) }
type I4 = interface{ F(S2) }

func Test1(param1 I1, param2 I3, arg int) {
param1.F(arg)
param2.F(S1{arg})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
distinctDefinedFs
| 2 |
declaredEntities
| methods.go:3:6:3:13 | IntAlias (1 declaration sites) |
| methods.go:5:6:5:7 | S1 (1 declaration sites) |
| methods.go:5:19:5:19 | x (2 declaration sites) |
| methods.go:6:6:6:7 | S2 (1 declaration sites) |
| methods.go:6:19:6:19 | x (2 declaration sites) |
| methods.go:8:6:8:7 | I1 (1 declaration sites) |
| methods.go:8:22:8:22 | F (2 declaration sites) |
| methods.go:9:6:9:7 | I2 (1 declaration sites) |
| methods.go:9:22:9:22 | F (2 declaration sites) |
| methods.go:10:6:10:7 | I3 (1 declaration sites) |
| methods.go:10:22:10:22 | F (2 declaration sites) |
| methods.go:11:6:11:7 | I4 (1 declaration sites) |
| methods.go:11:22:11:22 | F (2 declaration sites) |
| methods.go:13:6:13:10 | Test1 (1 declaration sites) |
| methods.go:13:12:13:17 | param1 (1 declaration sites) |
| methods.go:13:23:13:28 | param2 (1 declaration sites) |
| methods.go:13:34:13:36 | arg (1 declaration sites) |
24 changes: 24 additions & 0 deletions go/ql/test/library-tests/semmle/go/aliases/MethodDefs/test.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import go

newtype TEntityWithDeclInfo =
MkEntityWithDeclInfo(Entity e, int nDecls) { nDecls = count(e.getDeclaration()) and nDecls > 0 }

class EntityWithDeclInfo extends TEntityWithDeclInfo {
string toString() {
exists(Entity e, int nDecls | this = MkEntityWithDeclInfo(e, nDecls) |
result = e.toString() + " (" + nDecls + " declaration sites)"
)
}

predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
exists(Entity e | this = MkEntityWithDeclInfo(e, _) |
e.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
)
}
}

query predicate distinctDefinedFs(int ct) { ct = count(DeclaredFunction e | e.toString() = "F") }

query predicate declaredEntities(EntityWithDeclInfo e) { any() }

0 comments on commit cbd129f

Please sign in to comment.