aboutsummaryrefslogtreecommitdiff
path: root/vendor/honnef.co/go/tools/functions/pure.go
blob: d1c4d036e88bf58cd01b9a4f18347c8dfd74ee7f (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package functions

import (
	"go/token"
	"go/types"

	"honnef.co/go/tools/callgraph"
	"honnef.co/go/tools/lint"
	"honnef.co/go/tools/ssa"
)

// IsStub reports whether a function is a stub. A function is
// considered a stub if it has no instructions or exactly one
// instruction, which must be either returning only constant values or
// a panic.
func (d *Descriptions) IsStub(fn *ssa.Function) bool {
	if len(fn.Blocks) == 0 {
		return true
	}
	if len(fn.Blocks) > 1 {
		return false
	}
	instrs := lint.FilterDebug(fn.Blocks[0].Instrs)
	if len(instrs) != 1 {
		return false
	}

	switch instrs[0].(type) {
	case *ssa.Return:
		// Since this is the only instruction, the return value must
		// be a constant. We consider all constants as stubs, not just
		// the zero value. This does not, unfortunately, cover zero
		// initialised structs, as these cause additional
		// instructions.
		return true
	case *ssa.Panic:
		return true
	default:
		return false
	}
}

func (d *Descriptions) IsPure(fn *ssa.Function) bool {
	if fn.Signature.Results().Len() == 0 {
		// A function with no return values is empty or is doing some
		// work we cannot see (for example because of build tags);
		// don't consider it pure.
		return false
	}

	for _, param := range fn.Params {
		if _, ok := param.Type().Underlying().(*types.Basic); !ok {
			return false
		}
	}

	if fn.Blocks == nil {
		return false
	}
	checkCall := func(common *ssa.CallCommon) bool {
		if common.IsInvoke() {
			return false
		}
		builtin, ok := common.Value.(*ssa.Builtin)
		if !ok {
			if common.StaticCallee() != fn {
				if common.StaticCallee() == nil {
					return false
				}
				// TODO(dh): ideally, IsPure wouldn't be responsible
				// for avoiding infinite recursion, but
				// FunctionDescriptions would be.
				node := d.CallGraph.CreateNode(common.StaticCallee())
				if callgraph.PathSearch(node, func(other *callgraph.Node) bool {
					return other.Func == fn
				}) != nil {
					return false
				}
				if !d.Get(common.StaticCallee()).Pure {
					return false
				}
			}
		} else {
			switch builtin.Name() {
			case "len", "cap", "make", "new":
			default:
				return false
			}
		}
		return true
	}
	for _, b := range fn.Blocks {
		for _, ins := range b.Instrs {
			switch ins := ins.(type) {
			case *ssa.Call:
				if !checkCall(ins.Common()) {
					return false
				}
			case *ssa.Defer:
				if !checkCall(&ins.Call) {
					return false
				}
			case *ssa.Select:
				return false
			case *ssa.Send:
				return false
			case *ssa.Go:
				return false
			case *ssa.Panic:
				return false
			case *ssa.Store:
				return false
			case *ssa.FieldAddr:
				return false
			case *ssa.UnOp:
				if ins.Op == token.MUL || ins.Op == token.AND {
					return false
				}
			}
		}
	}
	return true
}