xref: /netbsd-src/usr.bin/make/unit-tests/varparse-errors.mk (revision 224a1fcf9385c9ea344f3f0ec6a1ff9347e98a32)
1*224a1fcfSrillig# $NetBSD: varparse-errors.mk,v 1.19 2024/08/29 20:20:37 rillig Exp $
2fc3bb009Srillig
31da4da3fSrillig# Tests for parsing and evaluating all kinds of expressions.
4fc3bb009Srillig#
5fc3bb009Srillig# This is the basis for redesigning the error handling in Var_Parse and
6fc3bb009Srillig# Var_Subst, collecting typical and not so typical use cases.
7fc3bb009Srillig#
8fc3bb009Srillig# See also:
9fc3bb009Srillig#	Var_Parse
10fc3bb009Srillig#	Var_Subst
11fc3bb009Srillig
12fc3bb009SrilligPLAIN=		plain value
13fc3bb009Srillig
14fc3bb009SrilligLITERAL_DOLLAR=	To get a dollar, double $$ it.
15fc3bb009Srillig
16fc3bb009SrilligINDIRECT=	An ${:Uindirect} value.
17fc3bb009Srillig
18fc3bb009SrilligREF_UNDEF=	A reference to an ${UNDEF}undefined variable.
19fc3bb009Srillig
20da2f1f97SrilligERR_UNCLOSED=	An ${UNCLOSED expression.
21fc3bb009Srillig
22fc3bb009SrilligERR_BAD_MOD=	An ${:Uindirect:Z} expression with an unknown modifier.
23fc3bb009Srillig
24fc3bb009SrilligERR_EVAL=	An evaluation error ${:Uvalue:C,.,\3,}.
25fc3bb009Srillig
261da4da3fSrillig# In a conditional, an expression that is not enclosed in quotes is
270c336a8cSrillig# expanded using the mode VARE_EVAL_DEFINED.
28fc3bb009Srillig# The variable itself must be defined.
29fc3bb009Srillig# It may refer to undefined variables though.
30fc3bb009Srillig.if ${REF_UNDEF} != "A reference to an undefined variable."
31fc3bb009Srillig.  error
32fc3bb009Srillig.endif
33fc3bb009Srillig
340ab8d018Srillig# As of 2020-12-01, errors in the variable name are silently ignored.
3500ba7da9Srillig# Since var.c 1.754 from 2020-12-20, unknown modifiers at parse time result
3600ba7da9Srillig# in an error message and a non-zero exit status.
37*224a1fcfSrillig# expect+1: Unknown modifier "Z"
380ab8d018SrilligVAR.${:U:Z}=	unknown modifier in the variable name
390ab8d018Srillig.if ${VAR.} != "unknown modifier in the variable name"
400ab8d018Srillig.  error
410ab8d018Srillig.endif
420ab8d018Srillig
430ab8d018Srillig# As of 2020-12-01, errors in the variable name are silently ignored.
4400ba7da9Srillig# Since var.c 1.754 from 2020-12-20, unknown modifiers at parse time result
4500ba7da9Srillig# in an error message and a non-zero exit status.
46*224a1fcfSrillig# expect+1: Unknown modifier "Z"
470ab8d018SrilligVAR.${:U:Z}post=	unknown modifier with text in the variable name
480ab8d018Srillig.if ${VAR.post} != "unknown modifier with text in the variable name"
490ab8d018Srillig.  error
500ab8d018Srillig.endif
510ab8d018Srillig
52971eb6dfSrillig# Demonstrate an edge case in which the 'static' for 'errorReported' in
53971eb6dfSrillig# Var_Subst actually makes a difference, preventing "a plethora of messages".
54971eb6dfSrillig# Given that this is an edge case and the error message is wrong and thus
55971eb6dfSrillig# misleading anyway, that piece of code is probably not necessary.  The wrong
56971eb6dfSrillig# condition was added in var.c 1.185 from 2014-05-19.
57971eb6dfSrillig#
58971eb6dfSrillig# To trigger this difference, the variable assignment must use the assignment
59971eb6dfSrillig# operator ':=' to make VarEvalMode_ShouldKeepUndef return true.  There must
60971eb6dfSrillig# be 2 expressions that create a parse error, which in this case is ':OX'.
61971eb6dfSrillig# These expressions must be nested in some way.  The below expressions are
62971eb6dfSrillig# minimal, that is, removing any part of it destroys the effect.
63971eb6dfSrillig#
64971eb6dfSrillig# Without the 'static', there would be one more message like this:
65971eb6dfSrillig#	Undefined variable "${:U:OX"
66971eb6dfSrillig#
67971eb6dfSrillig#.MAKEFLAGS: -dv
68971eb6dfSrilligIND=	${:OX}
69*224a1fcfSrillig# expect+4: Bad modifier ":OX"
70*224a1fcfSrillig# expect+3: Bad modifier ":OX"
71*224a1fcfSrillig# expect+2: Bad modifier ":OX"
72*224a1fcfSrillig# expect+1: Bad modifier ":OX"
73971eb6dfSrillig_:=	${:U:OX:U${IND}} ${:U:OX:U${IND}}
74971eb6dfSrillig#.MAKEFLAGS: -d0
75971eb6dfSrillig
761a1ff57eSrillig
771a1ff57eSrillig# Before var.c 1.032 from 2022-08-24, make complained about 'Unknown modifier'
781a1ff57eSrillig# or 'Bad modifier' when in fact the modifier was entirely correct, it was
791a1ff57eSrillig# just not delimited by either ':' or '}' but instead by '\0'.
80*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "Q"
81fe11cfeeSrilligUNCLOSED:=	${:U:Q
82*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "sh"
83fe11cfeeSrilligUNCLOSED:=	${:U:sh
84*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "tA"
85fe11cfeeSrilligUNCLOSED:=	${:U:tA
86*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "tsX"
87fe11cfeeSrilligUNCLOSED:=	${:U:tsX
88*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "ts"
89fe11cfeeSrilligUNCLOSED:=	${:U:ts
90*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "ts\040"
91fe11cfeeSrilligUNCLOSED:=	${:U:ts\040
92*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "u"
93fe11cfeeSrilligUNCLOSED:=	${:U:u
94*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "H"
95fe11cfeeSrilligUNCLOSED:=	${:U:H
96*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "[1]"
97fe11cfeeSrilligUNCLOSED:=	${:U:[1]
98*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "hash"
99fe11cfeeSrilligUNCLOSED:=	${:U:hash
100*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "range"
101fe11cfeeSrilligUNCLOSED:=	${:U:range
102*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "_"
103fe11cfeeSrilligUNCLOSED:=	${:U:_
104*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "gmtime"
105fe11cfeeSrilligUNCLOSED:=	${:U:gmtime
106*224a1fcfSrillig# expect+1: Unclosed expression, expecting '}' for modifier "localtime"
107fe11cfeeSrilligUNCLOSED:=	${:U:localtime
108