diff options
Diffstat (limited to 'pkg/compiler')
| -rw-r--r-- | pkg/compiler/check.go | 38 | ||||
| -rw-r--r-- | pkg/compiler/consts.go | 13 |
2 files changed, 47 insertions, 4 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 92e3e5e03..fbf84857d 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -676,6 +676,44 @@ func CollectUnused(desc *ast.Description, target *targets.Target, eh ast.ErrorHa return nodes, nil } +// CollectUnusedConsts returns unused defines/includes. This is used only for auto-generated descriptions. +func CollectUnusedConsts(desc *ast.Description, target *targets.Target, includeUse map[string]string, + eh ast.ErrorHandler) ([]ast.Node, error) { + comp := createCompiler(desc, target, eh) + comp.typecheck() + if comp.errors > 0 { + return nil, errors.New("typecheck failed") + } + + var unused []ast.Node + for file, info := range comp.extractConsts() { + if !comp.fileMeta(ast.Pos{File: file}).Automatic { + continue + } + usedDefines := make(map[string]bool) + usedIncludes := make(map[string]bool) + for _, c := range info.Consts { + if c.Used { + usedDefines[c.Name] = true + usedIncludes[includeUse[c.Name]] = true + } + } + for _, decl := range comp.desc.Nodes { + switch n := decl.(type) { + case *ast.Define: + if n.Pos.File == file && !usedDefines[n.Name.Name] { + unused = append(unused, n) + } + case *ast.Include: + if n.Pos.File == file && !usedIncludes[n.File.Value] { + unused = append(unused, n) + } + } + } + } + return unused, nil +} + func (comp *compiler) collectUnused() []ast.Node { var unused []ast.Node diff --git a/pkg/compiler/consts.go b/pkg/compiler/consts.go index bcbd40e52..6ce3f81f1 100644 --- a/pkg/compiler/consts.go +++ b/pkg/compiler/consts.go @@ -23,6 +23,7 @@ type ConstInfo struct { type Const struct { Name string Pos ast.Pos + Used bool // otherwise only defined } func ExtractConsts(desc *ast.Description, target *targets.Target, eh ast.ErrorHandler) map[string]*ConstInfo { @@ -86,7 +87,7 @@ func (comp *compiler) extractConsts() map[string]*ConstInfo { comp.error(pos, "redefining builtin const %v", name) } info.defines[name] = v - comp.addConst(ctx, pos, name) + ctx.addConst(pos, name, false) case *ast.Call: if comp.target.HasCallNumber(n.CallName) { comp.addConst(ctx, pos, comp.target.SyscallPrefix+n.CallName) @@ -178,10 +179,10 @@ func (comp *compiler) addConst(ctx *constContext, pos ast.Pos, name string) { if _, isFlag := comp.intFlags[name]; isFlag { return } - ctx.addConst(pos, name) + ctx.addConst(pos, name, true) for _, instantions := range ctx.instantionStack { for _, pos1 := range instantions { - ctx.addConst(pos1, name) + ctx.addConst(pos1, name, true) } } } @@ -193,11 +194,15 @@ type constInfo struct { incdirArray []string } -func (ctx *constContext) addConst(pos ast.Pos, name string) { +func (ctx *constContext) addConst(pos ast.Pos, name string, used bool) { info := ctx.getConstInfo(pos) + if c := info.consts[name]; c != nil && c.Used { + used = true + } info.consts[name] = &Const{ Pos: pos, Name: name, + Used: used, } } |
