make: print stack trace on fatal errors

The only fatal error that occurs while the makefiles are read in is the
one about recursive variables, which didn't give any hint about the
location before.

If a recursive variable is detected while evaluating the commands of a
target to be made, there is no location information, as before.
This commit is contained in:
rillig 2022-01-29 09:38:26 +00:00
parent 6f3942e76a
commit a270f08cb3
4 changed files with 20 additions and 9 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $ */
/* $NetBSD: main.c,v 1.577 2022/01/29 09:38:26 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -111,7 +111,7 @@
#include "trace.h"
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $");
MAKE_RCSID("$NetBSD: main.c,v 1.577 2022/01/29 09:38:26 rillig Exp $");
#if defined(MAKE_NATIVE) && !defined(lint)
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
"The Regents of the University of California. "
@ -1828,6 +1828,7 @@ Fatal(const char *fmt, ...)
va_end(ap);
(void)fprintf(stderr, "\n");
(void)fflush(stderr);
PrintStackTrace(true);
PrintOnError(NULL, "\n");

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.h,v 1.292 2022/01/29 01:07:31 rillig Exp $ */
/* $NetBSD: make.h,v 1.293 2022/01/29 09:38:26 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -832,6 +832,7 @@ bool GetBooleanExpr(const char *, bool);
void Parse_Init(void);
void Parse_End(void);
void PrintStackTrace(bool);
void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
bool Parse_VarAssign(const char *, bool, GNode *) MAKE_ATTR_USE;
void Parse_AddIncludeDir(const char *);

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.658 2022/01/29 01:07:31 rillig Exp $ */
/* $NetBSD: parse.c,v 1.659 2022/01/29 09:38:26 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -106,7 +106,7 @@
#include "pathnames.h"
/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: parse.c,v 1.658 2022/01/29 01:07:31 rillig Exp $");
MAKE_RCSID("$NetBSD: parse.c,v 1.659 2022/01/29 09:38:26 rillig Exp $");
/*
* A file being read.
@ -348,8 +348,14 @@ loadfile(const char *path, int fd)
return buf; /* may not be null-terminated */
}
static void
PrintStackTrace(void)
/*
* Print the current chain of .include and .for directives. In Parse_Fatal
* or other functions that already print the location, includingInnermost
* would be redundant, but in other cases like Error or Fatal it needs to be
* included.
*/
void
PrintStackTrace(bool includingInnermost)
{
const IncludedFile *entries;
size_t i, n;
@ -359,7 +365,7 @@ PrintStackTrace(void)
if (n == 0)
return;
if (entries[n - 1].forLoop == NULL)
if (!includingInnermost && entries[n - 1].forLoop == NULL)
n--; /* already in the diagnostic */
for (i = n; i-- > 0;) {
@ -484,7 +490,7 @@ ParseVErrorInternal(FILE *f, bool useVars, const char *fname, size_t lineno,
}
if (DEBUG(PARSE))
PrintStackTrace();
PrintStackTrace(false);
}
static void MAKE_ATTR_PRINTFLIKE(4, 5)

View File

@ -1,12 +1,15 @@
make: "var-recursive.mk" line 20: still there
Variable DIRECT is recursive.
in var-recursive.mk:21
make: stopped in unit-tests
Variable INDIRECT1 is recursive.
in var-recursive.mk:28
make: stopped in unit-tests
make: "var-recursive.mk" line 35: ok
Variable V is recursive.
in var-recursive.mk:43
make: stopped in unit-tests
exit status 0