aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler/consts.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2025-01-17 10:39:47 +0100
committerDmitry Vyukov <dvyukov@google.com>2025-01-17 18:09:32 +0000
commitf2cb035c8f931efff4a020b164e657f16f51934b (patch)
tree967cd39fb98171cba878893a41ca461ffa993c8c /pkg/compiler/consts.go
parent38ee454540b9b41d5cc173871dfbf7dd140e8abc (diff)
pkg/declextract: remove unused includes and defines
This is nice on its own, but this will also help to prevent lots of problems when we export more info from the clang tool in future. The clang tool does not know what will end up in the final descriptions, so it exports info about all consts that it encounters. As the result we pull in lots of includes/defines, and lots of kernel includes/defines are broken or create problems. So the fewer we have, the better.
Diffstat (limited to 'pkg/compiler/consts.go')
-rw-r--r--pkg/compiler/consts.go13
1 files changed, 9 insertions, 4 deletions
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,
}
}