1 /* $NetBSD: emit2.c,v 1.43 2026/01/11 18:11:38 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1994, 1995 Jochen Pohl 6 * All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jochen Pohl for 19 * The NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(__RCSID) 37 __RCSID("$NetBSD: emit2.c,v 1.43 2026/01/11 18:11:38 rillig Exp $"); 38 #endif 39 40 #include "lint2.h" 41 42 static void outfiles(void); 43 44 /* Write type into the output file. */ 45 static void 46 outtype(const type_t *tp) 47 { 48 static const char tt[NTSPEC] = "???BCCCSSIILLQQJJDDD?XXXV?TTTPAF"; 49 static const char ss[NTSPEC] = "??? su u u u u us l?s l ?sue "; 50 51 while (tp != NULL) { 52 tspec_t ts = tp->t_tspec; 53 if (ts == INT && tp->t_is_enum) 54 ts = ENUM; 55 if (!ch_isupper(tt[ts])) 56 errx(1, "internal error: outtype(%d)", ts); 57 if (tp->t_const) 58 outchar('c'); 59 if (tp->t_volatile) 60 outchar('v'); 61 if (ss[ts] != ' ') 62 outchar(ss[ts]); 63 if (ts == FUNC && tp->t_args != NULL && !tp->t_proto) 64 outchar('f'); 65 else 66 outchar(tt[ts]); 67 68 if (ts == ARRAY) { 69 outint(tp->t_dim); 70 } else if (ts == ENUM || ts == STRUCT || ts == UNION) { 71 if (tp->t_istag) { 72 outint(1); 73 outname(tp->t_tag->h_name); 74 } else if (tp->t_istynam) { 75 outint(2); 76 outname(tp->t_tynam->h_name); 77 } else if (tp->t_isuniqpos) { 78 outint(3); 79 outint(tp->t_uniqpos.p_line); 80 outchar('.'); 81 outint(tp->t_uniqpos.p_file); 82 outchar('.'); 83 outint(tp->t_uniqpos.p_uniq); 84 } else 85 errx(1, "internal error: outtype"); 86 } else if (ts == FUNC && tp->t_args != NULL) { 87 int na = 0; 88 for (const type_t **ap = tp->t_args; *ap != NULL; ap++) 89 na++; 90 if (tp->t_vararg) 91 na++; 92 outint(na); 93 for (const type_t **ap = tp->t_args; *ap != NULL; ap++) 94 outtype(*ap); 95 if (tp->t_vararg) 96 outchar('E'); 97 } 98 tp = tp->t_subt; 99 } 100 } 101 102 /* Write a definition. */ 103 static void 104 outdef(const hte_t *hte, const sym_t *sym) 105 { 106 107 outint(0); /* line number in C source file */ 108 outchar('d'); /* definition */ 109 outint(0); /* index of file where symbol was defined */ 110 outchar('.'); 111 outint(0); /* line number of definition */ 112 113 /* flags */ 114 if (sym->s_check_only_first_args) { 115 outchar('v'); 116 outint(sym->s_check_num_args); 117 } 118 if (sym->s_scanflike) { 119 outchar('S'); 120 outint(sym->s_scanflike_arg); 121 } 122 if (sym->s_printflike) { 123 outchar('P'); 124 outint(sym->s_printflike_arg); 125 } 126 /* definition or tentative definition */ 127 outchar(sym->s_def == DEF ? 'd' : 't'); 128 if (TP(sym->s_type)->t_tspec == FUNC) { 129 if (sym->s_function_has_return_value) 130 outchar('r'); 131 if (sym->s_old_style_function) 132 outchar('o'); 133 } 134 outchar('u'); /* used (no warning if not used) */ 135 outname(hte->h_name); 136 outtype(TP(sym->s_type)); 137 outchar('\n'); 138 } 139 140 /* Write the first definition of a name into the lint library. */ 141 static void 142 dumpname(const hte_t *hte) 143 { 144 sym_t *sym, *def; 145 146 /* static and undefined symbols are not written */ 147 if (hte->h_static || !hte->h_def) 148 return; 149 150 /* 151 * If there is a definition, write it. Otherwise, write a tentative 152 * definition. This is necessary because more than one tentative 153 * definition is allowed (except with sflag). 154 */ 155 def = NULL; 156 for (sym = hte->h_syms; sym != NULL; sym = sym->s_next) { 157 if (sym->s_def == DEF) { 158 def = sym; 159 break; 160 } 161 if (sym->s_def == TDEF && def == NULL) 162 def = sym; 163 } 164 if (def == NULL) 165 errx(1, "internal error: dumpname %s", hte->h_name); 166 167 outdef(hte, def); 168 } 169 170 /* Write a new lint library. */ 171 void 172 outlib(const char *name) 173 { 174 175 outopen(name); 176 177 outsrc(name); /* name of the lint library */ 178 179 outint(0); /* filename index of the lint library */ 180 outchar('s'); 181 outstrg(name); 182 outchar('\n'); 183 184 /* All files referenced by unnamed struct/union/enum declarations. */ 185 outfiles(); 186 187 /* Write all definitions with external linkage. */ 188 symtab_forall_sorted(dumpname); 189 190 outclose(); 191 } 192 193 /* Write out the name of a file referenced by a type. */ 194 struct outflist { 195 short ofl_num; 196 struct outflist *ofl_next; 197 }; 198 static struct outflist *outflist; 199 200 int 201 addoutfile(short num) 202 { 203 struct outflist *ofl, **pofl; 204 int i; 205 206 ofl = outflist; 207 pofl = &outflist; 208 i = 1; /* library is 0 */ 209 210 while (ofl != NULL) { 211 if (ofl->ofl_num == num) 212 break; 213 214 pofl = &ofl->ofl_next; 215 ofl = ofl->ofl_next; 216 i++; 217 } 218 219 if (ofl == NULL) { 220 ofl = *pofl = xmalloc(sizeof(**pofl)); 221 ofl->ofl_num = num; 222 ofl->ofl_next = NULL; 223 } 224 return i; 225 } 226 227 static void 228 outfiles(void) 229 { 230 struct outflist *ofl; 231 int i; 232 233 for (ofl = outflist, i = 1; ofl != NULL; ofl = ofl->ofl_next, i++) { 234 outint(i); 235 outchar('s'); 236 outstrg(fnames[ofl->ofl_num]); 237 outchar('\n'); 238 } 239 } 240