summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/mktypes.go
blob: e4044e2760a84f022d7ca88b27705140a6abd5d4 (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
package pkglint

import "strings"

// MkToken represents a contiguous string from a Makefile.
// It is either a literal string or a variable use.
//
// Example: /usr/share/${PKGNAME}/data consists of 3 tokens:
//  1. MkToken{Text: "/usr/share/"}
//  2. MkToken{Text: "${PKGNAME}", Varuse: &MkVarUse{varname: "PKGNAME"}}
//  3. MkToken{Text: "/data"}
//
type MkToken struct {
	Text   string    // Used for both literal text and variable uses
	Varuse *MkVarUse // For literal text, it is nil
}

// MkVarUse represents a reference to a Make variable, with optional modifiers.
//
// For nested variable expressions, the variable name can contain references
// to other variables. For example, ${TOOLS.${t}} is a MkVarUse with varname
// "TOOLS.${t}" and no modifiers.
//
// Example: ${PKGNAME}
//
// Example: ${PKGNAME:S/from/to/}
type MkVarUse struct {
	varname   string             // E.g. "PKGNAME", or "${BUILD_DEFS}"
	modifiers []MkVarUseModifier // E.g. "Q", "S/from/to/"
}

func (vu *MkVarUse) String() string { return sprintf("${%s%s}", vu.varname, vu.Mod()) }

type MkVarUseModifier struct {
	Text string
}

func (m MkVarUseModifier) IsQ() bool { return m.Text == "Q" }

func (m MkVarUseModifier) IsSuffixSubst() bool {
	// XXX: There are other cases
	return hasPrefix(m.Text, "=")
}

func (m MkVarUseModifier) MatchSubst() (ok bool, regex bool, from string, to string, options string) {
	p := NewMkLexer(m.Text, nil)
	return p.varUseModifierSubst('}')
}

// Subst evaluates an S/from/to/ modifier.
//
// Example:
//  MkVarUseModifier{"S,name,file,g"}.Subst("distname-1.0") => "distfile-1.0"
func (m MkVarUseModifier) Subst(str string) (string, bool) {
	// XXX: The call to MatchSubst is usually redundant because MatchSubst
	// is typically called directly before calling Subst.
	ok, regex, from, to, options := m.MatchSubst()
	if !ok {
		return "", false
	}

	leftAnchor := hasPrefix(from, "^")
	if leftAnchor {
		from = from[1:]
	}

	rightAnchor := hasSuffix(from, "$")
	if rightAnchor {
		from = from[:len(from)-1]
	}

	if regex && matches(from, `^[\w-]+$`) && matches(to, `^[^&$\\]*$`) {
		// The "from" pattern is so simple that it doesn't matter whether
		// the modifier is :S or :C, therefore treat it like the simpler :S.
		regex = false
	}

	if regex {
		// TODO: Maybe implement regular expression substitutions later.
		return "", false
	}

	result := mkopSubst(str, leftAnchor, from, rightAnchor, to, options)
	if trace.Tracing && result != str {
		trace.Stepf("Subst: %q %q => %q", str, m.Text, result)
	}
	return result, true
}

// MatchMatch tries to match the modifier to a :M or a :N pattern matching.
// Examples:
//  :Mpattern   => true,  true,  "pattern", true
//  :M*         => true,  true,  "*",       false
//  :M${VAR}    => true,  true,  "${VAR}",  false
//  :Npattern   => true,  false, "pattern", true
//  :X          => false
func (m MkVarUseModifier) MatchMatch() (ok bool, positive bool, pattern string, exact bool) {
	if hasPrefix(m.Text, "M") || hasPrefix(m.Text, "N") {
		// See devel/bmake/files/str.c:^Str_Match
		exact := !strings.ContainsAny(m.Text[1:], "*?[\\$")
		return true, m.Text[0] == 'M', m.Text[1:], exact
	}
	return false, false, "", false
}

func (m MkVarUseModifier) IsToLower() bool { return m.Text == "tl" }

// ChangesWords returns true if applying this modifier to a list variable
// may change the number of words in the list, or their boundaries.
func (m MkVarUseModifier) ChangesWords() bool {
	text := m.Text

	// See MkParser.varUseModifier for the meaning of these modifiers.
	switch text[0] {

	case 'E', 'H', 'M', 'N', 'O', 'R', 'T':
		return false

	case 'C', 'Q', 'S':
		// For the :C and :S modifiers, a more detailed analysis could reveal
		// cases that don't change the structure, such as :S,a,b,g or
		// :C,[0-9A-Za-z_],.,g, but not :C,x,,g.
		return true
	}

	switch text {

	case "tl", "tu":
		return false

	case "sh", "tW", "tw":
		return true
	}

	// If in doubt, be pessimistic. As of March 2019, the only code that
	// actually uses this function doesn't issue a possibly wrong warning
	// in such a case.
	return true
}

func (vu *MkVarUse) Mod() string {
	var mod strings.Builder
	for _, modifier := range vu.modifiers {
		mod.WriteString(":")
		mod.WriteString(modifier.Text)
	}
	return mod.String()
}

// IsExpression returns whether the varname is interpreted as an expression
// instead of a variable name (rare, only the modifiers :? and :L do this).
func (vu *MkVarUse) IsExpression() bool {
	if len(vu.modifiers) == 0 {
		return false
	}
	mod := vu.modifiers[0]
	return mod.Text == "L" || hasPrefix(mod.Text, "?")
}

func (vu *MkVarUse) IsQ() bool {
	mlen := len(vu.modifiers)
	return mlen > 0 && vu.modifiers[mlen-1].IsQ()
}

func (vu *MkVarUse) HasModifier(prefix string) bool {
	for _, mod := range vu.modifiers {
		if hasPrefix(mod.Text, prefix) {
			return true
		}
	}
	return false
}