Revised patch for catching extraneous .else's

We actually need to tack condTop and skipIfLevel.
Bump MAXIF while here.
Enhance the unit-test for conditionals to exercise more code.

PR: 24420
This commit is contained in:
sjg 2004-04-08 07:24:26 +00:00
parent 67f14f8fff
commit df910819de
3 changed files with 62 additions and 12 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: cond.c,v 1.21 2004/04/08 01:35:33 sjg Exp $ */
/* $NetBSD: cond.c,v 1.22 2004/04/08 07:24:26 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.21 2004/04/08 01:35:33 sjg Exp $";
static char rcsid[] = "$NetBSD: cond.c,v 1.22 2004/04/08 07:24:26 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.21 2004/04/08 01:35:33 sjg Exp $");
__RCSID("$NetBSD: cond.c,v 1.22 2004/04/08 07:24:26 sjg Exp $");
#endif
#endif /* not lint */
#endif
@ -171,8 +171,9 @@ static char *condExpr; /* The expression to parse */
static Token condPushBack=None; /* Single push-back token used in
* parsing */
#define MAXIF 30 /* greatest depth of #if'ing */
#define MAXIF 64 /* greatest depth of #if'ing */
static Boolean finalElse[MAXIF+1][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][skipIfLevel] = 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][skipIfLevel]) {
Parse_Error (PARSE_WARNING, "extra else");
} else {
finalElse[condTop][skipIfLevel] = TRUE;
}
if (condTop == MAXIF) {
Parse_Error (level, "if-less else");
return (COND_INVALID);
@ -1300,6 +1307,11 @@ Cond_Eval(char *line)
* we're skipping things...
*/
skipIfLevel += 1;
if (skipIfLevel >= MAXIF) {
Parse_Error (PARSE_FATAL, "Too many nested if's. %d max.", MAXIF);
return (COND_INVALID);
}
finalElse[condTop][skipIfLevel] = FALSE;
return(COND_SKIP);
}
@ -1315,6 +1327,7 @@ Cond_Eval(char *line)
}
if (!isElse) {
condTop -= 1;
finalElse[condTop][skipIfLevel] = FALSE;
} else if ((skipIfLevel != 0) || condStack[condTop]) {
/*
* If this is an else-type conditional, it should only take effect

View File

@ -1,4 +1,34 @@
# $Id: cond1,v 1.1 2004/04/08 00:59:01 sjg Exp $
# $Id: cond1,v 1.2 2004/04/08 07:24:26 sjg Exp $
# from bsd.obj.mk
MKOBJ?=no
.if ${MKOBJ} == "no"
o= no
.else
.if defined(notMAKEOBJDIRPREFIX) || defined(norMAKEOBJDIR)
.if defined(notMAKEOBJDIRPREFIX)
o=${MAKEOBJDIRPREFIX}${__curdir}
.else
o= ${MAKEOBJDIR}
.endif
.endif
o= o
.endif
# repeat the above to check we get the same result
.if ${MKOBJ} == "no"
o2= no
.else
.if defined(notMAKEOBJDIRPREFIX) || defined(norMAKEOBJDIR)
.if defined(notMAKEOBJDIRPREFIX)
o2=${MAKEOBJDIRPREFIX}${__curdir}
.else
o2= ${MAKEOBJDIR}
.endif
.endif
o2= o
.endif
PRIMES=2 3 5 7 11
NUMBERS=1 2 3 4 5
@ -10,21 +40,28 @@ X=not
X=
.endif
# We expect an extra else warning
.if ${MACHINE_ARCH} == no-such
A=one
.else
.if ${MACHINE_ARCH} == not-this
.if ${MACHINE_ARCH} == something-else
A=unlikely
.else
A=no
.endif
.endif
A=other
# We expect an extra else warning - we're not skipping here
.else
A=this should be an error
.endif
# We expect an extra else warning
.if $X != ""
.if ${MACHINE_ARCH} == no-such
.if $X == not
B=one
.else
B=other
# We expect an extra else warning - we are skipping here
.else
B=this should be an error
.endif
@ -34,4 +71,4 @@ B=unknown
all:
@echo "$n is $X prime"
@echo "A='$A' B='$B'"
@echo "A='$A' B='$B' o='$o,${o2}'"

View File

@ -1,7 +1,7 @@
make: "cond1" line 18: warning: extra else
make: "cond1" line 28: warning: extra else
make: "cond1" line 55: warning: extra else
make: "cond1" line 65: warning: extra else
2 is prime
A='other' B='unknown'
A='other' B='unknown' o='no,no'
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"