#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if defined(__RCSID)
__RCSID("$NetBSD: msg.c,v 1.27 2025/05/24 07:00:32 rillig Exp $");
#endif
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "lint2.h"
static const char *msgs[] = {
"%s is used in %s but never defined",
"%s is defined in %s but never used",
"%s is declared in %s but never used or defined",
"%s has multiple definitions in %s and %s",
"%s has its return value used inconsistently by %s and %s",
"%s %s '%s' at %s, versus '%s' at %s",
"%s has argument %d with type '%s' at %s, versus '%s' at %s",
"%s has %d parameters in %s, versus %d arguments in %s",
"%s returns a value that is always ignored",
"%s returns a value that is sometimes ignored",
"%s has its return value used in %s but doesn't return one",
"%s has parameter %d declared as '%s' in %s, versus '%s' in %s",
"%s has %d parameters in %s, versus %d in %s",
"%s is called with a malformed format string in %s",
"%s is called in %s with argument %d being incompatible with format string",
"%s is called in %s with too few arguments for format string",
"%s is called in %s with too many arguments for format string",
"%s's return type in %s must be declared before use in %s",
"%s is renamed multiple times in %s and %s",
};
void
msg(int n, ...)
{
va_list ap;
va_start(ap, n);
(void)vprintf(msgs[n], ap);
(void)printf(" [lint2:%03d]\n", n);
va_end(ap);
}
static const char *
lbasename(const char *path)
{
if (Fflag)
return path;
const char *base = path;
for (const char *p = path; *p != '\0'; p++)
if (*p == '/')
base = p + 1;
return base;
}
const char *
mkpos(const pos_t *posp)
{
static struct buffer {
char *buf;
size_t cap;
} buffers[2];
static unsigned int buf_index;
struct buffer *buf = buffers + buf_index;
buf_index ^= 1;
int filename;
int lineno;
if (Hflag && posp->p_src != posp->p_isrc) {
filename = posp->p_isrc;
lineno = posp->p_iline;
} else {
filename = posp->p_src;
lineno = posp->p_line;
}
bool qm = !Hflag && posp->p_src != posp->p_isrc;
const char *fn = lbasename(fnames[filename]);
size_t len = strlen(fn) + 1 + 1 + 3 * sizeof(int) + 1 + 1;
if (len > buf->cap)
buf->buf = xrealloc(buf->buf, buf->cap = len);
if (lineno != 0)
(void)snprintf(buf->buf, buf->cap, "%s%s(%d)",
fn, qm ? "?" : "", lineno);
else
(void)snprintf(buf->buf, buf->cap, "%s", fn);
return buf->buf;
}