This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
/
unused.go
78 lines (65 loc) · 1.79 KB
/
unused.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package semantic
import (
"github.com/ark-lang/ark/src/ast"
)
type UnusedCheck struct {
encountered []interface{}
encounteredDecl []ast.Node
uses map[interface{}]int
}
func (_ UnusedCheck) Name() string { return "unused" }
func (v *UnusedCheck) Init(s *SemanticAnalyzer) {
v.uses = make(map[interface{}]int)
v.encountered = nil
v.encounteredDecl = nil
}
func (v *UnusedCheck) EnterScope(s *SemanticAnalyzer) {}
func (v *UnusedCheck) ExitScope(s *SemanticAnalyzer) {}
func (v *UnusedCheck) PostVisit(s *SemanticAnalyzer, n ast.Node) {}
func (v *UnusedCheck) Visit(s *SemanticAnalyzer, n ast.Node) {
switch n := n.(type) {
case *ast.VariableDecl:
if !n.IsPublic() {
v.encountered = append(v.encountered, n.Variable)
v.encounteredDecl = append(v.encounteredDecl, n)
}
case *ast.DestructVarDecl:
if !n.IsPublic() {
for idx, vari := range n.Variables {
if !n.ShouldDiscard[idx] {
v.encountered = append(v.encountered, vari)
v.encounteredDecl = append(v.encounteredDecl, n)
}
}
}
case *ast.FunctionDecl:
if !n.IsPublic() {
v.encountered = append(v.encountered, n.Function)
v.encounteredDecl = append(v.encounteredDecl, n)
}
}
switch n := n.(type) {
case *ast.FunctionAccessExpr:
v.uses[n.Function]++
case *ast.VariableAccessExpr:
v.uses[n.Variable]++
}
}
func (v *UnusedCheck) Finalize(s *SemanticAnalyzer) {
v.AnalyzeUsage(s)
}
func (v *UnusedCheck) AnalyzeUsage(s *SemanticAnalyzer) {
for idx, it := range v.encountered {
decl := v.encounteredDecl[idx]
switch it := it.(type) {
case *ast.Variable:
if !it.IsImplicit && v.uses[it] == 0 {
s.Warn(decl, "Unused variable `%s`", it.Name)
}
case *ast.Function:
if v.uses[it] == 0 {
s.Warn(decl, "Unused function `%s`", it.Name)
}
}
}
}