Skip to content

Commit

Permalink
fixbug
Browse files Browse the repository at this point in the history
  • Loading branch information
fananchong committed Dec 19, 2023
1 parent 7f5da99 commit 90ef1cc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
58 changes: 30 additions & 28 deletions analyzer_globalvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (analyzer *VarAnalyzer) FindVar(pass *analysis.Pass) {
if !ok || genDecl.Tok != token.VAR {
continue
}
var mutexValueSpec *ast.ValueSpec
var mutexValueSpecs []*ast.ValueSpec
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
if !isMutexType2(valueSpec) {
Expand All @@ -33,38 +33,40 @@ func (analyzer *VarAnalyzer) FindVar(pass *analysis.Pass) {
v, _ := obj.(*types.Var)
isGlobal := !v.IsField() && !v.Embedded() && v.Parent() == pass.Pkg.Scope() // 全局变量
if isGlobal {
mutexValueSpec = valueSpec
mutexValueSpecs = append(mutexValueSpecs, valueSpec)
}
}
}
if mutexValueSpec == nil {
if len(mutexValueSpecs) == 0 {
continue
}
pos := pass.Fset.Position(mutexValueSpec.Pos())
mutexVar := analyzer.getGlobalVarByPos(analyzer.prog, pos)
comment := ""
if mutexValueSpec.Comment != nil {
comment = strings.ReplaceAll(mutexValueSpec.Comment.Text(), " ", "")
comment = strings.ReplaceAll(comment, "\n", "")
}
if comment == "" {
fmt.Printf("[mutex check] %v:%v mutex 变量没有注释,指明它要锁的变量\n", pos.Filename, pos.Line)
continue
}
if nolint(comment) {
continue
}
varNames := strings.Split(comment, ",")
for _, name := range varNames {
valueSpec := analyzer.getGlobalVarByName(pass, file, name)
if valueSpec == nil {
pos := pass.Fset.Position(mutexValueSpec.Pos())
fmt.Printf("[mutex check] %v:%v mutex 变量注释中的变量 %v ,未声明\n", pos.Filename, pos.Line, name)
break
} else {
pos := pass.Fset.Position(valueSpec.Pos())
v := analyzer.getGlobalVarByPos(analyzer.prog, pos)
analyzer.vars[v] = mutexVar
for _, mutexValueSpec := range mutexValueSpecs {
pos := pass.Fset.Position(mutexValueSpec.Pos())
mutexVar := analyzer.getGlobalVarByPos(analyzer.prog, pos)
comment := ""
if mutexValueSpec.Comment != nil {
comment = strings.ReplaceAll(mutexValueSpec.Comment.Text(), " ", "")
comment = strings.ReplaceAll(comment, "\n", "")
}
if comment == "" {
fmt.Printf("[mutex check] %v:%v mutex 变量没有注释,指明它要锁的变量\n", pos.Filename, pos.Line)
continue
}
if nolint(comment) {
continue
}
varNames := strings.Split(comment, ",")
for _, name := range varNames {
valueSpec := analyzer.getGlobalVarByName(pass, file, name)
if valueSpec == nil {
pos := pass.Fset.Position(mutexValueSpec.Pos())
fmt.Printf("[mutex check] %v:%v mutex 变量注释中的变量 %v ,未声明\n", pos.Filename, pos.Line, name)
break
} else {
pos := pass.Fset.Position(valueSpec.Pos())
v := analyzer.getGlobalVarByPos(analyzer.prog, pos)
analyzer.vars[v] = mutexVar
}
}
}
}
Expand Down
19 changes: 7 additions & 12 deletions test/test1.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package test

import (
"fmt"
"sync"
)
// var m1 sync.Mutex // a1
// var a1 int

var m1 sync.Mutex // a1
var a1 int

func f1() {
m1.Lock()
// defer m1.Unlock()
fmt.Print(a1)
}
// func f1() {
// m1.Lock()
// // defer m1.Unlock()
// fmt.Print(a1)
// }
8 changes: 8 additions & 0 deletions test/test10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test

import "sync"

var (
m1 = sync.RWMutex{}
m2 = &sync.RWMutex{}
)
31 changes: 13 additions & 18 deletions test/test8.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package test

import (
"fmt"
"sync"
)
// type b struct {
// *sync.Mutex // a8
// a8 int
// }

type b struct {
*sync.Mutex // a8
a8 int
}
// func (b1 *b) f1() {
// b1.Lock()
// // defer b1.Unlock()
// fmt.Print(b1.a8)
// }

func (b1 *b) f1() {
b1.Lock()
// defer b1.Unlock()
fmt.Print(b1.a8)
}

func init() {
b1 := b{}
b1.f1()
}
// func init() {
// b1 := b{}
// b1.f1()
// }

0 comments on commit 90ef1cc

Please sign in to comment.