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