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