-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* basic cycle detection * add cycle detection issue
- Loading branch information
Showing
6 changed files
with
249 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package lints | ||
|
||
import ( | ||
"go/parser" | ||
"go/token" | ||
"testing" | ||
) | ||
|
||
func TestDetectCycle(t *testing.T) { | ||
src := ` | ||
package main | ||
type A struct { | ||
B *B | ||
} | ||
type B struct { | ||
A *A | ||
} | ||
var ( | ||
x = &y | ||
y = &x | ||
) | ||
func a() { | ||
b() | ||
} | ||
func b() { | ||
a() | ||
} | ||
func outer() { | ||
var inner func() | ||
inner = func() { | ||
outer() | ||
} | ||
inner() | ||
}` | ||
|
||
fset := token.NewFileSet() | ||
f, err := parser.ParseFile(fset, "", src, 0) | ||
if err != nil { | ||
t.Fatalf("failed to parse source: %v", err) | ||
} | ||
|
||
cycle := newCycle() | ||
result := cycle.detectCycles(f) | ||
if len(result) != 6 { | ||
// [B A B] | ||
// [B A B] | ||
// [x y x] | ||
// [b a b] | ||
// [outer outer$anon<address> outer] | ||
// [outer outer] | ||
t.Errorf("unexpected result: %v", result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package lints | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
|
||
tt "github.com/gnoswap-labs/lint/internal/types" | ||
) | ||
|
||
func DetectCycle(filename string) ([]tt.Issue, error) { | ||
fset := token.NewFileSet() | ||
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := newCycle() | ||
cycles := c.detectCycles(node) | ||
|
||
var issues []tt.Issue | ||
for _, cycle := range cycles { | ||
issue := tt.Issue{ | ||
Rule: "cycle-detection", | ||
Filename: filename, | ||
Start: fset.Position(node.Pos()), | ||
End: fset.Position(node.End()), | ||
Message: "Detected cycle in function call: " + cycle, | ||
} | ||
issues = append(issues, issue) | ||
} | ||
return issues, nil | ||
} | ||
|
||
type cycle struct { | ||
dependencies map[string][]string | ||
visited map[string]bool | ||
stack []string | ||
cycles []string | ||
} | ||
|
||
func newCycle() *cycle { | ||
return &cycle{ | ||
dependencies: make(map[string][]string), | ||
visited: make(map[string]bool), | ||
} | ||
} | ||
|
||
func (c *cycle) analyzeFuncDecl(fn *ast.FuncDecl) { | ||
name := fn.Name.Name | ||
c.dependencies[name] = []string{} | ||
|
||
ast.Inspect(fn.Body, func(n ast.Node) bool { | ||
switch x := n.(type) { | ||
case *ast.CallExpr: | ||
if ident, ok := x.Fun.(*ast.Ident); ok { | ||
c.dependencies[name] = append(c.dependencies[name], ident.Name) | ||
} | ||
case *ast.FuncLit: | ||
c.analyzeFuncLit(x, name) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func (c *cycle) analyzeFuncLit(fn *ast.FuncLit, parentName string) { | ||
anonName := fmt.Sprintf("%s$anon%p", parentName, fn) | ||
c.dependencies[anonName] = []string{} | ||
|
||
ast.Inspect(fn.Body, func(n ast.Node) bool { | ||
switch x := n.(type) { | ||
case *ast.CallExpr: | ||
if ident, ok := x.Fun.(*ast.Ident); ok { | ||
c.dependencies[anonName] = append(c.dependencies[anonName], ident.Name) | ||
} | ||
case *ast.FuncLit: | ||
c.analyzeFuncLit(x, anonName) | ||
} | ||
return true | ||
}) | ||
|
||
// add dependency from parent to anonymous function | ||
c.dependencies[parentName] = append(c.dependencies[parentName], anonName) | ||
} | ||
|
||
func (c *cycle) detectCycles(node ast.Node) []string { | ||
ast.Inspect(node, func(n ast.Node) bool { | ||
switch x := n.(type) { | ||
case *ast.FuncDecl: | ||
c.analyzeFuncDecl(x) | ||
case *ast.TypeSpec: | ||
c.analyzeTypeSpec(x) | ||
case *ast.ValueSpec: | ||
c.analyzeValueSpec(x) | ||
case *ast.FuncLit: | ||
// handle top-level anonymous functions | ||
c.analyzeFuncLit(x, "topLevel") | ||
} | ||
return true | ||
}) | ||
|
||
for name := range c.dependencies { | ||
if !c.visited[name] { | ||
c.dfs(name) | ||
} | ||
} | ||
|
||
return c.cycles | ||
} | ||
|
||
func (c *cycle) analyzeTypeSpec(ts *ast.TypeSpec) { | ||
name := ts.Name.Name | ||
c.dependencies[name] = []string{} | ||
|
||
ast.Inspect(ts.Type, func(n ast.Node) bool { | ||
if ident, ok := n.(*ast.Ident); ok { | ||
c.dependencies[name] = append(c.dependencies[name], ident.Name) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func (c *cycle) analyzeValueSpec(vs *ast.ValueSpec) { | ||
for i, name := range vs.Names { | ||
c.dependencies[name.Name] = []string{} | ||
if vs.Values != nil && i < len(vs.Values) { | ||
ast.Inspect(vs.Values[i], func(n ast.Node) bool { | ||
if ident, ok := n.(*ast.Ident); ok { | ||
c.dependencies[name.Name] = append(c.dependencies[name.Name], ident.Name) | ||
} | ||
return true | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func (c *cycle) dfs(name string) { | ||
c.visited[name] = true | ||
c.stack = append(c.stack, name) | ||
|
||
for _, dep := range c.dependencies[name] { | ||
if !c.visited[dep] { | ||
c.dfs(dep) | ||
} else if contains(c.stack, dep) { | ||
cycle := append(c.stack[indexOf(c.stack, dep):], dep) | ||
res := fmt.Sprintf("%v", cycle) | ||
c.cycles = append(c.cycles, res) | ||
} | ||
} | ||
|
||
c.stack = c.stack[:len(c.stack)-1] | ||
} | ||
|
||
func contains(slice []string, item string) bool { | ||
for _, v := range slice { | ||
if v == item { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func indexOf(slice []string, item string) int { | ||
for i, v := range slice { | ||
if v == item { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pkg | ||
|
||
func a() { // want `a` | ||
b() | ||
} | ||
|
||
func b() { // want `b` | ||
a() | ||
} |