Skip to content

Commit

Permalink
Add test checking for entity counts and def-use matches under aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
smowton committed Sep 3, 2024
1 parent ea1870f commit a94601c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions go/ql/test/library-tests/semmle/go/aliases/defsuses.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 }

func Test1() int {
obj := S1{1}
obj.x = 2

var ptr *S2
ptr = &S1

return ptr.x
}
33 changes: 33 additions & 0 deletions go/ql/test/library-tests/semmle/go/aliases/test.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
lowLevelDefs
| defsuses.go:3:6:3:13 | IntAlias | defsuses.go:3:6:3:13 | IntAlias (1 declaration sites) |
| defsuses.go:5:6:5:7 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) |
| defsuses.go:5:20:5:20 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) |
| defsuses.go:5:20:5:20 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) |
| defsuses.go:6:6:6:7 | S2 | defsuses.go:6:6:6:7 | S2 (1 declaration sites) |
| defsuses.go:6:20:6:20 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) |
| defsuses.go:6:20:6:20 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) |
| defsuses.go:8:6:8:10 | Test1 | defsuses.go:8:6:8:10 | Test1 (1 declaration sites) |
| defsuses.go:9:2:9:4 | obj | defsuses.go:9:2:9:4 | obj (1 declaration sites) |
| defsuses.go:12:6:12:8 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) |
lowLevelUses
| defsuses.go:3:17:3:19 | int | file://:0:0:0:0 | int (0 declaration sites) |
| defsuses.go:5:22:5:24 | int | file://:0:0:0:0 | int (0 declaration sites) |
| defsuses.go:6:22:6:29 | IntAlias | defsuses.go:3:6:3:13 | IntAlias (1 declaration sites) |
| defsuses.go:8:14:8:16 | int | file://:0:0:0:0 | int (0 declaration sites) |
| defsuses.go:9:9:9:10 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) |
| defsuses.go:10:2:10:4 | obj | defsuses.go:9:2:9:4 | obj (1 declaration sites) |
| defsuses.go:10:6:10:6 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) |
| defsuses.go:10:6:10:6 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) |
| defsuses.go:12:11:12:12 | S2 | defsuses.go:6:6:6:7 | S2 (1 declaration sites) |
| defsuses.go:13:2:13:4 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) |
| defsuses.go:13:9:13:10 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) |
| defsuses.go:15:9:15:11 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) |
| defsuses.go:15:13:15:13 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) |
| defsuses.go:15:13:15:13 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) |
distinctDefinedXs
| 1 |
distinctUsedXs
| 1 |
fieldUseUsePairs
| defsuses.go:10:6:10:6 | x | defsuses.go:15:13:15:13 | x |
| defsuses.go:15:13:15:13 | x | defsuses.go:10:6:10:6 | x |
37 changes: 37 additions & 0 deletions go/ql/test/library-tests/semmle/go/aliases/test.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import go

Check failure on line 1 in go/ql/test/library-tests/semmle/go/aliases/test.ql

View workflow job for this annotation

GitHub Actions / Test Linux (Ubuntu)

[347/530 comp 5.1s eval 13ms] FAILED(RESULT) /home/runner/work/codeql/codeql/go/ql/test/library-tests/semmle/go/aliases/test.ql

newtype TEntityWithDeclInfo = MkEntityWithDeclInfo(Entity e)

class EntityWithDeclInfo extends TEntityWithDeclInfo {
string toString() {
exists(Entity e | this = MkEntityWithDeclInfo(e) |
result = e.toString() + " (" + count(e.getDeclaration()) + " 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 lowLevelDefs(Ident i, EntityWithDeclInfo ewrapped) {
exists(Entity e | ewrapped = MkEntityWithDeclInfo(e) | defs(i, e))
}

query predicate lowLevelUses(Ident i, EntityWithDeclInfo ewrapped) {
exists(Entity e | ewrapped = MkEntityWithDeclInfo(e) | uses(i, e))
}

query predicate distinctDefinedXs(int ct) {
ct = count(Entity e | defs(_, e) and e.toString() = "x")
}

query predicate distinctUsedXs(int ct) { ct = count(Entity e | uses(_, e) and e.toString() = "x") }

query predicate fieldUseUsePairs(Ident i1, Ident i2) {
exists(Field e | uses(i1, e) and uses(i2, e) and i1 != i2)
}

0 comments on commit a94601c

Please sign in to comment.