1# $NetBSD: directive-if.mk,v 1.11 2022/01/23 21:48:59 rillig Exp $
2#
3# Tests for the .if directive.
4#
5# See also:
6#	cond-*.mk
7
8# TODO: Implementation
9
10.if 0
11.  error
12.else
13.  info 0 evaluates to false.
14.endif
15
16.if 1
17.  info 1 evaluates to true.
18.else
19.  error
20.endif
21
22# There is no '.ifx'.
23#
24# The commit from 2005-05-01 intended to detect this situation, but it failed
25# to do this since the call to is_token had its arguments switched.  They were
26# expected as (str, token, token_len) but were actually passed as (token, str,
27# token_len).  This made is_token return true even if the directive was
28# directly followed by alphanumerical characters, which was wrong.  The
29# typical cases produced an error message such as "Malformed conditional
30# (x 123)", while the intended error message was "Unknown directive".
31#
32# Back at that time, the commits only modified the main code but did not add
33# the corresponding unit tests.  This allowed the bug to hide for more than
34# 15 years.
35#
36# Since 2020-11-10, the correct error message is produced.  The '.ifx' is no
37# longer interpreted as a variant of '.if', therefore the '.error' and '.else'
38# are interpreted as ordinary directives, producing the error messages
39# "if-less else" and "if-less endif".
40# expect+1: Unknown directive "ifx"
41.ifx 123
42# expect+1: This is not conditional.
43.info This is not conditional.
44# expect+1: if-less else
45.else
46# expect+1: This is not conditional.
47.info This is not conditional.
48# expect+1: if-less endif
49.endif
50
51# Missing condition.
52# expect+1: Malformed conditional ()
53.if
54.  error
55.else
56.  error
57.endif
58
59# A plain word must not start with a '"'.  It may contain a embedded quotes
60# though, which are kept.  The quotes need not be balanced.  The next space
61# ends the word, and the remaining " || 1" is parsed as "or true".
62.if ${:Uplain"""""} == plain""""" || 1
63.  info Quotes in plain words are probably a mistake.
64# XXX: Accepting quotes in plain words is probably a mistake as well.
65.else
66.  error
67.endif
68
69.if0
70.  error
71.else
72.  info Don't do this, always put a space after a directive.
73.endif
74
75.if${:U-3}
76.  info Don't do this, always put a space after a directive.
77.else
78.  error
79.endif
80
81.if${:U-3}>-4
82.  info Don't do this, always put a space around comparison operators.
83.else
84.  error
85.endif
86
87.if(1)
88.  info Don't do this, always put a space after a directive.
89.endif
90
91.if!0
92.  info Don't do this, always put a space after a directive.
93.endif
94
95
96# The directives '.ifdef' and '.ifmake' can be negated by inserting an 'n'.
97# This doesn't work for a plain '.if' though.
98#
99# expect+1: Unknown directive "ifn"
100.ifn 0
101