xref: /freebsd/contrib/bmake/unit-tests/varparse-errors.mk (revision 4fde40d9b540ea1a544cb4378a14f2f6da85ca6d)
1*4fde40d9SSimon J. Gerraty# $NetBSD: varparse-errors.mk,v 1.7 2022/08/24 22:09:41 rillig Exp $
2e2eeea75SSimon J. Gerraty
3e2eeea75SSimon J. Gerraty# Tests for parsing and evaluating all kinds of variable expressions.
4e2eeea75SSimon J. Gerraty#
5e2eeea75SSimon J. Gerraty# This is the basis for redesigning the error handling in Var_Parse and
6e2eeea75SSimon J. Gerraty# Var_Subst, collecting typical and not so typical use cases.
7e2eeea75SSimon J. Gerraty#
8e2eeea75SSimon J. Gerraty# See also:
9e2eeea75SSimon J. Gerraty#	VarParseResult
10e2eeea75SSimon J. Gerraty#	Var_Parse
11e2eeea75SSimon J. Gerraty#	Var_Subst
12e2eeea75SSimon J. Gerraty
13e2eeea75SSimon J. GerratyPLAIN=		plain value
14e2eeea75SSimon J. Gerraty
15e2eeea75SSimon J. GerratyLITERAL_DOLLAR=	To get a dollar, double $$ it.
16e2eeea75SSimon J. Gerraty
17e2eeea75SSimon J. GerratyINDIRECT=	An ${:Uindirect} value.
18e2eeea75SSimon J. Gerraty
19e2eeea75SSimon J. GerratyREF_UNDEF=	A reference to an ${UNDEF}undefined variable.
20e2eeea75SSimon J. Gerraty
21e2eeea75SSimon J. GerratyERR_UNCLOSED=	An ${UNCLOSED variable expression.
22e2eeea75SSimon J. Gerraty
23e2eeea75SSimon J. GerratyERR_BAD_MOD=	An ${:Uindirect:Z} expression with an unknown modifier.
24e2eeea75SSimon J. Gerraty
25e2eeea75SSimon J. GerratyERR_EVAL=	An evaluation error ${:Uvalue:C,.,\3,}.
26e2eeea75SSimon J. Gerraty
27e2eeea75SSimon J. Gerraty# In a conditional, a variable expression that is not enclosed in quotes is
28b0c40a00SSimon J. Gerraty# expanded using the mode VARE_UNDEFERR.
29e2eeea75SSimon J. Gerraty# The variable itself must be defined.
30e2eeea75SSimon J. Gerraty# It may refer to undefined variables though.
31e2eeea75SSimon J. Gerraty.if ${REF_UNDEF} != "A reference to an undefined variable."
32e2eeea75SSimon J. Gerraty.  error
33e2eeea75SSimon J. Gerraty.endif
34e2eeea75SSimon J. Gerraty
3506b9b3e0SSimon J. Gerraty# As of 2020-12-01, errors in the variable name are silently ignored.
3606b9b3e0SSimon J. Gerraty# Since var.c 1.754 from 2020-12-20, unknown modifiers at parse time result
3706b9b3e0SSimon J. Gerraty# in an error message and a non-zero exit status.
3806b9b3e0SSimon J. GerratyVAR.${:U:Z}=	unknown modifier in the variable name
3906b9b3e0SSimon J. Gerraty.if ${VAR.} != "unknown modifier in the variable name"
4006b9b3e0SSimon J. Gerraty.  error
4106b9b3e0SSimon J. Gerraty.endif
4206b9b3e0SSimon J. Gerraty
4306b9b3e0SSimon J. Gerraty# As of 2020-12-01, errors in the variable name are silently ignored.
4406b9b3e0SSimon J. Gerraty# Since var.c 1.754 from 2020-12-20, unknown modifiers at parse time result
4506b9b3e0SSimon J. Gerraty# in an error message and a non-zero exit status.
4606b9b3e0SSimon J. GerratyVAR.${:U:Z}post=	unknown modifier with text in the variable name
4706b9b3e0SSimon J. Gerraty.if ${VAR.post} != "unknown modifier with text in the variable name"
4806b9b3e0SSimon J. Gerraty.  error
4906b9b3e0SSimon J. Gerraty.endif
5006b9b3e0SSimon J. Gerraty
519f45a3c8SSimon J. Gerraty# Demonstrate an edge case in which the 'static' for 'errorReported' in
529f45a3c8SSimon J. Gerraty# Var_Subst actually makes a difference, preventing "a plethora of messages".
539f45a3c8SSimon J. Gerraty# Given that this is an edge case and the error message is wrong and thus
549f45a3c8SSimon J. Gerraty# misleading anyway, that piece of code is probably not necessary.  The wrong
559f45a3c8SSimon J. Gerraty# condition was added in var.c 1.185 from 2014-05-19.
569f45a3c8SSimon J. Gerraty#
579f45a3c8SSimon J. Gerraty# To trigger this difference, the variable assignment must use the assignment
589f45a3c8SSimon J. Gerraty# operator ':=' to make VarEvalMode_ShouldKeepUndef return true.  There must
599f45a3c8SSimon J. Gerraty# be 2 expressions that create a parse error, which in this case is ':OX'.
609f45a3c8SSimon J. Gerraty# These expressions must be nested in some way.  The below expressions are
619f45a3c8SSimon J. Gerraty# minimal, that is, removing any part of it destroys the effect.
629f45a3c8SSimon J. Gerraty#
639f45a3c8SSimon J. Gerraty# Without the 'static', there would be one more message like this:
649f45a3c8SSimon J. Gerraty#	Undefined variable "${:U:OX"
659f45a3c8SSimon J. Gerraty#
669f45a3c8SSimon J. Gerraty#.MAKEFLAGS: -dv
679f45a3c8SSimon J. GerratyIND=	${:OX}
689f45a3c8SSimon J. Gerraty_:=	${:U:OX:U${IND}} ${:U:OX:U${IND}}
699f45a3c8SSimon J. Gerraty#.MAKEFLAGS: -d0
709f45a3c8SSimon J. Gerraty
71*4fde40d9SSimon J. Gerraty
72*4fde40d9SSimon J. Gerraty# Before var.c 1.032 from 2022-08-24, make complained about 'Unknown modifier'
73*4fde40d9SSimon J. Gerraty# or 'Bad modifier' when in fact the modifier was entirely correct, it was
74*4fde40d9SSimon J. Gerraty# just not delimited by either ':' or '}' but instead by '\0'.
75*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:Q
76*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:sh
77*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:tA
78*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:tsX
79*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:ts
80*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:ts\040
81*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:u
82*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:H
83*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:[1]
84*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:hash
85*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:range
86*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:_
87*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:gmtime
88*4fde40d9SSimon J. GerratyUNCLOSED:=	${:U:localtime
89