Home | History | Annotate | Line # | Download | only in unit-tests
cond-undef-lint.mk revision 1.1
      1  1.1  rillig # $NetBSD: cond-undef-lint.mk,v 1.1 2020/09/14 06:44:50 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.1  rillig 
      9  1.1  rillig .MAKEFLAGS: -dL
     10  1.1  rillig 
     11  1.1  rillig # DEF is defined, UNDEF is not.
     12  1.1  rillig DEF=		defined
     13  1.1  rillig 
     14  1.1  rillig # An expression based on a defined variable is fine.
     15  1.1  rillig .if !${DEF}
     16  1.1  rillig .  error
     17  1.1  rillig .endif
     18  1.1  rillig 
     19  1.1  rillig # Since the condition fails to evaluate, neither of the branches is taken.
     20  1.1  rillig .if ${UNDEF}
     21  1.1  rillig .  error
     22  1.1  rillig .else
     23  1.1  rillig .  error
     24  1.1  rillig .endif
     25  1.1  rillig 
     26  1.1  rillig # The variable name depends on the undefined variable, which is probably a
     27  1.1  rillig # mistake.  The variable UNDEF, as used here, can be easily turned into
     28  1.1  rillig # an expression that is always defined, using the :U modifier.
     29  1.1  rillig #
     30  1.1  rillig # The outer expression does not generate an error message since there was
     31  1.1  rillig # already an error evaluating this variable's name.
     32  1.1  rillig #
     33  1.1  rillig # TODO: Suppress the error message "Variable VAR. is undefined".  That part
     34  1.1  rillig # of the expression must not be evaluated at all.
     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.1  rillig # The variable VAR.defined is not defined and thus generates an error message.
     42  1.1  rillig .if ${VAR.${DEF}}
     43  1.1  rillig .  error
     44  1.1  rillig .else
     45  1.1  rillig .  error
     46  1.1  rillig .endif
     47  1.1  rillig 
     48  1.1  rillig 
     49  1.1  rillig # Variables that are referenced indirectly may be undefined in a condition.
     50  1.1  rillig #
     51  1.1  rillig # A practical example for this is CFLAGS, which consists of CWARNS, COPTS
     52  1.1  rillig # and a few others.  Just because these nested variables are not defined,
     53  1.1  rillig # this does not make the condition invalid.
     54  1.1  rillig #
     55  1.1  rillig # The crucial point is that at the point where the variable appears in the
     56  1.1  rillig # condition, there is no way to influence the definedness of the nested
     57  1.1  rillig # variables.  In particular, there is no modifier that would turn undefined
     58  1.1  rillig # nested variables into empty strings, as an equivalent to the :U modifier.
     59  1.1  rillig INDIRECT=	${NESTED_UNDEF} ${NESTED_DEF}
     60  1.1  rillig NESTED_DEF=	nested-defined
     61  1.1  rillig 
     62  1.1  rillig # Since NESTED_UNDEF is not controllable at this point, it must not generate
     63  1.1  rillig # an error message.  This condition should generate no error message at all.
     64  1.1  rillig #
     65  1.1  rillig # TODO: Suppress the error message.
     66  1.1  rillig .if !${INDIRECT}
     67  1.1  rillig .  error
     68  1.1  rillig .endif
     69