ack/util/int/warn.c
George Koehler 36f16b0cb8 Cut down some clang warnings
Edit C code to reduce warnings from clang.  Most warnings are for
implicit declarations of functions, but some warnings want me to add
parentheses or curly braces, or to cast arguments for printf().

Make a few other changes, like declaring float_cst() in h/con_float to
be static, and using C99 bool in ego/ra/makeitems.c and
ego/share/makecldef.c.  Such changes don't silence warnings; I make
such changes while I silence warnings in the same file.  In
float_cst(), rename parameter `str` to `float_str`, so it doesn't
share a name with the global variable `str`.

Remove `const` from `newmodule(const char *)` in mach/proto/as to
silence a warning.  I wrongly added the `const` in d347207.

For warnings about implicit declarations of functions, the fix is to
declare the function before calling it.  For example, my OpenBSD
system needs <sys/wait.h> to declare wait().

In util/int, add "whatever.h" to declare more functions.  Remove old
declarations from "mem.h", to prefer the newer declarations of the
same functions in "data.h" and "stack.h".
2019-10-30 18:36:38 -04:00

153 lines
2.9 KiB
C

/** @file
* Warning management.
*/
/* $Id$ */
#include <stdio.h>
#include "logging.h"
#include "global.h"
#include "log.h"
#include "alloc.h"
#include "warn.h"
#include "linfil.h"
extern FILE *mess_fp; /* from io.c */
extern char *trap2text(); /* from trap.c */
/******** The warnings ********/
struct warn_msg {
char *wm_text;
int wm_nr;
};
#define WMASK 0x5555 /* powers of 4 */
PRIVATE struct warn_msg warn_msg[] = {
#include "warn_msg" /* generated from $(EM)/doc/int */
{0, 0} /* sentinel */
};
PRIVATE char *warn_text[WMSG+1];
void init_wmsg(void)
{
register int i;
register struct warn_msg *wmsg;
for (i = 0; i <= WMSG; i++) {
warn_text[i] = "*** Unknown warning (internal error) ***";
}
for (wmsg = &warn_msg[0]; wmsg->wm_nr; wmsg++) {
warn_text[wmsg->wm_nr] = wmsg->wm_text;
}
}
/******** The warning counters ********/
struct warn_cnt {
struct warn_cnt *next;
ptr wc_fil; /* file name pointer */
long wc_lin; /* line number */
long wc_cnt; /* the counter */
};
PRIVATE struct warn_cnt *warn_cnt[WMSG];
PRIVATE char warnmask[WMSG];
PRIVATE long count_wrn(int nr)
{ /* returns the occurrence counter for the warning with number
nr; keeps track of the warnings, sorted by warning number,
file name and line number.
*/
register struct warn_cnt **warn_hook = &warn_cnt[nr];
register struct warn_cnt *wrn;
while ( (wrn = *warn_hook) ) {
if (wrn->wc_fil == FIL && wrn->wc_lin == LIN) {
return ++wrn->wc_cnt;
}
warn_hook = &wrn->next;
}
wrn = (struct warn_cnt *)
Malloc((size) sizeof (struct warn_cnt), (char *)0);
if (!wrn) {
/* no problem */
return 1;
}
*warn_hook = wrn;
wrn->next = 0;
wrn->wc_fil = FIL;
wrn->wc_lin = LIN;
wrn->wc_cnt = 1;
return 1;
}
/******** The handling ********/
#define wmask_on(i) (warnmask[i])
PRIVATE int latest_warning_printed; /* set if ... */
/*ARGSUSED*/
void do_warn(int nr, int L, const char *F)
{
latest_warning_printed = 0;
if (nr < WMSG) {
if (!wmask_on(nr)) {
register long wrn_cnt = count_wrn(nr);
register char *wmsgtxt = warn_text[nr];
LOG(("@w1 warning: %s [%s: %d]", wmsgtxt, F, L));
if ( /* wrn_cnt is a power of two */
!((wrn_cnt-1) & wrn_cnt)
&& /* and it is the right power of two */
(WMASK & wrn_cnt)
) {
fprintf(mess_fp,
"(Warning %d, #%ld): %s at %s\n",
nr, wrn_cnt, wmsgtxt, position());
latest_warning_printed = 1;
}
}
}
else {
/* actually a trap number */
nr -= WMSG;
fprintf(mess_fp, "(Warning): Trap occurred - %s at %s\n",
trap2text(nr), position());
}
}
#ifdef LOGGING
void warningcont(int nr)
{
/* continued warning */
if (latest_warning_printed) {
if (!wmask_on(nr)) {
register char *wmsgtxt = warn_text[nr];
LOG(("@w1 warning cont.: %s", wmsgtxt));
fprintf(mess_fp,
"(Warning %d, cont.): %s at %s\n",
nr, wmsgtxt, position());
}
}
}
#endif /* LOGGING */
void set_wmask(int i)
{
if (i < WMSG) {
warnmask[i] = 1;
}
}