1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // Simplified dead code detector.
8 // Used for skipping shift checks on unreachable arch-specific code.
16 // updateDead puts unreachable "if" and "case" nodes into dead.
17 func updateDead(info *types.Info, dead map[ast.Node]bool, node ast.Node) {
19 // The node is already marked as dead.
23 // setDead marks the node and all the children as dead.
24 setDead := func(n ast.Node) {
25 ast.Inspect(n, func(node ast.Node) bool {
33 switch stmt := node.(type) {
35 // "if" branch is dead if its condition evaluates
37 v := info.Types[stmt.Cond].Value
41 if !constant.BoolVal(v) {
49 // Case clause with empty switch tag is dead if it evaluates
53 for _, stmt := range stmt.Body.List {
54 cc := stmt.(*ast.CaseClause)
59 for _, expr := range cc.List {
60 v := info.Types[expr].Value
61 if v == nil || v.Kind() != constant.Bool || constant.BoolVal(v) {
70 // Case clause is dead if its constant value doesn't match
71 // the constant value from the switch tag.
72 // TODO: This handles integer comparisons only.
73 v := info.Types[stmt.Tag].Value
74 if v == nil || v.Kind() != constant.Int {
77 tagN, ok := constant.Uint64Val(v)
82 for _, x := range stmt.Body.List {
83 cc := x.(*ast.CaseClause)
88 for _, expr := range cc.List {
89 v := info.Types[expr].Value
93 n, ok := constant.Uint64Val(v)