Home | History | Annotate | Line # | Download | only in unit-tests
      1  1.8  rillig # $NetBSD: cond-undef-lint.mk,v 1.8 2025/01/11 21:21:33 rillig Exp $
      2  1.1  rillig #
      3  1.1  rillig # Tests for defined and undefined variables in .if conditions, in lint mode.
      4  1.1  rillig #
      5  1.1  rillig # As of 2020-09-14, lint mode contains experimental code for printing
      6  1.1  rillig # accurate error messages in case of undefined variables, instead of the
      7  1.1  rillig # wrong "Malformed condition".
      8  1.2  rillig #
      9  1.2  rillig # See also:
     10  1.2  rillig #	opt-debug-lint.mk
     11  1.1  rillig 
     12  1.1  rillig .MAKEFLAGS: -dL
     13  1.1  rillig 
     14  1.1  rillig # DEF is defined, UNDEF is not.
     15  1.1  rillig DEF=		defined
     16  1.1  rillig 
     17  1.1  rillig # An expression based on a defined variable is fine.
     18  1.1  rillig .if !${DEF}
     19  1.1  rillig .  error
     20  1.1  rillig .endif
     21  1.1  rillig 
     22  1.1  rillig # Since the condition fails to evaluate, neither of the branches is taken.
     23  1.7  rillig # expect+1: Variable "UNDEF" is undefined
     24  1.1  rillig .if ${UNDEF}
     25  1.1  rillig .  error
     26  1.1  rillig .else
     27  1.1  rillig .  error
     28  1.1  rillig .endif
     29  1.1  rillig 
     30  1.1  rillig # The variable name depends on the undefined variable, which is probably a
     31  1.1  rillig # mistake.  The variable UNDEF, as used here, can be easily turned into
     32  1.1  rillig # an expression that is always defined, using the :U modifier.
     33  1.1  rillig #
     34  1.7  rillig # expect+1: Variable "VAR." is undefined
     35  1.1  rillig .if ${VAR.${UNDEF}}
     36  1.1  rillig .  error
     37  1.1  rillig .else
     38  1.1  rillig .  error
     39  1.1  rillig .endif
     40  1.1  rillig 
     41  1.8  rillig # The inner variable DEF is defined, but the resulting name VAR.defined
     42  1.8  rillig # refers to an undefined variable, thus an error message.
     43  1.3  rillig #
     44  1.7  rillig # expect+1: Variable "VAR.defined" is undefined
     45  1.1  rillig .if ${VAR.${DEF}}
     46  1.1  rillig .  error
     47  1.1  rillig .else
     48  1.1  rillig .  error
     49  1.1  rillig .endif
     50  1.1  rillig 
     51  1.1  rillig 
     52  1.1  rillig # Variables that are referenced indirectly may be undefined in a condition.
     53  1.1  rillig #
     54  1.1  rillig # A practical example for this is CFLAGS, which consists of CWARNS, COPTS
     55  1.1  rillig # and a few others.  Just because these nested variables are not defined,
     56  1.1  rillig # this does not make the condition invalid.
     57  1.1  rillig #
     58  1.1  rillig # The crucial point is that at the point where the variable appears in the
     59  1.1  rillig # condition, there is no way to influence the definedness of the nested
     60  1.1  rillig # variables.  In particular, there is no modifier that would turn undefined
     61  1.1  rillig # nested variables into empty strings, as an equivalent to the :U modifier.
     62  1.1  rillig INDIRECT=	${NESTED_UNDEF} ${NESTED_DEF}
     63  1.1  rillig NESTED_DEF=	nested-defined
     64  1.1  rillig 
     65  1.1  rillig # Since NESTED_UNDEF is not controllable at this point, it must not generate
     66  1.2  rillig # an error message, and it doesn't do so, since 2020-09-14.
     67  1.1  rillig .if !${INDIRECT}
     68  1.1  rillig .  error
     69  1.1  rillig .endif
     70