-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
functions.go
204 lines (164 loc) · 9.27 KB
/
functions.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package fmtsafe
import (
"strings"
"github.com/cockroachdb/cockroach/pkg/testutils/lint/passes/errwrap"
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// requireConstMsg records functions for which the last string
// argument must be a constant string.
var requireConstMsg = map[string]bool{
"github.com/cockroachdb/cockroach/pkg/util/log.Shout": true,
"github.com/cockroachdb/cockroach/pkg/util/log.Event": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VEvent": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VErrEvent": true,
"(*github.com/cockroachdb/cockroach/pkg/util/tracing/Span).Record": true,
"(*github.com/cockroachdb/cockroach/pkg/sql.optPlanningCtx).log": true,
}
/*
requireConstFmt records functions for which the string arg
before the final ellipsis must be a constant string.
Definitions surrounded in parentheses are functions attached to a struct.
For functions defined in the main package, a *second* entry is required
in the form (main.yourStruct).yourFuncF
*/
var requireConstFmt = map[string]bool{
// Logging things.
"log.Printf": true,
"log.Fatalf": true,
"log.Panicf": true,
"(*log.Logger).Fatalf": true,
"(*log.Logger).Panicf": true,
"(*log.Logger).Printf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.Shoutf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.Eventf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.vEventf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VEventf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VErrEventf": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VEventfDepth": true,
"github.com/cockroachdb/cockroach/pkg/util/log.VErrEventfDepth": true,
// Note: More of the logging functions are populated here via the
// init() function below.
"github.com/cockroachdb/cockroach/pkg/util/log.MakeLegacyEntry": true,
"github.com/cockroachdb/cockroach/pkg/util/log.makeUnstructuredEntry": true,
"github.com/cockroachdb/cockroach/pkg/util/log.FormatWithContextTags": true,
"github.com/cockroachdb/cockroach/pkg/util/log.formatOnlyArgs": true,
"github.com/cockroachdb/cockroach/pkg/util/log.renderArgsAsRedactable": true,
"github.com/cockroachdb/cockroach/pkg/util/log.formatArgs": true,
"github.com/cockroachdb/cockroach/pkg/util/log.logfDepth": true,
"github.com/cockroachdb/cockroach/pkg/util/log.shoutfDepth": true,
"github.com/cockroachdb/cockroach/pkg/util/log.logfDepthInternal": true,
"github.com/cockroachdb/cockroach/pkg/util/log.makeStartLine": true,
"github.com/cockroachdb/cockroach/pkg/util/log/logcrash.ReportOrPanic": true,
"github.com/cockroachdb/cockroach/pkg/roachpb.NewAmbiguousResultErrorf": true,
"(*github.com/cockroachdb/cockroach/pkg/util/tracing.Span).Recordf": true,
"(*github.com/cockroachdb/cockroach/pkg/util/tracing.spanInner).Recordf": true,
"(github.com/cockroachdb/cockroach/pkg/rpc.breakerLogger).Debugf": true,
"(github.com/cockroachdb/cockroach/pkg/rpc.breakerLogger).Infof": true,
"(*github.com/cockroachdb/cockroach/pkg/internal/rsg/yacc.Tree).errorf": true,
"(github.com/cockroachdb/cockroach/pkg/storage.pebbleLogger).Infof": true,
"(github.com/cockroachdb/cockroach/pkg/storage.pebbleLogger).Fatalf": true,
"(*github.com/cockroachdb/cockroach/pkg/util/grpcutil.grpcLogger).Infof": true,
"(*github.com/cockroachdb/cockroach/pkg/util/grpcutil.grpcLogger).Warningf": true,
"(*github.com/cockroachdb/cockroach/pkg/util/grpcutil.grpcLogger).Errorf": true,
"(*github.com/cockroachdb/cockroach/pkg/util/grpcutil.grpcLogger).Fatalf": true,
// Both of these signatures need to be included for the linter to not flag
// roachtest testImpl.addFailure since it is in the main package
// This could be a bug in nogo
"(*github.com/cockroachdb/cockroach/pkg/cmd/roachtest.testImpl).addFailure": true,
"(*main.testImpl).addFailure": true,
"(*main.testImpl).Fatalf": true,
"(*github.com/cockroachdb/cockroach/pkg/cmd/roachtest.testImpl).Fatalf": true,
"(*main.testImpl).Errorf": true,
"(*github.com/cockroachdb/cockroach/pkg/cmd/roachtest.testImpl).Errorf": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Debugf": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Infof": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Warningf": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Errorf": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Fatalf": true,
"(*github.com/cockroachdb/cockroach/pkg/kv/kvserver.raftLogger).Panicf": true,
"github.com/cockroachdb/cockroach/pkg/kv/kvserver.makeNonDeterministicFailure": true,
"github.com/cockroachdb/cockroach/pkg/kv/kvserver.wrapWithNonDeterministicFailure": true,
"(go.etcd.io/etcd/raft/v3.Logger).Debugf": true,
"(go.etcd.io/etcd/raft/v3.Logger).Infof": true,
"(go.etcd.io/etcd/raft/v3.Logger).Warningf": true,
"(go.etcd.io/etcd/raft/v3.Logger).Errorf": true,
"(go.etcd.io/etcd/raft/v3.Logger).Fatalf": true,
"(go.etcd.io/etcd/raft/v3.Logger).Panicf": true,
"(google.golang.org/grpc/grpclog.Logger).Infof": true,
"(google.golang.org/grpc/grpclog.Logger).Warningf": true,
"(google.golang.org/grpc/grpclog.Logger).Errorf": true,
"(github.com/cockroachdb/pebble.Logger).Infof": true,
"(github.com/cockroachdb/pebble.Logger).Fatalf": true,
"(github.com/cockroachdb/circuitbreaker.Logger).Infof": true,
"(github.com/cockroachdb/circuitbreaker.Logger).Debugf": true,
"github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/exprgen.errorf": true,
"github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/exprgen.wrapf": true,
"(*github.com/cockroachdb/cockroach/pkg/sql.connExecutor).sessionEventf": true,
"(*github.com/cockroachdb/cockroach/pkg/sql/logictest.logicTest).outf": true,
"(*github.com/cockroachdb/cockroach/pkg/sql/logictest.logicTest).Errorf": true,
"(*github.com/cockroachdb/cockroach/pkg/sql/logictest.logicTest).Fatalf": true,
"github.com/cockroachdb/cockroach/pkg/server.serverErrorf": true,
"github.com/cockroachdb/cockroach/pkg/server.guaranteedExitFatal": true,
"(*github.com/cockroachdb/cockroach/pkg/ccl/changefeedccl.kafkaLogAdapter).Printf": true,
"github.com/cockroachdb/redact.Sprintf": true,
"github.com/cockroachdb/redact.Fprintf": true,
"(github.com/cockroachdb/redact.SafePrinter).Printf": true,
"(github.com/cockroachdb/redact.SafeWriter).Printf": true,
"(*github.com/cockroachdb/redact.printer).Printf": true,
"(*github.com/cockroachdb/cockroach/pkg/sql/pgwire.authPipe).Logf": true,
"(github.com/cockroachdb/cockroach/pkg/sql/logictest/logictestbase.stdlogger).Fatalf": true,
"(github.com/cockroachdb/cockroach/pkg/sql/logictest/logictestbase.stdlogger).Logf": true,
// Error things are populated in the init() message.
}
func title(s string) string {
return cases.Title(language.English, cases.NoLower).String(s)
}
func init() {
for _, sev := range logpb.Severity_name {
capsev := title(strings.ToLower(sev))
// log.Infof, log.Warningf etc.
requireConstFmt["github.com/cockroachdb/cockroach/pkg/util/log."+capsev+"f"] = true
// log.VInfof, log.VWarningf etc.
requireConstFmt["github.com/cockroachdb/cockroach/pkg/util/log.V"+capsev+"f"] = true
// log.InfofDepth, log.WarningfDepth, etc.
requireConstFmt["github.com/cockroachdb/cockroach/pkg/util/log."+capsev+"fDepth"] = true
// log.Info, log.Warning, etc.
requireConstMsg["github.com/cockroachdb/cockroach/pkg/util/log."+capsev] = true
for _, ch := range logpb.Channel_name {
capch := strings.ReplaceAll(title(strings.ReplaceAll(strings.ToLower(ch), "_", " ")), " ", "")
// log.Ops.Infof, log.Ops.Warningf, etc.
requireConstFmt["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+")."+capsev+"f"] = true
// log.Ops.VInfof, log.Ops.VWarningf, etc.
requireConstFmt["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+").V"+capsev+"f"] = true
// log.Ops.InfofDepth, log.Ops.WarningfDepth, etc.
requireConstFmt["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+")."+capsev+"fDepth"] = true
// log.Ops.Info, logs.Ops.Warning, etc.
requireConstMsg["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+")."+capsev] = true
}
}
for _, ch := range logpb.Channel_name {
capch := strings.ReplaceAll(title(strings.ReplaceAll(strings.ToLower(ch), "_", " ")), " ", "")
// log.Ops.Shoutf, log.Dev.Shoutf, etc.
requireConstFmt["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+").Shoutf"] = true
// log.Ops.Shout, log.Dev.Shout, etc.
requireConstMsg["(github.com/cockroachdb/cockroach/pkg/util/log.logger"+capch+").Shout"] = true
}
for errorFn, formatStringIndex := range errwrap.ErrorFnFormatStringIndex {
if formatStringIndex < 0 {
requireConstMsg[errorFn] = true
} else {
requireConstFmt[errorFn] = true
}
}
}