Home | History | Annotate | Line # | Download | only in unit-tests
cond-short.mk revision 1.16
      1 # $NetBSD: cond-short.mk,v 1.16 2021/03/14 11:49:37 rillig Exp $
      2 #
      3 # Demonstrates that in conditions, the right-hand side of an && or ||
      4 # is only evaluated if it can actually influence the result.
      5 # This is called 'short-circuit evaluation' and is the usual evaluation
      6 # mode in most programming languages.  A notable exception is Ada, which
      7 # distinguishes between the operators 'And', 'And Then', 'Or', 'Or Else'.
      8 #
      9 # Before 2020-06-28, the right-hand side of an && or || operator was always
     10 # evaluated, which was wrong.  In cond.c 1.69 and var.c 1.197 on 2015-10-11,
     11 # Var_Parse got a new parameter named 'wantit'.  Since then it would have been
     12 # possible to skip evaluation of irrelevant variable expressions and only
     13 # parse them.  They were still evaluated though, the only difference to
     14 # relevant variable expressions was that in the irrelevant variable
     15 # expressions, undefined variables were allowed.
     16 #
     17 # See also:
     18 #	var-eval-short.mk, for short-circuited variable modifiers
     19 
     20 # The && operator:
     21 
     22 .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
     23 .endif
     24 
     25 .if 1 && ${echo "expected and" 1>&2 :L:sh}
     26 .endif
     27 
     28 .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
     29 .endif
     30 
     31 .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
     32 .endif
     33 
     34 .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
     35 .endif
     36 
     37 .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
     38 .endif
     39 
     40 # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
     41 # The whole !empty condition is only parsed and then discarded.
     42 VAR=	${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
     43 VAR13=	${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
     44 .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
     45 .endif
     46 
     47 VAR=	${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
     48 VAR23=	${VAR${:U22${echo   "expected VAR23" 1>&2 :L:sh}}}
     49 .if 1 && !empty(VAR${:U23${echo   "expected U23 condition" 1>&2 :L:sh}})
     50 .endif
     51 VAR=	# empty again, for the following tests
     52 
     53 # The :M modifier is only parsed, not evaluated.
     54 # Before 2020-07-02, it was wrongly evaluated.
     55 .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
     56 .endif
     57 
     58 .if 1 && !empty(VAR:M${:U${echo   "expected M pattern" 1>&2 :L:sh}})
     59 .endif
     60 
     61 .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
     62 .endif
     63 
     64 .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
     65 .endif
     66 
     67 .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
     68 .endif
     69 
     70 .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
     71 .endif
     72 
     73 .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
     74 .endif
     75 
     76 .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
     77 .endif
     78 
     79 # Irrelevant assignment modifiers are skipped as well.
     80 .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
     81 .endif
     82 .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
     83 .endif
     84 .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
     85 .endif
     86 .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
     87 .endif
     88 .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
     89 .  warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
     90 .endif
     91 
     92 # The || operator:
     93 
     94 .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
     95 .endif
     96 
     97 .if 0 || ${echo "expected or" 1>&2 :L:sh}
     98 .endif
     99 
    100 .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
    101 .endif
    102 
    103 .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
    104 .endif
    105 
    106 .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
    107 .endif
    108 
    109 .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
    110 .endif
    111 
    112 # Unreachable nested conditions are skipped completely as well.
    113 
    114 .if 0
    115 .  if ${echo "unexpected nested and" 1>&2 :L:sh}
    116 .  endif
    117 .endif
    118 
    119 .if 1
    120 .elif ${echo "unexpected nested or" 1>&2 :L:sh}
    121 .endif
    122 
    123 # make sure these do not cause complaint
    124 #.MAKEFLAGS: -dc
    125 
    126 # TODO: Rewrite this whole section and check all the conditions and variables.
    127 # Several of the assumptions are probably wrong here.
    128 # TODO: replace 'x=' with '.info' or '.error'.
    129 V42=	42
    130 iV1=	${V42}
    131 iV2=	${V66}
    132 
    133 .if defined(V42) && ${V42} > 0
    134 x=	Ok
    135 .else
    136 x=	Fail
    137 .endif
    138 x!=	echo 'defined(V42) && $${V42} > 0: $x' >&2; echo
    139 
    140 # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    141 # warning: "String comparison operator should be either == or !=".
    142 # This was because the variable expression ${iV2} was defined, but the
    143 # contained variable V66 was undefined.  The left-hand side of the comparison
    144 # therefore evaluated to the string "${V66}", which is obviously not a number.
    145 #
    146 # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    147 # comparisons.  Instead, they are only parsed and then discarded.
    148 #
    149 # At that time, there was not enough debug logging to see the details in the
    150 # -dA log.  To actually see it, add debug logging at the beginning and end of
    151 # Var_Parse.
    152 .if defined(V66) && ( ${iV2} < ${V42} )
    153 x=	Fail
    154 .else
    155 x=	Ok
    156 .endif
    157 # XXX: This condition doesn't match the one above. The quotes are missing
    158 # above.  This is a crucial detail since without quotes, the variable
    159 # expression ${iV2} evaluates to "${V66}", and with quotes, it evaluates to ""
    160 # since undefined variables are allowed and expand to an empty string.
    161 x!=	echo 'defined(V66) && ( "$${iV2}" < $${V42} ): $x' >&2; echo
    162 
    163 .if 1 || ${iV1} < ${V42}
    164 x=	Ok
    165 .else
    166 x=	Fail
    167 .endif
    168 x!=	echo '1 || $${iV1} < $${V42}: $x' >&2; echo
    169 
    170 # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    171 # warning: "String comparison operator should be either == or !=".
    172 # This was because the variable expression ${iV2} was defined, but the
    173 # contained variable V66 was undefined.  The left-hand side of the comparison
    174 # therefore evaluated to the string "${V66}", which is obviously not a number.
    175 #
    176 # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    177 # comparisons.  Instead, they are only parsed and then discarded.
    178 #
    179 # At that time, there was not enough debug logging to see the details in the
    180 # -dA log.  To actually see it, add debug logging at the beginning and end of
    181 # Var_Parse.
    182 .if 1 || ${iV2:U2} < ${V42}
    183 x=	Ok
    184 .else
    185 x=	Fail
    186 .endif
    187 x!=	echo '1 || $${iV2:U2} < $${V42}: $x' >&2; echo
    188 
    189 # the same expressions are fine when the lhs is expanded
    190 # ${iV1} expands to 42
    191 .if 0 || ${iV1} <= ${V42}
    192 x=	Ok
    193 .else
    194 x=	Fail
    195 .endif
    196 x!=	echo '0 || $${iV1} <= $${V42}: $x' >&2; echo
    197 
    198 # ${iV2:U2} expands to 2
    199 .if 0 || ${iV2:U2} < ${V42}
    200 x=	Ok
    201 .else
    202 x=	Fail
    203 .endif
    204 x!=	echo '0 || $${iV2:U2} < $${V42}: $x' >&2; echo
    205 
    206 # The right-hand side of the '&&' is irrelevant since the left-hand side
    207 # already evaluates to false.  Before cond.c 1.79 from 2020-07-09, it was
    208 # expanded nevertheless, although with a small modification:  undefined
    209 # variables may be used in these expressions without generating an error.
    210 .if defined(UNDEF) && ${UNDEF} != "undefined"
    211 .  error
    212 .endif
    213 
    214 all:
    215