varname.mk revision 1.13 1 1.13 rillig # $NetBSD: varname.mk,v 1.13 2023/08/19 11:09:02 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for special variables, such as .MAKE or .PARSEDIR.
4 1.5 rillig # And for variable names in general.
5 1.1 rillig
6 1.5 rillig .MAKEFLAGS: -dv
7 1.5 rillig
8 1.5 rillig # In variable names, braces are allowed, but they must be balanced.
9 1.5 rillig # Parentheses and braces may be mixed.
10 1.5 rillig VAR{{{}}}= 3 braces
11 1.5 rillig .if "${VAR{{{}}}}" != "3 braces"
12 1.5 rillig . error
13 1.5 rillig .endif
14 1.5 rillig
15 1.5 rillig # In variable expressions, the parser works differently. It doesn't treat
16 1.5 rillig # braces and parentheses equally, therefore the first closing brace already
17 1.5 rillig # marks the end of the variable name.
18 1.5 rillig VARNAME= VAR(((
19 1.5 rillig ${VARNAME}= 3 open parentheses
20 1.5 rillig .if "${VAR(((}}}}" != "3 open parentheses}}}"
21 1.5 rillig . error
22 1.5 rillig .endif
23 1.5 rillig
24 1.5 rillig # In the above test, the variable name is constructed indirectly. Neither
25 1.5 rillig # of the following expressions produces the intended effect.
26 1.7 rillig #
27 1.7 rillig # This is not a variable assignment since the parentheses and braces are not
28 1.7 rillig # balanced. At the end of the line, there are still 3 levels open, which
29 1.7 rillig # means the variable name is not finished.
30 1.11 rillig # expect+2: Error in archive specification: "VAR"
31 1.11 rillig # expect+1: No closing parenthesis in archive specification
32 1.6 rillig ${:UVAR(((}= try1
33 1.8 rillig # On the left-hand side of a variable assignments, the backslash is not parsed
34 1.8 rillig # as an escape character, therefore the parentheses still count to the nesting
35 1.8 rillig # level, which at the end of the line is still 3. Therefore this is not a
36 1.8 rillig # variable assignment as well.
37 1.13 rillig # expect+1: Invalid line '${:UVAR\(\(\(}= try2', expanded to 'VAR\(\(\(= try2'
38 1.6 rillig ${:UVAR\(\(\(}= try2
39 1.8 rillig # To assign to a variable with an arbitrary name, the variable name has to
40 1.8 rillig # come from an external source, not the text that is parsed in the assignment
41 1.8 rillig # itself. This is exactly the reason why further above, the indirect
42 1.8 rillig # ${VARNAME} works, while all other attempts fail.
43 1.8 rillig ${VARNAME}= try3
44 1.5 rillig
45 1.5 rillig .MAKEFLAGS: -d0
46 1.1 rillig
47 1.9 rillig # All variable names of a scope are stored in the same hash table, using a
48 1.10 rillig # simple hash function. Ensure that HashTable_Find handles collisions
49 1.9 rillig # correctly and that the correct variable is looked up. The strings "0x" and
50 1.9 rillig # "1Y" have the same hash code, as 31 * '0' + 'x' == 31 * '1' + 'Y'.
51 1.9 rillig V.0x= 0x
52 1.9 rillig V.1Y= 1Y
53 1.9 rillig .if ${V.0x} != "0x" || ${V.1Y} != "1Y"
54 1.9 rillig . error
55 1.9 rillig .endif
56 1.9 rillig
57 1.9 rillig # The string "ASDZguv", when used as a prefix of a variable name, keeps the
58 1.9 rillig # hash code unchanged, its own hash code is 0.
59 1.9 rillig ASDZguvV.0x= 0x
60 1.9 rillig ASDZguvV.1Y= 1Y
61 1.9 rillig .if ${ASDZguvV.0x} != "0x"
62 1.9 rillig . error
63 1.9 rillig .elif ${ASDZguvV.1Y} != "1Y"
64 1.9 rillig . error
65 1.9 rillig .endif
66 1.9 rillig
67 1.9 rillig # Ensure that variables with the same hash code whose name is a prefix of the
68 1.9 rillig # other can be accessed. In this case, the shorter variable name is defined
69 1.9 rillig # first to make it appear later in the bucket of the hash table.
70 1.9 rillig ASDZguv= once
71 1.9 rillig ASDZguvASDZguv= twice
72 1.9 rillig .if ${ASDZguv} != "once"
73 1.9 rillig . error
74 1.9 rillig .elif ${ASDZguvASDZguv} != "twice"
75 1.9 rillig . error
76 1.9 rillig .endif
77 1.9 rillig
78 1.9 rillig # Ensure that variables with the same hash code whose name is a prefix of the
79 1.9 rillig # other can be accessed. In this case, the longer variable name is defined
80 1.9 rillig # first to make it appear later in the bucket of the hash table.
81 1.9 rillig ASDZguvASDZguv.param= twice
82 1.9 rillig ASDZguv.param= once
83 1.9 rillig .if ${ASDZguv.param} != "once"
84 1.9 rillig . error
85 1.9 rillig .elif ${ASDZguvASDZguv.param} != "twice"
86 1.9 rillig . error
87 1.9 rillig .endif
88 1.9 rillig
89 1.1 rillig all:
90