Add check for extraneous .else's - based on patch from Arne H. Juul

in PR/24420.
Add a unit-test for conditionals.
Also in the unit-test makefile strip any .CURDIR in output.

PR: 24420
This commit is contained in:
sjg 2004-04-08 00:59:01 +00:00
parent dfa47cfea0
commit 8bef426d13
4 changed files with 58 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: cond.c,v 1.19 2004/01/06 01:18:52 sjg Exp $ */
/* $NetBSD: cond.c,v 1.20 2004/04/08 00:59:01 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -70,14 +70,14 @@
*/
#ifdef MAKE_BOOTSTRAP
static char rcsid[] = "$NetBSD: cond.c,v 1.19 2004/01/06 01:18:52 sjg Exp $";
static char rcsid[] = "$NetBSD: cond.c,v 1.20 2004/04/08 00:59:01 sjg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)cond.c 8.2 (Berkeley) 1/2/94";
#else
__RCSID("$NetBSD: cond.c,v 1.19 2004/01/06 01:18:52 sjg Exp $");
__RCSID("$NetBSD: cond.c,v 1.20 2004/04/08 00:59:01 sjg Exp $");
#endif
#endif /* not lint */
#endif
@ -173,6 +173,7 @@ static Token condPushBack=None; /* Single push-back token used in
#define MAXIF 30 /* greatest depth of #if'ing */
static Boolean finalElse[MAXIF+1]; /* Seen final else (stack) */
static Boolean condStack[MAXIF]; /* Stack of conditionals's values */
static int condTop = MAXIF; /* Top-most conditional */
static int skipIfLevel=0; /* Depth of skipped conditionals */
@ -1232,6 +1233,7 @@ Cond_Eval(char *line)
* so we return COND_PARSE, unless this endif isn't paired with
* a decent if.
*/
finalElse[condTop] = FALSE;
if (skipIfLevel != 0) {
skipIfLevel -= 1;
return (COND_SKIP);
@ -1266,6 +1268,11 @@ Cond_Eval(char *line)
* of the previous if we parsed.
*/
if (isElse && (line[0] == 's') && (line[1] == 'e')) {
if (finalElse[condTop]) {
Parse_Error (PARSE_WARNING, "extra else");
} else {
finalElse[condTop] = TRUE;
}
if (condTop == MAXIF) {
Parse_Error (level, "if-less else");
return (COND_INVALID);
@ -1315,6 +1322,7 @@ Cond_Eval(char *line)
}
if (!isElse) {
condTop -= 1;
finalElse[condTop] = FALSE;
} else if ((skipIfLevel != 0) || condStack[condTop]) {
/*
* If this is an else-type conditional, it should only take effect

View File

@ -1,4 +1,4 @@
# $Id: Makefile,v 1.11 2004/02/20 09:03:26 sjg Exp $
# $Id: Makefile,v 1.12 2004/04/08 00:59:01 sjg Exp $
#
# Unit tests for make(1)
# The main targets are:
@ -19,6 +19,7 @@ UNIT_TESTS:= ${.PARSEDIR}
# Simple sub-makefiles - we run them as a black box
# keep the list sorted.
SUBFILES= \
cond1 \
modmatch \
modts \
modword \
@ -40,12 +41,13 @@ clean:
TEST_MAKE?= ${MAKE}
# The driver.
# We always pretend .MAKE was called 'make' so the results
# can be compared.
# We always pretend .MAKE was called 'make'
# and strip ${.CURDIR}/ from the output
# so the results can be compared.
test:
@echo "${TEST_MAKE} -f ${MAKEFILE} > ${.TARGET}.out 2>&1"
@cd ${.OBJDIR} && ${TEST_MAKE} -f ${MAKEFILE} 2>&1 | \
sed 's,^${TEST_MAKE:T}:,make:,' > ${.TARGET}.out || { \
sed -e 's,^${TEST_MAKE:T}:,make:,' -e 's,${.CURDIR}/,,g' > ${.TARGET}.out || { \
tail ${.TARGET}.out; mv ${.TARGET}.out ${.TARGET}.fail; exit 1; }
diff -u ${UNIT_TESTS}/${.TARGET}.exp ${.TARGET}.out

View File

@ -0,0 +1,37 @@
# $Id: cond1,v 1.1 2004/04/08 00:59:01 sjg Exp $
PRIMES=2 3 5 7 11
NUMBERS=1 2 3 4 5
n=2
.if ${PRIMES:M$n} == ""
X=not
.else
X=
.endif
# We expect an extra else warning
.if ${MACHINE_ARCH} == no-such
A=one
.else
A=other
.else
A=this should be an error
.endif
# We expect an extra else warning
.if $X != ""
.if ${MACHINE_ARCH} == no-such
B=one
.else
B=other
.else
B=this should be an error
.endif
.else
B=unknown
.endif
all:
@echo "$n is $X prime"
@echo "A='$A' B='$B'"

View File

@ -1,3 +1,7 @@
make: "cond1" line 18: warning: extra else
make: "cond1" line 28: warning: extra else
2 is prime
A='other' B='unknown'
LIB=a X_LIBS:M${LIB${LIB:tu}} is "/tmp/liba.a"
LIB=a X_LIBS:M*/lib${LIB}.a is "/tmp/liba.a"
LIB=a X_LIBS:M*/lib${LIB}.a:tu is "/TMP/LIBA.A"