Skip to content

Commit 43c8026

Browse files
committed
sync to LuaHelper with commit: c578578
1 parent c49fda7 commit 43c8026

File tree

7 files changed

+26
-94
lines changed

7 files changed

+26
-94
lines changed

luahelper-lsp/langserver/check/analysis/analysis.go

+6-85
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package analysis
22

33
import (
4-
"fmt"
54
"luahelper-lsp/langserver/check/common"
65
"luahelper-lsp/langserver/check/projects"
76
"luahelper-lsp/langserver/check/results"
87
"luahelper-lsp/langserver/log"
8+
"sync"
99
)
1010

1111
// IgnoreInfo 忽略的信息
@@ -60,6 +60,9 @@ type Analysis struct {
6060
extMark string
6161
}
6262

63+
// 注解互斥锁
64+
var mutex sync.Mutex
65+
6366
// CreateAnalysis 创建一个分析的结构
6467
func CreateAnalysis(checkTerm results.CheckTerm, entryFile string) *Analysis {
6568
return &Analysis{
@@ -120,90 +123,8 @@ func (a *Analysis) enterScope() {
120123
// 出一个scope
121124
func (a *Analysis) exitScope() {
122125
a.curFunc.ExitScope()
123-
124-
// 如果是一轮校验,判断是否要校验局部变量是否定义未使用
125-
if !a.isFirstTerm() || a.realTimeFlag {
126-
return
127-
}
128-
129-
// 判断是否开启了局部变量定义了是否未使用的告警
130-
if common.GConfig.IsGlobalIgnoreErrType(common.CheckErrorLocalNoUse) {
131-
return
132-
}
133-
134-
scope := a.curScope
135-
if scope == nil {
136-
return
137-
}
138-
139-
fileResult := a.curResult
140-
141-
// 扫描当前scope,判断哪些局部变量定义了未使用
142-
for varName, varInfoList := range scope.LocVarMap {
143-
// _ 局部变量忽略, _G也忽略
144-
if varName == "_" || varName == "_G" {
145-
continue
146-
}
147-
148-
// class/struct/extension 中的 super / Self / Super 忽略
149-
if a.curScope.ExtMark == "class" && (varName == "__st" || varName == "Self" || varName == "Super") {
150-
continue
151-
}
152-
153-
if common.GConfig.IsIgnoreLocNotUseVar(varName) {
154-
// 如果为系统忽略的局部变量定义了,未使用的,忽略掉
155-
continue
156-
}
157-
158-
for _, oneVar := range varInfoList.VarVec {
159-
if oneVar.IsUse || oneVar.IsClose {
160-
continue
161-
}
162-
163-
// 定义的局部函数忽略
164-
if oneVar.ReferFunc != nil {
165-
continue
166-
}
167-
168-
// 判断指向的关联变量,是否为系统的函数或模块
169-
// 例如 local math = math 这样的忽略掉
170-
expName := common.GetExpName(oneVar.ReferExp)
171-
172-
// 1) 判断是否直接关联到的系统模块或函数
173-
oneStr := common.GetExpSubKey(expName)
174-
if oneStr != "" {
175-
if common.GConfig.IsInSysNotUseMap(oneStr) {
176-
// 为系统的模块或函数名,忽略掉
177-
continue
178-
}
179-
}
180-
181-
// 2) 判断是否关联到系统模块的成员, 例如:local concat = table.concat
182-
flagG, strRet := common.StrRemovePreG(expName)
183-
if flagG {
184-
expName = "!" + strRet
185-
}
186-
moduleName, keyName := common.GetTableStrTwoStr(expName)
187-
if moduleName != "" && keyName != "" {
188-
if common.GConfig.IsInSysNotUseMap(moduleName) {
189-
// 为系统的模块或函数名,忽略掉
190-
continue
191-
}
192-
}
193-
194-
errorStr := fmt.Sprintf("%s declared and not used", varName)
195-
fileResult.InsertError(common.CheckErrorLocalNoUse, errorStr, oneVar.Loc)
196-
197-
// 遍历所有的定义了未使用,只是简单的赋值
198-
for _, subVar := range oneVar.NoUseAssignLocs {
199-
errorStr := fmt.Sprintf("%s declared and not used, this just assign", varName)
200-
fileResult.InsertError(common.CheckErrorNoUseAssign, errorStr, subVar)
201-
}
202-
203-
// 清除掉
204-
oneVar.NoUseAssignLocs = nil
205-
}
206-
}
126+
a.checkLocVarCall()
127+
a.checkLocFuncCall()
207128
}
208129

209130
// 向分析的单个工程中,添加全局定义的变量

luahelper-lsp/langserver/check/analysis/analysis_check_func.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
)
99

10-
//函数调用处的参数类型检查
10+
// 函数调用处的参数类型检查
1111
func (a *Analysis) funcCallParamTypeCheck(node *ast.FuncCallStat, referFunc *common.FuncInfo, findTerm int) {
1212

1313
// 第二轮或第三轮函数参数check
@@ -33,7 +33,10 @@ func (a *Analysis) funcCallParamTypeCheck(node *ast.FuncCallStat, referFunc *com
3333
break
3434
}
3535

36+
mutex.Lock()
3637
allAnnTypeVec, ok := referFunc.ParamType[referFunc.ParamList[i]]
38+
mutex.Unlock()
39+
3740
if !ok {
3841
//该参数没写注解
3942
continue

luahelper-lsp/langserver/check/analysis/analysis_search.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ func (a *Analysis) ChangeSelfToReferVar(strTable string, prefixStr string) (str
10311031
} else {
10321032
// 获取第一轮的结构,因为此时RelateVar的结构还没有构造,只能查找第一轮的数据
10331033
fileStruct, _ := a.Projects.GetFirstFileStuct(fileResult.Name)
1034-
if fileStruct == nil {
1034+
if fileStruct == nil || fileStruct.FileResult == nil {
10351035
return
10361036
}
10371037

luahelper-lsp/langserver/check/analysis/analysis_util.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ func (a *Analysis) findVarDefine(varName string, varLoc lexer.Location) (find bo
1414
if find {
1515
if varInfo.IsParam {
1616
// 如果是函数参数 同时返回参数类型
17+
mutex.Lock()
1718
varType, ok := a.curFunc.ParamType[varName]
19+
mutex.Unlock()
1820
if ok {
1921
return find, varInfo, varType
2022
}
@@ -138,7 +140,7 @@ func (a *Analysis) findVarDefineWithPre(preName string, varName string, preLoc l
138140
return find, varInfo, false, varType
139141
}
140142

141-
//GetAnnTypeByExp 获取表达式类型字符串,如果是引用,则递归查找,(即支持类型传递)
143+
// GetAnnTypeByExp 获取表达式类型字符串,如果是引用,则递归查找,(即支持类型传递)
142144
func (a *Analysis) GetAnnTypeByExp(referExp ast.Exp, idx int) (retVec []string) {
143145
expType := common.GetExpType(referExp)
144146
argType := common.GetAnnTypeFromLuaType(expType)
@@ -283,16 +285,17 @@ func (a *Analysis) loadFuncParamAnnType(referFunc *common.FuncInfo) {
283285
paramTypeMap := a.Projects.GetFuncParamType(referFunc.FileName, referFunc.Loc.StartLine-1)
284286
if len(paramTypeMap) > 0 {
285287
//从函数上方获取到了注解
288+
mutex.Lock()
286289
for _, paramName := range referFunc.ParamList {
287290
if annTypeVec, ok := paramTypeMap[paramName]; ok {
288291
referFunc.ParamType[paramName] = []string{}
289292
for _, annTypeOne := range annTypeVec {
290293
typeOne := annotateast.GetAstTypeName(annTypeOne)
291294
referFunc.ParamType[paramName] = append(referFunc.ParamType[paramName], typeOne)
292-
293295
}
294296
}
295297
}
298+
mutex.Unlock()
296299

297300
//继续获取返回值注解
298301
referFunc.ReturnType = a.Projects.GetFuncReturnTypeVec(referFunc.FileName, referFunc.Loc.StartLine-1)
@@ -314,7 +317,11 @@ func (a *Analysis) loadFuncParamAnnType(referFunc *common.FuncInfo) {
314317
}
315318

316319
// 根据注解找成员函数
317-
referFunc.ParamType = a.Projects.GetFuncParamTypeByClass(classTypeStr, referFunc.FuncName)
320+
params := a.Projects.GetFuncParamTypeByClass(classTypeStr, referFunc.FuncName)
321+
mutex.Lock()
322+
referFunc.ParamType = params
323+
mutex.Unlock()
324+
318325
referFunc.ReturnType = a.Projects.GetFuncReturnTypeByClass(classTypeStr, referFunc.FuncName)
319326

320327
return

luahelper-lsp/langserver/lsp_cmd_run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (l *LspServer) RunLocalDiagnostices(localpath string) {
5454

5555
for file, info := range fileErrorMap {
5656
for _, errinfo := range info {
57-
fmt.Printf("%v`%v`%v\n", file, errinfo.Loc.StartLine, errinfo.ErrStr)
57+
fmt.Printf("%v, line=%v, errType=%v, errStr=%s\n", file, errinfo.Loc.StartLine, (int)(errinfo.ErrType),
58+
errinfo.ErrStr)
5859
}
5960
}
6061
}

luahelper-lsp/langserver/lsp_server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ func CreateServer() *jrpc2.Server {
128128
return lspServer.server
129129
}
130130

131-
//getAllProject 获取CheckProject
131+
// getAllProject 获取CheckProject
132132
func (g *LspServer) getAllProject() *check.AllProject {
133133
return g.project
134134
}
135135

136-
//getFileCache 获取文件缓冲map
136+
// getFileCache 获取文件缓冲map
137137
func (g *LspServer) getFileCache() *lspcommon.FileMapCache {
138138
return g.fileCache
139139
}

luahelper-lsp/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func main() {
5656
}
5757
}
5858

59-
//cmd 的方式运行rpc
59+
// cmd 的方式运行rpc
6060
func cmdRPC() {
6161
log.Debug("local stat running ....")
6262
Server := langserver.CreateServer()

0 commit comments

Comments
 (0)