Module Name: src Committed By: rillig Date: Sat Dec 16 23:38:35 UTC 2023
Modified Files: src/usr.bin/make/unit-tests: varmod-match.exp varmod-match.mk Log Message: tests/make: reorganize tests for the ':M' modifier Add a table of contents. Group the existing tests into sections. Fix the pattern of the test for the malformed ':M[\' modifier, now in line 283. Note that the tests for the pattern characters '*?\' are missing. To generate a diff of this commit: cvs rdiff -u -r1.10 -r1.11 src/usr.bin/make/unit-tests/varmod-match.exp cvs rdiff -u -r1.17 -r1.18 src/usr.bin/make/unit-tests/varmod-match.mk Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/unit-tests/varmod-match.exp diff -u src/usr.bin/make/unit-tests/varmod-match.exp:1.10 src/usr.bin/make/unit-tests/varmod-match.exp:1.11 --- src/usr.bin/make/unit-tests/varmod-match.exp:1.10 Fri Jun 23 04:56:54 2023 +++ src/usr.bin/make/unit-tests/varmod-match.exp Sat Dec 16 23:38:35 2023 @@ -1,25 +1,14 @@ -CondParser_Eval: ${NUMBERS:M[A-Z]*} != "One Two Three Four" -Comparing "One Two Three Four" != "One Two Three Four" -CondParser_Eval: ${NUMBERS:M[^A-Z]*} != "five six seven" -Comparing "five six seven" != "five six seven" -CondParser_Eval: ${NUMBERS:M[^s]*[ex]} != "One Three five" -Comparing "One Three five" != "One Three five" -CondParser_Eval: ${:U****************:M****************b} -CondParser_Eval: ${:U..................................................b:M*?*?*?*?*?a} -CondParser_Eval: ${:Ua \$ sign:M*$$*} != "\$" -Comparing "$" != "$" -CondParser_Eval: ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" -Comparing "any-asterisk" != "any-asterisk" -make: "varmod-match.mk" line 162: warning: Unfinished character list in pattern '[' of modifier ':M' -make: "varmod-match.mk" line 162: Unknown modifier "]" -make: "varmod-match.mk" line 162: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") -make: "varmod-match.mk" line 205: warning: Unfinished character list in pattern 'a[' of modifier ':M' -make: "varmod-match.mk" line 213: warning: Unfinished character list in pattern 'a[^' of modifier ':M' -make: "varmod-match.mk" line 221: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M' -make: "varmod-match.mk" line 229: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M' -make: "varmod-match.mk" line 238: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M' -make: "varmod-match.mk" line 258: warning: Unfinished character range in pattern '[x-' of modifier ':M' -make: "varmod-match.mk" line 270: warning: Unfinished character range in pattern '[^x-' of modifier ':M' +make: "varmod-match.mk" line 236: warning: Unfinished character list in pattern 'a[' of modifier ':M' +make: "varmod-match.mk" line 244: warning: Unfinished character list in pattern 'a[^' of modifier ':M' +make: "varmod-match.mk" line 252: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M' +make: "varmod-match.mk" line 260: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M' +make: "varmod-match.mk" line 269: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M' +make: "varmod-match.mk" line 283: warning: Unfinished character list in pattern '?[\' of modifier ':M' +make: "varmod-match.mk" line 291: warning: Unfinished character range in pattern '[x-' of modifier ':M' +make: "varmod-match.mk" line 303: warning: Unfinished character range in pattern '[^x-' of modifier ':M' +make: "varmod-match.mk" line 311: warning: Unfinished character list in pattern '[' of modifier ':M' +make: "varmod-match.mk" line 311: Unknown modifier "]" +make: "varmod-match.mk" line 311: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 Index: src/usr.bin/make/unit-tests/varmod-match.mk diff -u src/usr.bin/make/unit-tests/varmod-match.mk:1.17 src/usr.bin/make/unit-tests/varmod-match.mk:1.18 --- src/usr.bin/make/unit-tests/varmod-match.mk:1.17 Sun Nov 19 21:47:52 2023 +++ src/usr.bin/make/unit-tests/varmod-match.mk Sat Dec 16 23:38:35 2023 @@ -1,97 +1,46 @@ -# $NetBSD: varmod-match.mk,v 1.17 2023/11/19 21:47:52 rillig Exp $ +# $NetBSD: varmod-match.mk,v 1.18 2023/12/16 23:38:35 rillig Exp $ # -# Tests for the :M variable modifier, which filters words that match the +# Tests for the ':M' modifier, which keeps only those words that match the # given pattern. # -# See ApplyModifier_Match, ParseModifier_Match and ModifyWord_Match. - -.MAKEFLAGS: -dc +# Table of contents +# +# 1. Pattern characters '*', '?' and '\' +# 2. Character lists and character ranges +# 3. Parsing and escaping +# 4. Interaction with other modifiers +# 5. Performance +# 6. Error handling +# 7. Historical bugs NUMBERS= One Two Three Four five six seven -# Only keep words that start with an uppercase letter. -.if ${NUMBERS:M[A-Z]*} != "One Two Three Four" -. error -.endif - -# Only keep words that start with a character other than an uppercase letter. -.if ${NUMBERS:M[^A-Z]*} != "five six seven" -. error -.endif - -# Only keep words that don't start with s and at the same time end with -# either of [ex]. +# 1. Pattern characters '*', '?' and '\' # -# This test case ensures that the negation from the first character class -# does not propagate to the second character class. -.if ${NUMBERS:M[^s]*[ex]} != "One Three five" -. error -.endif +# * matches 0 or more characters +# ? matches 1 character +# \x matches the character 'x' -# Before 2020-06-13, this expression called Str_Match 601,080,390 times. -# Since 2020-06-13, this expression calls Str_Match 1 time. -.if ${:U****************:M****************b} -.endif +# TODO -# Before 2023-06-22, this expression called Str_Match 2,621,112 times. -# Adding another '*?' to the pattern called Str_Match 20,630,572 times. -# Adding another '*?' to the pattern called Str_Match 136,405,672 times. -# Adding another '*?' to the pattern called Str_Match 773,168,722 times. -# Adding another '*?' to the pattern called Str_Match 3,815,481,072 times. -# Since 2023-06-22, Str_Match no longer backtracks. -.if ${:U..................................................b:M*?*?*?*?*?a} -.endif -# To match a dollar sign in a word, double it. +# 2. Character lists and character ranges # -# This is different from the :S and :C variable modifiers, where a '$' -# has to be escaped as '\$'. -.if ${:Ua \$ sign:M*$$*} != "\$" +# [...] matches 1 character from the listed characters +# [^...] matches 1 character from the unlisted characters +# [a-z] matches 1 character from the range 'a' to 'z' +# [z-a] matches 1 character from the range 'a' to 'z' + +# Only keep words that start with an uppercase letter. +.if ${NUMBERS:M[A-Z]*} != "One Two Three Four" . error .endif -# In the :M modifier, '\$' does not escape a dollar. Instead it is -# interpreted as a backslash followed by whatever expression the -# '$' starts. -# -# This differs from the :S, :C and several other variable modifiers. -${:U*}= asterisk -.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" +# Only keep words that start with a character other than an uppercase letter. +.if ${NUMBERS:M[^A-Z]*} != "five six seven" . error .endif -# TODO: ${VAR:M(((}}}} -# TODO: ${VAR:M{{{)))} -# TODO: ${VAR:M${UNBALANCED}} -# TODO: ${VAR:M${:U(((\}\}\}}} - -.MAKEFLAGS: -d0 - -# Special characters: -# * matches 0 or more arbitrary characters -# ? matches a single arbitrary character -# \ starts an escape sequence, only outside ranges -# [ starts a set for matching a single character -# ] ends a set for matching a single character -# - in a set, forms a range of characters -# ^ as the first character in a set, negates the set -# ( during parsing of the pattern, starts a nesting level -# ) during parsing of the pattern, ends a nesting level -# { during parsing of the pattern, starts a nesting level -# } during parsing of the pattern, ends a nesting level -# : during parsing of the pattern, finishes the pattern -# $ during parsing of the pattern, starts a nested expression -# # in a line except a shell command, starts a comment -# -# Pattern parts: -# * matches 0 or more arbitrary characters -# ? matches exactly 1 arbitrary character -# \x matches exactly the character 'x' -# [...] matches exactly 1 character from the set -# [^...] matches exactly 1 character outside the set -# [a-z] matches exactly 1 character from the range 'a' to 'z' -# - # [] matches never .if ${ ab a[]b a[b a b :L:M[]} != "" . error @@ -134,12 +83,82 @@ ${:U*}= asterisk . error .endif -# [\] matches a single backslash -WORDS= a\b a[\]b ab +# [\] matches a single backslash; no escaping takes place in +# character ranges +# Without the 'b' in the below words, the backslash would end a word and thus +# influence how the string is split into words. +WORDS= a\b a[\]b ab a\\b .if ${WORDS:Ma[\]b} != "a\\b" . error .endif +# [[-]] May look like it would match a single '[', '\' or ']', but +# the inner ']' has two roles: it is the upper bound of the +# character range as well as the closing character of the +# character list. The outer ']' is just a regular character. +WORDS= [ ] [] \] ]] +.if ${WORDS:M[[-]]} != "[] \\] ]]" +. error +.endif + +# [b[-]a] +# Same as for '[[-]]': the character list stops at the first +# ']', and the 'a]' is treated as a literal string. +WORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba] +.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]" +. error +.endif + +# [-] Matches a single '-' since the '-' only becomes part of a +# character range if it is preceded and followed by another +# character. +WORDS= - -] +.if ${WORDS:M[-]} != "-" +. error +.endif + +# Only keep words that don't start with s and at the same time end with +# either of [ex]. +# +# This test case ensures that the negation from the first character class +# does not propagate to the second character class. +.if ${NUMBERS:M[^s]*[ex]} != "One Three five" +. error +.endif + + +# 3. Parsing and escaping +# +# * matches 0 or more characters +# ? matches 1 character +# \ outside a character list, escapes the following character +# [ starts a character list for matching 1 character +# ] ends a character list for matching 1 character +# - in a character list, forms a character range +# ^ at the beginning of a character list, negates the list +# ( while parsing the pattern, starts a nesting level +# ) while parsing the pattern, ends a nesting level +# { while parsing the pattern, starts a nesting level +# } while parsing the pattern, ends a nesting level +# : while parsing the pattern, terminates the pattern +# $ while parsing the pattern, starts a nested expression +# # in a line except a shell command, starts a comment + +# The pattern can come from an expression. For single-letter +# variables, either the short form or the long form can be used, just as +# everywhere else. +PRIMES= 2 3 5 7 11 +n= 2 +.if ${PRIMES:M$n} != "2" +. error +.endif +.if ${PRIMES:M${n}} != "2" +. error +.endif +.if ${PRIMES:M${:U2}} != "2" +. error +.endif + # : terminates the pattern .if ${ A * :L:M:} != "" . error @@ -155,50 +174,64 @@ WORDS= a\b a[\]b ab . error .endif -# [:] matches never since the ':' starts the next modifier -# expect+3: warning: Unfinished character list in pattern '[' of modifier ':M' -# expect+2: Unknown modifier "]" -# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") -.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":" -. error -.else +# To match a dollar sign in a word, double it. +# +# This is different from the :S and :C variable modifiers, where a '$' +# has to be escaped as '\$'. +.if ${:Ua \$ sign:M*$$*} != "\$" . error .endif -# [\] matches exactly a backslash; no escaping takes place in -# character ranges -# Without the 'a' in the below words, the backslash would end a word and thus -# influence how the string is split into words. -WORDS= 1\a 2\\a -.if ${WORDS:M?[\]a} != "1\\a" +# In the :M modifier, '\$' does not escape a dollar. Instead it is +# interpreted as a backslash followed by whatever expression the +# '$' starts. +# +# This differs from the :S, :C and several other variable modifiers. +${:U*}= asterisk +.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" . error .endif -# [[-]] May look like it would match a single '[', '\' or ']', but -# the inner ']' has two roles: it is the upper bound of the -# character range as well as the closing character of the -# character list. The outer ']' is just a regular character. -WORDS= [ ] [] \] ]] -.if ${WORDS:M[[-]]} != "[] \\] ]]" +# TODO: ${VAR:M(((}}}} +# TODO: ${VAR:M{{{)))} +# TODO: ${VAR:M${UNBALANCED}} +# TODO: ${VAR:M${:U(((\}\}\}}} + + +# 4. Interaction with other modifiers + +# The modifier ':tW' prevents splitting at whitespace. Even leading and +# trailing whitespace is preserved. +.if ${ plain string :L:tW:M*} != " plain string " . error .endif -# [b[-]a] -# Same as for '[[-]]': the character list stops at the first -# ']', and the 'a]' is treated as a literal string. -WORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba] -.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]" +# Without the modifier ':tW', the string is split into words. All whitespace +# around and between the words is normalized to a single space. +.if ${ plain string :L:M*} != "plain string" . error .endif -# [-] Matches a single '-' since the '-' only becomes part of a -# character range if it is preceded and followed by another -# character. -WORDS= - -] -.if ${WORDS:M[-]} != "-" -. error + +# 5. Performance + +# Before 2020-06-13, this expression called Str_Match 601,080,390 times. +# Since 2020-06-13, this expression calls Str_Match 1 time. +.if ${:U****************:M****************b} +.endif + +# Before 2023-06-22, this expression called Str_Match 2,621,112 times. +# Adding another '*?' to the pattern called Str_Match 20,630,572 times. +# Adding another '*?' to the pattern called Str_Match 136,405,672 times. +# Adding another '*?' to the pattern called Str_Match 773,168,722 times. +# Adding another '*?' to the pattern called Str_Match 3,815,481,072 times. +# Since 2023-06-22, Str_Match no longer backtracks. +.if ${:U..................................................b:M*?*?*?*?*?a} .endif + +# 6. Error handling + # [ Incomplete empty character list, never matches. WORDS= a a[ # expect+1: warning: Unfinished character list in pattern 'a[' of modifier ':M' @@ -247,7 +280,9 @@ WORDS= - + x xx 0 1 2 3 4 [x1-3 # part of the word. Only the very last word of a string can be # '\', as there is no following space that could be escaped. WORDS= \\ \a ${:Ux\\} -.if ${WORDS:M?[\]} != "\\\\ x\\" +PATTERN= ${:U?[\\} +# expect+1: warning: Unfinished character list in pattern '?[\' of modifier ':M' +.if ${WORDS:M${PATTERN}} != "\\\\ x\\" . error .endif @@ -271,35 +306,18 @@ WORDS= [x- x x- y yyyyy . error .endif - -# The modifier ':tW' prevents splitting at whitespace. Even leading and -# trailing whitespace is preserved. -.if ${ plain string :L:tW:M*} != " plain string " +# [:] matches never since the ':' starts the next modifier +# expect+3: warning: Unfinished character list in pattern '[' of modifier ':M' +# expect+2: Unknown modifier "]" +# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") +.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":" . error -.endif - -# Without the modifier ':tW', the string is split into words. All whitespace -# around and between the words is normalized to a single space. -.if ${ plain string :L:M*} != "plain string" +.else . error .endif -# The pattern can come from an expression. For single-letter -# variables, either the short form or the long form can be used, just as -# everywhere else. -PRIMES= 2 3 5 7 11 -n= 2 -.if ${PRIMES:M$n} != "2" -. error -.endif -.if ${PRIMES:M${n}} != "2" -. error -.endif -.if ${PRIMES:M${:U2}} != "2" -. error -.endif - +# 7. Historical bugs # Before var.c 1.1031 from 2022-08-24, the following expressions caused an # out-of-bounds read beyond the indirect ':M' modifiers.