Skip to content
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

Handle assignments to global variables from the init function #253

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion assertion/global/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,32 @@ func run(pass *analysis.Pass) ([]annotation.FullTrigger, error) {
if !conf.IsFileInScope(file) {
continue
}
initFuncDecl := getInitFuncDecl(file)

for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.VAR {
continue
}
for _, spec := range genDecl.Specs {
fullTriggers = append(fullTriggers, analyzeValueSpec(pass, spec.(*ast.ValueSpec))...)
fullTriggers = append(fullTriggers, analyzeValueSpec(pass, spec.(*ast.ValueSpec), initFuncDecl)...)
}
}
}

return fullTriggers, nil
}

// getInitFuncDecl searches for the init function declaration in the given *ast.File.
// It returns the *ast.FuncDecl representing the init function if found, or nil otherwise.
func getInitFuncDecl(file *ast.File) *ast.FuncDecl {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only returns the first init function. I believe there can be an arbitrary number of them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I have made the changes you suggested and added additional tests.

if file == nil {
return nil
}
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok && funcDecl.Name.Name == "init" {
return funcDecl
}
}
return nil
}
40 changes: 36 additions & 4 deletions assertion/global/globalvarinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
)

// analyzeValueSpec returns full triggers corresponding to the declaration
func analyzeValueSpec(pass *analysis.Pass, spec *ast.ValueSpec) []annotation.FullTrigger {
func analyzeValueSpec(pass *analysis.Pass, spec *ast.ValueSpec, initFuncDecl *ast.FuncDecl) []annotation.FullTrigger {
var fullTriggers []annotation.FullTrigger

consumers := getGlobalConsumers(pass, spec)
consumers := getGlobalConsumers(pass, spec, initFuncDecl)

for i, ident := range spec.Names {
if consumers[i] == nil {
Expand Down Expand Up @@ -63,12 +63,12 @@ func analyzeValueSpec(pass *analysis.Pass, spec *ast.ValueSpec) []annotation.Ful
}

// Returns a list of consumers corresponding to a global level variable declaration
func getGlobalConsumers(pass *analysis.Pass, valspec *ast.ValueSpec) []*annotation.ConsumeTrigger {
func getGlobalConsumers(pass *analysis.Pass, valspec *ast.ValueSpec, initFuncDecl *ast.FuncDecl) []*annotation.ConsumeTrigger {
consumers := make([]*annotation.ConsumeTrigger, len(valspec.Names))

for i, name := range valspec.Names {
// Types that are not nilable are eliminated here
if !util.TypeBarsNilness(pass.TypesInfo.TypeOf(name)) && !util.IsEmptyExpr(name) {
if !util.TypeBarsNilness(pass.TypesInfo.TypeOf(name)) && !util.IsEmptyExpr(name) && !hasGlobalVarAssignInInitFunc(valspec, initFuncDecl) {
v := pass.TypesInfo.ObjectOf(name).(*types.Var)
consumers[i] = &annotation.ConsumeTrigger{
Annotation: &annotation.GlobalVarAssign{
Expand All @@ -85,6 +85,38 @@ func getGlobalConsumers(pass *analysis.Pass, valspec *ast.ValueSpec) []*annotati
return consumers
}

// Checks if all the global variables represented by spec are assigned values within the init function.
// It returns true if all variables are assigned, false otherwise.
// If initFuncDecl is nil, it returns false.
func hasGlobalVarAssignInInitFunc(spec *ast.ValueSpec, initFuncDecl *ast.FuncDecl) bool {
if initFuncDecl == nil {
return false
}
assignedVars := make(map[string]bool)
for _, name := range spec.Names {
assignedVars[name.Name] = false
}
ast.Inspect(initFuncDecl.Body, func(node ast.Node) bool {
if assign, ok := node.(*ast.AssignStmt); ok {
for _, lhs := range assign.Lhs {
if ident, ok := lhs.(*ast.Ident); ok {
if _, exists := assignedVars[ident.Name]; exists {
assignedVars[ident.Name] = true
}
}
}
}
return true
})

for _, assigned := range assignedVars {
if !assigned {
return false
}
}
return true
}

// Returns a producer in the cases: 1) func call 2) literal nil 3) another global var 4) struct field/method.
// In all other cases, it returns nil.
func getGlobalProducer(pass *analysis.Pass, valspec *ast.ValueSpec, lid int, rid int) *annotation.ProduceTrigger {
Expand Down
8 changes: 8 additions & 0 deletions testdata/src/go.uber.org/globalvars/globalvarinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ var x = 3

// This should throw an error since it is not initialized
var noInit *int //want "assigned into global variable"
var _init *int
var _initMult1, _initMult2 *int

func init() {
_init = new(int)
_initMult1 = new(int)
_initMult2 = new(int)
}

// nilable(nilableVar)
var nilableVar *int
Expand Down
Loading