Skip to content

Go: extract entities for type parameters #15216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
542 changes: 542 additions & 0 deletions go/downgrades/6006685ee4d59aafd6088f32ae088a2f1888149c/old.dbscheme

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class TypeParamType_ extends @typeparamtype {
string toString() { result = "Type Param Type" }
}

class CompositeType_ extends @compositetype {
string toString() { result = "Composite Type" }
}

class DeclaredFunction_ extends @declfunctionobject {
string toString() { result = "Declared Function" }

// Ident_ getDeclaration() { defs(result, this) }
string getName() { objects(this, _, result) }
}

from TypeParamType_ tp, string name, CompositeType_ bound, DeclaredFunction_ declFunction
where
typeparam(tp, name, bound) and
declFunction = min(DeclaredFunction_ df | | df order by df.getName())
select tp, name, bound, declFunction, 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
description: Recreate unused column from the `typeparam` relation
compatibility: full
typeparam.rel: run typeparam.qlo
11 changes: 3 additions & 8 deletions go/extractor/dbscheme/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,6 @@ var ObjectType = NewPrimaryKeyType("@object")
// ObjectKind is a case type for distinguishing different kinds of built-in and declared objects
var ObjectKind = NewCaseType(ObjectType, "kind")

// TypeParamParentObjectType is the type of objects that can have type parameters as children
var TypeParamParentObjectType = NewUnionType("@typeparamparentobject")

// DeclObjectType is the type of declared objects
var DeclObjectType = NewUnionType("@declobject")

Expand All @@ -698,7 +695,7 @@ var PkgObjectType = ObjectKind.NewBranch("@pkgobject")
var TypeObjectType = NewUnionType("@typeobject")

// DeclTypeObjectType is the type of declared named types
var DeclTypeObjectType = ObjectKind.NewBranch("@decltypeobject", TypeObjectType, DeclObjectType, TypeParamParentObjectType)
var DeclTypeObjectType = ObjectKind.NewBranch("@decltypeobject", TypeObjectType, DeclObjectType)

// BuiltinTypeObjectType is the type of built-in named types
var BuiltinTypeObjectType = ObjectKind.NewBranch("@builtintypeobject", TypeObjectType, BuiltinObjectType)
Expand All @@ -725,7 +722,7 @@ var DeclVarObjectType = ObjectKind.NewBranch("@declvarobject", VarObjectType, De
var FunctionObjectType = NewUnionType("@functionobject", ValueObjectType)

// DeclFuncObjectType is the type of declared functions, including (abstract and concrete) methods
var DeclFuncObjectType = ObjectKind.NewBranch("@declfunctionobject", FunctionObjectType, DeclObjectType, TypeParamParentObjectType)
var DeclFuncObjectType = ObjectKind.NewBranch("@declfunctionobject", FunctionObjectType, DeclObjectType)

// BuiltinFuncObjectType is the type of built-in functions
var BuiltinFuncObjectType = ObjectKind.NewBranch("@builtinfunctionobject", FunctionObjectType, BuiltinObjectType)
Expand Down Expand Up @@ -1214,6 +1211,4 @@ var TypeParamTable = NewTable("typeparam",
EntityColumn(TypeParamType, "tp").Unique(),
StringColumn("name"),
EntityColumn(CompositeType, "bound"),
EntityColumn(TypeParamParentObjectType, "parent"),
IntColumn("idx"),
).KeySet("parent", "idx")
)
82 changes: 32 additions & 50 deletions go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

var MaxGoRoutines int
var typeParamParent map[*types.TypeParam]types.Object = make(map[*types.TypeParam]types.Object)

func init() {
// this sets the number of threads that the Go runtime will spawn; this is separate
Expand Down Expand Up @@ -398,17 +397,18 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
if !exists {
// Populate type parameter parents for functions. Note that methods
// do not appear as objects in any scope, so they have to be dealt
// with separately in extractMethods.
// with separately using extractMethod when we extract the named
// type or interface type that they are defined on.
if funcObj, ok := obj.(*types.Func); ok {
populateTypeParamParents(funcObj.Type().(*types.Signature).TypeParams(), obj)
populateTypeParamParents(funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
util.PopulateTypeParamParents(funcObj.Type().(*types.Signature).TypeParams(), obj)
util.PopulateTypeParamParents(funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
}
// Populate type parameter parents for named types. Note that we
// skip type aliases as the original type should be the parent
// of any type parameters.
if typeNameObj, ok := obj.(*types.TypeName); ok && !typeNameObj.IsAlias() {
if tp, ok := typeNameObj.Type().(*types.Named); ok {
populateTypeParamParents(tp.TypeParams(), obj)
util.PopulateTypeParamParents(tp.TypeParams(), obj)
}
}
extractObject(tw, obj, lbl)
Expand All @@ -434,8 +434,8 @@ func extractMethod(tw *trap.Writer, meth *types.Func) trap.Label {
if !exists {
// Populate type parameter parents for methods. They do not appear as
// objects in any scope, so they have to be dealt with separately here.
populateTypeParamParents(meth.Type().(*types.Signature).TypeParams(), meth)
populateTypeParamParents(meth.Type().(*types.Signature).RecvTypeParams(), meth)
util.PopulateTypeParamParents(meth.Type().(*types.Signature).TypeParams(), meth)
util.PopulateTypeParamParents(meth.Type().(*types.Signature).RecvTypeParams(), meth)
extractObject(tw, meth, methlbl)
}

Expand Down Expand Up @@ -1597,15 +1597,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
extractUnderlyingType(tw, lbl, underlying)
trackInstantiatedStructFields(tw, tp, origintp)

entitylbl, exists := tw.Labeler.LookupObjectID(origintp.Obj(), lbl)
if entitylbl == trap.InvalidLabel {
log.Printf("Omitting type-object binding for unknown object %v.\n", origintp.Obj())
} else {
if !exists {
extractObject(tw, origintp.Obj(), entitylbl)
}
dbscheme.TypeObjectTable.Emit(tw, lbl, entitylbl)
}
extractTypeObject(tw, lbl, origintp.Obj())

// ensure all methods have labels - note that methods do not have a
// parent scope, so they are not dealt with by `extractScopes`
Expand All @@ -1624,9 +1616,10 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
}
case *types.TypeParam:
kind = dbscheme.TypeParamType.Index()
parentlbl := getTypeParamParentLabel(tw, tp)
constraintLabel := extractType(tw, tp.Constraint())
dbscheme.TypeParamTable.Emit(tw, lbl, tp.Obj().Name(), constraintLabel, parentlbl, tp.Index())
dbscheme.TypeParamTable.Emit(tw, lbl, tp.Obj().Name(), constraintLabel)

extractTypeObject(tw, lbl, tp.Obj())
case *types.Union:
kind = dbscheme.TypeSetLiteral.Index()
for i := 0; i < tp.Len(); i++ {
Expand Down Expand Up @@ -1765,8 +1758,14 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
}
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
case *types.TypeParam:
parentlbl := getTypeParamParentLabel(tw, tp)
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%s;typeparamtype", parentlbl, tp.Obj().Name()))
entitylbl, exists := tw.Labeler.LookupObjectID(tp.Obj(), lbl)
if entitylbl == trap.InvalidLabel {
panic(fmt.Sprintf("Cannot construct label for type parameter type %v (underlying object is %v).\n", tp, tp.Obj()))
}
if !exists {
extractObject(tw, tp.Obj(), entitylbl)
}
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};typeparamtype", entitylbl))
case *types.Union:
var b strings.Builder
for i := 0; i < tp.Len(); i++ {
Expand All @@ -1788,6 +1787,19 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
return lbl, exists
}

// extractTypeObject extracts a single type object and emits it to the type object table.
func extractTypeObject(tw *trap.Writer, lbl trap.Label, entity *types.TypeName) {
entitylbl, exists := tw.Labeler.LookupObjectID(entity, lbl)
if entitylbl == trap.InvalidLabel {
log.Printf("Omitting type-object binding for unknown object %v.\n", entity)
} else {
if !exists {
extractObject(tw, entity, entitylbl)
}
dbscheme.TypeObjectTable.Emit(tw, lbl, entitylbl)
}
}

// extractKeyType extracts `key` as the key type of the map type `mp`
func extractKeyType(tw *trap.Writer, mp trap.Label, key types.Type) {
dbscheme.KeyTypeTable.Emit(tw, mp, extractType(tw, key))
Expand Down Expand Up @@ -1950,15 +1962,6 @@ func extractTypeParamDecls(tw *trap.Writer, fields *ast.FieldList, parent trap.L
}
}

// populateTypeParamParents sets `parent` as the parent of the elements of `typeparams`
func populateTypeParamParents(typeparams *types.TypeParamList, parent types.Object) {
if typeparams != nil {
for idx := 0; idx < typeparams.Len(); idx++ {
setTypeParamParent(typeparams.At(idx), parent)
}
}
}

// getobjectBeingUsed looks up `ident` in `tw.Package.TypesInfo.Uses` and makes
// some changes to the object to avoid returning objects relating to instantiated
// types.
Expand Down Expand Up @@ -2003,27 +2006,6 @@ func trackInstantiatedStructFields(tw *trap.Writer, tp, origintp *types.Named) {
}
}

func getTypeParamParentLabel(tw *trap.Writer, tp *types.TypeParam) trap.Label {
parent, exists := typeParamParent[tp]
if !exists {
log.Fatalf("Parent of type parameter does not exist: %s %s", tp.String(), tp.Constraint().String())
}
parentlbl, _ := tw.Labeler.ScopedObjectID(parent, func() trap.Label {
log.Fatalf("getTypeLabel() called for parent of type parameter %s", tp.String())
return trap.InvalidLabel
})
return parentlbl
}

func setTypeParamParent(tp *types.TypeParam, newobj types.Object) {
obj, exists := typeParamParent[tp]
if !exists {
typeParamParent[tp] = newobj
} else if newobj != obj {
log.Fatalf("Parent of type parameter '%s %s' being set to a different value: '%s' vs '%s'", tp.String(), tp.Constraint().String(), obj, newobj)
}
}

// skipExtractingValueForLeftOperand returns true if the left operand of `be`
// should not have its value extracted because it is an intermediate value in a
// string concatenation - specifically that the right operand is a string
Expand Down
6 changes: 4 additions & 2 deletions go/extractor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/github/codeql-go/extractor
go 1.22.0

require (
golang.org/x/mod v0.15.0
golang.org/x/tools v0.18.0
golang.org/x/mod v0.18.0
golang.org/x/tools v0.22.0
)

require golang.org/x/sync v0.7.0 // indirect
12 changes: 6 additions & 6 deletions go/extractor/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
1 change: 1 addition & 0 deletions go/extractor/trap/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions go/extractor/trap/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/types"

"github.com/github/codeql-go/extractor/util"
"golang.org/x/tools/go/types/objectpath"
)

// Label represents a label
Expand Down Expand Up @@ -149,6 +150,7 @@ func (l *Labeler) ScopedObjectID(object types.Object, getTypeLabel func() Label)
if !exists {
scope := object.Parent()
if scope == nil {
// Note that `scope` should only be nil for methods and struct fields
panic(fmt.Sprintf("Object has no scope: %v :: %v.\n", object,
l.tw.Package.Fset.Position(object.Pos())))
} else {
Expand All @@ -157,7 +159,15 @@ func (l *Labeler) ScopedObjectID(object types.Object, getTypeLabel func() Label)
// referenced from other files via their method
methlbl, _ := l.MethodID(meth, getTypeLabel())
label, _ = l.ReceiverObjectID(object, methlbl)
} else if path, err := objectpath.For(object); err == nil {
// If we can, use the object path as the label. This is needed
// for type parameters. Note that `objectpath` returns an error
// for non-exported objects.
label = l.GlobalID(fmt.Sprintf("%v,{%s};object", object.Pkg().Path(), path))
} else {
// Otherwise, construct a label based on the scope and the object's name.
// If the scope is not package scope then it will be given a fresh label,
// which will be different when it is referenced from different packages.
scopeLbl := l.ScopeID(scope, object.Pkg())
label = l.GlobalID(fmt.Sprintf("{%v},%s;object", scopeLbl, object.Name()))
}
Expand Down Expand Up @@ -192,6 +202,15 @@ func findMethodOnTypeWithGivenReceiver(tp types.Type, object types.Object) *type
return nil
}

func getTypeParamType(object types.Object) *types.TypeParam {
typeNameObject, ok := object.(*types.TypeName)
if !ok {
return nil
}
typeParamType, _ := typeNameObject.Type().(*types.TypeParam)
return typeParamType
}

// ReceiverObjectID associates a label with the given object and returns it, together with a flag indicating whether
// the object already had a label associated with it; the object must be the receiver of `methlbl`, since that label
// is used to construct the label of the object
Expand Down
29 changes: 29 additions & 0 deletions go/extractor/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"errors"
"fmt"
"go/types"
"io/fs"
"log"
"net/url"
Expand Down Expand Up @@ -287,3 +288,31 @@ func getImportPathFromRepoURL(repourl string) string {
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
return host + "/" + path
}

var typeParamParent map[*types.TypeParam]types.Object = make(map[*types.TypeParam]types.Object)

func GetTypeParamParent(tp *types.TypeParam) types.Object {
parent, exists := typeParamParent[tp]
if !exists {
log.Fatalf("Parent of type parameter does not exist: %s %s", tp.String(), tp.Constraint().String())
}
return parent
}

// PopulateTypeParamParents sets `parent` as the parent of the elements of `typeparams`
func PopulateTypeParamParents(typeparams *types.TypeParamList, parent types.Object) {
if typeparams != nil {
for idx := 0; idx < typeparams.Len(); idx++ {
setTypeParamParent(typeparams.At(idx), parent)
}
}
}

func setTypeParamParent(tp *types.TypeParam, newobj types.Object) {
obj, exists := typeParamParent[tp]
if !exists {
typeParamParent[tp] = newobj
} else if newobj != obj {
log.Fatalf("Parent of type parameter '%s %s' being set to a different value: '%s' vs '%s'", tp.String(), tp.Constraint().String(), obj, newobj)
}
}
2 changes: 1 addition & 1 deletion go/extractor/vendor/golang.org/x/mod/modfile/read.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading