Fix NamedType.getMethod to take interface embedding into account.

This commit is contained in:
Max Schaefer
2020-02-28 10:37:14 +00:00
parent 2eba7dee6f
commit 90f1a7da75
10 changed files with 66 additions and 19 deletions

View File

@@ -575,6 +575,13 @@ class NamedType extends @namedtype, CompositeType {
/** Gets the type which this type is defined to be. */
Type getBaseType() { underlying_type(this, result) }
override Method getMethod(string m) {
result = CompositeType.super.getMethod(m)
or
methodhosts(result, this) and
result.getName() = m
}
override Type getUnderlyingType() { result = getBaseType().getUnderlyingType() }
}

View File

@@ -0,0 +1,9 @@
| * Foo | 1 |
| A | 1 |
| A2 | 1 |
| AC | 3 |
| AEmbedded | 1 |
| AExtended | 2 |
| B | 2 |
| C | 2 |
| Foo | 1 |

View File

@@ -0,0 +1,5 @@
import go
from Type t
where t.getPackage().getName().regexpMatch("main|pkg1|pkg2")
select t.pp(), strictcount(t.getMethod(_))

View File

@@ -0,0 +1,15 @@
| A | m | func() |
| A2 | m | func() |
| AC | m | func() |
| AC | n | func() |
| AC | o | func() |
| AEmbedded | m | func() |
| AExtended | m | func() |
| AExtended | n | func() |
| B | m | func() |
| B | n | func() |
| C | n | func() |
| C | o | func() |
| T | half | func() Foo |
| T3 | half | func() Foo |
| T4 | half | func() Foo |

View File

@@ -0,0 +1,7 @@
import go
from NamedType t, string m, Type tp
where
exists(t.getEntity().getDeclaration()) and
t.getBaseType().hasMethod(m, tp)
select t, m, tp.pp()

View File

@@ -1,14 +1,14 @@
| A | m | func() |
| AC | m | func() |
| AC | n | func() |
| AC | o | func() |
| AEmbedded | m | func() |
| AExtended | m | func() |
| AExtended | n | func() |
| B | m | func() |
| B | n | func() |
| C | n | func() |
| C | o | func() |
| T | half | func() Foo |
| T3 | half | func() Foo |
| T4 | half | func() Foo |
| * Foo | half | pkg1/tst.go:33:16:33:19 | half |
| A | m | pkg1/interfaces.go:4:2:4:2 | m |
| A2 | m | pkg1/interfaces.go:32:2:32:2 | m |
| AC | m | pkg1/interfaces.go:4:2:4:2 | m |
| AC | n | pkg1/interfaces.go:13:2:13:2 | n |
| AC | o | pkg1/interfaces.go:14:2:14:2 | o |
| AEmbedded | m | pkg1/interfaces.go:4:2:4:2 | m |
| AExtended | m | pkg1/interfaces.go:4:2:4:2 | m |
| AExtended | n | pkg1/interfaces.go:28:2:28:2 | n |
| B | m | pkg1/interfaces.go:8:2:8:2 | m |
| B | n | pkg1/interfaces.go:9:2:9:2 | n |
| C | n | pkg1/interfaces.go:13:2:13:2 | n |
| C | o | pkg1/interfaces.go:14:2:14:2 | o |
| Foo | half | pkg1/tst.go:33:16:33:19 | half |

View File

@@ -1,7 +1,5 @@
import go
from NamedType t, string m, Type tp
where
exists(t.getEntity().getDeclaration()) and
t.getBaseType().hasMethod(m, tp)
select t, m, tp.pp()
from Type t, string m
where t.getPackage().getName().regexpMatch("main|pkg1|pkg2")
select t.pp(), m, t.getMethod(m)

View File

@@ -1,4 +1,5 @@
| A | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.A |
| A2 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.A2 |
| AC | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.AC |
| AEmbedded | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.AEmbedded |
| AExtended | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types/pkg1.AExtended |

View File

@@ -1,4 +1,5 @@
| A | A |
| A2 | A2 |
| AC | AC |
| AEmbedded | AEmbedded |
| AExtended | AExtended |

View File

@@ -27,3 +27,7 @@ type AExtended interface {
A
n()
}
type A2 interface {
m()
}