Skip to content

Commit 73baca2

Browse files
Merge pull request #5 from chinmay-sawant/fix-4-astref
Refactored the code as per the #4 changes
2 parents e050e2f + 6f67282 commit 73baca2

File tree

10 files changed

+523
-231
lines changed

10 files changed

+523
-231
lines changed

cmd/analyzer/utils.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func FindFunctionBody(lines []string, funcLine int) (int, int) {
5555
func FindCalls(bodyLines []string) []string {
5656
var calls []string
5757
reCalls := regexp.MustCompile(`(\w+(?:\.\w+)*)\(`)
58+
// Enhanced regex to capture function references passed as arguments
59+
// This captures function names that appear as arguments (not followed by parentheses)
60+
reFuncRefs := regexp.MustCompile(`(?:\s|,|\()(handle[A-Za-z]\w+)(?:\s*[,\)\s]|$)`)
5861

5962
// Standard library packages to ignore
6063
standardPackages := map[string]bool{
@@ -140,7 +143,46 @@ func FindCalls(bodyLines []string) []string {
140143
"Wait": true,
141144
}
142145

146+
// Keywords and common variable names to exclude
147+
excludeKeywords := map[string]bool{
148+
"true": true,
149+
"false": true,
150+
"nil": true,
151+
"err": true,
152+
"error": true,
153+
"string": true,
154+
"int": true,
155+
"float": true,
156+
"bool": true,
157+
"byte": true,
158+
"rune": true,
159+
"if": true,
160+
"for": true,
161+
"switch": true,
162+
"case": true,
163+
"default": true,
164+
"return": true,
165+
"break": true,
166+
"continue": true,
167+
"goto": true,
168+
"var": true,
169+
"const": true,
170+
"type": true,
171+
"func": true,
172+
"package": true,
173+
"import": true,
174+
"range": true,
175+
"select": true,
176+
"go": true,
177+
"defer": true,
178+
"chan": true,
179+
"map": true,
180+
"struct": true,
181+
"interface": true,
182+
}
183+
143184
for _, line := range bodyLines {
185+
// Find traditional function calls (package.function())
144186
matches := reCalls.FindAllStringSubmatch(line, -1)
145187
for _, match := range matches {
146188
call := match[1]
@@ -174,6 +216,34 @@ func FindCalls(bodyLines []string) []string {
174216
calls = append(calls, call)
175217
}
176218
}
219+
220+
// Find function references passed as arguments
221+
refMatches := reFuncRefs.FindAllStringSubmatch(line, -1)
222+
for _, match := range refMatches {
223+
if len(match) > 1 {
224+
funcRef := strings.TrimSpace(match[1])
225+
226+
// Skip if it's a keyword or common variable name
227+
if excludeKeywords[funcRef] {
228+
continue
229+
}
230+
231+
// Skip if it's too short to be meaningful
232+
if len(funcRef) < 3 {
233+
continue
234+
}
235+
236+
// Skip if it contains dots (already handled by reCalls)
237+
if strings.Contains(funcRef, ".") {
238+
continue
239+
}
240+
241+
// Add the current package prefix to make it consistent with other calls
242+
if !contains(calls, funcRef) {
243+
calls = append(calls, funcRef)
244+
}
245+
}
246+
}
177247
}
178248
return calls
179249
}

cmd/main.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ func findFunctions(filePath, absPath, module string) ([]analyzer.FunctionInfo, e
9393
}
9494
}
9595

96+
// Collect all function names in this file for reference resolution
97+
var localFunctions []string
98+
for _, line := range lines {
99+
if matches := re.FindStringSubmatch(line); matches != nil {
100+
localFunctions = append(localFunctions, matches[1])
101+
}
102+
}
103+
96104
for i, line := range lines {
97105
if matches := re.FindStringSubmatch(line); matches != nil {
98106
funcInfo := analyzer.FunctionInfo{
@@ -104,7 +112,23 @@ func findFunctions(filePath, absPath, module string) ([]analyzer.FunctionInfo, e
104112
start, end := analyzer.FindFunctionBody(lines, i)
105113
if start != -1 && end != -1 && start+1 < end && end < len(lines) {
106114
calls := analyzer.FindCalls(lines[start+1 : end])
107-
funcInfo.Calls = calls
115+
116+
// Resolve local function references by adding package prefix
117+
var resolvedCalls []string
118+
for _, call := range calls {
119+
if !strings.Contains(call, ".") {
120+
// Check if it's a local function reference
121+
for _, localFunc := range localFunctions {
122+
if call == localFunc {
123+
resolvedCalls = append(resolvedCalls, packageName+"."+call)
124+
break
125+
}
126+
}
127+
} else {
128+
resolvedCalls = append(resolvedCalls, call)
129+
}
130+
}
131+
funcInfo.Calls = resolvedCalls
108132
}
109133
funcs = append(funcs, funcInfo)
110134
}

cmd/server/main.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,19 +439,52 @@ func findFunctions(filePath, absPath, module string) ([]analyzer.FunctionInfo, e
439439
if err != nil {
440440
return nil, err
441441
}
442+
443+
// Find package name
442444
var packageName string
443445
for _, line := range lines {
444446
if strings.HasPrefix(line, "package ") {
445447
packageName = strings.TrimSpace(strings.TrimPrefix(line, "package "))
446448
break
447449
}
448450
}
451+
452+
// Collect all function names in this file for reference resolution
453+
var localFunctions []string
454+
for _, line := range lines {
455+
if matches := re.FindStringSubmatch(line); matches != nil {
456+
localFunctions = append(localFunctions, matches[1])
457+
}
458+
}
459+
449460
for i, line := range lines {
450461
if matches := re.FindStringSubmatch(line); matches != nil {
451-
fi := analyzer.FunctionInfo{Name: packageName + "." + matches[1], Line: i + 1, FilePath: relPath}
462+
fi := analyzer.FunctionInfo{
463+
Name: packageName + "." + matches[1],
464+
Line: i + 1,
465+
FilePath: relPath,
466+
}
467+
// Find function body
452468
start, end := analyzer.FindFunctionBody(lines, i)
453469
if start != -1 && end != -1 && start+1 < end && end < len(lines) {
454-
fi.Calls = analyzer.FindCalls(lines[start+1 : end])
470+
calls := analyzer.FindCalls(lines[start+1 : end])
471+
472+
// Resolve local function references by adding package prefix
473+
var resolvedCalls []string
474+
for _, call := range calls {
475+
if !strings.Contains(call, ".") {
476+
// Check if it's a local function reference
477+
for _, localFunc := range localFunctions {
478+
if call == localFunc {
479+
resolvedCalls = append(resolvedCalls, packageName+"."+call)
480+
break
481+
}
482+
}
483+
} else {
484+
resolvedCalls = append(resolvedCalls, call)
485+
}
486+
}
487+
fi.Calls = resolvedCalls
455488
}
456489
funcs = append(funcs, fi)
457490
}

0 commit comments

Comments
 (0)