1 /* $NetBSD: lint1.h,v 1.25 2011/06/24 01:10:31 christos 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 "lint.h" 36 #include "op.h" 37 38 /* 39 * XXX - Super conservative so that works for most systems, but we should 40 * not depend on the host settings but the target settings in determining 41 * the alignment. The only valid use for this is in mem1.c; uses in decl.c 42 * are bogus. 43 */ 44 #ifndef WORST_ALIGN 45 #ifdef _LP64 46 # define AVAL 15 47 #else 48 # define AVAL 7 49 #endif 50 #define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL) 51 #endif 52 53 /* 54 * Describes the position of a declaration or anything else. 55 */ 56 typedef struct { 57 int p_line; 58 const char *p_file; 59 int p_uniq; /* uniquifier */ 60 } pos_t; 61 62 /* Copies curr_pos, keeping things unique. */ 63 #define UNIQUE_CURR_POS(pos) \ 64 do { \ 65 STRUCT_ASSIGN((pos), curr_pos); \ 66 curr_pos.p_uniq++; \ 67 if (curr_pos.p_file == csrc_pos.p_file) \ 68 csrc_pos.p_uniq++; \ 69 } while (0) 70 71 /* 72 * Strings cannot be referenced to simply by a pointer to its first 73 * char. This is because strings can contain NUL characters other than the 74 * trailing NUL. 75 * 76 * Strings are stored with a trailing NUL. 77 */ 78 typedef struct strg { 79 tspec_t st_tspec; /* CHAR or WCHAR */ 80 size_t st_len; /* length without trailing NUL */ 81 union { 82 u_char *_st_cp; 83 wchar_t *_st_wcp; 84 } st_u; 85 } strg_t; 86 87 #define st_cp st_u._st_cp 88 #define st_wcp st_u._st_wcp 89 90 /* 91 * qualifiers (only for lex/yacc interface) 92 */ 93 typedef enum { 94 CONST, VOLATILE, RESTRICT 95 } tqual_t; 96 97 /* 98 * Integer and floating point values are stored in this structure 99 */ 100 typedef struct { 101 tspec_t v_tspec; 102 int v_ansiu; /* set if an integer constant is 103 unsigned in ANSI C */ 104 union { 105 int64_t _v_quad; /* integers */ 106 ldbl_t _v_ldbl; /* floats */ 107 } v_u; 108 } val_t; 109 110 #define v_quad v_u._v_quad 111 #define v_ldbl v_u._v_ldbl 112 113 /* 114 * Structures of type str_t uniqely identify structures. This can't 115 * be done in structures of type type_t, because these are copied 116 * if they must be modified. So it would not be possible to check 117 * if to structures are identical by comparing the pointers to 118 * the type structures. 119 * 120 * The typename is used if the structure is unnamed to identify 121 * the structure type in pass 2. 122 */ 123 typedef struct { 124 u_int size; /* size in bit */ 125 u_int align : 15; /* alignment in bit */ 126 u_int sincompl : 1; /* set if incomplete type */ 127 struct sym *memb; /* list of members */ 128 struct sym *stag; /* symbol table entry of tag */ 129 struct sym *stdef; /* symbol table entry of first typename */ 130 } str_t; 131 132 /* 133 * same as above for enums 134 */ 135 typedef struct { 136 u_int eincompl : 1; /* incomplete enum type */ 137 struct sym *elem; /* list of enumerators */ 138 struct sym *etag; /* symbol table entry of tag */ 139 struct sym *etdef; /* symbol table entry of first typename */ 140 } enum_t; 141 142 /* 143 * Types are represented by concatenation of structures of type type_t 144 * via t_subt. 145 */ 146 struct type { 147 tspec_t t_tspec; /* type specifier */ 148 u_int t_aincompl : 1; /* incomplete array type */ 149 u_int t_const : 1; /* const modifier */ 150 u_int t_volatile : 1; /* volatile modifier */ 151 u_int t_proto : 1; /* function prototype (t_args valid) */ 152 u_int t_vararg : 1; /* prototype with ... */ 153 u_int t_typedef : 1; /* type defined with typedef */ 154 u_int t_isfield : 1; /* type is bitfield */ 155 u_int t_isenum : 1; /* type is (or was) enum (t_enum valid) */ 156 u_int t_ispacked : 1; /* type is packed */ 157 union { 158 int _t_dim; /* dimension */ 159 str_t *_t_str; /* struct/union tag */ 160 enum_t *_t_enum; /* enum tag */ 161 struct sym *_t_args; /* arguments (if t_proto) */ 162 } t_u; 163 struct { 164 u_int _t_flen : 8; /* length of bit-field */ 165 u_int _t_foffs : 24; /* offset of bit-field */ 166 } t_b; 167 struct type *t_subt; /* element type (arrays), return value 168 (functions), or type pointer points to */ 169 }; 170 171 #define t_dim t_u._t_dim 172 #define t_str t_u._t_str 173 #define t_field t_u._t_field 174 #define t_enum t_u._t_enum 175 #define t_args t_u._t_args 176 #define t_flen t_b._t_flen 177 #define t_foffs t_b._t_foffs 178 179 /* 180 * types of symbols 181 */ 182 typedef enum { 183 FVFT, /* variables, functions, type names, enums */ 184 FMOS, /* members of structs or unions */ 185 FTAG, /* tags */ 186 FLAB /* labels */ 187 } symt_t; 188 189 /* 190 * storage classes 191 */ 192 typedef enum { 193 NOSCL, 194 EXTERN, /* external symbols (indep. of decl_t) */ 195 STATIC, /* static symbols (local and global) */ 196 AUTO, /* automatic symbols (except register) */ 197 REG, /* register */ 198 TYPEDEF, /* typedef */ 199 STRTAG, 200 UNIONTAG, 201 ENUMTAG, 202 MOS, /* member of struct */ 203 MOU, /* member of union */ 204 ENUMCON, /* enumerator */ 205 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */ 206 ARG, /* argument */ 207 PARG, /* used in declaration stack during prototype 208 declaration */ 209 INLINE /* only used by the parser */ 210 } scl_t; 211 212 /* 213 * symbol table entry 214 */ 215 typedef struct sym { 216 const char *s_name; /* name */ 217 const char *s_rename; /* renamed symbol's given name */ 218 pos_t s_dpos; /* position of last (prototype)definition, 219 prototypedeclaration, no-prototype-def., 220 tentative definition or declaration, 221 in this order */ 222 pos_t s_spos; /* position of first initialisation */ 223 pos_t s_upos; /* position of first use */ 224 symt_t s_kind; /* type of symbol */ 225 u_int s_keyw : 1; /* keyword */ 226 u_int s_field : 1; /* bit-field */ 227 u_int s_set : 1; /* variable set, label defined */ 228 u_int s_used : 1; /* variable/label used */ 229 u_int s_arg : 1; /* symbol is function argument */ 230 u_int s_reg : 1; /* symbol is register variable */ 231 u_int s_defarg : 1; /* undefined symbol in old style function 232 definition */ 233 u_int s_rimpl : 1; /* return value of function implicit decl. */ 234 u_int s_osdef : 1; /* symbol stems from old style function def. */ 235 u_int s_inline : 1; /* true if this is a inline function */ 236 struct sym *s_xsym; /* for local declared external symbols pointer 237 to external symbol with same name */ 238 def_t s_def; /* declared, tentative defined, defined */ 239 scl_t s_scl; /* storage class */ 240 int s_blklev; /* level of declaration, -1 if not in symbol 241 table */ 242 type_t *s_type; /* type */ 243 val_t s_value; /* value (if enumcon) */ 244 union { 245 str_t *_s_st; /* tag, if it is a struct/union member */ 246 enum_t *_s_et; /* tag, if it is a enumerator */ 247 tspec_t _s_tsp; /* type (only for keywords) */ 248 tqual_t _s_tqu; /* qualifier (only for keywords) */ 249 struct sym *_s_args; /* arguments in old style function 250 definitions */ 251 } u; 252 struct sym *s_link; /* next symbol with same hash value */ 253 struct sym **s_rlink; /* pointer to s_link of prev. symbol */ 254 struct sym *s_nxt; /* next struct/union member, enumerator, 255 argument */ 256 struct sym *s_dlnxt; /* next symbol declared on same level */ 257 } sym_t; 258 259 #define s_styp u._s_st 260 #define s_etyp u._s_et 261 #define s_tspec u._s_tsp 262 #define s_tqual u._s_tqu 263 #define s_args u._s_args 264 265 /* 266 * Used to keep some informations about symbols before they are entered 267 * into the symbol table. 268 */ 269 typedef struct sbuf { 270 const char *sb_name; /* name of symbol */ 271 size_t sb_len; /* length (without '\0') */ 272 int sb_hash; /* hash value */ 273 sym_t *sb_sym; /* symbol table entry */ 274 struct sbuf *sb_nxt; /* for freelist */ 275 } sbuf_t; 276 277 278 /* 279 * tree node 280 */ 281 typedef struct tnode { 282 op_t tn_op; /* operator */ 283 type_t *tn_type; /* type */ 284 u_int tn_lvalue : 1; /* node is lvalue */ 285 u_int tn_cast : 1; /* if tn_op == CVT its an explicit cast */ 286 u_int tn_parn : 1; /* node parenthesized */ 287 union { 288 struct { 289 struct tnode *_tn_left; /* (left) operand */ 290 struct tnode *_tn_right; /* right operand */ 291 } tn_s; 292 sym_t *_tn_sym; /* symbol if op == NAME */ 293 val_t *_tn_val; /* value if op == CON */ 294 strg_t *_tn_strg; /* string if op == STRING */ 295 } tn_u; 296 } tnode_t; 297 298 #define tn_left tn_u.tn_s._tn_left 299 #define tn_right tn_u.tn_s._tn_right 300 #define tn_sym tn_u._tn_sym 301 #define tn_val tn_u._tn_val 302 #define tn_strg tn_u._tn_strg 303 304 /* 305 * For nested declarations a stack exists, which holds all information 306 * needed for the current level. dcs points to the top element of this 307 * stack. 308 * 309 * ctx describes the context of the current declaration. Its value is 310 * one of 311 * EXTERN global declarations 312 * MOS oder MOU declarations of struct or union members 313 * ENUMCON declarations of enums 314 * ARG declaration of arguments in old style function definitions 315 * PARG declaration of arguments in function prototypes 316 * AUTO declaration of local symbols 317 * ABSTRACT abstract declarations (sizeof, casts) 318 * 319 */ 320 typedef struct dinfo { 321 tspec_t d_atyp; /* VOID, CHAR, INT, or COMPLEX */ 322 tspec_t d_cmod; /* FLOAT, or DOUBLE */ 323 tspec_t d_smod; /* SIGNED or UNSIGN */ 324 tspec_t d_lmod; /* SHORT, LONG or QUAD */ 325 scl_t d_scl; /* storage class */ 326 type_t *d_type; /* after deftyp() pointer to the type used 327 for all declarators */ 328 sym_t *d_rdcsym; /* redeclared symbol */ 329 int d_offset; /* offset of next structure member */ 330 int d_stralign; /* alignment required for current structure */ 331 scl_t d_ctx; /* context of declaration */ 332 u_int d_const : 1; /* const in declaration specifiers */ 333 u_int d_volatile : 1; /* volatile in declaration specifiers */ 334 u_int d_inline : 1; /* inline in declaration specifiers */ 335 u_int d_mscl : 1; /* multiple storage classes */ 336 u_int d_terr : 1; /* invalid type combination */ 337 u_int d_nedecl : 1; /* 1 if at least a tag is declared */ 338 u_int d_vararg : 1; /* ... in in current function decl. */ 339 u_int d_proto : 1; /* current funct. decl. is prototype */ 340 u_int d_notyp : 1; /* set if no type specifier was present */ 341 u_int d_asm : 1; /* set if d_ctx == AUTO and asm() present */ 342 u_int d_ispacked : 1; /* packed */ 343 type_t *d_tagtyp; /* tag during member declaration */ 344 sym_t *d_fargs; /* list of arguments during function def. */ 345 pos_t d_fdpos; /* position of function definition */ 346 sym_t *d_dlsyms; /* first symbol declared at this level */ 347 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl. 348 at this level */ 349 sym_t *d_fpsyms; /* symbols defined in prototype */ 350 struct dinfo *d_nxt; /* next level */ 351 } dinfo_t; 352 353 /* 354 * Type of stack which is used for initialisation of aggregate types. 355 */ 356 typedef struct istk { 357 type_t *i_type; /* type of initialisation */ 358 type_t *i_subt; /* type of next level */ 359 u_int i_brace : 1; /* need } for pop */ 360 u_int i_nolimit : 1; /* incomplete array type */ 361 u_int i_namedmem : 1; /* has c9x named members */ 362 sym_t *i_mem; /* next structure member */ 363 int i_cnt; /* # of remaining elements */ 364 struct istk *i_nxt; /* previous level */ 365 } istk_t; 366 367 /* 368 * Used to collect information about pointers and qualifiers in 369 * declarators. 370 */ 371 typedef struct pqinf { 372 int p_pcnt; /* number of asterisks */ 373 u_int p_const : 1; 374 u_int p_volatile : 1; 375 struct pqinf *p_nxt; 376 } pqinf_t; 377 378 /* 379 * Case values are stored in a list of type clst_t. 380 */ 381 typedef struct clst { 382 val_t cl_val; 383 struct clst *cl_nxt; 384 } clst_t; 385 386 /* 387 * Used to keep informations about nested control statements. 388 */ 389 typedef struct cstk { 390 int c_env; /* type of statement (T_IF, ...) */ 391 u_int c_loop : 1; /* continue && break are valid */ 392 u_int c_switch : 1; /* case && break are valid */ 393 u_int c_break : 1; /* loop/switch has break */ 394 u_int c_cont : 1; /* loop has continue */ 395 u_int c_default : 1; /* switch has default */ 396 u_int c_infinite : 1; /* break condition always false 397 (for (;;), while (1)) */ 398 u_int c_rchif : 1; /* end of if-branch reached */ 399 u_int c_noretval : 1; /* had "return;" */ 400 u_int c_retval : 1; /* had "return (e);" */ 401 type_t *c_swtype; /* type of switch expression */ 402 clst_t *c_clst; /* list of case values */ 403 struct mbl *c_fexprm; /* saved memory for end of loop 404 expression in for() */ 405 tnode_t *c_f3expr; /* end of loop expr in for() */ 406 pos_t c_fpos; /* position of end of loop expr */ 407 pos_t c_cfpos; /* same for csrc_pos */ 408 struct cstk *c_nxt; /* outer control statement */ 409 } cstk_t; 410 411 typedef struct { 412 size_t lo; 413 size_t hi; 414 } range_t; 415 416 #include "externs1.h" 417 418 #define ERR_SETSIZE 1024 419 #define __NERRBITS (sizeof(unsigned int)) 420 421 typedef struct err_set { 422 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS]; 423 } err_set; 424 425 #define ERR_SET(n, p) \ 426 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS))) 427 #define ERR_CLR(n, p) \ 428 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS))) 429 #define ERR_ISSET(n, p) \ 430 ((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) 431 #define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p))) 432 433 #define LERROR(fmt, args...) lerror(__FILE__, __LINE__, fmt, ##args) 434 435 extern err_set msgset; 436