MCPcopy
hub / github.com/uber-go/nilaway / newObservedMap

Function newObservedMap

annotation/map.go:420–695  ·  view source on GitHub ↗
(pass *analysishelper.EnhancedPass, files []*ast.File)

Source from the content-addressed store, hash-verified

418}
419
420func newObservedMap(pass *analysishelper.EnhancedPass, files []*ast.File) *ObservedMap {
421 conf := pass.ResultOf[config.Analyzer].(*config.Config)
422 // TODO - only store annotations for fields/vars/parameters of types that do not bar nilness
423
424 fieldAnnMap := make(map[*types.Var]Val)
425 funcParamAnnMap := make(map[*types.Func][]Val)
426 funcRetAnnMap := make(map[*types.Func][]Val)
427 funcRecvAnnMap := make(map[*types.Func]Val)
428 paramIndexMap := make(map[*types.Var]int)
429 deepTypeAnnMap := make(map[*types.TypeName]Val)
430 globalVarsAnnMap := make(map[*types.Var]Val)
431
432 funcObjToFuncDecl := make(map[*types.Func]*ast.FuncDecl)
433 funcCallSiteParamAnnMap := make(map[CallSite][]ArgLocAndVal)
434 funcCallSiteRetAnnMap := make(map[CallSite][]Val)
435
436 typeOf := func(expr ast.Expr) types.Type {
437 return pass.TypesInfo.Types[expr].Type
438 }
439
440 // for a function declaration, accumulate its parameters from an *ast.Fieldlist object
441 // listing them, look them up in the docstring, and return an equally long list of
442 // annotationVals
443 accFromFieldList := func(set nilabilitySet, fieldList *ast.FieldList, isParamList bool,
444 isCallSiteAnnotation bool) []Val {
445 if fieldList == nil {
446 // this is included for nil-safety
447 return nil
448 }
449
450 var annVals []Val
451 var lookupKey string
452 for _, field := range fieldList.List {
453 if len(field.Names) == 0 {
454 // case of anonymous field - on which we do not permit annotations
455 // non-named fields
456
457 if isParamList {
458 lookupKey = paramStr(len(annVals))
459 } else {
460 lookupKey = resultStr(len(annVals))
461 }
462
463 annVals = append(annVals, set.checkNilability(lookupKey, typeOf(field.Type)))
464 } else {
465 for _, name := range field.Names {
466 declFld := pass.TypesInfo.ObjectOf(name).(*types.Var)
467
468 // for each named field, check the docstring for its Annotation and append that
469 paramIndexMap[declFld] = len(annVals)
470
471 var fieldType types.Type
472 if t, ok := field.Type.(*ast.Ellipsis); ok {
473 // in the case that our argument is variadic (hence has a type expression
474 // of the form `...T`, we treat the arguments as having type `T` not type
475 // `T[]`
476 fieldType = typeOf(t.Elt)
477 } else {

Callers 1

runFunction · 0.85

Calls 11

FuncIdentFromCallExprFunction · 0.92
paramStrFunction · 0.85
resultStrFunction · 0.85
getLineFromPosFunction · 0.85
checkNilabilityMethod · 0.80
ObjectOfMethod · 0.80
PanicMethod · 0.80
IsFileInScopeMethod · 0.80
PosToLocationMethod · 0.80
PosMethod · 0.45

Tested by

no test coverage detected