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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
package main
// When files are read in by pkglint, they are interpreted in terms of
// lines. For Makefiles, line continuations are handled properly, allowing
// multiple raw lines to end in a single logical line. For other files
// there is a 1:1 translation.
//
// A difference between the raw and the logical lines is that the
// raw lines include the line end sequence, whereas the logical lines
// do not.
//
// Some methods allow modification of the raw lines contained in the
// logical line, but leave the “text” field untouched. These methods are
// used in the --autofix mode.
import (
"fmt"
"io"
"strconv"
"strings"
)
type RawLine struct {
Lineno int
orignl string
textnl string
}
func (rline *RawLine) String() string {
return strconv.Itoa(rline.Lineno) + ":" + rline.textnl
}
type Line struct {
Fname string
firstLine int32 // Zero means not applicable, -1 means EOF
lastLine int32 // Usually the same as firstLine, may differ in Makefiles
Text string
raw []*RawLine
changed bool
before []*RawLine
after []*RawLine
autofixMessage *string
}
func NewLine(fname string, lineno int, text string, rawLines []*RawLine) *Line {
return NewLineMulti(fname, lineno, lineno, text, rawLines)
}
// NewLineMulti is for logical Makefile lines that end with backslash.
func NewLineMulti(fname string, firstLine, lastLine int, text string, rawLines []*RawLine) *Line {
return &Line{fname, int32(firstLine), int32(lastLine), text, rawLines, false, nil, nil, nil}
}
// NewLineEOF creates a dummy line for logging, with the “line number” EOF.
func NewLineEOF(fname string) *Line {
return NewLineMulti(fname, -1, 0, "", nil)
}
func (line *Line) rawLines() []*RawLine {
switch { // prevent inlining
}
return append(append(append([]*RawLine(nil), line.before...), line.raw...), line.after...)
}
func (line *Line) linenos() string {
switch {
case line.firstLine == -1:
return "EOF"
case line.firstLine == 0:
return ""
case line.firstLine == line.lastLine:
return strconv.Itoa(int(line.firstLine))
default:
return strconv.Itoa(int(line.firstLine)) + "--" + strconv.Itoa(int(line.lastLine))
}
}
func (line *Line) ReferenceFrom(other *Line) string {
if line.Fname != other.Fname {
return line.Fname + ":" + line.linenos()
}
return "line " + line.linenos()
}
func (line *Line) IsMultiline() bool {
return line.firstLine > 0 && line.firstLine != line.lastLine
}
func (line *Line) printSource(out io.Writer) {
if G.opts.PrintSource {
io.WriteString(out, "\n")
for _, rawLine := range line.rawLines() {
if rawLine.textnl != rawLine.orignl {
if rawLine.orignl != "" {
io.WriteString(out, "- "+rawLine.orignl)
}
if rawLine.textnl != "" {
io.WriteString(out, "+ "+rawLine.textnl)
}
} else {
io.WriteString(out, "> "+rawLine.orignl)
}
}
}
}
func (line *Line) Fatalf(format string, args ...interface{}) {
line.printSource(G.logErr)
Fatalf(line.Fname, line.linenos(), format, args...)
}
func (line *Line) Errorf(format string, args ...interface{}) {
line.printSource(G.logOut)
Errorf(line.Fname, line.linenos(), format, args...)
line.logAutofix()
}
func (line *Line) Error0(format string) { line.Errorf(format) }
func (line *Line) Error1(format, arg1 string) { line.Errorf(format, arg1) }
func (line *Line) Error2(format, arg1, arg2 string) { line.Errorf(format, arg1, arg2) }
func (line *Line) Warnf(format string, args ...interface{}) {
line.printSource(G.logOut)
Warnf(line.Fname, line.linenos(), format, args...)
line.logAutofix()
}
func (line *Line) Warn0(format string) { line.Warnf(format) }
func (line *Line) Warn1(format, arg1 string) { line.Warnf(format, arg1) }
func (line *Line) Warn2(format, arg1, arg2 string) { line.Warnf(format, arg1, arg2) }
func (line *Line) Notef(format string, args ...interface{}) {
line.printSource(G.logOut)
Notef(line.Fname, line.linenos(), format, args...)
line.logAutofix()
}
func (line *Line) Note0(format string) { line.Notef(format) }
func (line *Line) Note1(format, arg1 string) { line.Notef(format, arg1) }
func (line *Line) Note2(format, arg1, arg2 string) { line.Notef(format, arg1, arg2) }
func (line *Line) Debugf(format string, args ...interface{}) {
line.printSource(G.logOut)
Debugf(line.Fname, line.linenos(), format, args...)
line.logAutofix()
}
func (line *Line) Debug1(format, arg1 string) { line.Debugf(format, arg1) }
func (line *Line) Debug2(format, arg1, arg2 string) { line.Debugf(format, arg1, arg2) }
func (line *Line) String() string {
return line.Fname + ":" + line.linenos() + ": " + line.Text
}
func (line *Line) logAutofix() {
if line.autofixMessage != nil {
autofixf(line.Fname, line.linenos(), "%s", *line.autofixMessage)
line.autofixMessage = nil
}
}
func (line *Line) AutofixInsertBefore(text string) bool {
if G.opts.PrintAutofix || G.opts.Autofix {
line.before = append(line.before, &RawLine{0, "", text + "\n"})
}
return line.RememberAutofix("Inserting a line %q before this line.", text)
}
func (line *Line) AutofixInsertAfter(text string) bool {
if G.opts.PrintAutofix || G.opts.Autofix {
line.after = append(line.after, &RawLine{0, "", text + "\n"})
}
return line.RememberAutofix("Inserting a line %q after this line.", text)
}
func (line *Line) AutofixDelete() bool {
if G.opts.PrintAutofix || G.opts.Autofix {
for _, rawLine := range line.raw {
rawLine.textnl = ""
}
}
return line.RememberAutofix("Deleting this line.")
}
func (line *Line) AutofixReplace(from, to string) bool {
for _, rawLine := range line.raw {
if rawLine.Lineno != 0 {
if replaced := strings.Replace(rawLine.textnl, from, to, 1); replaced != rawLine.textnl {
if G.opts.PrintAutofix || G.opts.Autofix {
rawLine.textnl = replaced
}
return line.RememberAutofix("Replacing %q with %q.", from, to)
}
}
}
return false
}
func (line *Line) AutofixReplaceRegexp(from, to string) bool {
for _, rawLine := range line.raw {
if rawLine.Lineno != 0 {
if replaced := regcomp(from).ReplaceAllString(rawLine.textnl, to); replaced != rawLine.textnl {
if G.opts.PrintAutofix || G.opts.Autofix {
rawLine.textnl = replaced
}
return line.RememberAutofix("Replacing regular expression %q with %q.", from, to)
}
}
}
return false
}
func (line *Line) RememberAutofix(format string, args ...interface{}) (hasBeenFixed bool) {
if line.firstLine < 1 {
return false
}
line.changed = true
if G.opts.Autofix {
autofixf(line.Fname, line.linenos(), format, args...)
return true
}
if G.opts.PrintAutofix {
msg := fmt.Sprintf(format, args...)
line.autofixMessage = &msg
}
return false
}
func (line *Line) CheckAbsolutePathname(text string) {
if G.opts.DebugTrace {
defer tracecall1(text)()
}
// In the GNU coding standards, DESTDIR is defined as a (usually
// empty) prefix that can be used to install files to a different
// location from what they have been built for. Therefore
// everything following it is considered an absolute pathname.
//
// Another context where absolute pathnames usually appear is in
// assignments like "bindir=/bin".
if m, path := match1(text, `(?:^|\$[{(]DESTDIR[)}]|[\w_]+\s*=\s*)(/(?:[^"'\s]|"[^"*]"|'[^']*')*)`); m {
if matches(path, `^/\w`) {
checkwordAbsolutePathname(line, path)
}
}
}
func (line *Line) CheckLength(maxlength int) {
if len(line.Text) > maxlength {
line.Warnf("Line too long (should be no more than %d characters).", maxlength)
Explain3(
"Back in the old time, terminals with 80x25 characters were common.",
"And this is still the default size of many terminal emulators.",
"Moderately short lines also make reading easier.")
}
}
func (line *Line) CheckValidCharacters(reChar string) {
rest := regcomp(reChar).ReplaceAllString(line.Text, "")
if rest != "" {
uni := ""
for _, c := range rest {
uni += fmt.Sprintf(" %U", c)
}
line.Warn1("Line contains invalid characters (%s).", uni[1:])
}
}
func (line *Line) CheckTrailingWhitespace() {
if hasSuffix(line.Text, " ") || hasSuffix(line.Text, "\t") {
if !line.AutofixReplaceRegexp(`\s+\n$`, "\n") {
line.Note0("Trailing white-space.")
Explain2(
"When a line ends with some white-space, that space is in most cases",
"irrelevant and can be removed.")
}
}
}
func (line *Line) CheckRcsid(prefixRe, suggestedPrefix string) bool {
if G.opts.DebugTrace {
defer tracecall2(prefixRe, suggestedPrefix)()
}
if matches(line.Text, `^`+prefixRe+`\$`+`NetBSD(?::[^\$]+)?\$$`) {
return true
}
if !line.AutofixInsertBefore(suggestedPrefix + "$" + "NetBSD$") {
line.Error1("Expected %q.", suggestedPrefix+"$"+"NetBSD$")
Explain3(
"Several files in pkgsrc must contain the CVS Id, so that their",
"current version can be traced back later from a binary package.",
"This is to ensure reproducible builds, for example for finding bugs.")
}
return false
}
|