8000 Make TypeParamParent map global · github/codeql-go@ac081dc · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit ac081dc

Browse files
owen-mcsmowton
authored andcommitted
Make TypeParamParent map global
1 parent 8477053 commit ac081dc

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

extractor/extractor.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
)
3030

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

3334
func init() {
3435
// this sets the number of threads that the Go runtime will spawn; this is separate
@@ -365,13 +366,13 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
365366
// do not appear as objects in any scope, so they have to be dealt
366367
// with separately in extractMethods.
367368
if funcObj, ok := obj.(*types.Func); ok {
368-
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).TypeParams(), lbl)
369-
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).RecvTypeParams(), lbl)
369+
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).TypeParams(), obj)
370+
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
370371
}
371372
// Populate type parameter parents for named types.
372373
if typeNameObj, ok := obj.(*types.TypeName); ok {
373374
if tp, ok := typeNameObj.Type().(*types.Named); ok {
374-
populateTypeParamParents(tw, tp.TypeParams(), lbl)
375+
populateTypeParamParents(tw, tp.TypeParams(), obj)
375376
}
376377
}
377378
extractObject(tw, obj, lbl)
@@ -397,8 +398,8 @@ func extractMethod(tw *trap.Writer, meth *types.Func) trap.Label {
397398
if !exists {
398399
// Populate type parameter parents for methods. They do not appear as
399400
// objects in any scope, so they have to be dealt with separately here.
400-
populateTypeParamParents(tw, meth.Type().(*types.Signature).TypeParams(), methlbl)
401-
populateTypeParamParents(tw, meth.Type().(*types.Signature).RecvTypeParams(), methlbl)
401+
populateTypeParamParents(tw, meth.Type().(*types.Signature).TypeParams(), meth)
402+
populateTypeParamParents(tw, meth.Type().(*types.Signature).RecvTypeParams(), meth)
402403
extractObject(tw, meth, methlbl)
403404
}
404405

@@ -1099,6 +1100,7 @@ func extractExprs(tw *trap.Writer, exprs []ast.Expr, parent trap.Label, idx int,
10991100
func extractTypeOf(tw *trap.Writer, expr ast.Expr, lbl trap.Label) {
11001101
tp := typeOf(tw, expr)
11011102
if tp != nil {
1103+
// log.Printf("Extracting type of expr %v", expr)
11021104
tplbl := extractType(tw, tp)
11031105
dbscheme.TypeOfTable.Emit(tw, lbl, tplbl)
11041106
}
@@ -1574,10 +1576,7 @@ func extractType(tw *trap.Writer, tp types.Type) trap.Label {
15741576
}
15751577
case *types.TypeParam:
15761578
kind = dbscheme.TypeParamType.Index()
1577-
parentlbl, exists := tw.TypeParamParent[tp]
1578-
if !exists {
1579-
log.Fatalf("Parent of type parameter does not exist: %s", tp.String())
1580-
}
1579+
parentlbl := getTypeParamParentLabel(tw, tp)
15811580
constraintLabel := extractType(tw, tp.Constraint())
15821581
dbscheme.TypeParamTable.Emit(tw, lbl, tp.Obj().Name(), constraintLabel, parentlbl, tp.Index())
15831582
case *types.Union:
@@ -1718,7 +1717,7 @@ func getTypeLabel(tw *trap.Writer, tp types.Type) (trap.Label, bool) {
17181717
}
17191718
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%s};namedtype", entitylbl))
17201719
case *types.TypeParam:
1721-
parentlbl := tw.TypeParamParent[tp]
1720+
parentlbl := getTypeParamParentLabel(tw, tp)
17221721
lbl = tw.Labeler.GlobalID(fmt.Sprintf("{%v},%s;typeparamtype", parentlbl, tp.Obj().Name()))
17231722
case *types.Union:
17241723
var b strings.Builder
@@ -1903,11 +1902,11 @@ func extractTypeParamDecls(tw *trap.Writer, fields *ast.FieldList, parent trap.L
19031902
}
19041903
}
19051904

1906-
// populateTypeParamParents sets `parentlbl` as the parent of the elements of `typeparams`
1907-
func populateTypeParamParents(tw *trap.Writer, typeparams *types.TypeParamList, parentlbl trap.Label) {
1905+
// populateTypeParamParents sets `parent` as the parent of the elements of `typeparams`
1906+
func populateTypeParamParents(tw *trap.Writer, typeparams *types.TypeParamList, parent types.Object) {
19081907
if typeparams != nil {
19091908
for idx := 0; idx < typeparams.Len(); idx++ {
1910-
tw.TypeParamParent[typeparams.At(idx)] = parentlbl
1909+
setTypeParamParent(typeparams.At(idx), parent)
19111910
}
19121911
}
19131912
}
@@ -1999,3 +1998,24 @@ func trackInstantiatedStructFields(tw *trap.Writer, tp, origintp *types.Named) {
19991998
}
20001999
}
20012000
}
2001+
2002+
func getTypeParamParentLabel(tw *trap.Writer, tp *types.TypeParam) trap.Label {
2003+
parent, exists := typeParamParent[tp]
2004+
if !exists {
2005+
log.Fatalf("Parent of type parameter does not exist: %s %s", tp.String(), tp.Constraint().String())
2006+
}
2007+
parentlbl, _ := tw.Labeler.ScopedObjectID(parent, func() trap.Label {
2008+
log.Fatalf("getTypeLabel() called for parent of type parameter %s", tp.String())
2009+
return trap.InvalidLabel
2010+
})
2011+
return parentlbl
2012+
}
2013+
2014+
func setTypeParamParent(tp *types.TypeParam, newobj types.Object) {
2015+
obj, exists := typeParamParent[tp]
2016+
if !exists {
2017+
typeParamParent[tp] = newobj
2018+
} else if newobj != obj {
2019+
log.Fatalf("Parent of type parameter '%s %s' being set to a different value: '%s' vs '%s'", tp.String(), tp.Constraint().String(), obj, newobj)
2020+
}
2021+
}

extractor/trap/trapwriter.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type Writer struct {
2626
trapFilePath string
2727
Package *packages.Package
2828
TypesOverride map[ast.Expr]types.Type
29-
TypeParamParent map[*types.TypeParam]Label
3029
ObjectsOverride map[types.Object]types.Object
3130
}
3231

@@ -66,7 +65,6 @@ func NewWriter(path string, pkg *packages.Package) (*Writer, error) {
6665
trapFilePath,
6766
pkg,
6867
make(map[ast.Expr]types.Type),
69-
make(map[*types.TypeParam]Label),
7068
make(map[types.Object]types.Object),
7169
}
7270
tw.Labeler = newLabeler(tw)

0 commit comments

Comments
 (0)
0