Home | History | Annotate | Line # | Download | only in unit-tests
cond-short.mk revision 1.19
      1  1.19  rillig # $NetBSD: cond-short.mk,v 1.19 2021/12/27 18:54:19 rillig Exp $
      2   1.1  rillig #
      3   1.1  rillig # Demonstrates that in conditions, the right-hand side of an && or ||
      4   1.2  rillig # is only evaluated if it can actually influence the result.
      5  1.12  rillig # This is called 'short-circuit evaluation' and is the usual evaluation
      6  1.12  rillig # mode in most programming languages.  A notable exception is Ada, which
      7  1.12  rillig # distinguishes between the operators 'And', 'And Then', 'Or', 'Or Else'.
      8   1.1  rillig #
      9  1.13  rillig # Before 2020-06-28, the right-hand side of an && or || operator was always
     10  1.13  rillig # evaluated, which was wrong.  In cond.c 1.69 and var.c 1.197 on 2015-10-11,
     11  1.13  rillig # Var_Parse got a new parameter named 'wantit'.  Since then it would have been
     12  1.13  rillig # possible to skip evaluation of irrelevant variable expressions and only
     13  1.13  rillig # parse them.  They were still evaluated though, the only difference to
     14  1.13  rillig # relevant variable expressions was that in the irrelevant variable
     15  1.17  rillig # expressions, undefined variables were allowed.  This allowed for conditions
     16  1.17  rillig # like 'defined(VAR) && ${VAR:S,from,to,} != ""', which no longer produced an
     17  1.19  rillig # error message 'Malformed conditional', but the irrelevant expression was
     18  1.19  rillig # still evaluated.
     19  1.17  rillig #
     20  1.17  rillig # Since the initial commit on 1993-03-21, the manual page has been saying that
     21  1.17  rillig # make 'will only evaluate a conditional as far as is necessary to determine',
     22  1.17  rillig # but that was wrong.  The code in cond.c 1.1 from 1993-03-21 looks good since
     23  1.17  rillig # it calls Var_Parse(condExpr, VAR_CMD, doEval,&varSpecLen,&doFree), but the
     24  1.19  rillig # definition of Var_Parse did not call the third parameter 'doEval', as would
     25  1.17  rillig # be expected, but instead 'err', accompanied by the comment 'TRUE if
     26  1.17  rillig # undefined variables are an error'.  This subtle difference between 'do not
     27  1.17  rillig # evaluate at all' and 'allow undefined variables' led to the unexpected
     28  1.17  rillig # evaluation.
     29  1.16  rillig #
     30  1.16  rillig # See also:
     31  1.16  rillig #	var-eval-short.mk, for short-circuited variable modifiers
     32   1.2  rillig 
     33  1.16  rillig # The && operator:
     34   1.1  rillig 
     35   1.1  rillig .if 0 && ${echo "unexpected and" 1>&2 :L:sh}
     36   1.1  rillig .endif
     37   1.1  rillig 
     38   1.1  rillig .if 1 && ${echo "expected and" 1>&2 :L:sh}
     39   1.1  rillig .endif
     40   1.1  rillig 
     41   1.2  rillig .if 0 && exists(nonexistent${echo "unexpected and exists" 1>&2 :L:sh})
     42   1.2  rillig .endif
     43   1.2  rillig 
     44   1.2  rillig .if 1 && exists(nonexistent${echo "expected and exists" 1>&2 :L:sh})
     45   1.2  rillig .endif
     46   1.2  rillig 
     47   1.2  rillig .if 0 && empty(${echo "unexpected and empty" 1>&2 :L:sh})
     48   1.2  rillig .endif
     49   1.2  rillig 
     50   1.2  rillig .if 1 && empty(${echo "expected and empty" 1>&2 :L:sh})
     51   1.2  rillig .endif
     52   1.2  rillig 
     53   1.4  rillig # "VAR U11" is not evaluated; it was evaluated before 2020-07-02.
     54   1.4  rillig # The whole !empty condition is only parsed and then discarded.
     55   1.3  rillig VAR=	${VAR${:U11${echo "unexpected VAR U11" 1>&2 :L:sh}}}
     56   1.3  rillig VAR13=	${VAR${:U12${echo "unexpected VAR13" 1>&2 :L:sh}}}
     57   1.3  rillig .if 0 && !empty(VAR${:U13${echo "unexpected U13 condition" 1>&2 :L:sh}})
     58   1.3  rillig .endif
     59   1.3  rillig 
     60   1.3  rillig VAR=	${VAR${:U21${echo "unexpected VAR U21" 1>&2 :L:sh}}}
     61   1.3  rillig VAR23=	${VAR${:U22${echo   "expected VAR23" 1>&2 :L:sh}}}
     62   1.3  rillig .if 1 && !empty(VAR${:U23${echo   "expected U23 condition" 1>&2 :L:sh}})
     63   1.3  rillig .endif
     64   1.5  rillig VAR=	# empty again, for the following tests
     65   1.3  rillig 
     66   1.5  rillig # The :M modifier is only parsed, not evaluated.
     67   1.5  rillig # Before 2020-07-02, it was wrongly evaluated.
     68   1.4  rillig .if 0 && !empty(VAR:M${:U${echo "unexpected M pattern" 1>&2 :L:sh}})
     69   1.4  rillig .endif
     70   1.4  rillig 
     71   1.5  rillig .if 1 && !empty(VAR:M${:U${echo   "expected M pattern" 1>&2 :L:sh}})
     72   1.5  rillig .endif
     73   1.5  rillig 
     74   1.6  rillig .if 0 && !empty(VAR:S,from,${:U${echo "unexpected S modifier" 1>&2 :L:sh}},)
     75   1.6  rillig .endif
     76   1.6  rillig 
     77   1.6  rillig .if 0 && !empty(VAR:C,from,${:U${echo "unexpected C modifier" 1>&2 :L:sh}},)
     78   1.6  rillig .endif
     79   1.6  rillig 
     80   1.6  rillig .if 0 && !empty("" == "" :? ${:U${echo "unexpected ? modifier" 1>&2 :L:sh}} :)
     81   1.6  rillig .endif
     82   1.6  rillig 
     83   1.6  rillig .if 0 && !empty(VAR:old=${:U${echo "unexpected = modifier" 1>&2 :L:sh}})
     84   1.6  rillig .endif
     85   1.6  rillig 
     86   1.6  rillig .if 0 && !empty(1 2 3:L:@var@${:U${echo "unexpected @ modifier" 1>&2 :L:sh}}@)
     87   1.6  rillig .endif
     88   1.6  rillig 
     89   1.6  rillig .if 0 && !empty(:U${:!echo "unexpected exclam modifier" 1>&2 !})
     90   1.6  rillig .endif
     91   1.6  rillig 
     92   1.8  rillig # Irrelevant assignment modifiers are skipped as well.
     93   1.8  rillig .if 0 && ${1 2 3:L:@i@${FIRST::?=$i}@}
     94   1.8  rillig .endif
     95   1.8  rillig .if 0 && ${1 2 3:L:@i@${LAST::=$i}@}
     96   1.8  rillig .endif
     97   1.8  rillig .if 0 && ${1 2 3:L:@i@${APPENDED::+=$i}@}
     98   1.8  rillig .endif
     99   1.8  rillig .if 0 && ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@}
    100   1.8  rillig .endif
    101   1.8  rillig .if defined(FIRST) || defined(LAST) || defined(APPENDED) || defined(RAN)
    102  1.10  rillig .  warning first=${FIRST} last=${LAST} appended=${APPENDED} ran=${RAN}
    103   1.8  rillig .endif
    104   1.8  rillig 
    105  1.16  rillig # The || operator:
    106   1.2  rillig 
    107   1.1  rillig .if 1 || ${echo "unexpected or" 1>&2 :L:sh}
    108   1.1  rillig .endif
    109   1.1  rillig 
    110   1.1  rillig .if 0 || ${echo "expected or" 1>&2 :L:sh}
    111   1.1  rillig .endif
    112   1.1  rillig 
    113   1.2  rillig .if 1 || exists(nonexistent${echo "unexpected or exists" 1>&2 :L:sh})
    114   1.2  rillig .endif
    115   1.2  rillig 
    116   1.2  rillig .if 0 || exists(nonexistent${echo "expected or exists" 1>&2 :L:sh})
    117   1.2  rillig .endif
    118   1.2  rillig 
    119   1.2  rillig .if 1 || empty(${echo "unexpected or empty" 1>&2 :L:sh})
    120   1.2  rillig .endif
    121   1.2  rillig 
    122   1.2  rillig .if 0 || empty(${echo "expected or empty" 1>&2 :L:sh})
    123   1.2  rillig .endif
    124   1.2  rillig 
    125  1.19  rillig # Unreachable nested conditions are skipped completely as well.  These skipped
    126  1.19  rillig # lines may even contain syntax errors.  This allows to skip syntactically
    127  1.19  rillig # incompatible new features in older versions of make.
    128   1.1  rillig 
    129   1.1  rillig .if 0
    130   1.1  rillig .  if ${echo "unexpected nested and" 1>&2 :L:sh}
    131   1.1  rillig .  endif
    132   1.1  rillig .endif
    133   1.1  rillig 
    134   1.1  rillig .if 1
    135   1.1  rillig .elif ${echo "unexpected nested or" 1>&2 :L:sh}
    136   1.1  rillig .endif
    137   1.1  rillig 
    138   1.7     sjg # make sure these do not cause complaint
    139   1.7     sjg #.MAKEFLAGS: -dc
    140   1.7     sjg 
    141  1.12  rillig # TODO: Rewrite this whole section and check all the conditions and variables.
    142  1.12  rillig # Several of the assumptions are probably wrong here.
    143  1.12  rillig # TODO: replace 'x=' with '.info' or '.error'.
    144  1.11  rillig V42=	42
    145  1.11  rillig iV1=	${V42}
    146  1.11  rillig iV2=	${V66}
    147   1.7     sjg 
    148   1.7     sjg .if defined(V42) && ${V42} > 0
    149  1.11  rillig x=	Ok
    150   1.7     sjg .else
    151  1.11  rillig x=	Fail
    152   1.7     sjg .endif
    153  1.14  rillig x!=	echo 'defined(V42) && $${V42} > 0: $x' >&2; echo
    154   1.9  rillig 
    155  1.13  rillig # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    156  1.13  rillig # warning: "String comparison operator should be either == or !=".
    157  1.13  rillig # This was because the variable expression ${iV2} was defined, but the
    158  1.13  rillig # contained variable V66 was undefined.  The left-hand side of the comparison
    159  1.13  rillig # therefore evaluated to the string "${V66}", which is obviously not a number.
    160  1.13  rillig #
    161  1.13  rillig # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    162  1.13  rillig # comparisons.  Instead, they are only parsed and then discarded.
    163  1.13  rillig #
    164  1.13  rillig # At that time, there was not enough debug logging to see the details in the
    165  1.13  rillig # -dA log.  To actually see it, add debug logging at the beginning and end of
    166  1.13  rillig # Var_Parse.
    167   1.7     sjg .if defined(V66) && ( ${iV2} < ${V42} )
    168  1.11  rillig x=	Fail
    169   1.7     sjg .else
    170  1.11  rillig x=	Ok
    171   1.7     sjg .endif
    172  1.13  rillig # XXX: This condition doesn't match the one above. The quotes are missing
    173  1.13  rillig # above.  This is a crucial detail since without quotes, the variable
    174  1.13  rillig # expression ${iV2} evaluates to "${V66}", and with quotes, it evaluates to ""
    175  1.13  rillig # since undefined variables are allowed and expand to an empty string.
    176  1.14  rillig x!=	echo 'defined(V66) && ( "$${iV2}" < $${V42} ): $x' >&2; echo
    177   1.9  rillig 
    178   1.7     sjg .if 1 || ${iV1} < ${V42}
    179  1.11  rillig x=	Ok
    180   1.7     sjg .else
    181  1.11  rillig x=	Fail
    182   1.7     sjg .endif
    183  1.14  rillig x!=	echo '1 || $${iV1} < $${V42}: $x' >&2; echo
    184   1.9  rillig 
    185  1.13  rillig # With cond.c 1.76 from 2020-07-03, the following condition triggered a
    186  1.13  rillig # warning: "String comparison operator should be either == or !=".
    187  1.13  rillig # This was because the variable expression ${iV2} was defined, but the
    188  1.13  rillig # contained variable V66 was undefined.  The left-hand side of the comparison
    189  1.13  rillig # therefore evaluated to the string "${V66}", which is obviously not a number.
    190  1.13  rillig #
    191  1.13  rillig # This was fixed in cond.c 1.79 from 2020-07-09 by not evaluating irrelevant
    192  1.13  rillig # comparisons.  Instead, they are only parsed and then discarded.
    193  1.13  rillig #
    194  1.13  rillig # At that time, there was not enough debug logging to see the details in the
    195  1.13  rillig # -dA log.  To actually see it, add debug logging at the beginning and end of
    196  1.13  rillig # Var_Parse.
    197   1.7     sjg .if 1 || ${iV2:U2} < ${V42}
    198  1.11  rillig x=	Ok
    199   1.7     sjg .else
    200  1.11  rillig x=	Fail
    201   1.7     sjg .endif
    202  1.14  rillig x!=	echo '1 || $${iV2:U2} < $${V42}: $x' >&2; echo
    203   1.9  rillig 
    204   1.7     sjg # the same expressions are fine when the lhs is expanded
    205   1.7     sjg # ${iV1} expands to 42
    206   1.7     sjg .if 0 || ${iV1} <= ${V42}
    207  1.11  rillig x=	Ok
    208   1.7     sjg .else
    209  1.11  rillig x=	Fail
    210   1.7     sjg .endif
    211  1.14  rillig x!=	echo '0 || $${iV1} <= $${V42}: $x' >&2; echo
    212   1.9  rillig 
    213   1.7     sjg # ${iV2:U2} expands to 2
    214   1.7     sjg .if 0 || ${iV2:U2} < ${V42}
    215  1.11  rillig x=	Ok
    216   1.7     sjg .else
    217  1.11  rillig x=	Fail
    218   1.7     sjg .endif
    219  1.14  rillig x!=	echo '0 || $${iV2:U2} < $${V42}: $x' >&2; echo
    220   1.7     sjg 
    221  1.15  rillig # The right-hand side of the '&&' is irrelevant since the left-hand side
    222  1.15  rillig # already evaluates to false.  Before cond.c 1.79 from 2020-07-09, it was
    223  1.15  rillig # expanded nevertheless, although with a small modification:  undefined
    224  1.15  rillig # variables may be used in these expressions without generating an error.
    225  1.12  rillig .if defined(UNDEF) && ${UNDEF} != "undefined"
    226  1.12  rillig .  error
    227  1.12  rillig .endif
    228  1.12  rillig 
    229  1.18  rillig 
    230  1.18  rillig # Ensure that irrelevant conditions do not influence the result of the whole
    231  1.18  rillig # condition.  As of cond.c 1.302 from 2021-12-11, an irrelevant function call
    232  1.18  rillig # evaluates to true (see CondParser_FuncCall and CondParser_FuncCallEmpty), an
    233  1.18  rillig # irrelevant comparison evaluates to false (see CondParser_Comparison).
    234  1.18  rillig #
    235  1.18  rillig # An irrelevant true bubbles up to the outermost CondParser_And, where it is
    236  1.18  rillig # ignored.  An irrelevant false bubbles up to the outermost CondParser_Or,
    237  1.18  rillig # where it is ignored.
    238  1.18  rillig #
    239  1.18  rillig # If the condition parser should ever be restructured, the bubbling up of the
    240  1.18  rillig # irrelevant evaluation results might show up accidentally.  Prevent this.
    241  1.18  rillig DEF=	defined
    242  1.18  rillig .undef UNDEF
    243  1.18  rillig 
    244  1.18  rillig .if 0 && defined(DEF)
    245  1.18  rillig .  error
    246  1.18  rillig .endif
    247  1.18  rillig 
    248  1.18  rillig .if 1 && defined(DEF)
    249  1.18  rillig .else
    250  1.18  rillig .  error
    251  1.18  rillig .endif
    252  1.18  rillig 
    253  1.18  rillig .if 0 && defined(UNDEF)
    254  1.18  rillig .  error
    255  1.18  rillig .endif
    256  1.18  rillig 
    257  1.18  rillig .if 1 && defined(UNDEF)
    258  1.18  rillig .  error
    259  1.18  rillig .endif
    260  1.18  rillig 
    261  1.18  rillig .if 0 || defined(DEF)
    262  1.18  rillig .else
    263  1.18  rillig .  error
    264  1.18  rillig .endif
    265  1.18  rillig 
    266  1.18  rillig .if 1 || defined(DEF)
    267  1.18  rillig .else
    268  1.18  rillig .  error
    269  1.18  rillig .endif
    270  1.18  rillig 
    271  1.18  rillig .if 0 || defined(UNDEF)
    272  1.18  rillig .  error
    273  1.18  rillig .endif
    274  1.18  rillig 
    275  1.18  rillig .if 1 || defined(UNDEF)
    276  1.18  rillig .else
    277  1.18  rillig .  error
    278  1.18  rillig .endif
    279  1.18  rillig 
    280  1.18  rillig 
    281   1.1  rillig all:
    282