Fixes #34869
Change-Id: I21bc60b9a5d1204dade1cceed6cddccf5b537b0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/200958
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
// Record user init functions.
for i := 0; i < renameinitgen; i++ {
s := lookupN("init.", i)
+ fn := asNode(s.Def).Name.Defn
+ // Skip init functions with empty bodies.
+ // noder.go doesn't allow external init functions, and
+ // order.go has already removed any OEMPTY nodes, so
+ // checking Len() == 0 is sufficient here.
+ if fn.Nbody.Len() == 0 {
+ continue
+ }
fns = append(fns, s.Linksym())
}
func deadcode(fn *Node) {
deadcodeslice(fn.Nbody)
+ deadcodefn(fn)
+}
+
+func deadcodefn(fn *Node) {
+ if fn.Nbody.Len() == 0 {
+ return
+ }
+
+ for _, n := range fn.Nbody.Slice() {
+ if n.Ninit.Len() > 0 {
+ return
+ }
+ switch n.Op {
+ case OIF:
+ if !Isconst(n.Left, CTBOOL) || n.Nbody.Len() > 0 || n.Rlist.Len() > 0 {
+ return
+ }
+ case OFOR:
+ if !Isconst(n.Left, CTBOOL) || n.Left.Bool() {
+ return
+ }
+ default:
+ return
+ }
+ }
+
+ fn.Nbody.Set([]*Node{nod(OEMPTY, nil, nil)})
}
func deadcodeslice(nn Nodes) {