Home | History | Annotate | Line # | Download | only in unit-tests
cond-undef-lint.mk revision 1.7
      1  1.7  rillig # $NetBSD: cond-undef-lint.mk,v 1.7 2025/01/11 20:54:45 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.1  rillig # The outer expression does not generate an error message since there was
     35  1.1  rillig # already an error evaluating this variable's name.
     36  1.1  rillig #
     37  1.1  rillig # TODO: Suppress the error message "Variable VAR. is undefined".  That part
     38  1.1  rillig # of the expression must not be evaluated at all.
     39  1.7  rillig # expect+2: Variable "UNDEF" is undefined
     40  1.7  rillig # expect+1: Variable "VAR." is undefined
     41  1.1  rillig .if ${VAR.${UNDEF}}
     42  1.1  rillig .  error
     43  1.1  rillig .else
     44  1.1  rillig .  error
     45  1.1  rillig .endif
     46  1.1  rillig 
     47  1.1  rillig # The variable VAR.defined is not defined and thus generates an error message.
     48  1.3  rillig #
     49  1.3  rillig # TODO: This pattern looks a lot like CFLAGS.${OPSYS}, which is at least
     50  1.3  rillig # debatable.  Or would any practical use of CFLAGS.${OPSYS} be via an indirect
     51  1.3  rillig # expression, as in the next example?
     52  1.7  rillig # expect+1: Variable "VAR.defined" is undefined
     53  1.1  rillig .if ${VAR.${DEF}}
     54  1.1  rillig .  error
     55  1.1  rillig .else
     56  1.1  rillig .  error
     57  1.1  rillig .endif
     58  1.1  rillig 
     59  1.1  rillig 
     60  1.1  rillig # Variables that are referenced indirectly may be undefined in a condition.
     61  1.1  rillig #
     62  1.1  rillig # A practical example for this is CFLAGS, which consists of CWARNS, COPTS
     63  1.1  rillig # and a few others.  Just because these nested variables are not defined,
     64  1.1  rillig # this does not make the condition invalid.
     65  1.1  rillig #
     66  1.1  rillig # The crucial point is that at the point where the variable appears in the
     67  1.1  rillig # condition, there is no way to influence the definedness of the nested
     68  1.1  rillig # variables.  In particular, there is no modifier that would turn undefined
     69  1.1  rillig # nested variables into empty strings, as an equivalent to the :U modifier.
     70  1.1  rillig INDIRECT=	${NESTED_UNDEF} ${NESTED_DEF}
     71  1.1  rillig NESTED_DEF=	nested-defined
     72  1.1  rillig 
     73  1.1  rillig # Since NESTED_UNDEF is not controllable at this point, it must not generate
     74  1.2  rillig # an error message, and it doesn't do so, since 2020-09-14.
     75  1.1  rillig .if !${INDIRECT}
     76  1.1  rillig .  error
     77  1.1  rillig .endif
     78