summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mklineparser.go
blob: 1d8ef7cad1db3b402250707e54dc560cf70e2258 (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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package pkglint

import (
	"netbsd.org/pkglint/textproc"
	"strings"
)

type MkLineParser struct{}

func NewMkLineParser() MkLineParser { return MkLineParser{} }

// Parse parses the text of a Makefile line to see what kind of line
// it is: variable assignment, include, comment, etc.
//
// See devel/bmake/parse.c:/^Parse_File/
func (p MkLineParser) Parse(line *Line) *MkLine {
	text := line.Text

	if hasPrefix(text, " ") && line.Basename != "bsd.buildlink3.mk" {
		line.Warnf("Makefile lines should not start with space characters.")
		line.Explain(
			"If this line should be a shell command connected to a target, use a tab character for indentation.",
			"Otherwise remove the leading whitespace.")
	}

	// Check for shell commands first because these cannot have comments
	// at the end of the line.
	if hasPrefix(text, "\t") {
		lex := textproc.NewLexer(text)
		lex.SkipHspace()

		splitResult := p.split(line, lex.Rest(), false)
		if lex.PeekByte() == '#' {
			return p.parseCommentOrEmpty(line, p.split(line, lex.Rest(), true))
		}
		return p.parseShellcmd(line, splitResult)
	}

	splitResult := p.split(line, text, true)

	if mkline := p.parseVarassign(line, text, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseCommentOrEmpty(line, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseDirective(line, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseInclude(line, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseSysinclude(line, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseDependency(line, splitResult); mkline != nil {
		return mkline
	}
	if mkline := p.parseMergeConflict(line, splitResult); mkline != nil {
		return mkline
	}

	// The %q is deliberate here since it shows possible strange characters.
	line.Errorf("Unknown Makefile line format: %q.", text)
	return &MkLine{line, splitResult, nil}
}

func (p MkLineParser) parseVarassign(line *Line, text string, splitResult mkLineSplitResult) *MkLine {
	m, a := p.MatchVarassign(line, text, &splitResult)
	if !m {
		return nil
	}

	p.fixSpaceAfterVarname(line, a)
	p.checkUnintendedComment(&splitResult, a, line)

	return &MkLine{line, splitResult, a}
}

func (p MkLineParser) MatchVarassign(line *Line, text string, splitResult *mkLineSplitResult) (bool, *mkLineAssign) {

	// A commented variable assignment does not have leading whitespace.
	// Otherwise line 1 of almost every Makefile fragment would need to
	// be scanned for a variable assignment even though it only contains
	// the $NetBSD CVS Id.
	commented := splitResult.main == "" && splitResult.hasComment
	if commented {
		clex := textproc.NewLexer(splitResult.comment)
		if clex.SkipHspace() || clex.EOF() {
			return false, nil
		}
		*splitResult = p.split(nil, text[1:], true)
	}

	lexer := NewMkTokensLexer(splitResult.tokens)
	mainStart := lexer.Mark()

	for !commented && lexer.SkipByte(' ') {
	}

	varnameStart := lexer.Mark()
	// TODO: duplicated code in MkLexer.Varname
	for lexer.NextBytesSet(VarbaseBytes) != "" || lexer.NextVarUse() != nil {
	}
	if lexer.SkipByte('.') || hasPrefix(splitResult.main, "SITES_") {
		for lexer.NextBytesSet(VarparamBytes) != "" || lexer.NextVarUse() != nil {
		}
	}

	varname := lexer.Since(varnameStart)

	if varname == "" {
		return false, nil
	}

	spaceAfterVarname := lexer.NextHspace()

	opStart := lexer.Mark()
	switch lexer.PeekByte() {
	case '!', '+', ':', '?':
		lexer.Skip(1)
	}
	if !lexer.SkipByte('=') {
		return false, nil
	}
	op := NewMkOperator(lexer.Since(opStart))

	if hasSuffix(varname, "+") && op == opAssign && spaceAfterVarname == "" {
		varname = varname[:len(varname)-1]
		op = opAssignAppend
	}

	lexer.SkipHspace()

	value := trimHspace(lexer.Rest())
	parsedValueAlign := condStr(commented, "#", "") + lexer.Since(mainStart)
	valueAlign := p.getRawValueAlign(line.raw[0].Orig(), parsedValueAlign)
	if value == "" {
		valueAlign += splitResult.spaceBeforeComment
		splitResult.spaceBeforeComment = ""
	}

	return true, &mkLineAssign{
		commented:         commented,
		varname:           varname,
		varcanon:          varnameCanon(varname),
		varparam:          varnameParam(varname),
		spaceAfterVarname: spaceAfterVarname,
		op:                op,
		valueAlign:        valueAlign,
		value:             value,
		valueMk:           nil, // filled in lazily
		valueMkRest:       "",  // filled in lazily
		fields:            nil, // filled in lazily
	}
}

func (p MkLineParser) fixSpaceAfterVarname(line *Line, a *mkLineAssign) {
	if !(a.spaceAfterVarname != "") {
		return
	}

	varname := a.varname
	op := a.op
	switch {
	case hasSuffix(varname, "+") && (op == opAssign || op == opAssignAppend):
		break
	case matches(varname, `^[a-z]`) && op == opAssignEval:
		break

	default:
		before := a.valueAlign
		after := alignWith(varname+op.String(), before)

		fix := line.Autofix()
		fix.Notef("Unnecessary space after variable name %q.", varname)
		fix.Replace(before, after)
		fix.Apply()
	}
}

func (p MkLineParser) checkUnintendedComment(splitResult *mkLineSplitResult, a *mkLineAssign, line *Line) {
	if !(splitResult.hasComment && a.value != "" && splitResult.spaceBeforeComment == "") {
		return
	}

	line.Warnf("The # character starts a Makefile comment.")
	line.Explain(
		"In a variable assignment, an unescaped # starts a comment that",
		"continues until the end of the line.",
		"To escape the #, write \\#.",
		"",
		"If this # character intentionally starts a comment,",
		"it should be preceded by a space in order to make it more visible.")
}

func (p MkLineParser) parseShellcmd(line *Line, splitResult mkLineSplitResult) *MkLine {
	return &MkLine{line, splitResult, mkLineShell{line.Text[1:]}}
}

func (p MkLineParser) parseCommentOrEmpty(line *Line, splitResult mkLineSplitResult) *MkLine {
	trimmedText := trimHspace(line.Text)

	if strings.HasPrefix(trimmedText, "#") {
		return &MkLine{line, splitResult, mkLineComment{}}
	}

	if trimmedText == "" {
		return &MkLine{line, splitResult, mkLineEmpty{}}
	}

	return nil
}

func (p MkLineParser) parseDirective(line *Line, splitResult mkLineSplitResult) *MkLine {
	text := line.Text
	if !hasPrefix(text, ".") { // TODO: use lexer instead
		return nil
	}

	lexer := textproc.NewLexer(splitResult.main[1:])

	indent := lexer.NextHspace()
	directive := lexer.NextBytesSet(LowerDash)
	switch directive {
	case "if", "else", "elif", "endif",
		"ifdef", "ifndef",
		"for", "endfor", "undef",
		"error", "warning", "info",
		"export", "export-env", "unexport", "unexport-env":
		break
	default:
		// Intentionally not supported are: ifmake ifnmake elifdef elifndef elifmake elifnmake.
		return nil
	}

	lexer.SkipHspace()

	args := lexer.Rest()

	// In .if and .endif lines the space surrounding the comment is irrelevant.
	// Especially for checking that the .endif comment matches the .if condition,
	// it must be trimmed.
	trimmedComment := trimHspace(splitResult.comment)

	return &MkLine{line, splitResult, &mkLineDirective{indent, directive, args, trimmedComment, nil, nil, nil}}
}

func (p MkLineParser) parseInclude(line *Line, splitResult mkLineSplitResult) *MkLine {
	m, indent, directive, includedFile := MatchMkInclude(splitResult.main)
	if !m {
		return nil
	}

	return &MkLine{line, splitResult, &mkLineInclude{directive == "include", false, indent, includedFile, nil}}
}

func (p MkLineParser) parseSysinclude(line *Line, splitResult mkLineSplitResult) *MkLine {
	m, indent, directive, includedFile := match3(splitResult.main, `^\.([\t ]*)(s?include)[\t ]+<([^>]+)>$`)
	if !m {
		return nil
	}

	return &MkLine{line, splitResult, &mkLineInclude{directive == "include", true, indent, NewRelPathString(includedFile), nil}}
}

func (p MkLineParser) parseDependency(line *Line, splitResult mkLineSplitResult) *MkLine {
	// XXX: Replace this regular expression with proper parsing.
	// There might be a ${VAR:M*.c} in these variables, which the below regular expression cannot handle.
	m, targets, whitespace, sources := match3(line.Text, `^([^\t :]+(?:[\t ]*[^\t :]+)*)([\t ]*):[\t ]*([^#]*?)(?:[\t ]*#.*)?$`)
	if !m {
		return nil
	}

	if whitespace != "" {
		line.Notef("Space before colon in dependency line.")
	}
	return &MkLine{line, splitResult, mkLineDependency{targets, sources}}
}

func (p MkLineParser) parseMergeConflict(line *Line, splitResult mkLineSplitResult) *MkLine {
	if !matches(line.Text, `^(<<<<<<<|=======|>>>>>>>)`) {
		return nil
	}

	return &MkLine{line, splitResult, nil}
}

// split parses a logical line from a Makefile (that is, after joining
// the lines that end in a backslash) into two parts: the main part and the
// comment.
//
// This applies to all line types except those starting with a tab, which
// contain the shell commands to be associated with make targets. These cannot
// have comments.
//
// If line is given, it is used for logging parse errors and warnings
// about round parentheses instead of curly braces, as well as ambiguous
// variables of the form $v instead of ${v}.
//
// If trimComment is true, the main task of this method is to split the
// text into tokens. The remaining space is placed into spaceBeforeComment,
// but hasComment will always be false, and comment will always be empty.
// This behavior is useful for shell commands (which are indented with a
// single tab).
func (MkLineParser) split(diag Autofixer, text string, trimComment bool) mkLineSplitResult {
	assert(!hasPrefix(text, "\t"))

	var mainWithSpaces, comment string
	if trimComment {
		mainWithSpaces, comment = NewMkLineParser().unescapeComment(text)
	} else {
		mainWithSpaces = text
	}

	parser := NewMkLexer(mainWithSpaces, diag)
	lexer := parser.lexer

	parseOther := func() string {
		sb := NewLazyStringBuilder(lexer.Rest())

		for !lexer.EOF() {
			if lexer.SkipString("$$") {
				sb.WriteString("$$")
				continue
			}

			other := lexer.NextBytesFunc(func(b byte) bool { return b != '$' })
			if other == "" {
				break
			}

			sb.WriteString(other)
		}

		return sb.String()
	}

	var tokens []*MkToken
	for !lexer.EOF() {
		mark := lexer.Mark()

		if varUse := parser.VarUse(); varUse != nil {
			tokens = append(tokens, &MkToken{lexer.Since(mark), varUse})

		} else if other := parseOther(); other != "" {
			tokens = append(tokens, &MkToken{other, nil})

		} else {
			assert(lexer.SkipByte('$'))
			tokens = append(tokens, &MkToken{"$", nil})
		}
	}

	hasComment := comment != ""
	if hasComment {
		comment = comment[1:]
	}

	mainTrimmed := rtrimHspace(mainWithSpaces)
	spaceBeforeComment := mainWithSpaces[len(mainTrimmed):]
	if spaceBeforeComment != "" {
		tokenText := &tokens[len(tokens)-1].Text
		*tokenText = rtrimHspace(*tokenText)
		if *tokenText == "" {
			if len(tokens) == 1 {
				tokens = nil
			} else {
				tokens = tokens[:len(tokens)-1]
			}
		}
	}

	return mkLineSplitResult{mainTrimmed, tokens, spaceBeforeComment, hasComment, "", comment}
}

// unescapeComment takes a Makefile line, as written in a file, and splits
// it into the main part and the comment.
//
// The comment starts at the first #. Except if it is preceded by an odd number
// of backslashes. Or by an opening bracket.
//
// The main text is returned including leading and trailing whitespace.
// Any escaped # is unescaped, that is, \# becomes #.
//
// The comment is returned including the leading "#", if any.
// If the line has no comment, it is an empty string.
func (MkLineParser) unescapeComment(text string) (main, comment string) {
	var sb strings.Builder

	lexer := textproc.NewLexer(text)

again:
	if plain := lexer.NextBytesSet(unescapeMkCommentSafeChars); plain != "" {
		sb.WriteString(plain)
		goto again
	}

	switch {
	case lexer.SkipString("\\#"):
		sb.WriteByte('#')

	case lexer.PeekByte() == '\\' && len(lexer.Rest()) >= 2:
		sb.WriteString(lexer.Rest()[:2])
		lexer.Skip(2)

	case lexer.SkipByte('\\'):
		sb.WriteByte('\\')

	case lexer.SkipString("[#"):
		// See devel/bmake/files/parse.c:/as in modifier/
		sb.WriteString("[#")

	case lexer.SkipByte('['):
		sb.WriteByte('[')

	default:
		main = sb.String()
		if lexer.PeekByte() == '#' {
			return main, lexer.Rest()
		}

		assert(lexer.EOF())
		return main, ""
	}

	goto again
}

func (MkLineParser) getRawValueAlign(raw, parsed string) string {
	r := textproc.NewLexer(raw)
	p := textproc.NewLexer(parsed)
	mark := r.Mark()

	for !p.EOF() {
		pch := p.PeekByte()
		rch := r.PeekByte()

		switch {
		case pch == rch:
			p.Skip(1)
			r.Skip(1)

		case pch == ' ', pch == '\t':
			p.SkipHspace()
			r.SkipHspace()

		default:
			assert(pch == '#')
			assert(r.SkipString("\\#"))
			p.Skip(1)
		}
	}

	return r.Since(mark)
}

type mkLineSplitResult struct {
	// The text of the line, without the comment at the end of the line,
	// and with # signs unescaped.
	main               string
	tokens             []*MkToken
	spaceBeforeComment string
	hasComment         bool
	rationale          string // filled in later, by MkLines.collectRationale
	comment            string
}