Home | History | Annotate | Line # | Download | only in unit-tests
varmod-match.mk revision 1.12
      1  1.12  rillig # $NetBSD: varmod-match.mk,v 1.12 2022/08/24 21:03:57 rillig Exp $
      2   1.1  rillig #
      3   1.2  rillig # Tests for the :M variable modifier, which filters words that match the
      4   1.2  rillig # given pattern.
      5   1.4  rillig #
      6   1.4  rillig # See ApplyModifier_Match and ModifyWord_Match for the implementation.
      7   1.1  rillig 
      8   1.4  rillig .MAKEFLAGS: -dc
      9   1.3  rillig 
     10   1.3  rillig NUMBERS=	One Two Three Four five six seven
     11   1.3  rillig 
     12   1.5  rillig # Only keep words that start with an uppercase letter.
     13   1.4  rillig .if ${NUMBERS:M[A-Z]*} != "One Two Three Four"
     14   1.4  rillig .  error
     15   1.4  rillig .endif
     16   1.4  rillig 
     17   1.5  rillig # Only keep words that start with a character other than an uppercase letter.
     18   1.4  rillig .if ${NUMBERS:M[^A-Z]*} != "five six seven"
     19   1.4  rillig .  error
     20   1.4  rillig .endif
     21   1.4  rillig 
     22   1.5  rillig # Only keep words that don't start with s and at the same time end with
     23   1.5  rillig # either of [ex].
     24   1.5  rillig #
     25   1.5  rillig # This test case ensures that the negation from the first character class
     26   1.5  rillig # does not propagate to the second character class.
     27   1.4  rillig .if ${NUMBERS:M[^s]*[ex]} != "One Three five"
     28   1.4  rillig .  error
     29   1.4  rillig .endif
     30   1.3  rillig 
     31  1.10  rillig # Before 2020-06-13, this expression called Str_Match 601,080,390 times.
     32  1.10  rillig # Since 2020-06-13, this expression calls Str_Match 1 time.
     33   1.4  rillig .if ${:U****************:M****************b}
     34   1.4  rillig .endif
     35   1.4  rillig 
     36  1.10  rillig # As of 2022-06-11, this expression calls Str_Match 5,242,223 times.
     37  1.10  rillig # Adding another '*?' to the pattern calls Str_Match 41,261,143 times.
     38  1.10  rillig .if ${:U..................................................b:M*?*?*?*?*?a}
     39  1.10  rillig .endif
     40  1.10  rillig 
     41   1.4  rillig # To match a dollar sign in a word, double it.
     42   1.5  rillig #
     43   1.4  rillig # This is different from the :S and :C variable modifiers, where a '$'
     44   1.5  rillig # has to be escaped as '\$'.
     45   1.4  rillig .if ${:Ua \$ sign:M*$$*} != "\$"
     46   1.4  rillig .  error
     47   1.4  rillig .endif
     48   1.4  rillig 
     49   1.5  rillig # In the :M modifier, '\$' does not escape a dollar.  Instead it is
     50   1.5  rillig # interpreted as a backslash followed by whatever expression the
     51   1.5  rillig # '$' starts.
     52   1.5  rillig #
     53   1.5  rillig # This differs from the :S, :C and several other variable modifiers.
     54   1.4  rillig ${:U*}=		asterisk
     55   1.4  rillig .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
     56   1.4  rillig .  error
     57   1.4  rillig .endif
     58   1.4  rillig 
     59   1.6  rillig # TODO: ${VAR:M(((}}}}
     60   1.6  rillig # TODO: ${VAR:M{{{)))}
     61   1.6  rillig # TODO: ${VAR:M${UNBALANCED}}
     62   1.6  rillig # TODO: ${VAR:M${:U(((\}\}\}}}
     63   1.6  rillig 
     64   1.7  rillig .MAKEFLAGS: -d0
     65   1.7  rillig 
     66   1.7  rillig # Special characters:
     67   1.7  rillig #	*	matches 0 or more arbitrary characters
     68   1.7  rillig #	?	matches a single arbitrary character
     69   1.7  rillig #	\	starts an escape sequence, only outside ranges
     70   1.7  rillig #	[	starts a set for matching a single character
     71   1.7  rillig #	]	ends a set for matching a single character
     72   1.7  rillig #	-	in a set, forms a range of characters
     73   1.7  rillig #	^	as the first character in a set, negates the set
     74   1.7  rillig #	(	during parsing of the pattern, starts a nesting level
     75   1.7  rillig #	)	during parsing of the pattern, ends a nesting level
     76   1.7  rillig #	{	during parsing of the pattern, starts a nesting level
     77   1.7  rillig #	}	during parsing of the pattern, ends a nesting level
     78   1.7  rillig #	:	during parsing of the pattern, finishes the pattern
     79   1.7  rillig #	$	during parsing of the pattern, starts a nested expression
     80   1.7  rillig #	#	in a line except a shell command, starts a comment
     81   1.7  rillig #
     82   1.7  rillig # Pattern parts:
     83   1.7  rillig #	*	matches 0 or more arbitrary characters
     84   1.7  rillig #	?	matches exactly 1 arbitrary character
     85   1.7  rillig #	\x	matches exactly the character 'x'
     86   1.7  rillig #	[...]	matches exactly 1 character from the set
     87   1.7  rillig #	[^...]	matches exactly 1 character outside the set
     88   1.7  rillig #	[a-z]	matches exactly 1 character from the range 'a' to 'z'
     89   1.7  rillig #
     90   1.7  rillig 
     91   1.7  rillig #	[]	matches never
     92   1.7  rillig .if ${ ab a[]b a[b a b :L:M[]} != ""
     93   1.7  rillig .  error
     94   1.7  rillig .endif
     95   1.7  rillig 
     96   1.7  rillig #	a[]b	matches never
     97   1.7  rillig .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
     98   1.7  rillig .  error
     99   1.7  rillig .endif
    100   1.7  rillig 
    101   1.7  rillig #	[^]	matches exactly 1 arbitrary character
    102   1.7  rillig .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
    103   1.7  rillig .  error
    104   1.7  rillig .endif
    105   1.7  rillig 
    106   1.7  rillig #	a[^]b	matches 'a', then exactly 1 arbitrary character, then 'b'
    107   1.7  rillig .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
    108   1.7  rillig .  error
    109   1.7  rillig .endif
    110   1.7  rillig 
    111   1.7  rillig #	[Nn0]	matches exactly 1 character from the set 'N', 'n', '0'
    112   1.7  rillig .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
    113   1.7  rillig .  error
    114   1.7  rillig .endif
    115   1.7  rillig 
    116   1.7  rillig #	[a-c]	matches exactly 1 character from the range 'a' to 'c'
    117   1.7  rillig .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
    118   1.7  rillig .  error
    119   1.7  rillig .endif
    120   1.7  rillig 
    121   1.7  rillig #	[c-a]	matches the same as [a-c]
    122   1.7  rillig .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
    123   1.7  rillig .  error
    124   1.7  rillig .endif
    125   1.7  rillig 
    126   1.7  rillig #	[^a-c67]
    127   1.8  rillig #		matches a single character, except for 'a', 'b', 'c', '6' or
    128   1.8  rillig #		'7'
    129   1.7  rillig .if ${ A B C a b c d 5 6 7 8 [a-c] [a] :L:M[^a-c67]} != "A B C d 5 8"
    130   1.7  rillig .  error
    131   1.7  rillig .endif
    132   1.7  rillig 
    133  1.11  rillig #	[\]	matches a single backslash
    134  1.11  rillig WORDS=		a\b a[\]b ab
    135  1.11  rillig .if ${WORDS:Ma[\]b} != "a\\b"
    136  1.11  rillig .  error
    137  1.11  rillig .endif
    138  1.11  rillig 
    139   1.7  rillig #	:	terminates the pattern
    140   1.7  rillig .if ${ A * :L:M:} != ""
    141   1.7  rillig .  error
    142   1.7  rillig .endif
    143   1.7  rillig 
    144   1.7  rillig #	\:	matches a colon
    145   1.7  rillig .if ${ ${:U\: \:\:} :L:M\:} != ":"
    146   1.7  rillig .  error
    147   1.7  rillig .endif
    148   1.7  rillig 
    149   1.7  rillig #	${:U\:}	matches a colon
    150   1.7  rillig .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
    151   1.7  rillig .  error
    152   1.7  rillig .endif
    153   1.7  rillig 
    154   1.7  rillig #	[:]	matches never since the ':' starts the next modifier
    155   1.7  rillig # expect+2: Unknown modifier "]"
    156   1.7  rillig # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
    157   1.7  rillig .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
    158   1.7  rillig .  error
    159   1.7  rillig .else
    160   1.7  rillig .  error
    161   1.7  rillig .endif
    162   1.7  rillig 
    163   1.7  rillig #	[\]	matches exactly a backslash; no escaping takes place in
    164   1.7  rillig #		character ranges
    165  1.11  rillig # Without the 'a' in the below words, the backslash would end a word and thus
    166  1.11  rillig # influence how the string is split into words.
    167  1.11  rillig WORDS=		1\a 2\\a
    168  1.11  rillig .if ${WORDS:M?[\]a} != "1\\a"
    169  1.11  rillig .  error
    170  1.11  rillig .endif
    171  1.11  rillig 
    172  1.11  rillig #	[[-]]	May look like it would match a single '[', '\' or ']', but
    173  1.11  rillig #		the inner ']' has two roles: it is the upper bound of the
    174  1.11  rillig #		character range as well as the closing character of the
    175  1.11  rillig #		character list.  The outer ']' is just a regular character.
    176  1.11  rillig WORDS=		[ ] [] \] ]]
    177  1.11  rillig .if ${WORDS:M[[-]]} != "[] \\] ]]"
    178  1.11  rillig .  error
    179  1.11  rillig .endif
    180  1.11  rillig 
    181  1.11  rillig #	[b[-]a]
    182  1.11  rillig #		Same as for '[[-]]': the character list stops at the first
    183  1.11  rillig #		']', and the 'a]' is treated as a literal string.
    184  1.11  rillig WORDS=		[a \a ]a []a \]a ]]a [a] \a] ]a] ba]
    185  1.11  rillig .if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]"
    186  1.11  rillig .  error
    187  1.11  rillig .endif
    188  1.11  rillig 
    189  1.11  rillig #	[-]	Matches a single '-' since the '-' only becomes part of a
    190  1.11  rillig #		character range if it is preceded and followed by another
    191  1.11  rillig #		character.
    192  1.11  rillig WORDS=		- -]
    193  1.11  rillig .if ${WORDS:M[-]} != "-"
    194  1.11  rillig .  error
    195  1.11  rillig .endif
    196  1.11  rillig 
    197  1.11  rillig #	[	Incomplete empty character list, never matches.
    198  1.11  rillig WORDS=		a a[
    199  1.11  rillig .if ${WORDS:Ma[} != ""
    200  1.11  rillig .  error
    201  1.11  rillig .endif
    202  1.11  rillig 
    203  1.11  rillig #	[^	Incomplete negated empty character list, matches any single
    204  1.11  rillig #		character.
    205  1.11  rillig WORDS=		a a[ aX
    206  1.11  rillig .if ${WORDS:Ma[^} != "a[ aX"
    207   1.7  rillig .  error
    208   1.7  rillig .endif
    209   1.7  rillig 
    210  1.11  rillig #	[-x1-3	Incomplete character list, matches those elements that can be
    211  1.11  rillig #		parsed without lookahead.
    212  1.11  rillig WORDS=		- + x xx 0 1 2 3 4 [x1-3
    213  1.11  rillig .if ${WORDS:M[-x1-3} != "- x 1 2 3"
    214  1.11  rillig .  error
    215  1.11  rillig .endif
    216  1.11  rillig 
    217  1.11  rillig #	[^-x1-3
    218  1.11  rillig #		Incomplete negated character list, matches any character
    219  1.11  rillig #		except those elements that can be parsed without lookahead.
    220  1.11  rillig WORDS=		- + x xx 0 1 2 3 4 [x1-3
    221  1.11  rillig .if ${WORDS:M[^-x1-3} != "+ 0 4"
    222  1.11  rillig .  error
    223  1.11  rillig .endif
    224  1.11  rillig 
    225  1.11  rillig #	[\	Incomplete character list containing a single '\'.
    226  1.11  rillig #
    227  1.11  rillig #		A word can only end with a backslash if the preceding
    228  1.11  rillig #		character is a backslash as well; in all other cases the final
    229  1.11  rillig #		backslash would escape the following space, making the space
    230  1.11  rillig #		part of the word.  Only the very last word of a string can be
    231  1.11  rillig #		'\', as there is no following space that could be escaped.
    232  1.11  rillig WORDS=		\\ \a ${:Ux\\}
    233  1.11  rillig .if ${WORDS:M?[\]} != "\\\\ x\\"
    234  1.11  rillig .  error
    235  1.11  rillig .endif
    236  1.11  rillig 
    237  1.11  rillig #	[x-	Incomplete character list containing an incomplete character
    238  1.11  rillig #		range, matches only the 'x'.
    239  1.11  rillig WORDS=		[x- x x- y
    240  1.11  rillig .if ${WORDS:M[x-} != "x"
    241  1.11  rillig .  error
    242  1.11  rillig .endif
    243  1.11  rillig 
    244  1.11  rillig #	[^x-	Incomplete negated character list containing an incomplete
    245  1.11  rillig #		character range; matches each word that does not have an 'x'
    246  1.11  rillig #		at the position of the character list.
    247  1.11  rillig #
    248  1.11  rillig #		XXX: Even matches strings that are longer than a single
    249  1.11  rillig #		character.
    250  1.11  rillig WORDS=		[x- x x- y yyyyy
    251  1.11  rillig .if ${WORDS:M[^x-} != "[x- y yyyyy"
    252  1.11  rillig .  error
    253  1.11  rillig .endif
    254   1.7  rillig 
    255   1.7  rillig 
    256   1.7  rillig # The modifier ':tW' prevents splitting at whitespace.  Even leading and
    257   1.7  rillig # trailing whitespace is preserved.
    258   1.7  rillig .if ${   plain   string   :L:tW:M*} != "   plain   string   "
    259   1.7  rillig .  error
    260   1.7  rillig .endif
    261   1.7  rillig 
    262   1.7  rillig # Without the modifier ':tW', the string is split into words.  All whitespace
    263   1.7  rillig # around and between the words is normalized to a single space.
    264   1.7  rillig .if ${   plain    string   :L:M*} != "plain string"
    265   1.7  rillig .  error
    266   1.7  rillig .endif
    267   1.9  rillig 
    268   1.9  rillig 
    269   1.9  rillig # The pattern can come from a variable expression.  For single-letter
    270   1.9  rillig # variables, either the short form or the long form can be used, just as
    271   1.9  rillig # everywhere else.
    272   1.9  rillig PRIMES=	2 3 5 7 11
    273   1.9  rillig n=	2
    274   1.9  rillig .if ${PRIMES:M$n} != "2"
    275   1.9  rillig .  error
    276   1.9  rillig .endif
    277   1.9  rillig .if ${PRIMES:M${n}} != "2"
    278   1.9  rillig .  error
    279   1.9  rillig .endif
    280   1.9  rillig .if ${PRIMES:M${:U2}} != "2"
    281   1.9  rillig .  error
    282   1.9  rillig .endif
    283  1.12  rillig 
    284  1.12  rillig 
    285  1.12  rillig # Before var.c 1.1031 from 2022-08-24, the following expressions caused an
    286  1.12  rillig # out-of-bounds read beyond the indirect ':M' modifiers.
    287  1.12  rillig .if ${:U:${:UM\\}}		# The ':M' pattern need not be unescaped, the
    288  1.12  rillig .  error			# resulting pattern is '\', it never matches
    289  1.12  rillig .endif				# anything.
    290  1.12  rillig .if ${:U:${:UM\\\:\\}}		# The ':M' pattern must be unescaped, the
    291  1.12  rillig .  error			# resulting pattern is ':\', it never matches
    292  1.12  rillig .endif				# anything.
    293