Home | History | Annotate | Line # | Download | only in unit-tests
varmod-edge.mk revision 1.20.2.1
      1  1.20.2.1  perseant # $NetBSD: varmod-edge.mk,v 1.20.2.1 2024/07/01 01:01:15 perseant Exp $
      2       1.1    rillig #
      3       1.1    rillig # Tests for edge cases in variable modifiers.
      4       1.1    rillig #
      5       1.1    rillig # These tests demonstrate the current implementation in small examples.
      6       1.1    rillig # They may contain surprising behavior.
      7       1.1    rillig #
      8       1.1    rillig # Each test consists of:
      9       1.1    rillig # - INP, the input to the test
     10       1.1    rillig # - MOD, the expression for testing the modifier
     11       1.1    rillig # - EXP, the expected output
     12       1.1    rillig 
     13       1.1    rillig TESTS+=		M-paren
     14       1.1    rillig INP.M-paren=	(parentheses) {braces} (opening closing) ()
     15       1.1    rillig MOD.M-paren=	${INP.M-paren:M(*)}
     16       1.1    rillig EXP.M-paren=	(parentheses) ()
     17       1.1    rillig 
     18       1.1    rillig # The first closing brace matches the opening parenthesis.
     19      1.18    rillig # The second closing brace actually ends the expression.
     20       1.1    rillig #
     21       1.1    rillig # XXX: This is unexpected but rarely occurs in practice.
     22       1.1    rillig TESTS+=		M-mixed
     23       1.1    rillig INP.M-mixed=	(paren-brace} (
     24       1.1    rillig MOD.M-mixed=	${INP.M-mixed:M(*}}
     25       1.1    rillig EXP.M-mixed=	(paren-brace}
     26       1.1    rillig 
     27       1.4    rillig # After the :M modifier has parsed the pattern, only the closing brace
     28       1.4    rillig # and the colon are unescaped. The other characters are left as-is.
     29       1.4    rillig # To actually see this effect, the backslashes in the :M modifier need
     30       1.4    rillig # to be doubled since single backslashes would simply be unescaped by
     31       1.4    rillig # Str_Match.
     32       1.4    rillig #
     33       1.4    rillig # XXX: This is unexpected. The opening brace should also be unescaped.
     34       1.4    rillig TESTS+=		M-unescape
     35       1.4    rillig INP.M-unescape=	({}): \(\{\}\)\: \(\{}\):
     36       1.4    rillig MOD.M-unescape=	${INP.M-unescape:M\\(\\{\\}\\)\\:}
     37       1.4    rillig EXP.M-unescape=	\(\{}\):
     38       1.4    rillig 
     39       1.1    rillig # When the :M and :N modifiers are parsed, the pattern finishes as soon
     40       1.1    rillig # as open_parens + open_braces == closing_parens + closing_braces. This
     41       1.1    rillig # means that ( and } form a matching pair.
     42       1.1    rillig #
     43      1.18    rillig # Nested expressions are not parsed as such. Instead, only the
     44       1.1    rillig # parentheses and braces are counted. This leads to a parse error since
     45       1.1    rillig # the nested expression is not "${:U*)}" but only "${:U*)", which is
     46       1.1    rillig # missing the closing brace. The expression is evaluated anyway.
     47       1.1    rillig # The final brace in the output comes from the end of M.nest-mix.
     48       1.1    rillig #
     49       1.1    rillig # XXX: This is unexpected but rarely occurs in practice.
     50       1.1    rillig TESTS+=		M-nest-mix
     51       1.1    rillig INP.M-nest-mix=	(parentheses)
     52       1.1    rillig MOD.M-nest-mix=	${INP.M-nest-mix:M${:U*)}}
     53       1.1    rillig EXP.M-nest-mix=	(parentheses)}
     54      1.19    rillig # make: Unclosed expression, expecting '}' for modifier "U*)" of variable "" with value "*)"
     55       1.1    rillig 
     56       1.1    rillig # In contrast to parentheses and braces, the brackets are not counted
     57       1.1    rillig # when the :M modifier is parsed since Makefile variables only take the
     58       1.1    rillig # ${VAR} or $(VAR) forms, but not $[VAR].
     59       1.1    rillig #
     60       1.1    rillig # The final ] in the pattern is needed to close the character class.
     61       1.1    rillig TESTS+=		M-nest-brk
     62       1.1    rillig INP.M-nest-brk=	[ [[ [[[
     63       1.1    rillig MOD.M-nest-brk=	${INP.M-nest-brk:M${:U[[[[[]}}
     64       1.1    rillig EXP.M-nest-brk=	[
     65       1.1    rillig 
     66       1.1    rillig # The pattern in the nested variable has an unclosed character class.
     67       1.1    rillig # No error is reported though, and the pattern is closed implicitly.
     68       1.1    rillig #
     69       1.1    rillig # XXX: It is unexpected that no error is reported.
     70       1.2    rillig # See str.c, function Str_Match.
     71       1.5    rillig #
     72       1.5    rillig # Before 2019-12-02, this test case triggered an out-of-bounds read
     73       1.5    rillig # in Str_Match.
     74       1.1    rillig TESTS+=		M-pat-err
     75       1.1    rillig INP.M-pat-err=	[ [[ [[[
     76       1.1    rillig MOD.M-pat-err=	${INP.M-pat-err:M${:U[[}}
     77       1.1    rillig EXP.M-pat-err=	[
     78       1.1    rillig 
     79       1.2    rillig # The first backslash does not escape the second backslash.
     80       1.2    rillig # Therefore, the second backslash escapes the parenthesis.
     81       1.2    rillig # This means that the pattern ends there.
     82       1.2    rillig # The final } in the output comes from the end of MOD.M-bsbs.
     83       1.2    rillig #
     84       1.2    rillig # If the first backslash were to escape the second backslash, the first
     85       1.2    rillig # closing brace would match the opening parenthesis (see M-mixed), and
     86       1.2    rillig # the second closing brace would be needed to close the variable.
     87       1.3    rillig # After that, the remaining backslash would escape the parenthesis in
     88       1.3    rillig # the pattern, therefore (} would match.
     89       1.2    rillig TESTS+=		M-bsbs
     90       1.3    rillig INP.M-bsbs=	(} \( \(}
     91       1.2    rillig MOD.M-bsbs=	${INP.M-bsbs:M\\(}}
     92       1.2    rillig EXP.M-bsbs=	\(}
     93       1.3    rillig #EXP.M-bsbs=	(}	# If the first backslash were to escape ...
     94       1.2    rillig 
     95       1.4    rillig # The backslash in \( does not escape the parenthesis, therefore it
     96       1.4    rillig # counts for the nesting level and matches with the first closing brace.
     97       1.4    rillig # The second closing brace closes the variable, and the third is copied
     98       1.4    rillig # literally.
     99       1.4    rillig #
    100       1.4    rillig # The second :M in the pattern is nested between ( and }, therefore it
    101       1.4    rillig # does not start a new modifier.
    102       1.4    rillig TESTS+=		M-bs1-par
    103       1.4    rillig INP.M-bs1-par=	( (:M (:M} \( \(:M \(:M}
    104       1.4    rillig MOD.M-bs1-par=	${INP.M-bs1-par:M\(:M*}}}
    105       1.4    rillig EXP.M-bs1-par=	(:M}}
    106       1.4    rillig 
    107       1.4    rillig # The double backslash is passed verbatim to the pattern matcher.
    108       1.4    rillig # The Str_Match pattern is \\(:M*}, and there the backslash is unescaped.
    109       1.4    rillig # Again, the ( takes place in the nesting level, and there is no way to
    110       1.4    rillig # prevent this, no matter how many backslashes are used.
    111       1.4    rillig TESTS+=		M-bs2-par
    112       1.4    rillig INP.M-bs2-par=	( (:M (:M} \( \(:M \(:M}
    113       1.4    rillig MOD.M-bs2-par=	${INP.M-bs2-par:M\\(:M*}}}
    114       1.4    rillig EXP.M-bs2-par=	\(:M}}
    115       1.4    rillig 
    116       1.6    rillig # Str_Match uses a recursive algorithm for matching the * patterns.
    117       1.6    rillig # Make sure that it survives patterns with 128 asterisks.
    118       1.6    rillig # That should be enough for all practical purposes.
    119       1.6    rillig # To produce a stack overflow, just add more :Qs below.
    120       1.6    rillig TESTS+=		M-128
    121       1.6    rillig INP.M-128=	${:U\\:Q:Q:Q:Q:Q:Q:Q:S,\\,x,g}
    122       1.6    rillig PAT.M-128=	${:U\\:Q:Q:Q:Q:Q:Q:Q:S,\\,*,g}
    123       1.6    rillig MOD.M-128=	${INP.M-128:M${PAT.M-128}}
    124       1.6    rillig EXP.M-128=	${INP.M-128}
    125       1.6    rillig 
    126       1.6    rillig # This is the normal SysV substitution. Nothing surprising here.
    127       1.6    rillig TESTS+=		eq-ext
    128       1.6    rillig INP.eq-ext=	file.c file.cc
    129       1.6    rillig MOD.eq-ext=	${INP.eq-ext:%.c=%.o}
    130       1.6    rillig EXP.eq-ext=	file.o file.cc
    131       1.6    rillig 
    132       1.6    rillig # The SysV := modifier is greedy and consumes all the modifier text
    133       1.6    rillig # up until the closing brace or parenthesis. The :Q may look like a
    134       1.6    rillig # modifier, but it really isn't, that's why it appears in the output.
    135       1.6    rillig TESTS+=		eq-q
    136       1.6    rillig INP.eq-q=	file.c file.cc
    137       1.6    rillig MOD.eq-q=	${INP.eq-q:%.c=%.o:Q}
    138       1.6    rillig EXP.eq-q=	file.o:Q file.cc
    139       1.6    rillig 
    140       1.6    rillig # The = in the := modifier can be escaped.
    141       1.6    rillig TESTS+=		eq-bs
    142       1.6    rillig INP.eq-bs=	file.c file.c=%.o
    143       1.7  christos MOD.eq-bs=	${INP.eq-bs:%.c\=%.o=%.ext}
    144       1.6    rillig EXP.eq-bs=	file.c file.ext
    145       1.6    rillig 
    146       1.8    rillig # Having only an escaped '=' results in a parse error.
    147       1.8    rillig # The call to "pattern.lhs = ParseModifierPart" fails.
    148       1.6    rillig TESTS+=		eq-esc
    149       1.6    rillig INP.eq-esc=	file.c file...
    150       1.6    rillig MOD.eq-esc=	${INP.eq-esc:a\=b}
    151       1.6    rillig EXP.eq-esc=	# empty
    152      1.12    rillig # make: Unfinished modifier for INP.eq-esc ('=' missing)
    153       1.6    rillig 
    154       1.9    rillig TESTS+=		colon
    155       1.9    rillig INP.colon=	value
    156       1.9    rillig MOD.colon=	${INP.colon:}
    157       1.9    rillig EXP.colon=	value
    158       1.9    rillig 
    159      1.10    rillig TESTS+=		colons
    160      1.10    rillig INP.colons=	value
    161      1.10    rillig MOD.colons=	${INP.colons::::}
    162      1.10    rillig EXP.colons=	# empty
    163      1.10    rillig 
    164       1.1    rillig .for test in ${TESTS}
    165  1.20.2.1  perseant # expect+3: while evaluating variable "MOD.eq-esc": while evaluating variable "INP.eq-esc": Unfinished modifier ('=' missing)
    166      1.20    rillig # expect+2: while evaluating variable "MOD.colons": while evaluating variable "INP.colons": Unknown modifier ":"
    167      1.20    rillig # expect+1: while evaluating variable "MOD.colons": while evaluating variable "INP.colons": Unknown modifier ":"
    168       1.1    rillig .  if ${MOD.${test}} == ${EXP.${test}}
    169      1.17    rillig # expect+16: ok M-paren
    170      1.17    rillig # expect+15: ok M-mixed
    171      1.17    rillig # expect+14: ok M-unescape
    172      1.17    rillig # expect+13: ok M-nest-mix
    173      1.17    rillig # expect+12: ok M-nest-brk
    174      1.17    rillig # expect+11: ok M-pat-err
    175      1.17    rillig # expect+10: ok M-bsbs
    176      1.17    rillig # expect+09: ok M-bs1-par
    177      1.17    rillig # expect+08: ok M-bs2-par
    178      1.17    rillig # expect+07: ok M-128
    179      1.17    rillig # expect+06: ok eq-ext
    180      1.17    rillig # expect+05: ok eq-q
    181      1.17    rillig # expect+04: ok eq-bs
    182      1.17    rillig # expect+03: ok eq-esc
    183      1.17    rillig # expect+02: ok colon
    184      1.17    rillig # expect+01: ok colons
    185      1.13    rillig .    info ok ${test}
    186       1.1    rillig .  else
    187      1.13    rillig .    warning error in ${test}: expected "${EXP.${test}}", got "${MOD.${test}}"
    188       1.1    rillig .  endif
    189       1.1    rillig .endfor
    190      1.11    rillig 
    191      1.14    rillig # Even in expressions based on an unnamed variable, there may be errors.
    192      1.14    rillig # XXX: The error message should mention the variable name of the expression,
    193      1.14    rillig # even though that name is empty in this case.
    194      1.17    rillig # expect+2: Malformed conditional (${:Z})
    195      1.20    rillig # expect+1: while evaluating "${:Z}": Unknown modifier "Z"
    196      1.14    rillig .if ${:Z}
    197      1.14    rillig .  error
    198      1.14    rillig .else
    199      1.14    rillig .  error
    200      1.14    rillig .endif
    201      1.14    rillig 
    202      1.14    rillig # Even in expressions based on an unnamed variable, there may be errors.
    203      1.15    rillig #
    204      1.15    rillig # Before var.c 1.842 from 2021-02-23, the error message did not surround the
    205      1.15    rillig # variable name with quotes, leading to the rather confusing "Unfinished
    206      1.15    rillig # modifier for  (',' missing)", having two spaces in a row.
    207      1.15    rillig #
    208  1.20.2.1  perseant # expect+2: while evaluating "${:S,}": Unfinished modifier (',' missing)
    209      1.17    rillig # expect+1: Malformed conditional (${:S,})
    210      1.14    rillig .if ${:S,}
    211      1.14    rillig .  error
    212      1.14    rillig .else
    213      1.14    rillig .  error
    214      1.14    rillig .endif
    215      1.14    rillig 
    216      1.11    rillig all:
    217      1.11    rillig 	@echo ok
    218