1 1.27 rillig /* $NetBSD: msg.c,v 1.27 2025/05/24 07:00:32 rillig Exp $ */ 2 1.2 cgd 3 1.1 cgd /* 4 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl 5 1.1 cgd * All Rights Reserved. 6 1.1 cgd * 7 1.1 cgd * Redistribution and use in source and binary forms, with or without 8 1.1 cgd * modification, are permitted provided that the following conditions 9 1.1 cgd * are met: 10 1.1 cgd * 1. Redistributions of source code must retain the above copyright 11 1.1 cgd * notice, this list of conditions and the following disclaimer. 12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer in the 14 1.1 cgd * documentation and/or other materials provided with the distribution. 15 1.1 cgd * 3. All advertising materials mentioning features or use of this software 16 1.1 cgd * must display the following acknowledgement: 17 1.24 rillig * This product includes software developed by Jochen Pohl for 18 1.1 cgd * The NetBSD Project. 19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products 20 1.1 cgd * derived from this software without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 cgd */ 33 1.1 cgd 34 1.8 jmc #if HAVE_NBTOOL_CONFIG_H 35 1.8 jmc #include "nbtool_config.h" 36 1.8 jmc #endif 37 1.8 jmc 38 1.4 christos #include <sys/cdefs.h> 39 1.17 rillig #if defined(__RCSID) 40 1.27 rillig __RCSID("$NetBSD: msg.c,v 1.27 2025/05/24 07:00:32 rillig Exp $"); 41 1.1 cgd #endif 42 1.1 cgd 43 1.12 rillig #include <stdarg.h> 44 1.1 cgd #include <stdio.h> 45 1.6 tv #include <string.h> 46 1.1 cgd 47 1.1 cgd #include "lint2.h" 48 1.1 cgd 49 1.26 rillig static const char *msgs[] = { 50 1.26 rillig "%s is used in %s but never defined", // 0 51 1.26 rillig "%s is defined in %s but never used", // 1 52 1.26 rillig "%s is declared in %s but never used or defined", // 2 53 1.26 rillig "%s has multiple definitions in %s and %s", // 3 54 1.26 rillig "%s has its return value used inconsistently by %s and %s", // 4 55 1.27 rillig "%s %s '%s' at %s, versus '%s' at %s", // 5 56 1.26 rillig "%s has argument %d with type '%s' at %s, versus '%s' at %s", // 6 57 1.26 rillig "%s has %d parameters in %s, versus %d arguments in %s", // 7 58 1.26 rillig "%s returns a value that is always ignored", // 8 59 1.26 rillig "%s returns a value that is sometimes ignored", // 9 60 1.26 rillig "%s has its return value used in %s but doesn't return one", // 10 61 1.26 rillig "%s has parameter %d declared as '%s' in %s, versus '%s' in %s", // 11 62 1.26 rillig "%s has %d parameters in %s, versus %d in %s", // 12 63 1.26 rillig "%s is called with a malformed format string in %s", // 13 64 1.26 rillig "%s is called in %s with argument %d being incompatible with format string", // 14 65 1.26 rillig "%s is called in %s with too few arguments for format string", // 15 66 1.26 rillig "%s is called in %s with too many arguments for format string", // 16 67 1.26 rillig "%s's return type in %s must be declared before use in %s", // 17 68 1.26 rillig "%s is renamed multiple times in %s and %s", // 18 69 1.1 cgd }; 70 1.1 cgd 71 1.1 cgd void 72 1.1 cgd msg(int n, ...) 73 1.1 cgd { 74 1.20 rillig va_list ap; 75 1.1 cgd 76 1.1 cgd va_start(ap, n); 77 1.1 cgd 78 1.1 cgd (void)vprintf(msgs[n], ap); 79 1.26 rillig (void)printf(" [lint2:%03d]\n", n); 80 1.1 cgd 81 1.1 cgd va_end(ap); 82 1.1 cgd } 83 1.1 cgd 84 1.1 cgd /* 85 1.1 cgd * Return a pointer to the last component of a path. 86 1.1 cgd */ 87 1.1 cgd static const char * 88 1.6 tv lbasename(const char *path) 89 1.1 cgd { 90 1.1 cgd 91 1.1 cgd if (Fflag) 92 1.13 rillig return path; 93 1.1 cgd 94 1.19 rillig const char *base = path; 95 1.19 rillig for (const char *p = path; *p != '\0'; p++) 96 1.19 rillig if (*p == '/') 97 1.19 rillig base = p + 1; 98 1.19 rillig return base; 99 1.1 cgd } 100 1.1 cgd 101 1.1 cgd /* 102 1.1 cgd * Create a string which describes a position in a source file. 103 1.1 cgd */ 104 1.1 cgd const char * 105 1.21 rillig mkpos(const pos_t *posp) 106 1.1 cgd { 107 1.23 rillig static struct buffer { 108 1.23 rillig char *buf; 109 1.23 rillig size_t cap; 110 1.23 rillig } buffers[2]; 111 1.23 rillig static unsigned int buf_index; 112 1.23 rillig 113 1.23 rillig struct buffer *buf = buffers + buf_index; 114 1.23 rillig buf_index ^= 1; 115 1.1 cgd 116 1.22 rillig int filename; 117 1.22 rillig int lineno; 118 1.1 cgd if (Hflag && posp->p_src != posp->p_isrc) { 119 1.22 rillig filename = posp->p_isrc; 120 1.22 rillig lineno = posp->p_iline; 121 1.1 cgd } else { 122 1.22 rillig filename = posp->p_src; 123 1.22 rillig lineno = posp->p_line; 124 1.1 cgd } 125 1.1 cgd 126 1.22 rillig bool qm = !Hflag && posp->p_src != posp->p_isrc; 127 1.22 rillig const char *fn = lbasename(fnames[filename]); 128 1.22 rillig size_t len = strlen(fn) + 1 + 1 + 3 * sizeof(int) + 1 + 1; 129 1.22 rillig 130 1.23 rillig if (len > buf->cap) 131 1.23 rillig buf->buf = xrealloc(buf->buf, buf->cap = len); 132 1.22 rillig if (lineno != 0) 133 1.23 rillig (void)snprintf(buf->buf, buf->cap, "%s%s(%d)", 134 1.22 rillig fn, qm ? "?" : "", lineno); 135 1.21 rillig else 136 1.23 rillig (void)snprintf(buf->buf, buf->cap, "%s", fn); 137 1.1 cgd 138 1.23 rillig return buf->buf; 139 1.1 cgd } 140