1 /* $NetBSD: decl.c,v 1.182 2021/05/16 11:11:36 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 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) && !defined(lint) 41 __RCSID("$NetBSD: decl.c,v 1.182 2021/05/16 11:11:36 rillig Exp $"); 42 #endif 43 44 #include <sys/param.h> 45 #include <limits.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #include "lint1.h" 50 51 const char *unnamed = "<unnamed>"; 52 53 /* shared type structures for arithmetic types and void */ 54 static type_t *typetab; 55 56 /* value of next enumerator during declaration of enum types */ 57 int enumval; 58 59 /* 60 * pointer to top element of a stack which contains information local 61 * to nested declarations 62 */ 63 dinfo_t *dcs; 64 65 static type_t *tdeferr(type_t *, tspec_t); 66 static void settdsym(type_t *, sym_t *); 67 static tspec_t merge_type_specifiers(tspec_t, tspec_t); 68 static void align(int, int); 69 static sym_t *newtag(sym_t *, scl_t, bool, bool); 70 static bool eqargs(const type_t *, const type_t *, bool *); 71 static bool mnoarg(const type_t *, bool *); 72 static bool check_old_style_definition(sym_t *, sym_t *); 73 static bool check_prototype_declaration(sym_t *, sym_t *); 74 static sym_t *new_style_function(sym_t *, sym_t *); 75 static void old_style_function(sym_t *, sym_t *); 76 static void declare_external_in_block(sym_t *); 77 static bool check_init(sym_t *); 78 static void check_argument_usage(bool, sym_t *); 79 static void check_variable_usage(bool, sym_t *); 80 static void check_label_usage(sym_t *); 81 static void check_tag_usage(sym_t *); 82 static void check_global_variable(const sym_t *); 83 static void check_global_variable_size(const sym_t *); 84 85 /* 86 * initializes all global vars used in declarations 87 */ 88 void 89 initdecl(void) 90 { 91 int i; 92 93 /* declaration stack */ 94 dcs = xcalloc(1, sizeof(*dcs)); 95 dcs->d_ctx = EXTERN; 96 dcs->d_ldlsym = &dcs->d_dlsyms; 97 98 /* type information and classification */ 99 inittyp(); 100 101 /* shared type structures */ 102 typetab = xcalloc(NTSPEC, sizeof(*typetab)); 103 for (i = 0; i < NTSPEC; i++) 104 typetab[i].t_tspec = NOTSPEC; 105 typetab[BOOL].t_tspec = BOOL; 106 typetab[CHAR].t_tspec = CHAR; 107 typetab[SCHAR].t_tspec = SCHAR; 108 typetab[UCHAR].t_tspec = UCHAR; 109 typetab[SHORT].t_tspec = SHORT; 110 typetab[USHORT].t_tspec = USHORT; 111 typetab[INT].t_tspec = INT; 112 typetab[UINT].t_tspec = UINT; 113 typetab[LONG].t_tspec = LONG; 114 typetab[ULONG].t_tspec = ULONG; 115 typetab[QUAD].t_tspec = QUAD; 116 typetab[UQUAD].t_tspec = UQUAD; 117 typetab[FLOAT].t_tspec = FLOAT; 118 typetab[DOUBLE].t_tspec = DOUBLE; 119 typetab[LDOUBLE].t_tspec = LDOUBLE; 120 typetab[FCOMPLEX].t_tspec = FCOMPLEX; 121 typetab[DCOMPLEX].t_tspec = DCOMPLEX; 122 typetab[LCOMPLEX].t_tspec = LCOMPLEX; 123 typetab[COMPLEX].t_tspec = COMPLEX; 124 typetab[VOID].t_tspec = VOID; 125 /* 126 * Next two are not real types. They are only used by the parser 127 * to return keywords "signed" and "unsigned" 128 */ 129 typetab[SIGNED].t_tspec = SIGNED; 130 typetab[UNSIGN].t_tspec = UNSIGN; 131 } 132 133 /* 134 * Returns a shared type structure for arithmetic types and void. 135 * 136 * It's important to duplicate this structure (using dup_type() or 137 * expr_dup_type()) if it is to be modified (adding qualifiers or anything 138 * else). 139 */ 140 type_t * 141 gettyp(tspec_t t) 142 { 143 144 /* TODO: make the return type 'const' */ 145 return &typetab[t]; 146 } 147 148 type_t * 149 dup_type(const type_t *tp) 150 { 151 type_t *ntp; 152 153 ntp = getblk(sizeof(*ntp)); 154 *ntp = *tp; 155 return ntp; 156 } 157 158 /* 159 * Use expr_dup_type() instead of dup_type() inside expressions (if the 160 * allocated memory should be freed after the expr). 161 */ 162 type_t * 163 expr_dup_type(const type_t *tp) 164 { 165 type_t *ntp; 166 167 ntp = expr_zalloc(sizeof(*ntp)); 168 *ntp = *tp; 169 return ntp; 170 } 171 172 /* 173 * Returns whether the argument is void or an incomplete array, 174 * struct, union or enum type. 175 */ 176 bool 177 is_incomplete(const type_t *tp) 178 { 179 tspec_t t; 180 181 if ((t = tp->t_tspec) == VOID) { 182 return true; 183 } else if (t == ARRAY) { 184 return tp->t_incomplete_array; 185 } else if (t == STRUCT || t == UNION) { 186 return tp->t_str->sou_incomplete; 187 } else if (t == ENUM) { 188 return tp->t_enum->en_incomplete; 189 } 190 return false; 191 } 192 193 /* 194 * Mark an array, struct, union or enum type as complete or incomplete. 195 */ 196 void 197 setcomplete(type_t *tp, bool complete) 198 { 199 tspec_t t; 200 201 if ((t = tp->t_tspec) == ARRAY) { 202 tp->t_incomplete_array = !complete; 203 } else if (t == STRUCT || t == UNION) { 204 tp->t_str->sou_incomplete = !complete; 205 } else { 206 lint_assert(t == ENUM); 207 tp->t_enum->en_incomplete = !complete; 208 } 209 } 210 211 /* 212 * Remember the storage class of the current declaration in dcs->d_scl 213 * (the top element of the declaration stack) and detect multiple 214 * storage classes. 215 */ 216 void 217 add_storage_class(scl_t sc) 218 { 219 220 if (sc == INLINE) { 221 if (dcs->d_inline) 222 /* duplicate '%s' */ 223 warning(10, "inline"); 224 dcs->d_inline = true; 225 return; 226 } 227 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC || 228 dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) { 229 /* storage class after type is obsolescent */ 230 warning(83); 231 } 232 if (dcs->d_scl == NOSCL) { 233 dcs->d_scl = sc; 234 } else { 235 /* 236 * multiple storage classes. An error will be reported in 237 * deftyp(). 238 */ 239 dcs->d_mscl = true; 240 } 241 } 242 243 /* 244 * Remember the type, modifier or typedef name returned by the parser 245 * in *dcs (top element of decl stack). This information is used in 246 * deftyp() to build the type used for all declarators in this 247 * declaration. 248 * 249 * If tp->t_typedef is 1, the type comes from a previously defined typename. 250 * Otherwise it comes from a type specifier (int, long, ...) or a 251 * struct/union/enum tag. 252 */ 253 void 254 add_type(type_t *tp) 255 { 256 tspec_t t; 257 #ifdef DEBUG 258 printf("%s: %s\n", __func__, type_name(tp)); 259 #endif 260 if (tp->t_typedef) { 261 /* 262 * something like "typedef int a; int a b;" 263 * This should not happen with current grammar. 264 */ 265 lint_assert(dcs->d_type == NULL); 266 lint_assert(dcs->d_abstract_type == NOTSPEC); 267 lint_assert(dcs->d_sign_mod == NOTSPEC); 268 lint_assert(dcs->d_rank_mod == NOTSPEC); 269 270 dcs->d_type = tp; 271 return; 272 } 273 274 t = tp->t_tspec; 275 276 if (t == STRUCT || t == UNION || t == ENUM) { 277 /* 278 * something like "int struct a ..." 279 * struct/union/enum with anything else is not allowed 280 */ 281 if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC || 282 dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) { 283 /* 284 * remember that an error must be reported in 285 * deftyp(). 286 */ 287 dcs->d_terr = true; 288 dcs->d_abstract_type = NOTSPEC; 289 dcs->d_sign_mod = NOTSPEC; 290 dcs->d_rank_mod = NOTSPEC; 291 } 292 dcs->d_type = tp; 293 return; 294 } 295 296 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { 297 /* 298 * something like "struct a int" 299 * struct/union/enum with anything else is not allowed 300 */ 301 dcs->d_terr = true; 302 return; 303 } 304 305 if (t == COMPLEX) { 306 if (dcs->d_complex_mod == FLOAT) 307 t = FCOMPLEX; 308 else if (dcs->d_complex_mod == DOUBLE) 309 t = DCOMPLEX; 310 else { 311 /* invalid type for _Complex */ 312 error(308); 313 t = DCOMPLEX; /* just as a fallback */ 314 } 315 dcs->d_complex_mod = NOTSPEC; 316 } 317 318 if (t == LONG && dcs->d_rank_mod == LONG) { 319 /* "long long" or "long ... long" */ 320 t = QUAD; 321 dcs->d_rank_mod = NOTSPEC; 322 if (!quadflg) 323 /* %s C does not support 'long long' */ 324 c99ism(265, tflag ? "traditional" : "c89"); 325 } 326 327 if (dcs->d_type != NULL && dcs->d_type->t_typedef) { 328 /* something like "typedef int a; a long ..." */ 329 dcs->d_type = tdeferr(dcs->d_type, t); 330 return; 331 } 332 333 /* now it can be only a combination of arithmetic types and void */ 334 if (t == SIGNED || t == UNSIGN) { 335 /* 336 * remember specifiers "signed" & "unsigned" in 337 * dcs->d_sign_mod 338 */ 339 if (dcs->d_sign_mod != NOTSPEC) 340 /* 341 * more than one "signed" and/or "unsigned"; print 342 * an error in deftyp() 343 */ 344 dcs->d_terr = true; 345 dcs->d_sign_mod = t; 346 } else if (t == SHORT || t == LONG || t == QUAD) { 347 /* 348 * remember specifiers "short", "long" and "long long" in 349 * dcs->d_rank_mod 350 */ 351 if (dcs->d_rank_mod != NOTSPEC) 352 /* more than one, print error in deftyp() */ 353 dcs->d_terr = true; 354 dcs->d_rank_mod = t; 355 } else if (t == FLOAT || t == DOUBLE) { 356 if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) { 357 if (dcs->d_complex_mod != NOTSPEC 358 || (t == FLOAT && dcs->d_rank_mod == LONG)) 359 dcs->d_terr = true; 360 dcs->d_complex_mod = t; 361 } else { 362 if (dcs->d_abstract_type != NOTSPEC) 363 dcs->d_terr = true; 364 dcs->d_abstract_type = t; 365 } 366 } else if (t == PTR) { 367 dcs->d_type = tp; 368 } else { 369 /* 370 * remember specifiers "void", "char", "int", 371 * or "_Complex" in dcs->d_abstract_type 372 */ 373 if (dcs->d_abstract_type != NOTSPEC) 374 /* more than one, print error in deftyp() */ 375 dcs->d_terr = true; 376 dcs->d_abstract_type = t; 377 } 378 } 379 380 /* 381 * called if a list of declaration specifiers contains a typedef name 382 * and other specifiers (except struct, union, enum, typedef name) 383 */ 384 static type_t * 385 tdeferr(type_t *td, tspec_t t) 386 { 387 tspec_t t2; 388 389 t2 = td->t_tspec; 390 391 switch (t) { 392 case SIGNED: 393 case UNSIGN: 394 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || 395 t2 == QUAD) { 396 if (!tflag) 397 /* modifying typedef with '%s'; only ... */ 398 warning(5, ttab[t].tt_name); 399 td = dup_type(gettyp(merge_type_specifiers(t2, t))); 400 td->t_typedef = true; 401 return td; 402 } 403 break; 404 case SHORT: 405 if (t2 == INT || t2 == UINT) { 406 /* modifying typedef with '%s'; only qualifiers ... */ 407 warning(5, "short"); 408 td = dup_type(gettyp(t2 == INT ? SHORT : USHORT)); 409 td->t_typedef = true; 410 return td; 411 } 412 break; 413 case LONG: 414 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || 415 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { 416 /* modifying typedef with '%s'; only qualifiers ... */ 417 warning(5, "long"); 418 if (t2 == INT) { 419 td = gettyp(LONG); 420 } else if (t2 == UINT) { 421 td = gettyp(ULONG); 422 } else if (t2 == LONG) { 423 td = gettyp(QUAD); 424 } else if (t2 == ULONG) { 425 td = gettyp(UQUAD); 426 } else if (t2 == FLOAT) { 427 td = gettyp(DOUBLE); 428 } else if (t2 == DOUBLE) { 429 td = gettyp(LDOUBLE); 430 } else if (t2 == DCOMPLEX) { 431 td = gettyp(LCOMPLEX); 432 } 433 td = dup_type(td); 434 td->t_typedef = true; 435 return td; 436 } 437 break; 438 /* LINTED206: (enumeration values not handled in switch) */ 439 case NOTSPEC: 440 case USHORT: 441 case UCHAR: 442 case SCHAR: 443 case CHAR: 444 case BOOL: 445 case FUNC: 446 case ARRAY: 447 case PTR: 448 case ENUM: 449 case UNION: 450 case STRUCT: 451 case VOID: 452 case LDOUBLE: 453 case FLOAT: 454 case DOUBLE: 455 case UQUAD: 456 case QUAD: 457 #ifdef INT128_SIZE 458 case UINT128: 459 case INT128: 460 #endif 461 case ULONG: 462 case UINT: 463 case INT: 464 case FCOMPLEX: 465 case DCOMPLEX: 466 case LCOMPLEX: 467 case COMPLEX: 468 break; 469 } 470 471 /* Anything other is not accepted. */ 472 473 dcs->d_terr = true; 474 return td; 475 } 476 477 /* 478 * Remember the symbol of a typedef name (2nd arg) in a struct, union 479 * or enum tag if the typedef name is the first defined for this tag. 480 * 481 * If the tag is unnamed, the typdef name is used for identification 482 * of this tag in lint2. Although it's possible that more than one typedef 483 * name is defined for one tag, the first name defined should be unique 484 * if the tag is unnamed. 485 */ 486 static void 487 settdsym(type_t *tp, sym_t *sym) 488 { 489 tspec_t t; 490 491 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 492 if (tp->t_str->sou_first_typedef == NULL) 493 tp->t_str->sou_first_typedef = sym; 494 } else if (t == ENUM) { 495 if (tp->t_enum->en_first_typedef == NULL) 496 tp->t_enum->en_first_typedef = sym; 497 } 498 } 499 500 static size_t 501 bitfieldsize(sym_t **mem) 502 { 503 size_t len = (*mem)->s_type->t_flen; 504 while (*mem != NULL && (*mem)->s_type->t_bitfield) { 505 len += (*mem)->s_type->t_flen; 506 *mem = (*mem)->s_next; 507 } 508 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE; 509 } 510 511 static void 512 setpackedsize(type_t *tp) 513 { 514 struct_or_union *sp; 515 sym_t *mem; 516 517 switch (tp->t_tspec) { 518 case STRUCT: 519 case UNION: 520 sp = tp->t_str; 521 sp->sou_size_in_bits = 0; 522 for (mem = sp->sou_first_member; 523 mem != NULL; mem = mem->s_next) { 524 if (mem->s_type->t_bitfield) { 525 sp->sou_size_in_bits += bitfieldsize(&mem); 526 if (mem == NULL) 527 break; 528 } 529 size_t x = (size_t)type_size_in_bits(mem->s_type); 530 if (tp->t_tspec == STRUCT) 531 sp->sou_size_in_bits += x; 532 else if (x > sp->sou_size_in_bits) 533 sp->sou_size_in_bits = x; 534 } 535 break; 536 default: 537 /* %s attribute ignored for %s */ 538 warning(326, "packed", type_name(tp)); 539 break; 540 } 541 } 542 543 void 544 addpacked(void) 545 { 546 if (dcs->d_type == NULL) 547 dcs->d_packed = true; 548 else 549 setpackedsize(dcs->d_type); 550 } 551 552 void 553 add_attr_used(void) 554 { 555 dcs->d_used = true; 556 } 557 558 /* 559 * Remember a qualifier which is part of the declaration specifiers 560 * (and not the declarator) in the top element of the declaration stack. 561 * Also detect multiple qualifiers of the same kind. 562 563 * The remembered qualifier is used by deftyp() to construct the type 564 * for all declarators. 565 */ 566 void 567 add_qualifier(tqual_t q) 568 { 569 570 if (q == CONST) { 571 if (dcs->d_const) { 572 /* duplicate '%s' */ 573 warning(10, "const"); 574 } 575 dcs->d_const = true; 576 } else if (q == VOLATILE) { 577 if (dcs->d_volatile) { 578 /* duplicate '%s' */ 579 warning(10, "volatile"); 580 } 581 dcs->d_volatile = true; 582 } else { 583 lint_assert(q == RESTRICT || q == THREAD); 584 /* Silently ignore these qualifiers. */ 585 } 586 } 587 588 /* 589 * Go to the next declaration level (structs, nested structs, blocks, 590 * argument declaration lists ...) 591 */ 592 void 593 begin_declaration_level(scl_t sc) 594 { 595 dinfo_t *di; 596 597 /* put a new element on the declaration stack */ 598 di = xcalloc(1, sizeof(*di)); 599 di->d_next = dcs; 600 dcs = di; 601 di->d_ctx = sc; 602 di->d_ldlsym = &di->d_dlsyms; 603 if (dflag) 604 (void)printf("%s(%p %d)\n", __func__, dcs, (int)sc); 605 606 } 607 608 /* 609 * Go back to previous declaration level 610 */ 611 void 612 end_declaration_level(void) 613 { 614 dinfo_t *di; 615 616 if (dflag) 617 (void)printf("%s(%p %d)\n", __func__, dcs, (int)dcs->d_ctx); 618 619 lint_assert(dcs->d_next != NULL); 620 di = dcs; 621 dcs = di->d_next; 622 switch (di->d_ctx) { 623 case MOS: 624 case MOU: 625 case CTCONST: 626 /* 627 * Symbols declared in (nested) structs or enums are 628 * part of the next level (they are removed from the 629 * symbol table if the symbols of the outer level are 630 * removed). 631 */ 632 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 633 dcs->d_ldlsym = di->d_ldlsym; 634 break; 635 case ARG: 636 /* 637 * All symbols in dcs->d_dlsyms are introduced in old style 638 * argument declarations (it's not clean, but possible). 639 * They are appended to the list of symbols declared in 640 * an old style argument identifier list or a new style 641 * parameter type list. 642 */ 643 if (di->d_dlsyms != NULL) { 644 *di->d_ldlsym = dcs->d_func_proto_syms; 645 dcs->d_func_proto_syms = di->d_dlsyms; 646 } 647 break; 648 case ABSTRACT: 649 /* 650 * casts and sizeof 651 * Append all symbols declared in the abstract declaration 652 * to the list of symbols declared in the surrounding 653 * declaration or block. 654 * XXX I'm not sure whether they should be removed from the 655 * symbol table now or later. 656 */ 657 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 658 dcs->d_ldlsym = di->d_ldlsym; 659 break; 660 case AUTO: 661 /* check usage of local vars */ 662 check_usage(di); 663 /* FALLTHROUGH */ 664 case PROTO_ARG: 665 /* usage of arguments will be checked by funcend() */ 666 rmsyms(di->d_dlsyms); 667 break; 668 case EXTERN: 669 /* there is nothing after external declarations */ 670 /* FALLTHROUGH */ 671 default: 672 lint_assert(/*CONSTCOND*/false); 673 } 674 free(di); 675 } 676 677 /* 678 * Set flag d_asm in all declaration stack elements up to the 679 * outermost one. 680 * 681 * This is used to mark compound statements which have, possibly in 682 * nested compound statements, asm statements. For these compound 683 * statements no warnings about unused or uninitialized variables are 684 * printed. 685 * 686 * There is no need to clear d_asm in dinfo structs with context AUTO, 687 * because these structs are freed at the end of the compound statement. 688 * But it must be cleared in the outermost dinfo struct, which has 689 * context EXTERN. This could be done in clrtyp() and would work for C90, 690 * but not for C99 or C++ (due to mixed statements and declarations). Thus 691 * we clear it in global_clean_up_decl(), which is used to do some cleanup 692 * after global declarations/definitions. 693 */ 694 void 695 setasm(void) 696 { 697 dinfo_t *di; 698 699 for (di = dcs; di != NULL; di = di->d_next) 700 di->d_asm = true; 701 } 702 703 /* 704 * Clean all elements of the top element of declaration stack which 705 * will be used by the next declaration 706 */ 707 void 708 clrtyp(void) 709 { 710 711 dcs->d_abstract_type = NOTSPEC; 712 dcs->d_complex_mod = NOTSPEC; 713 dcs->d_sign_mod = NOTSPEC; 714 dcs->d_rank_mod = NOTSPEC; 715 dcs->d_scl = NOSCL; 716 dcs->d_type = NULL; 717 dcs->d_const = false; 718 dcs->d_volatile = false; 719 dcs->d_inline = false; 720 dcs->d_mscl = false; 721 dcs->d_terr = false; 722 dcs->d_nonempty_decl = false; 723 dcs->d_notyp = false; 724 } 725 726 static void 727 dcs_adjust_storage_class(void) 728 { 729 if (dcs->d_ctx == EXTERN) { 730 if (dcs->d_scl == REG || dcs->d_scl == AUTO) { 731 /* illegal storage class */ 732 error(8); 733 dcs->d_scl = NOSCL; 734 } 735 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) { 736 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) { 737 /* only register valid as formal parameter storage... */ 738 error(9); 739 dcs->d_scl = NOSCL; 740 } 741 } 742 } 743 744 /* 745 * Create a type structure from the information gathered in 746 * the declaration stack. 747 * Complain about storage classes which are not possible in current 748 * context. 749 */ 750 void 751 deftyp(void) 752 { 753 tspec_t t, s, l, c; 754 type_t *tp; 755 756 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */ 757 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */ 758 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */ 759 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */ 760 tp = dcs->d_type; 761 762 #ifdef DEBUG 763 printf("%s: %s\n", __func__, type_name(tp)); 764 #endif 765 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && 766 tp == NULL) 767 dcs->d_notyp = true; 768 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && 769 tp == NULL) 770 t = c; 771 772 if (tp != NULL) { 773 lint_assert(t == NOTSPEC); 774 lint_assert(s == NOTSPEC); 775 lint_assert(l == NOTSPEC); 776 } 777 778 if (tp == NULL) { 779 switch (t) { 780 case BOOL: 781 break; 782 case NOTSPEC: 783 t = INT; 784 /* FALLTHROUGH */ 785 case INT: 786 if (s == NOTSPEC) 787 s = SIGNED; 788 break; 789 case CHAR: 790 if (l != NOTSPEC) { 791 dcs->d_terr = true; 792 l = NOTSPEC; 793 } 794 break; 795 case FLOAT: 796 if (l == LONG) { 797 l = NOTSPEC; 798 t = DOUBLE; 799 if (!tflag) 800 /* use 'double' instead of 'long ... */ 801 warning(6); 802 } 803 break; 804 case DOUBLE: 805 if (l == LONG) { 806 case LDOUBLE: 807 l = NOTSPEC; 808 t = LDOUBLE; 809 if (tflag) 810 /* 'long double' is illegal in ... */ 811 warning(266); 812 } 813 break; 814 case DCOMPLEX: 815 if (l == LONG) { 816 l = NOTSPEC; 817 t = LCOMPLEX; 818 if (tflag) 819 /* 'long double' is illegal in ... */ 820 warning(266); 821 } 822 break; 823 case VOID: 824 case FCOMPLEX: 825 case LCOMPLEX: 826 break; 827 default: 828 INTERNAL_ERROR("deftyp(%s)", tspec_name(t)); 829 } 830 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 831 dcs->d_terr = true; 832 l = s = NOTSPEC; 833 } 834 if (l != NOTSPEC) 835 t = l; 836 dcs->d_type = gettyp(merge_type_specifiers(t, s)); 837 } 838 839 if (dcs->d_mscl) { 840 /* only one storage class allowed */ 841 error(7); 842 } 843 if (dcs->d_terr) { 844 /* illegal type combination */ 845 error(4); 846 } 847 848 dcs_adjust_storage_class(); 849 850 if (dcs->d_const && dcs->d_type->t_const) { 851 lint_assert(dcs->d_type->t_typedef); 852 /* typedef already qualified with '%s' */ 853 warning(68, "const"); 854 } 855 if (dcs->d_volatile && dcs->d_type->t_volatile) { 856 lint_assert(dcs->d_type->t_typedef); 857 /* typedef already qualified with '%s' */ 858 warning(68, "volatile"); 859 } 860 861 if (dcs->d_const || dcs->d_volatile) { 862 dcs->d_type = dup_type(dcs->d_type); 863 dcs->d_type->t_const |= dcs->d_const; 864 dcs->d_type->t_volatile |= dcs->d_volatile; 865 } 866 } 867 868 /* 869 * Merge type specifiers (char, ..., long long, signed, unsigned). 870 */ 871 static tspec_t 872 merge_type_specifiers(tspec_t t, tspec_t s) 873 { 874 875 if (s != SIGNED && s != UNSIGN) 876 return t; 877 878 if (t == CHAR) 879 return s == SIGNED ? SCHAR : UCHAR; 880 if (t == SHORT) 881 return s == SIGNED ? SHORT : USHORT; 882 if (t == INT) 883 return s == SIGNED ? INT : UINT; 884 if (t == LONG) 885 return s == SIGNED ? LONG : ULONG; 886 if (t == QUAD) 887 return s == SIGNED ? QUAD : UQUAD; 888 return t; 889 } 890 891 /* 892 * Return the length of a type in bits. 893 * 894 * Printing a message if the outermost dimension of an array is 0 must 895 * be done by the caller. All other problems are reported by length() 896 * if name is not NULL. 897 */ 898 int 899 length(const type_t *tp, const char *name) 900 { 901 int elem, elsz; 902 903 elem = 1; 904 while (tp != NULL && tp->t_tspec == ARRAY) { 905 elem *= tp->t_dim; 906 tp = tp->t_subt; 907 } 908 if (tp == NULL) 909 return -1; 910 911 switch (tp->t_tspec) { 912 case FUNC: 913 /* compiler takes size of function */ 914 INTERNAL_ERROR("%s", msgs[12]); 915 /* NOTREACHED */ 916 case STRUCT: 917 case UNION: 918 if (is_incomplete(tp) && name != NULL) { 919 /* incomplete structure or union %s: %s */ 920 error(31, tp->t_str->sou_tag->s_name, name); 921 } 922 elsz = tp->t_str->sou_size_in_bits; 923 break; 924 case ENUM: 925 if (is_incomplete(tp) && name != NULL) { 926 /* incomplete enum type: %s */ 927 warning(13, name); 928 } 929 /* FALLTHROUGH */ 930 default: 931 elsz = size_in_bits(tp->t_tspec); 932 if (elsz <= 0) 933 INTERNAL_ERROR("length(%d)", elsz); 934 break; 935 } 936 return elem * elsz; 937 } 938 939 int 940 alignment_in_bits(const type_t *tp) 941 { 942 size_t a; 943 tspec_t t; 944 945 while (tp != NULL && tp->t_tspec == ARRAY) 946 tp = tp->t_subt; 947 948 if (tp == NULL) 949 return -1; 950 951 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 952 a = tp->t_str->sou_align_in_bits; 953 } else if (t == FUNC) { 954 /* compiler takes alignment of function */ 955 error(14); 956 a = WORST_ALIGN(1) * CHAR_SIZE; 957 } else { 958 if ((a = size_in_bits(t)) == 0) { 959 a = CHAR_SIZE; 960 } else if (a > WORST_ALIGN(1) * CHAR_SIZE) { 961 a = WORST_ALIGN(1) * CHAR_SIZE; 962 } 963 } 964 lint_assert(a >= CHAR_SIZE); 965 lint_assert(a <= WORST_ALIGN(1) * CHAR_SIZE); 966 return a; 967 } 968 969 /* 970 * Concatenate two lists of symbols by s_next. Used by declarations of 971 * struct/union/enum elements and parameters. 972 */ 973 sym_t * 974 lnklst(sym_t *l1, sym_t *l2) 975 { 976 sym_t *l; 977 978 if ((l = l1) == NULL) 979 return l2; 980 while (l1->s_next != NULL) 981 l1 = l1->s_next; 982 l1->s_next = l2; 983 return l; 984 } 985 986 /* 987 * Check if the type of the given symbol is valid and print an error 988 * message if it is not. 989 * 990 * Invalid types are: 991 * - arrays of incomplete types or functions 992 * - functions returning arrays or functions 993 * - void types other than type of function or pointer 994 */ 995 void 996 check_type(sym_t *sym) 997 { 998 tspec_t to, t; 999 type_t **tpp, *tp; 1000 1001 tpp = &sym->s_type; 1002 to = NOTSPEC; 1003 while ((tp = *tpp) != NULL) { 1004 t = tp->t_tspec; 1005 /* 1006 * If this is the type of an old style function definition, 1007 * a better warning is printed in funcdef(). 1008 */ 1009 if (t == FUNC && !tp->t_proto && 1010 !(to == NOTSPEC && sym->s_osdef)) { 1011 if (sflag && hflag) 1012 /* function declaration is not a prototype */ 1013 warning(287); 1014 } 1015 if (to == FUNC) { 1016 if (t == FUNC || t == ARRAY) { 1017 /* function returns illegal type */ 1018 error(15); 1019 if (t == FUNC) { 1020 *tpp = derive_type(*tpp, PTR); 1021 } else { 1022 *tpp = derive_type((*tpp)->t_subt, PTR); 1023 } 1024 return; 1025 } else if (tp->t_const || tp->t_volatile) { 1026 if (sflag) { /* XXX or better !tflag ? */ 1027 /* function cannot return const... */ 1028 warning(228); 1029 } 1030 } 1031 } if (to == ARRAY) { 1032 if (t == FUNC) { 1033 /* array of function is illegal */ 1034 error(16); 1035 *tpp = gettyp(INT); 1036 return; 1037 } else if (t == ARRAY && tp->t_dim == 0) { 1038 /* null dimension */ 1039 error(17); 1040 return; 1041 } else if (t == VOID) { 1042 /* illegal use of 'void' */ 1043 error(18); 1044 *tpp = gettyp(INT); 1045 #if 0 /* errors are produced by length() */ 1046 } else if (is_incomplete(tp)) { 1047 /* array of incomplete type */ 1048 if (sflag) { 1049 /* array of incomplete type */ 1050 error(301); 1051 } else { 1052 /* array of incomplete type */ 1053 warning(301); 1054 } 1055 #endif 1056 } 1057 } else if (to == NOTSPEC && t == VOID) { 1058 if (dcs->d_ctx == PROTO_ARG) { 1059 if (sym->s_scl != ABSTRACT) { 1060 lint_assert(sym->s_name != unnamed); 1061 /* void param. cannot have name: %s */ 1062 error(61, sym->s_name); 1063 *tpp = gettyp(INT); 1064 } 1065 } else if (dcs->d_ctx == ABSTRACT) { 1066 /* ok */ 1067 } else if (sym->s_scl != TYPEDEF) { 1068 /* void type for '%s' */ 1069 error(19, sym->s_name); 1070 *tpp = gettyp(INT); 1071 } 1072 } 1073 if (t == VOID && to != PTR) { 1074 if (tp->t_const || tp->t_volatile) { 1075 /* inappropriate qualifiers with 'void' */ 1076 warning(69); 1077 tp->t_const = tp->t_volatile = false; 1078 } 1079 } 1080 tpp = &tp->t_subt; 1081 to = t; 1082 } 1083 } 1084 1085 /* 1086 * In traditional C, the only portable type for bit-fields is unsigned int. 1087 * 1088 * In C90, the only allowed types for bit-fields are int, signed int and 1089 * unsigned int (3.5.2.1). There is no mention of implementation-defined 1090 * types. 1091 * 1092 * In C99, the only portable types for bit-fields are _Bool, signed int and 1093 * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other 1094 * implementation-defined type". 1095 */ 1096 static void 1097 check_bit_field_type(sym_t *dsym, type_t **const inout_tp, tspec_t *inout_t) 1098 { 1099 type_t *tp = *inout_tp; 1100 tspec_t t = *inout_t; 1101 1102 if (t == CHAR || t == UCHAR || t == SCHAR || 1103 t == SHORT || t == USHORT || t == ENUM) { 1104 if (!bitfieldtype_ok) { 1105 if (sflag) { 1106 /* bit-field type '%s' invalid in ANSI C */ 1107 warning(273, type_name(tp)); 1108 } else if (pflag) { 1109 /* nonportable bit-field type '%s' */ 1110 warning(34, type_name(tp)); 1111 } 1112 } 1113 } else if (t == INT && dcs->d_sign_mod == NOTSPEC) { 1114 if (pflag && !bitfieldtype_ok) { 1115 /* bit-field of type plain 'int' has ... */ 1116 warning(344); 1117 } 1118 } else if (t != INT && t != UINT && t != BOOL) { 1119 /* 1120 * Non-integer types are always illegal for bitfields, 1121 * regardless of BITFIELDTYPE. Integer types not dealt with 1122 * above are okay only if BITFIELDTYPE is in effect. 1123 */ 1124 if (!(bitfieldtype_ok || gflag) || !is_integer(t)) { 1125 /* illegal bit-field type '%s' */ 1126 warning(35, type_name(tp)); 1127 int sz = tp->t_flen; 1128 dsym->s_type = tp = dup_type(gettyp(t = INT)); 1129 if ((tp->t_flen = sz) > size_in_bits(t)) 1130 tp->t_flen = size_in_bits(t); 1131 *inout_t = t; 1132 *inout_tp = tp; 1133 } 1134 } 1135 } 1136 1137 static void 1138 declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp) 1139 { 1140 1141 check_bit_field_type(dsym, inout_tp, inout_t); 1142 1143 type_t *const tp = *inout_tp; 1144 tspec_t const t = *inout_t; 1145 if (tp->t_flen < 0 || tp->t_flen > (ssize_t)size_in_bits(t)) { 1146 /* illegal bit-field size: %d */ 1147 error(36, tp->t_flen); 1148 tp->t_flen = size_in_bits(t); 1149 } else if (tp->t_flen == 0 && dsym->s_name != unnamed) { 1150 /* zero size bit-field */ 1151 error(37); 1152 tp->t_flen = size_in_bits(t); 1153 } 1154 if (dsym->s_scl == MOU) { 1155 /* illegal use of bit-field */ 1156 error(41); 1157 dsym->s_type->t_bitfield = false; 1158 dsym->s_bitfield = false; 1159 } 1160 } 1161 1162 /* 1163 * Process the declarator of a struct/union element. 1164 */ 1165 sym_t * 1166 declarator_1_struct_union(sym_t *dsym) 1167 { 1168 type_t *tp; 1169 tspec_t t; 1170 int sz; 1171 int o = 0; /* Appease GCC */ 1172 1173 lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU); 1174 1175 if (dcs->d_redeclared_symbol != NULL) { 1176 /* should be ensured by storesym() */ 1177 lint_assert(dcs->d_redeclared_symbol->s_scl == MOS || 1178 dcs->d_redeclared_symbol->s_scl == MOU); 1179 1180 if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) { 1181 /* duplicate member name: %s */ 1182 error(33, dsym->s_name); 1183 rmsym(dcs->d_redeclared_symbol); 1184 } 1185 } 1186 1187 check_type(dsym); 1188 1189 t = (tp = dsym->s_type)->t_tspec; 1190 1191 if (dsym->s_bitfield) { 1192 declare_bit_field(dsym, &t, &tp); 1193 } else if (t == FUNC) { 1194 /* function illegal in structure or union */ 1195 error(38); 1196 dsym->s_type = tp = derive_type(tp, t = PTR); 1197 } 1198 1199 /* 1200 * bit-fields of length 0 are not warned about because length() 1201 * does not return the length of the bit-field but the length 1202 * of the type the bit-field is packed in (it's ok) 1203 */ 1204 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1205 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1206 /* zero sized array in struct is a C99 extension: %s */ 1207 c99ism(39, dsym->s_name); 1208 } 1209 } 1210 1211 if (dcs->d_ctx == MOU) { 1212 o = dcs->d_offset; 1213 dcs->d_offset = 0; 1214 } 1215 if (dsym->s_bitfield) { 1216 align(alignment_in_bits(tp), tp->t_flen); 1217 dsym->s_value.v_quad = 1218 (dcs->d_offset / size_in_bits(t)) * size_in_bits(t); 1219 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; 1220 dcs->d_offset += tp->t_flen; 1221 } else { 1222 align(alignment_in_bits(tp), 0); 1223 dsym->s_value.v_quad = dcs->d_offset; 1224 dcs->d_offset += sz; 1225 } 1226 if (dcs->d_ctx == MOU) { 1227 if (o > dcs->d_offset) 1228 dcs->d_offset = o; 1229 } 1230 1231 check_function_definition(dsym, false); 1232 1233 /* 1234 * Clear the BITFIELDTYPE indicator after processing each 1235 * structure element. 1236 */ 1237 bitfieldtype_ok = false; 1238 1239 return dsym; 1240 } 1241 1242 /* 1243 * Aligns next structure element as required. 1244 * 1245 * al contains the required alignment, len the length of a bit-field. 1246 */ 1247 static void 1248 align(int al, int len) 1249 { 1250 int no; 1251 1252 /* 1253 * The alignment of the current element becomes the alignment of 1254 * the struct/union if it is larger than the current alignment 1255 * of the struct/union. 1256 */ 1257 if (al > dcs->d_stralign) 1258 dcs->d_stralign = al; 1259 1260 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1261 if (len == 0 || dcs->d_offset + len > no) 1262 dcs->d_offset = no; 1263 } 1264 1265 /* 1266 * Remember the width of the field in its type structure. 1267 */ 1268 sym_t * 1269 bitfield(sym_t *dsym, int len) 1270 { 1271 1272 if (dsym == NULL) { 1273 dsym = getblk(sizeof(*dsym)); 1274 dsym->s_name = unnamed; 1275 dsym->s_kind = FMEMBER; 1276 dsym->s_scl = MOS; 1277 dsym->s_type = gettyp(UINT); 1278 dsym->s_block_level = -1; 1279 } 1280 dsym->s_type = dup_type(dsym->s_type); 1281 dsym->s_type->t_bitfield = true; 1282 dsym->s_type->t_flen = len; 1283 dsym->s_bitfield = true; 1284 return dsym; 1285 } 1286 1287 /* 1288 * Collect information about a sequence of asterisks and qualifiers in a 1289 * list of type pqinf_t. 1290 * Qualifiers always refer to the left asterisk. 1291 * The rightmost asterisk will be at the top of the list. 1292 */ 1293 pqinf_t * 1294 merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2) 1295 { 1296 pqinf_t *p; 1297 1298 if (p2->p_pcnt != 0) { 1299 /* left '*' at the end of the list */ 1300 for (p = p2; p->p_next != NULL; p = p->p_next) 1301 continue; 1302 p->p_next = p1; 1303 return p2; 1304 } else { 1305 if (p2->p_const) { 1306 if (p1->p_const) { 1307 /* duplicate '%s' */ 1308 warning(10, "const"); 1309 } 1310 p1->p_const = true; 1311 } 1312 if (p2->p_volatile) { 1313 if (p1->p_volatile) { 1314 /* duplicate '%s' */ 1315 warning(10, "volatile"); 1316 } 1317 p1->p_volatile = true; 1318 } 1319 free(p2); 1320 return p1; 1321 } 1322 } 1323 1324 /* 1325 * The following 3 functions extend the type of a declarator with 1326 * pointer, function and array types. 1327 * 1328 * The current type is the type built by deftyp() (dcs->d_type) and 1329 * pointer, function and array types already added for this 1330 * declarator. The new type extension is inserted between both. 1331 */ 1332 sym_t * 1333 add_pointer(sym_t *decl, pqinf_t *pi) 1334 { 1335 type_t **tpp, *tp; 1336 pqinf_t *npi; 1337 1338 tpp = &decl->s_type; 1339 while (*tpp != NULL && *tpp != dcs->d_type) 1340 tpp = &(*tpp)->t_subt; 1341 if (*tpp == NULL) 1342 return decl; 1343 1344 while (pi != NULL) { 1345 *tpp = tp = getblk(sizeof(*tp)); 1346 tp->t_tspec = PTR; 1347 tp->t_const = pi->p_const; 1348 tp->t_volatile = pi->p_volatile; 1349 *(tpp = &tp->t_subt) = dcs->d_type; 1350 npi = pi->p_next; 1351 free(pi); 1352 pi = npi; 1353 } 1354 return decl; 1355 } 1356 1357 /* 1358 * If a dimension was specified, dim is true, otherwise false 1359 * n is the specified dimension 1360 */ 1361 sym_t * 1362 add_array(sym_t *decl, bool dim, int n) 1363 { 1364 type_t **tpp, *tp; 1365 1366 tpp = &decl->s_type; 1367 while (*tpp != NULL && *tpp != dcs->d_type) 1368 tpp = &(*tpp)->t_subt; 1369 if (*tpp == NULL) 1370 return decl; 1371 1372 *tpp = tp = getblk(sizeof(*tp)); 1373 tp->t_tspec = ARRAY; 1374 tp->t_subt = dcs->d_type; 1375 tp->t_dim = n; 1376 1377 if (n < 0) { 1378 /* negative array dimension (%d) */ 1379 error(20, n); 1380 n = 0; 1381 } else if (n == 0 && dim) { 1382 /* zero sized array is a C99 extension */ 1383 c99ism(322); 1384 } else if (n == 0 && !dim) { 1385 setcomplete(tp, false); 1386 } 1387 1388 return decl; 1389 } 1390 1391 sym_t * 1392 add_function(sym_t *decl, sym_t *args) 1393 { 1394 type_t **tpp, *tp; 1395 1396 if (dcs->d_proto) { 1397 if (tflag) 1398 /* function prototypes are illegal in traditional C */ 1399 warning(270); 1400 args = new_style_function(decl, args); 1401 } else { 1402 old_style_function(decl, args); 1403 } 1404 1405 /* 1406 * The symbols are removed from the symbol table by 1407 * end_declaration_level after add_function. To be able to restore 1408 * them if this is a function definition, a pointer to the list of all 1409 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of 1410 * the arguments (concatenated by s_next) is stored in 1411 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is 1412 * the declaration stack element created for the list of params and is 1413 * removed after add_function.) 1414 */ 1415 if (dcs->d_next->d_ctx == EXTERN && 1416 decl->s_type == dcs->d_next->d_type) { 1417 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms; 1418 dcs->d_next->d_func_args = args; 1419 } 1420 1421 tpp = &decl->s_type; 1422 while (*tpp != NULL && *tpp != dcs->d_next->d_type) 1423 tpp = &(*tpp)->t_subt; 1424 if (*tpp == NULL) 1425 return decl; 1426 1427 *tpp = tp = getblk(sizeof(*tp)); 1428 tp->t_tspec = FUNC; 1429 tp->t_subt = dcs->d_next->d_type; 1430 if ((tp->t_proto = dcs->d_proto) != false) 1431 tp->t_args = args; 1432 tp->t_vararg = dcs->d_vararg; 1433 1434 return decl; 1435 } 1436 1437 /* 1438 * Called for new style function declarations. 1439 */ 1440 /* ARGSUSED */ 1441 static sym_t * 1442 new_style_function(sym_t *decl, sym_t *args) 1443 { 1444 sym_t *arg, *sym; 1445 scl_t sc; 1446 int n; 1447 1448 /* 1449 * Declarations of structs/unions/enums in param lists are legal, 1450 * but senseless. 1451 */ 1452 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 1453 sc = sym->s_scl; 1454 if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) { 1455 /* dubious tag declaration: %s %s */ 1456 warning(85, storage_class_name(sc), sym->s_name); 1457 } 1458 } 1459 1460 n = 1; 1461 for (arg = args; arg != NULL; arg = arg->s_next) { 1462 if (arg->s_type->t_tspec == VOID) { 1463 if (n > 1 || arg->s_next != NULL) { 1464 /* void must be sole parameter */ 1465 error(60); 1466 arg->s_type = gettyp(INT); 1467 } 1468 } 1469 n++; 1470 } 1471 1472 /* return NULL if first param is VOID */ 1473 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL; 1474 } 1475 1476 /* 1477 * Called for old style function declarations. 1478 */ 1479 static void 1480 old_style_function(sym_t *decl, sym_t *args) 1481 { 1482 1483 /* 1484 * Remember list of params only if this is really seams to be 1485 * a function definition. 1486 */ 1487 if (dcs->d_next->d_ctx == EXTERN && 1488 decl->s_type == dcs->d_next->d_type) { 1489 /* 1490 * We assume that this becomes a function definition. If 1491 * we are wrong, it's corrected in check_function_definition(). 1492 */ 1493 if (args != NULL) { 1494 decl->s_osdef = true; 1495 decl->s_args = args; 1496 } 1497 } else { 1498 if (args != NULL) 1499 /* function prototype parameters must have types */ 1500 warning(62); 1501 } 1502 } 1503 1504 /* 1505 * Lists of identifiers in functions declarations are allowed only if 1506 * it's also a function definition. If this is not the case, print an 1507 * error message. 1508 */ 1509 void 1510 check_function_definition(sym_t *sym, bool msg) 1511 { 1512 1513 if (sym->s_osdef) { 1514 if (msg) { 1515 /* incomplete or misplaced function definition */ 1516 error(22); 1517 } 1518 sym->s_osdef = false; 1519 sym->s_args = NULL; 1520 } 1521 } 1522 1523 /* 1524 * Process the name in a declarator. 1525 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or 1526 * TYPEDEF. 1527 * s_def and s_reg are valid after declarator_name(). 1528 */ 1529 sym_t * 1530 declarator_name(sym_t *sym) 1531 { 1532 scl_t sc = NOSCL; 1533 1534 if (sym->s_scl == NOSCL) { 1535 dcs->d_redeclared_symbol = NULL; 1536 } else if (sym->s_defarg) { 1537 sym->s_defarg = false; 1538 dcs->d_redeclared_symbol = NULL; 1539 } else { 1540 dcs->d_redeclared_symbol = sym; 1541 sym = pushdown(sym); 1542 } 1543 1544 switch (dcs->d_ctx) { 1545 case MOS: 1546 case MOU: 1547 /* Set parent */ 1548 sym->s_styp = dcs->d_tagtyp->t_str; 1549 sym->s_def = DEF; 1550 sym->s_value.v_tspec = INT; 1551 sc = dcs->d_ctx; 1552 break; 1553 case EXTERN: 1554 /* 1555 * static and external symbols without "extern" are 1556 * considered to be tentative defined, external 1557 * symbols with "extern" are declared, and typedef names 1558 * are defined. Tentative defined and declared symbols 1559 * may become defined if an initializer is present or 1560 * this is a function definition. 1561 */ 1562 if ((sc = dcs->d_scl) == NOSCL) { 1563 sc = EXTERN; 1564 sym->s_def = TDEF; 1565 } else if (sc == STATIC) { 1566 sym->s_def = TDEF; 1567 } else if (sc == TYPEDEF) { 1568 sym->s_def = DEF; 1569 } else { 1570 lint_assert(sc == EXTERN); 1571 sym->s_def = DECL; 1572 } 1573 break; 1574 case PROTO_ARG: 1575 sym->s_arg = true; 1576 /* FALLTHROUGH */ 1577 case ARG: 1578 if ((sc = dcs->d_scl) == NOSCL) { 1579 sc = AUTO; 1580 } else { 1581 lint_assert(sc == REG); 1582 sym->s_reg = true; 1583 sc = AUTO; 1584 } 1585 sym->s_def = DEF; 1586 break; 1587 case AUTO: 1588 if ((sc = dcs->d_scl) == NOSCL) { 1589 /* 1590 * XXX somewhat ugly because we dont know whether 1591 * this is AUTO or EXTERN (functions). If we are 1592 * wrong it must be corrected in declare_local(), 1593 * where we have the necessary type information. 1594 */ 1595 sc = AUTO; 1596 sym->s_def = DEF; 1597 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { 1598 sym->s_def = DEF; 1599 } else if (sc == REG) { 1600 sym->s_reg = true; 1601 sc = AUTO; 1602 sym->s_def = DEF; 1603 } else { 1604 lint_assert(sc == EXTERN); 1605 sym->s_def = DECL; 1606 } 1607 break; 1608 default: 1609 lint_assert(/*CONSTCOND*/false); 1610 } 1611 sym->s_scl = sc; 1612 1613 sym->s_type = dcs->d_type; 1614 1615 dcs->d_func_proto_syms = NULL; 1616 1617 return sym; 1618 } 1619 1620 /* 1621 * Process a name in the list of formal parameters in an old style function 1622 * definition. 1623 */ 1624 sym_t * 1625 old_style_function_name(sym_t *sym) 1626 { 1627 1628 if (sym->s_scl != NOSCL) { 1629 if (block_level == sym->s_block_level) { 1630 /* redeclaration of formal parameter %s */ 1631 error(21, sym->s_name); 1632 lint_assert(sym->s_defarg); 1633 } 1634 sym = pushdown(sym); 1635 } 1636 sym->s_type = gettyp(INT); 1637 sym->s_scl = AUTO; 1638 sym->s_def = DEF; 1639 sym->s_defarg = sym->s_arg = true; 1640 return sym; 1641 } 1642 1643 /* 1644 * Create the type of a tag. 1645 * 1646 * tag points to the symbol table entry of the tag 1647 * kind is the kind of the tag (STRUCT/UNION/ENUM) 1648 * decl is true if the type of the tag will be completed in this declaration 1649 * (the following token is T_LBRACE) 1650 * semi is true if the following token is T_SEMI 1651 */ 1652 type_t * 1653 mktag(sym_t *tag, tspec_t kind, bool decl, bool semi) 1654 { 1655 scl_t scl; 1656 type_t *tp; 1657 1658 if (kind == STRUCT) { 1659 scl = STRUCT_TAG; 1660 } else if (kind == UNION) { 1661 scl = UNION_TAG; 1662 } else { 1663 lint_assert(kind == ENUM); 1664 scl = ENUM_TAG; 1665 } 1666 1667 if (tag != NULL) { 1668 if (tag->s_scl != NOSCL) { 1669 tag = newtag(tag, scl, decl, semi); 1670 } else { 1671 /* a new tag, no empty declaration */ 1672 dcs->d_next->d_nonempty_decl = true; 1673 if (scl == ENUM_TAG && !decl) { 1674 if (!tflag && (sflag || pflag)) 1675 /* forward reference to enum type */ 1676 warning(42); 1677 } 1678 } 1679 if (tag->s_scl == NOSCL) { 1680 tag->s_scl = scl; 1681 tag->s_type = tp = getblk(sizeof(*tp)); 1682 tp->t_packed = dcs->d_packed; 1683 } else { 1684 tp = tag->s_type; 1685 } 1686 } else { 1687 tag = getblk(sizeof(*tag)); 1688 tag->s_name = unnamed; 1689 UNIQUE_CURR_POS(tag->s_def_pos); 1690 tag->s_kind = FTAG; 1691 tag->s_scl = scl; 1692 tag->s_block_level = -1; 1693 tag->s_type = tp = getblk(sizeof(*tp)); 1694 tp->t_packed = dcs->d_packed; 1695 dcs->d_next->d_nonempty_decl = true; 1696 } 1697 1698 if (tp->t_tspec == NOTSPEC) { 1699 tp->t_tspec = kind; 1700 if (kind != ENUM) { 1701 tp->t_str = getblk(sizeof(*tp->t_str)); 1702 tp->t_str->sou_align_in_bits = CHAR_SIZE; 1703 tp->t_str->sou_tag = tag; 1704 } else { 1705 tp->t_is_enum = true; 1706 tp->t_enum = getblk(sizeof(*tp->t_enum)); 1707 tp->t_enum->en_tag = tag; 1708 } 1709 setcomplete(tp, false); 1710 } 1711 return tp; 1712 } 1713 1714 /* 1715 * Checks all possible cases of tag redeclarations. 1716 * decl is true if T_LBRACE follows 1717 * semi is true if T_SEMI follows 1718 */ 1719 static sym_t * 1720 newtag(sym_t *tag, scl_t scl, bool decl, bool semi) 1721 { 1722 1723 if (tag->s_block_level < block_level) { 1724 if (semi) { 1725 /* "struct a;" */ 1726 if (!tflag) { 1727 if (!sflag) 1728 /* decl. introduces new type ... */ 1729 warning(44, storage_class_name(scl), 1730 tag->s_name); 1731 tag = pushdown(tag); 1732 } else if (tag->s_scl != scl) { 1733 /* base type is really '%s %s' */ 1734 warning(45, storage_class_name(tag->s_scl), 1735 tag->s_name); 1736 } 1737 dcs->d_next->d_nonempty_decl = true; 1738 } else if (decl) { 1739 /* "struct a { ... } " */ 1740 if (hflag) 1741 /* redefinition hides earlier one: %s */ 1742 warning(43, tag->s_name); 1743 tag = pushdown(tag); 1744 dcs->d_next->d_nonempty_decl = true; 1745 } else if (tag->s_scl != scl) { 1746 /* base type is really '%s %s' */ 1747 warning(45, storage_class_name(tag->s_scl), 1748 tag->s_name); 1749 /* declaration introduces new type in ANSI C: %s %s */ 1750 if (!sflag) { 1751 /* decl. introduces new type in ANSI C: %s %s */ 1752 warning(44, storage_class_name(scl), 1753 tag->s_name); 1754 } 1755 tag = pushdown(tag); 1756 dcs->d_next->d_nonempty_decl = true; 1757 } 1758 } else { 1759 if (tag->s_scl != scl) { 1760 /* (%s) tag redeclared */ 1761 error(46, storage_class_name(tag->s_scl)); 1762 print_previous_declaration(-1, tag); 1763 tag = pushdown(tag); 1764 dcs->d_next->d_nonempty_decl = true; 1765 } else if (decl && !is_incomplete(tag->s_type)) { 1766 /* (%s) tag redeclared */ 1767 error(46, storage_class_name(tag->s_scl)); 1768 print_previous_declaration(-1, tag); 1769 tag = pushdown(tag); 1770 dcs->d_next->d_nonempty_decl = true; 1771 } else if (semi || decl) { 1772 dcs->d_next->d_nonempty_decl = true; 1773 } 1774 } 1775 return tag; 1776 } 1777 1778 const char * 1779 storage_class_name(scl_t sc) 1780 { 1781 const char *s; 1782 1783 switch (sc) { 1784 case EXTERN: s = "extern"; break; 1785 case STATIC: s = "static"; break; 1786 case AUTO: s = "auto"; break; 1787 case REG: s = "register"; break; 1788 case TYPEDEF: s = "typedef"; break; 1789 case STRUCT_TAG:s = "struct"; break; 1790 case UNION_TAG: s = "union"; break; 1791 case ENUM_TAG: s = "enum"; break; 1792 default: lint_assert(/*CONSTCOND*/false); 1793 } 1794 return s; 1795 } 1796 1797 /* 1798 * tp points to the type of the tag, fmem to the list of members. 1799 */ 1800 type_t * 1801 complete_tag_struct_or_union(type_t *tp, sym_t *fmem) 1802 { 1803 tspec_t t; 1804 struct_or_union *sp; 1805 int n; 1806 sym_t *mem; 1807 1808 setcomplete(tp, true); 1809 1810 t = tp->t_tspec; 1811 align(dcs->d_stralign, 0); 1812 sp = tp->t_str; 1813 sp->sou_align_in_bits = dcs->d_stralign; 1814 sp->sou_first_member = fmem; 1815 if (tp->t_packed) 1816 setpackedsize(tp); 1817 else 1818 sp->sou_size_in_bits = dcs->d_offset; 1819 1820 if (sp->sou_size_in_bits == 0) { 1821 /* zero sized %s is a C9X feature */ 1822 c99ism(47, ttab[t].tt_name); 1823 } 1824 1825 n = 0; 1826 for (mem = fmem; mem != NULL; mem = mem->s_next) { 1827 /* bind anonymous members to the structure */ 1828 if (mem->s_styp == NULL) { 1829 mem->s_styp = sp; 1830 if (mem->s_type->t_bitfield) { 1831 sp->sou_size_in_bits += bitfieldsize(&mem); 1832 if (mem == NULL) 1833 break; 1834 } 1835 sp->sou_size_in_bits += type_size_in_bits(mem->s_type); 1836 } 1837 if (mem->s_name != unnamed) 1838 n++; 1839 } 1840 1841 if (n == 0 && sp->sou_size_in_bits != 0) { 1842 /* %s has no named members */ 1843 warning(65, t == STRUCT ? "structure" : "union"); 1844 } 1845 return tp; 1846 } 1847 1848 type_t * 1849 complete_tag_enum(type_t *tp, sym_t *fmem) 1850 { 1851 1852 setcomplete(tp, true); 1853 tp->t_enum->en_first_enumerator = fmem; 1854 return tp; 1855 } 1856 1857 /* 1858 * Processes the name of an enumerator in an enum declaration. 1859 * 1860 * sym points to the enumerator 1861 * val is the value of the enumerator 1862 * impl is true if the value of the enumerator was not explicitly specified. 1863 */ 1864 sym_t * 1865 enumeration_constant(sym_t *sym, int val, bool impl) 1866 { 1867 1868 if (sym->s_scl != NOSCL) { 1869 if (sym->s_block_level == block_level) { 1870 /* no hflag, because this is illegal!!! */ 1871 if (sym->s_arg) { 1872 /* enumeration constant hides parameter: %s */ 1873 warning(57, sym->s_name); 1874 } else { 1875 /* redeclaration of %s */ 1876 error(27, sym->s_name); 1877 /* 1878 * inside blocks it should not be too 1879 * complicated to find the position of the 1880 * previous declaration 1881 */ 1882 if (block_level == 0) 1883 print_previous_declaration(-1, sym); 1884 } 1885 } else { 1886 if (hflag) 1887 /* redefinition hides earlier one: %s */ 1888 warning(43, sym->s_name); 1889 } 1890 sym = pushdown(sym); 1891 } 1892 sym->s_scl = CTCONST; 1893 sym->s_type = dcs->d_tagtyp; 1894 sym->s_value.v_tspec = INT; 1895 sym->s_value.v_quad = val; 1896 if (impl && val - 1 == TARG_INT_MAX) { 1897 /* overflow in enumeration values: %s */ 1898 warning(48, sym->s_name); 1899 } 1900 enumval = val + 1; 1901 return sym; 1902 } 1903 1904 /* 1905 * Process a single external declarator. 1906 */ 1907 static void 1908 declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming) 1909 { 1910 bool dowarn, rval, redec; 1911 sym_t *rdsym; 1912 char *s; 1913 1914 if (renaming != NULL) { 1915 lint_assert(dsym->s_rename == NULL); 1916 1917 s = getlblk(1, renaming->sb_len + 1); 1918 (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1); 1919 dsym->s_rename = s; 1920 } 1921 1922 check_function_definition(dsym, true); 1923 1924 check_type(dsym); 1925 1926 if (initflg && !check_init(dsym)) 1927 dsym->s_def = DEF; 1928 1929 /* 1930 * Declarations of functions are marked as "tentative" in 1931 * declarator_name(). This is wrong because there are no 1932 * tentative function definitions. 1933 */ 1934 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) 1935 dsym->s_def = DECL; 1936 1937 if (dcs->d_inline) { 1938 if (dsym->s_type->t_tspec == FUNC) { 1939 dsym->s_inline = true; 1940 } else { 1941 /* variable declared inline: %s */ 1942 warning(268, dsym->s_name); 1943 } 1944 } 1945 1946 /* Write the declaration into the output file */ 1947 if (plibflg && llibflg && 1948 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { 1949 /* 1950 * With both LINTLIBRARY and PROTOLIB the prototype is 1951 * written as a function definition to the output file. 1952 */ 1953 rval = dsym->s_type->t_subt->t_tspec != VOID; 1954 outfdef(dsym, &dsym->s_def_pos, rval, false, NULL); 1955 } else { 1956 outsym(dsym, dsym->s_scl, dsym->s_def); 1957 } 1958 1959 if ((rdsym = dcs->d_redeclared_symbol) != NULL) { 1960 1961 /* 1962 * If the old symbol stems from an old style function 1963 * definition, we have remembered the params in rdsmy->s_args 1964 * and compare them with the params of the prototype. 1965 */ 1966 if (rdsym->s_osdef && dsym->s_type->t_proto) { 1967 redec = check_old_style_definition(rdsym, dsym); 1968 } else { 1969 redec = false; 1970 } 1971 1972 if (!redec && 1973 !check_redeclaration(dsym, (dowarn = false, &dowarn))) { 1974 1975 if (dowarn) { 1976 if (sflag) 1977 /* redeclaration of %s */ 1978 error(27, dsym->s_name); 1979 else 1980 /* redeclaration of %s */ 1981 warning(27, dsym->s_name); 1982 print_previous_declaration(-1, rdsym); 1983 } 1984 1985 /* 1986 * Take over the remembered params if the new symbol 1987 * is not a prototype. 1988 */ 1989 if (rdsym->s_osdef && !dsym->s_type->t_proto) { 1990 dsym->s_osdef = rdsym->s_osdef; 1991 dsym->s_args = rdsym->s_args; 1992 dsym->s_def_pos = rdsym->s_def_pos; 1993 } 1994 1995 /* 1996 * Remember the position of the declaration if the 1997 * old symbol was a prototype and the new is not. 1998 * Also remember the position if the old symbol 1999 * was defined and the new is not. 2000 */ 2001 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) { 2002 dsym->s_def_pos = rdsym->s_def_pos; 2003 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) { 2004 dsym->s_def_pos = rdsym->s_def_pos; 2005 } 2006 2007 /* 2008 * Copy usage information of the name into the new 2009 * symbol. 2010 */ 2011 copy_usage_info(dsym, rdsym); 2012 2013 /* Once a name is defined, it remains defined. */ 2014 if (rdsym->s_def == DEF) 2015 dsym->s_def = DEF; 2016 2017 /* once a function is inline, it remains inline */ 2018 if (rdsym->s_inline) 2019 dsym->s_inline = true; 2020 2021 complete_type(dsym, rdsym); 2022 2023 } 2024 2025 rmsym(rdsym); 2026 } 2027 2028 if (dsym->s_scl == TYPEDEF) { 2029 dsym->s_type = dup_type(dsym->s_type); 2030 dsym->s_type->t_typedef = true; 2031 settdsym(dsym->s_type, dsym); 2032 } 2033 2034 } 2035 2036 void 2037 declare(sym_t *decl, bool initflg, sbuf_t *renaming) 2038 { 2039 2040 if (dcs->d_ctx == EXTERN) { 2041 declare_extern(decl, initflg, renaming); 2042 } else if (dcs->d_ctx == ARG) { 2043 if (renaming != NULL) { 2044 /* symbol renaming can't be used on function arguments */ 2045 error(310); 2046 } else 2047 (void)declare_argument(decl, initflg); 2048 } else { 2049 lint_assert(dcs->d_ctx == AUTO); 2050 if (renaming != NULL) { 2051 /* symbol renaming can't be used on automatic variables */ 2052 error(311); 2053 } else 2054 declare_local(decl, initflg); 2055 } 2056 } 2057 2058 /* 2059 * Copies information about usage into a new symbol table entry of 2060 * the same symbol. 2061 */ 2062 void 2063 copy_usage_info(sym_t *sym, sym_t *rdsym) 2064 { 2065 2066 sym->s_set_pos = rdsym->s_set_pos; 2067 sym->s_use_pos = rdsym->s_use_pos; 2068 sym->s_set = rdsym->s_set; 2069 sym->s_used = rdsym->s_used; 2070 } 2071 2072 /* 2073 * Prints an error and returns true if a symbol is redeclared/redefined. 2074 * Otherwise returns false and, in some cases of minor problems, prints 2075 * a warning. 2076 */ 2077 bool 2078 check_redeclaration(sym_t *dsym, bool *dowarn) 2079 { 2080 sym_t *rsym; 2081 2082 if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) { 2083 /* redeclaration of %s */ 2084 error(27, dsym->s_name); 2085 print_previous_declaration(-1, rsym); 2086 return true; 2087 } 2088 if (rsym->s_scl == TYPEDEF) { 2089 /* typedef redeclared: %s */ 2090 error(89, dsym->s_name); 2091 print_previous_declaration(-1, rsym); 2092 return true; 2093 } 2094 if (dsym->s_scl == TYPEDEF) { 2095 /* redeclaration of %s */ 2096 error(27, dsym->s_name); 2097 print_previous_declaration(-1, rsym); 2098 return true; 2099 } 2100 if (rsym->s_def == DEF && dsym->s_def == DEF) { 2101 /* redefinition of %s */ 2102 error(28, dsym->s_name); 2103 print_previous_declaration(-1, rsym); 2104 return true; 2105 } 2106 if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) { 2107 /* redeclaration of %s */ 2108 error(27, dsym->s_name); 2109 print_previous_declaration(-1, rsym); 2110 return true; 2111 } 2112 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN) 2113 return false; 2114 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC) 2115 return false; 2116 if (rsym->s_scl == STATIC && dsym->s_def == DECL) 2117 return false; 2118 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) { 2119 /* 2120 * All cases except "int a = 1; static int a;" are caught 2121 * above with or without a warning 2122 */ 2123 /* redeclaration of %s */ 2124 error(27, dsym->s_name); 2125 print_previous_declaration(-1, rsym); 2126 return true; 2127 } 2128 if (rsym->s_scl == EXTERN) { 2129 /* previously declared extern, becomes static: %s */ 2130 warning(29, dsym->s_name); 2131 print_previous_declaration(-1, rsym); 2132 return false; 2133 } 2134 /* 2135 * Now it's one of: 2136 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" 2137 */ 2138 /* redeclaration of %s; ANSI C requires "static" */ 2139 if (sflag) { 2140 /* redeclaration of %s; ANSI C requires static */ 2141 warning(30, dsym->s_name); 2142 print_previous_declaration(-1, rsym); 2143 } 2144 dsym->s_scl = STATIC; 2145 return false; 2146 } 2147 2148 static bool 2149 qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual) 2150 { 2151 if (tp1->t_const != tp2->t_const && !ignqual && !tflag) 2152 return false; 2153 2154 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag) 2155 return false; 2156 2157 return true; 2158 } 2159 2160 bool 2161 eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual) 2162 { 2163 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID) 2164 return false; 2165 2166 if (!qualifiers_correspond(tp1, tp2, ignqual)) 2167 return false; 2168 2169 return true; 2170 } 2171 2172 2173 /* 2174 * Checks if two types are compatible. 2175 * 2176 * ignqual ignore qualifiers of type; used for function params 2177 * promot promote left type; used for comparison of params of 2178 * old style function definitions with params of prototypes. 2179 * *dowarn set to true if an old style function declaration is not 2180 * compatible with a prototype 2181 */ 2182 bool 2183 eqtype(const type_t *tp1, const type_t *tp2, 2184 bool ignqual, bool promot, bool *dowarn) 2185 { 2186 tspec_t t; 2187 2188 while (tp1 != NULL && tp2 != NULL) { 2189 2190 t = tp1->t_tspec; 2191 if (promot) { 2192 if (t == FLOAT) { 2193 t = DOUBLE; 2194 } else if (t == CHAR || t == SCHAR) { 2195 t = INT; 2196 } else if (t == UCHAR) { 2197 t = tflag ? UINT : INT; 2198 } else if (t == SHORT) { 2199 t = INT; 2200 } else if (t == USHORT) { 2201 /* CONSTCOND */ 2202 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT; 2203 } 2204 } 2205 2206 if (t != tp2->t_tspec) 2207 return false; 2208 2209 if (!qualifiers_correspond(tp1, tp2, ignqual)) 2210 return false; 2211 2212 if (t == STRUCT || t == UNION) 2213 return tp1->t_str == tp2->t_str; 2214 2215 if (t == ARRAY && tp1->t_dim != tp2->t_dim) { 2216 if (tp1->t_dim != 0 && tp2->t_dim != 0) 2217 return false; 2218 } 2219 2220 /* dont check prototypes for traditional */ 2221 if (t == FUNC && !tflag) { 2222 if (tp1->t_proto && tp2->t_proto) { 2223 if (!eqargs(tp1, tp2, dowarn)) 2224 return false; 2225 } else if (tp1->t_proto) { 2226 if (!mnoarg(tp1, dowarn)) 2227 return false; 2228 } else if (tp2->t_proto) { 2229 if (!mnoarg(tp2, dowarn)) 2230 return false; 2231 } 2232 } 2233 2234 tp1 = tp1->t_subt; 2235 tp2 = tp2->t_subt; 2236 ignqual = promot = false; 2237 2238 } 2239 2240 return tp1 == tp2; 2241 } 2242 2243 /* 2244 * Compares the parameter types of two prototypes. 2245 */ 2246 static bool 2247 eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn) 2248 { 2249 sym_t *a1, *a2; 2250 2251 if (tp1->t_vararg != tp2->t_vararg) 2252 return false; 2253 2254 a1 = tp1->t_args; 2255 a2 = tp2->t_args; 2256 2257 while (a1 != NULL && a2 != NULL) { 2258 2259 if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn)) 2260 return false; 2261 2262 a1 = a1->s_next; 2263 a2 = a2->s_next; 2264 2265 } 2266 2267 return a1 == a2; 2268 } 2269 2270 /* 2271 * mnoarg() (matches functions with no argument type information) 2272 * returns whether all parameters of a prototype are compatible with 2273 * an old style function declaration. 2274 * This is the case if the following conditions are met: 2275 * 1. the prototype has a fixed number of parameters 2276 * 2. no parameter is of type float 2277 * 3. no parameter is converted to another type if integer promotion 2278 * is applied on it 2279 */ 2280 static bool 2281 mnoarg(const type_t *tp, bool *dowarn) 2282 { 2283 sym_t *arg; 2284 tspec_t t; 2285 2286 if (tp->t_vararg) { 2287 if (dowarn != NULL) 2288 *dowarn = true; 2289 } 2290 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) { 2291 if ((t = arg->s_type->t_tspec) == FLOAT || 2292 t == CHAR || t == SCHAR || t == UCHAR || 2293 t == SHORT || t == USHORT) { 2294 if (dowarn != NULL) 2295 *dowarn = true; 2296 } 2297 } 2298 return true; 2299 } 2300 2301 /* 2302 * Compares a prototype declaration with the remembered arguments of 2303 * a previous old style function definition. 2304 */ 2305 static bool 2306 check_old_style_definition(sym_t *rdsym, sym_t *dsym) 2307 { 2308 sym_t *args, *pargs, *arg, *parg; 2309 int narg, nparg, n; 2310 bool dowarn, msg; 2311 2312 args = rdsym->s_args; 2313 pargs = dsym->s_type->t_args; 2314 2315 msg = false; 2316 2317 narg = nparg = 0; 2318 for (arg = args; arg != NULL; arg = arg->s_next) 2319 narg++; 2320 for (parg = pargs; parg != NULL; parg = parg->s_next) 2321 nparg++; 2322 if (narg != nparg) { 2323 /* prototype does not match old-style definition */ 2324 error(63); 2325 msg = true; 2326 goto end; 2327 } 2328 2329 arg = args; 2330 parg = pargs; 2331 n = 1; 2332 while (narg-- > 0) { 2333 dowarn = false; 2334 /* 2335 * If it does not match due to promotion and sflag is 2336 * not set we print only a warning. 2337 */ 2338 if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) || 2339 dowarn) { 2340 /* prototype does not match old style defn., arg #%d */ 2341 error(299, n); 2342 msg = true; 2343 } 2344 arg = arg->s_next; 2345 parg = parg->s_next; 2346 n++; 2347 } 2348 2349 end: 2350 if (msg) 2351 /* old style definition */ 2352 print_previous_declaration(300, rdsym); 2353 2354 return msg; 2355 } 2356 2357 /* 2358 * Completes a type by copying the dimension and prototype information 2359 * from a second compatible type. 2360 * 2361 * Following lines are legal: 2362 * "typedef a[]; a b; a b[10]; a c; a c[20];" 2363 * "typedef ft(); ft f; f(int); ft g; g(long);" 2364 * This means that, if a type is completed, the type structure must 2365 * be duplicated. 2366 */ 2367 void 2368 complete_type(sym_t *dsym, sym_t *ssym) 2369 { 2370 type_t **dstp, *src; 2371 type_t *dst; 2372 2373 dstp = &dsym->s_type; 2374 src = ssym->s_type; 2375 2376 while ((dst = *dstp) != NULL) { 2377 lint_assert(src != NULL); 2378 lint_assert(dst->t_tspec == src->t_tspec); 2379 if (dst->t_tspec == ARRAY) { 2380 if (dst->t_dim == 0 && src->t_dim != 0) { 2381 *dstp = dst = dup_type(dst); 2382 dst->t_dim = src->t_dim; 2383 setcomplete(dst, true); 2384 } 2385 } else if (dst->t_tspec == FUNC) { 2386 if (!dst->t_proto && src->t_proto) { 2387 *dstp = dst = dup_type(dst); 2388 dst->t_proto = true; 2389 dst->t_args = src->t_args; 2390 } 2391 } 2392 dstp = &dst->t_subt; 2393 src = src->t_subt; 2394 } 2395 } 2396 2397 /* 2398 * Completes the declaration of a single argument. 2399 */ 2400 sym_t * 2401 declare_argument(sym_t *sym, bool initflg) 2402 { 2403 tspec_t t; 2404 2405 check_function_definition(sym, true); 2406 2407 check_type(sym); 2408 2409 if (dcs->d_redeclared_symbol != NULL && 2410 dcs->d_redeclared_symbol->s_block_level == block_level) { 2411 /* redeclaration of formal parameter %s */ 2412 error(237, sym->s_name); 2413 rmsym(dcs->d_redeclared_symbol); 2414 sym->s_arg = true; 2415 } 2416 2417 if (!sym->s_arg) { 2418 /* declared argument %s is missing */ 2419 error(53, sym->s_name); 2420 sym->s_arg = true; 2421 } 2422 2423 if (initflg) { 2424 /* cannot initialize parameter: %s */ 2425 error(52, sym->s_name); 2426 } 2427 2428 if ((t = sym->s_type->t_tspec) == ARRAY) { 2429 sym->s_type = derive_type(sym->s_type->t_subt, PTR); 2430 } else if (t == FUNC) { 2431 if (tflag) 2432 /* a function is declared as an argument: %s */ 2433 warning(50, sym->s_name); 2434 sym->s_type = derive_type(sym->s_type, PTR); 2435 } else if (t == FLOAT) { 2436 if (tflag) 2437 sym->s_type = gettyp(DOUBLE); 2438 } 2439 2440 if (dcs->d_inline) 2441 /* argument declared inline: %s */ 2442 warning(269, sym->s_name); 2443 2444 /* 2445 * Arguments must have complete types. lengths() prints the needed 2446 * error messages (null dimension is impossible because arrays are 2447 * converted to pointers). 2448 */ 2449 if (sym->s_type->t_tspec != VOID) 2450 (void)length(sym->s_type, sym->s_name); 2451 2452 sym->s_used = dcs->d_used; 2453 mark_as_set(sym); 2454 2455 return sym; 2456 } 2457 2458 void 2459 check_func_lint_directives(void) 2460 { 2461 sym_t *arg; 2462 int narg, n; 2463 tspec_t t; 2464 2465 /* check for illegal combinations of lint directives */ 2466 if (printflike_argnum != -1 && scanflike_argnum != -1) { 2467 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */ 2468 warning(289); 2469 printflike_argnum = scanflike_argnum = -1; 2470 } 2471 if (nvararg != -1 && 2472 (printflike_argnum != -1 || scanflike_argnum != -1)) { 2473 /* dubious use of ** VARARGS ** with ** %s ** */ 2474 warning(288, 2475 printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE"); 2476 nvararg = -1; 2477 } 2478 2479 /* 2480 * check if the argument of a lint directive is compatible with the 2481 * number of arguments. 2482 */ 2483 narg = 0; 2484 for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next) 2485 narg++; 2486 if (nargusg > narg) { 2487 /* argument number mismatch with directive: ** %s ** */ 2488 warning(283, "ARGSUSED"); 2489 nargusg = 0; 2490 } 2491 if (nvararg > narg) { 2492 /* argument number mismatch with directive: ** %s ** */ 2493 warning(283, "VARARGS"); 2494 nvararg = 0; 2495 } 2496 if (printflike_argnum > narg) { 2497 /* argument number mismatch with directive: ** %s ** */ 2498 warning(283, "PRINTFLIKE"); 2499 printflike_argnum = -1; 2500 } else if (printflike_argnum == 0) { 2501 printflike_argnum = -1; 2502 } 2503 if (scanflike_argnum > narg) { 2504 /* argument number mismatch with directive: ** %s ** */ 2505 warning(283, "SCANFLIKE"); 2506 scanflike_argnum = -1; 2507 } else if (scanflike_argnum == 0) { 2508 scanflike_argnum = -1; 2509 } 2510 if (printflike_argnum != -1 || scanflike_argnum != -1) { 2511 narg = printflike_argnum != -1 2512 ? printflike_argnum : scanflike_argnum; 2513 arg = dcs->d_func_args; 2514 for (n = 1; n < narg; n++) 2515 arg = arg->s_next; 2516 if (arg->s_type->t_tspec != PTR || 2517 ((t = arg->s_type->t_subt->t_tspec) != CHAR && 2518 t != UCHAR && t != SCHAR)) { 2519 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */ 2520 warning(293, narg); 2521 printflike_argnum = scanflike_argnum = -1; 2522 } 2523 } 2524 } 2525 2526 /* 2527 * Warn about arguments in old style function definitions that default to int. 2528 * Check that an old style function definition is compatible to a previous 2529 * prototype. 2530 */ 2531 void 2532 check_func_old_style_arguments(void) 2533 { 2534 sym_t *args, *arg, *pargs, *parg; 2535 int narg, nparg; 2536 bool msg; 2537 2538 args = funcsym->s_args; 2539 pargs = funcsym->s_type->t_args; 2540 2541 /* 2542 * print a warning for each argument of an old style function 2543 * definition which defaults to int 2544 */ 2545 for (arg = args; arg != NULL; arg = arg->s_next) { 2546 if (arg->s_defarg) { 2547 /* argument type defaults to 'int': %s */ 2548 warning(32, arg->s_name); 2549 arg->s_defarg = false; 2550 mark_as_set(arg); 2551 } 2552 } 2553 2554 /* 2555 * If this is an old style function definition and a prototype 2556 * exists, compare the types of arguments. 2557 */ 2558 if (funcsym->s_osdef && funcsym->s_type->t_proto) { 2559 /* 2560 * If the number of arguments does not match, we need not 2561 * continue. 2562 */ 2563 narg = nparg = 0; 2564 msg = false; 2565 for (parg = pargs; parg != NULL; parg = parg->s_next) 2566 nparg++; 2567 for (arg = args; arg != NULL; arg = arg->s_next) 2568 narg++; 2569 if (narg != nparg) { 2570 /* parameter mismatch: %d declared, %d defined */ 2571 error(51, nparg, narg); 2572 msg = true; 2573 } else { 2574 parg = pargs; 2575 arg = args; 2576 while (narg-- > 0) { 2577 msg |= check_prototype_declaration(arg, parg); 2578 parg = parg->s_next; 2579 arg = arg->s_next; 2580 } 2581 } 2582 if (msg) 2583 /* prototype declaration */ 2584 print_previous_declaration(285, 2585 dcs->d_redeclared_symbol); 2586 2587 /* from now on the prototype is valid */ 2588 funcsym->s_osdef = false; 2589 funcsym->s_args = NULL; 2590 } 2591 } 2592 2593 /* 2594 * Checks compatibility of an old style function definition with a previous 2595 * prototype declaration. 2596 * Returns true if the position of the previous declaration should be reported. 2597 */ 2598 static bool 2599 check_prototype_declaration(sym_t *arg, sym_t *parg) 2600 { 2601 type_t *tp, *ptp; 2602 bool dowarn, msg; 2603 2604 tp = arg->s_type; 2605 ptp = parg->s_type; 2606 2607 msg = false; 2608 dowarn = false; 2609 2610 if (!eqtype(tp, ptp, true, true, &dowarn)) { 2611 if (eqtype(tp, ptp, true, false, &dowarn)) { 2612 /* type does not match prototype: %s */ 2613 gnuism(58, arg->s_name); 2614 msg = sflag || !gflag; 2615 } else { 2616 /* type does not match prototype: %s */ 2617 error(58, arg->s_name); 2618 msg = true; 2619 } 2620 } else if (dowarn) { 2621 if (sflag) 2622 /* type does not match prototype: %s */ 2623 error(58, arg->s_name); 2624 else 2625 /* type does not match prototype: %s */ 2626 warning(58, arg->s_name); 2627 msg = true; 2628 } 2629 2630 return msg; 2631 } 2632 2633 /* 2634 * Completes a single local declaration/definition. 2635 */ 2636 void 2637 declare_local(sym_t *dsym, bool initflg) 2638 { 2639 2640 /* Correct a mistake done in declarator_name(). */ 2641 if (dsym->s_type->t_tspec == FUNC) { 2642 dsym->s_def = DECL; 2643 if (dcs->d_scl == NOSCL) 2644 dsym->s_scl = EXTERN; 2645 } 2646 2647 if (dsym->s_type->t_tspec == FUNC) { 2648 if (dsym->s_scl == STATIC) { 2649 /* dubious static function at block level: %s */ 2650 warning(93, dsym->s_name); 2651 dsym->s_scl = EXTERN; 2652 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) { 2653 /* function has illegal storage class: %s */ 2654 error(94, dsym->s_name); 2655 dsym->s_scl = EXTERN; 2656 } 2657 } 2658 2659 /* 2660 * functions may be declared inline at local scope, although 2661 * this has no effect for a later definition of the same 2662 * function. 2663 * XXX it should have an effect if tflag is set. this would 2664 * also be the way gcc behaves. 2665 */ 2666 if (dcs->d_inline) { 2667 if (dsym->s_type->t_tspec == FUNC) { 2668 dsym->s_inline = true; 2669 } else { 2670 /* variable declared inline: %s */ 2671 warning(268, dsym->s_name); 2672 } 2673 } 2674 2675 check_function_definition(dsym, true); 2676 2677 check_type(dsym); 2678 2679 if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN) 2680 declare_external_in_block(dsym); 2681 2682 if (dsym->s_scl == EXTERN) { 2683 /* 2684 * XXX if the static variable at level 0 is only defined 2685 * later, checking will be possible. 2686 */ 2687 if (dsym->s_ext_sym == NULL) { 2688 outsym(dsym, EXTERN, dsym->s_def); 2689 } else { 2690 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def); 2691 } 2692 } 2693 2694 if (dcs->d_redeclared_symbol != NULL) { 2695 2696 if (dcs->d_redeclared_symbol->s_block_level == 0) { 2697 2698 switch (dsym->s_scl) { 2699 case AUTO: 2700 if (hflag) 2701 /* automatic hides external decl.: %s */ 2702 warning(86, dsym->s_name); 2703 break; 2704 case STATIC: 2705 if (hflag) 2706 /* static hides external decl.: %s */ 2707 warning(87, dsym->s_name); 2708 break; 2709 case TYPEDEF: 2710 if (hflag) 2711 /* typedef hides external decl.: %s */ 2712 warning(88, dsym->s_name); 2713 break; 2714 case EXTERN: 2715 /* 2716 * Warnings and errors are printed in 2717 * declare_external_in_block() 2718 */ 2719 break; 2720 default: 2721 lint_assert(/*CONSTCOND*/false); 2722 } 2723 2724 } else if (dcs->d_redeclared_symbol->s_block_level == 2725 block_level) { 2726 2727 /* no hflag, because it's illegal! */ 2728 if (dcs->d_redeclared_symbol->s_arg) { 2729 /* 2730 * if !tflag, a "redeclaration of %s" error 2731 * is produced below 2732 */ 2733 if (tflag) { 2734 if (hflag) 2735 /* decl. hides parameter: %s */ 2736 warning(91, dsym->s_name); 2737 rmsym(dcs->d_redeclared_symbol); 2738 } 2739 } 2740 2741 } else if (dcs->d_redeclared_symbol->s_block_level < 2742 block_level) { 2743 2744 if (hflag) 2745 /* declaration hides earlier one: %s */ 2746 warning(95, dsym->s_name); 2747 2748 } 2749 2750 if (dcs->d_redeclared_symbol->s_block_level == block_level) { 2751 2752 /* redeclaration of %s */ 2753 error(27, dsym->s_name); 2754 rmsym(dcs->d_redeclared_symbol); 2755 2756 } 2757 2758 } 2759 2760 if (initflg && !check_init(dsym)) { 2761 dsym->s_def = DEF; 2762 mark_as_set(dsym); 2763 } 2764 2765 if (dsym->s_scl == TYPEDEF) { 2766 dsym->s_type = dup_type(dsym->s_type); 2767 dsym->s_type->t_typedef = true; 2768 settdsym(dsym->s_type, dsym); 2769 } 2770 2771 /* 2772 * Before we can check the size we must wait for a initialization 2773 * which may follow. 2774 */ 2775 } 2776 2777 /* 2778 * Processes (re)declarations of external symbols inside blocks. 2779 */ 2780 static void 2781 declare_external_in_block(sym_t *dsym) 2782 { 2783 bool eqt, dowarn; 2784 sym_t *esym; 2785 2786 /* look for a symbol with the same name */ 2787 esym = dcs->d_redeclared_symbol; 2788 while (esym != NULL && esym->s_block_level != 0) { 2789 while ((esym = esym->s_link) != NULL) { 2790 if (esym->s_kind != FVFT) 2791 continue; 2792 if (strcmp(dsym->s_name, esym->s_name) == 0) 2793 break; 2794 } 2795 } 2796 if (esym == NULL) 2797 return; 2798 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) { 2799 /* gcc accepts this without a warning, pcc prints an error. */ 2800 /* redeclaration of %s */ 2801 warning(27, dsym->s_name); 2802 print_previous_declaration(-1, esym); 2803 return; 2804 } 2805 2806 dowarn = false; 2807 eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn); 2808 2809 if (!eqt || dowarn) { 2810 if (esym->s_scl == EXTERN) { 2811 /* inconsistent redeclaration of extern: %s */ 2812 warning(90, dsym->s_name); 2813 print_previous_declaration(-1, esym); 2814 } else { 2815 /* inconsistent redeclaration of static: %s */ 2816 warning(92, dsym->s_name); 2817 print_previous_declaration(-1, esym); 2818 } 2819 } 2820 2821 if (eqt) { 2822 /* 2823 * Remember the external symbol so we can update usage 2824 * information at the end of the block. 2825 */ 2826 dsym->s_ext_sym = esym; 2827 } 2828 } 2829 2830 /* 2831 * Print an error or a warning if the symbol cannot be initialized due 2832 * to type/storage class. Return whether an error has been detected. 2833 */ 2834 static bool 2835 check_init(sym_t *sym) 2836 { 2837 bool erred; 2838 2839 erred = false; 2840 2841 if (sym->s_type->t_tspec == FUNC) { 2842 /* cannot initialize function: %s */ 2843 error(24, sym->s_name); 2844 erred = true; 2845 } else if (sym->s_scl == TYPEDEF) { 2846 /* cannot initialize typedef: %s */ 2847 error(25, sym->s_name); 2848 erred = true; 2849 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) { 2850 /* cannot initialize "extern" declaration: %s */ 2851 if (dcs->d_ctx == EXTERN) { 2852 /* cannot initialize extern declaration: %s */ 2853 warning(26, sym->s_name); 2854 } else { 2855 /* cannot initialize extern declaration: %s */ 2856 error(26, sym->s_name); 2857 erred = true; 2858 } 2859 } 2860 2861 return erred; 2862 } 2863 2864 /* 2865 * Create a symbol for an abstract declaration. 2866 */ 2867 sym_t * 2868 abstract_name(void) 2869 { 2870 sym_t *sym; 2871 2872 lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG); 2873 2874 sym = getblk(sizeof(*sym)); 2875 2876 sym->s_name = unnamed; 2877 sym->s_def = DEF; 2878 sym->s_scl = ABSTRACT; 2879 sym->s_block_level = -1; 2880 2881 if (dcs->d_ctx == PROTO_ARG) 2882 sym->s_arg = true; 2883 2884 sym->s_type = dcs->d_type; 2885 dcs->d_redeclared_symbol = NULL; 2886 dcs->d_vararg = false; 2887 2888 return sym; 2889 } 2890 2891 /* 2892 * Removes anything which has nothing to do on global level. 2893 */ 2894 void 2895 global_clean_up(void) 2896 { 2897 2898 while (dcs->d_next != NULL) 2899 end_declaration_level(); 2900 2901 cleanup(); 2902 block_level = 0; 2903 mem_block_level = 0; 2904 2905 /* 2906 * remove all information about pending lint directives without 2907 * warnings. 2908 */ 2909 global_clean_up_decl(true); 2910 } 2911 2912 /* 2913 * Process an abstract type declaration 2914 */ 2915 sym_t * 2916 declare_1_abstract(sym_t *sym) 2917 { 2918 2919 check_function_definition(sym, true); 2920 check_type(sym); 2921 return sym; 2922 } 2923 2924 /* 2925 * Checks size after declarations of variables and their initialization. 2926 */ 2927 void 2928 check_size(sym_t *dsym) 2929 { 2930 2931 if (dsym->s_def != DEF) 2932 return; 2933 if (dsym->s_scl == TYPEDEF) 2934 return; 2935 if (dsym->s_type->t_tspec == FUNC) 2936 return; 2937 2938 if (length(dsym->s_type, dsym->s_name) == 0 && 2939 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) { 2940 if (tflag) { 2941 /* empty array declaration: %s */ 2942 warning(190, dsym->s_name); 2943 } else { 2944 /* empty array declaration: %s */ 2945 error(190, dsym->s_name); 2946 } 2947 } 2948 } 2949 2950 /* 2951 * Mark an object as set if it is not already 2952 */ 2953 void 2954 mark_as_set(sym_t *sym) 2955 { 2956 2957 if (!sym->s_set) { 2958 sym->s_set = true; 2959 UNIQUE_CURR_POS(sym->s_set_pos); 2960 } 2961 } 2962 2963 /* 2964 * Mark an object as used if it is not already 2965 */ 2966 void 2967 mark_as_used(sym_t *sym, bool fcall, bool szof) 2968 { 2969 2970 if (!sym->s_used) { 2971 sym->s_used = true; 2972 UNIQUE_CURR_POS(sym->s_use_pos); 2973 } 2974 /* 2975 * for function calls another record is written 2976 * 2977 * XXX Should symbols used in sizeof() be treated as used or not? 2978 * Probably not, because there is no sense to declare an 2979 * external variable only to get their size. 2980 */ 2981 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN) 2982 outusg(sym); 2983 } 2984 2985 /* 2986 * Prints warnings for a list of variables and labels (concatenated 2987 * with s_dlnxt) if these are not used or only set. 2988 */ 2989 void 2990 check_usage(dinfo_t *di) 2991 { 2992 sym_t *sym; 2993 int mklwarn; 2994 2995 /* for this warning LINTED has no effect */ 2996 mklwarn = lwarn; 2997 lwarn = LWARN_ALL; 2998 2999 #ifdef DEBUG 3000 printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, 3001 lwarn); 3002 #endif 3003 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) 3004 check_usage_sym(di->d_asm, sym); 3005 lwarn = mklwarn; 3006 #ifdef DEBUG 3007 printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line, 3008 lwarn); 3009 #endif 3010 } 3011 3012 /* 3013 * Prints a warning for a single variable or label if it is not used or 3014 * only set. 3015 */ 3016 void 3017 check_usage_sym(bool novar, sym_t *sym) 3018 { 3019 3020 if (sym->s_block_level == -1) 3021 return; 3022 3023 if (sym->s_kind == FVFT && sym->s_arg) 3024 check_argument_usage(novar, sym); 3025 else if (sym->s_kind == FVFT) 3026 check_variable_usage(novar, sym); 3027 else if (sym->s_kind == FLABEL) 3028 check_label_usage(sym); 3029 else if (sym->s_kind == FTAG) 3030 check_tag_usage(sym); 3031 } 3032 3033 static void 3034 check_argument_usage(bool novar, sym_t *arg) 3035 { 3036 3037 lint_assert(arg->s_set); 3038 3039 if (novar) 3040 return; 3041 3042 if (!arg->s_used && vflag) { 3043 /* argument '%s' unused in function '%s' */ 3044 warning_at(231, &arg->s_def_pos, arg->s_name, funcsym->s_name); 3045 } 3046 } 3047 3048 static void 3049 check_variable_usage(bool novar, sym_t *sym) 3050 { 3051 scl_t sc; 3052 sym_t *xsym; 3053 3054 lint_assert(block_level != 0); 3055 lint_assert(sym->s_block_level != 0); 3056 3057 /* errors in expressions easily cause lots of these warnings */ 3058 if (nerr != 0) 3059 return; 3060 3061 /* 3062 * XXX Only variables are checked, although types should 3063 * probably also be checked 3064 */ 3065 if ((sc = sym->s_scl) != EXTERN && sc != STATIC && 3066 sc != AUTO && sc != REG) { 3067 return; 3068 } 3069 3070 if (novar) 3071 return; 3072 3073 if (sc == EXTERN) { 3074 if (!sym->s_used && !sym->s_set) { 3075 /* '%s' unused in function '%s' */ 3076 warning_at(192, &sym->s_def_pos, 3077 sym->s_name, funcsym->s_name); 3078 } 3079 } else { 3080 if (sym->s_set && !sym->s_used) { 3081 /* '%s' set but not used in function '%s' */ 3082 warning_at(191, &sym->s_set_pos, 3083 sym->s_name, funcsym->s_name); 3084 } else if (!sym->s_used) { 3085 /* '%s' unused in function '%s' */ 3086 warning_at(192, &sym->s_def_pos, 3087 sym->s_name, funcsym->s_name); 3088 } 3089 } 3090 3091 if (sc == EXTERN) { 3092 /* 3093 * information about usage is taken over into the symbol 3094 * table entry at level 0 if the symbol was locally declared 3095 * as an external symbol. 3096 * 3097 * XXX This is wrong for symbols declared static at level 0 3098 * if the usage information stems from sizeof(). This is 3099 * because symbols at level 0 only used in sizeof() are 3100 * considered to not be used. 3101 */ 3102 if ((xsym = sym->s_ext_sym) != NULL) { 3103 if (sym->s_used && !xsym->s_used) { 3104 xsym->s_used = true; 3105 xsym->s_use_pos = sym->s_use_pos; 3106 } 3107 if (sym->s_set && !xsym->s_set) { 3108 xsym->s_set = true; 3109 xsym->s_set_pos = sym->s_set_pos; 3110 } 3111 } 3112 } 3113 } 3114 3115 static void 3116 check_label_usage(sym_t *lab) 3117 { 3118 3119 lint_assert(block_level == 1); 3120 lint_assert(lab->s_block_level == 1); 3121 3122 if (lab->s_set && !lab->s_used) { 3123 /* label %s unused in function %s */ 3124 warning_at(232, &lab->s_set_pos, lab->s_name, funcsym->s_name); 3125 } else if (!lab->s_set) { 3126 /* undefined label %s */ 3127 warning_at(23, &lab->s_use_pos, lab->s_name); 3128 } 3129 } 3130 3131 static void 3132 check_tag_usage(sym_t *sym) 3133 { 3134 3135 if (!is_incomplete(sym->s_type)) 3136 return; 3137 3138 /* always complain about incomplete tags declared inside blocks */ 3139 if (!zflag || dcs->d_ctx != EXTERN) 3140 return; 3141 3142 switch (sym->s_type->t_tspec) { 3143 case STRUCT: 3144 /* struct %s never defined */ 3145 warning_at(233, &sym->s_def_pos, sym->s_name); 3146 break; 3147 case UNION: 3148 /* union %s never defined */ 3149 warning_at(234, &sym->s_def_pos, sym->s_name); 3150 break; 3151 case ENUM: 3152 /* enum %s never defined */ 3153 warning_at(235, &sym->s_def_pos, sym->s_name); 3154 break; 3155 default: 3156 lint_assert(/*CONSTCOND*/false); 3157 } 3158 } 3159 3160 /* 3161 * Called after the entire translation unit has been parsed. 3162 * Changes tentative definitions into definitions. 3163 * Performs some tests on global symbols. Detected problems are: 3164 * - defined variables of incomplete type 3165 * - constant variables which are not initialized 3166 * - static symbols which are never used 3167 */ 3168 void 3169 check_global_symbols(void) 3170 { 3171 sym_t *sym; 3172 3173 if (block_level != 0 || dcs->d_next != NULL) 3174 norecover(); 3175 3176 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 3177 if (sym->s_block_level == -1) 3178 continue; 3179 if (sym->s_kind == FVFT) { 3180 check_global_variable(sym); 3181 } else if (sym->s_kind == FTAG) { 3182 check_tag_usage(sym); 3183 } else { 3184 lint_assert(sym->s_kind == FMEMBER); 3185 } 3186 } 3187 } 3188 3189 static void 3190 check_unused_static_global_variable(const sym_t *sym) 3191 { 3192 if (sym->s_type->t_tspec == FUNC) { 3193 if (sym->s_def == DEF) { 3194 if (!sym->s_inline) 3195 /* static function %s unused */ 3196 warning_at(236, &sym->s_def_pos, sym->s_name); 3197 } else { 3198 /* static function %s declared but not defined */ 3199 warning_at(290, &sym->s_def_pos, sym->s_name); 3200 } 3201 } else if (!sym->s_set) { 3202 /* static variable %s unused */ 3203 warning_at(226, &sym->s_def_pos, sym->s_name); 3204 } else { 3205 /* static variable %s set but not used */ 3206 warning_at(307, &sym->s_def_pos, sym->s_name); 3207 } 3208 } 3209 3210 static void 3211 check_static_global_variable(const sym_t *sym) 3212 { 3213 if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) { 3214 /* static function called but not defined: %s() */ 3215 error_at(225, &sym->s_use_pos, sym->s_name); 3216 } 3217 3218 if (!sym->s_used) 3219 check_unused_static_global_variable(sym); 3220 3221 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) { 3222 /* const object %s should have initializer */ 3223 warning_at(227, &sym->s_def_pos, sym->s_name); 3224 } 3225 } 3226 3227 static void 3228 check_global_variable(const sym_t *sym) 3229 { 3230 3231 if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST) 3232 return; 3233 3234 if (sym->s_scl == NOSCL) 3235 return; /* May be caused by a syntax error. */ 3236 3237 lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC); 3238 3239 check_global_variable_size(sym); 3240 3241 if (sym->s_scl == STATIC) 3242 check_static_global_variable(sym); 3243 } 3244 3245 static void 3246 check_global_variable_size(const sym_t *sym) 3247 { 3248 pos_t cpos; 3249 int length_in_bits; 3250 3251 if (sym->s_def != TDEF) 3252 return; 3253 if (sym->s_type->t_tspec == FUNC) 3254 /* 3255 * this can happen if a syntax error occurred after a 3256 * function declaration 3257 */ 3258 return; 3259 3260 cpos = curr_pos; 3261 curr_pos = sym->s_def_pos; 3262 length_in_bits = length(sym->s_type, sym->s_name); 3263 curr_pos = cpos; 3264 3265 if (length_in_bits == 0 && 3266 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) { 3267 if (tflag || (sym->s_scl == EXTERN && !sflag)) { 3268 /* empty array declaration: %s */ 3269 warning_at(190, &sym->s_def_pos, sym->s_name); 3270 } else { 3271 /* empty array declaration: %s */ 3272 error_at(190, &sym->s_def_pos, sym->s_name); 3273 } 3274 } 3275 } 3276 3277 /* 3278 * Prints information about location of previous definition/declaration. 3279 */ 3280 void 3281 print_previous_declaration(int msg, const sym_t *psym) 3282 { 3283 3284 if (!rflag) 3285 return; 3286 3287 if (msg != -1) { 3288 (message_at)(msg, &psym->s_def_pos); 3289 } else if (psym->s_def == DEF || psym->s_def == TDEF) { 3290 /* previous definition of %s */ 3291 message_at(261, &psym->s_def_pos, psym->s_name); 3292 } else { 3293 /* previous declaration of %s */ 3294 message_at(260, &psym->s_def_pos, psym->s_name); 3295 } 3296 } 3297 3298 /* 3299 * Gets a node for a constant and returns the value of this constant 3300 * as integer. 3301 * 3302 * If the node is not constant or too large for int or of type float, 3303 * a warning will be printed. 3304 * 3305 * to_int_constant() should be used only inside declarations. If it is used in 3306 * expressions, it frees the memory used for the expression. 3307 */ 3308 int 3309 to_int_constant(tnode_t *tn, bool required) 3310 { 3311 int i; 3312 tspec_t t; 3313 val_t *v; 3314 3315 v = constant(tn, required); 3316 3317 if (tn == NULL) { 3318 i = 1; 3319 goto done; 3320 } 3321 3322 /* 3323 * Abstract declarations are used inside expression. To free 3324 * the memory would be a fatal error. 3325 * We don't free blocks that are inside casts because these 3326 * will be used later to match types. 3327 */ 3328 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT) 3329 expr_free_all(); 3330 3331 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) { 3332 i = (int)v->v_ldbl; 3333 /* integral constant expression expected */ 3334 error(55); 3335 } else { 3336 i = (int)v->v_quad; 3337 if (is_uinteger(t)) { 3338 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) { 3339 /* integral constant too large */ 3340 warning(56); 3341 } 3342 } else { 3343 if (v->v_quad > (int64_t)TARG_INT_MAX || 3344 v->v_quad < (int64_t)TARG_INT_MIN) { 3345 /* integral constant too large */ 3346 warning(56); 3347 } 3348 } 3349 } 3350 3351 done: 3352 free(v); 3353 return i; 3354 } 3355