| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: func.c,v 1.41 2021/01/01 09:11:40 rillig Exp $ */ | | 1 | /* $NetBSD: func.c,v 1.42 2021/01/01 10:55:28 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1994, 1995 Jochen Pohl | | 4 | * Copyright (c) 1994, 1995 Jochen Pohl |
5 | * All Rights Reserved. | | 5 | * All Rights Reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -27,27 +27,27 @@ | | | @@ -27,27 +27,27 @@ |
27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | */ | | 32 | */ |
33 | | | 33 | |
34 | #if HAVE_NBTOOL_CONFIG_H | | 34 | #if HAVE_NBTOOL_CONFIG_H |
35 | #include "nbtool_config.h" | | 35 | #include "nbtool_config.h" |
36 | #endif | | 36 | #endif |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | #if defined(__RCSID) && !defined(lint) | | 39 | #if defined(__RCSID) && !defined(lint) |
40 | __RCSID("$NetBSD: func.c,v 1.41 2021/01/01 09:11:40 rillig Exp $"); | | 40 | __RCSID("$NetBSD: func.c,v 1.42 2021/01/01 10:55:28 rillig Exp $"); |
41 | #endif | | 41 | #endif |
42 | | | 42 | |
43 | #include <stdlib.h> | | 43 | #include <stdlib.h> |
44 | #include <string.h> | | 44 | #include <string.h> |
45 | | | 45 | |
46 | #include "lint1.h" | | 46 | #include "lint1.h" |
47 | #include "cgram.h" | | 47 | #include "cgram.h" |
48 | | | 48 | |
49 | /* | | 49 | /* |
50 | * Contains a pointer to the symbol table entry of the current function | | 50 | * Contains a pointer to the symbol table entry of the current function |
51 | * definition. | | 51 | * definition. |
52 | */ | | 52 | */ |
53 | sym_t *funcsym; | | 53 | sym_t *funcsym; |
| @@ -389,147 +389,145 @@ funcend(void) | | | @@ -389,147 +389,145 @@ funcend(void) |
389 | | | 389 | |
390 | /* | | 390 | /* |
391 | * remove all symbols declared during argument declaration from | | 391 | * remove all symbols declared during argument declaration from |
392 | * the symbol table | | 392 | * the symbol table |
393 | */ | | 393 | */ |
394 | lint_assert(dcs->d_next == NULL); | | 394 | lint_assert(dcs->d_next == NULL); |
395 | lint_assert(dcs->d_ctx == EXTERN); | | 395 | lint_assert(dcs->d_ctx == EXTERN); |
396 | rmsyms(dcs->d_fpsyms); | | 396 | rmsyms(dcs->d_fpsyms); |
397 | | | 397 | |
398 | /* must be set on level 0 */ | | 398 | /* must be set on level 0 */ |
399 | reached = 1; | | 399 | reached = 1; |
400 | } | | 400 | } |
401 | | | 401 | |
402 | /* | | | |
403 | * Process a label. | | | |
404 | * | | | |
405 | * typ type of the label (T_NAME, T_DEFAULT or T_CASE). | | | |
406 | * sym symbol table entry of label if typ == T_NAME | | | |
407 | * tn expression if typ == T_CASE | | | |
408 | */ | | | |
409 | void | | 402 | void |
410 | label(int typ, sym_t *sym, tnode_t *tn) | | 403 | named_label(sym_t *sym) |
| | | 404 | { |
| | | 405 | |
| | | 406 | if (sym->s_set) { |
| | | 407 | /* label %s redefined */ |
| | | 408 | error(194, sym->s_name); |
| | | 409 | } else { |
| | | 410 | mark_as_set(sym); |
| | | 411 | } |
| | | 412 | |
| | | 413 | reached = 1; |
| | | 414 | } |
| | | 415 | |
| | | 416 | void |
| | | 417 | case_label(tnode_t *tn) |
411 | { | | 418 | { |
412 | cstk_t *ci; | | 419 | cstk_t *ci; |
413 | clst_t *cl; | | 420 | clst_t *cl; |
414 | val_t *v; | | 421 | val_t *v; |
415 | val_t nv; | | 422 | val_t nv; |
416 | tspec_t t; | | 423 | tspec_t t; |
417 | | | 424 | |
418 | switch (typ) { | | 425 | /* find the stack entry for the innermost switch statement */ |
419 | | | 426 | for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) |
420 | case T_NAME: | | 427 | continue; |
421 | if (sym->s_set) { | | | |
422 | /* label %s redefined */ | | | |
423 | error(194, sym->s_name); | | | |
424 | } else { | | | |
425 | mark_as_set(sym); | | | |
426 | } | | | |
427 | break; | | | |
428 | | | | |
429 | case T_CASE: | | | |
430 | | | 428 | |
431 | /* find the stack entry for the innermost switch statement */ | | 429 | if (ci == NULL) { |
432 | for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) | | 430 | /* case not in switch */ |
433 | continue; | | 431 | error(195); |
434 | | | 432 | tn = NULL; |
435 | if (ci == NULL) { | | 433 | } else if (tn != NULL && tn->tn_op != CON) { |
436 | /* case not in switch */ | | 434 | /* non-constant case expression */ |
437 | error(195); | | 435 | error(197); |
438 | tn = NULL; | | 436 | tn = NULL; |
439 | } else if (tn != NULL && tn->tn_op != CON) { | | 437 | } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) { |
440 | /* non-constant case expression */ | | 438 | /* non-integral case expression */ |
441 | error(197); | | 439 | error(198); |
442 | tn = NULL; | | 440 | tn = NULL; |
443 | } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) { | | 441 | } |
444 | /* non-integral case expression */ | | | |
445 | error(198); | | | |
446 | tn = NULL; | | | |
447 | } | | | |
448 | | | 442 | |
449 | if (tn != NULL) { | | 443 | if (tn != NULL) { |
450 | | | 444 | |
451 | lint_assert(ci->c_swtype != NULL); | | 445 | lint_assert(ci->c_swtype != NULL); |
452 | | | 446 | |
453 | if (reached && !ftflg) { | | 447 | if (reached && !ftflg) { |
454 | if (hflag) | | 448 | if (hflag) |
455 | /* fallthrough on case statement */ | | 449 | /* fallthrough on case statement */ |
456 | warning(220); | | 450 | warning(220); |
457 | } | | 451 | } |
458 | | | 452 | |
459 | t = tn->tn_type->t_tspec; | | 453 | t = tn->tn_type->t_tspec; |
460 | if (t == LONG || t == ULONG || | | 454 | if (t == LONG || t == ULONG || |
461 | t == QUAD || t == UQUAD) { | | 455 | t == QUAD || t == UQUAD) { |
462 | if (tflag) | | 456 | if (tflag) |
463 | /* case label must be of type ... */ | | 457 | /* case label must be of type ... */ |
464 | warning(203); | | 458 | warning(203); |
465 | } | | 459 | } |
466 | | | 460 | |
| | | 461 | /* |
| | | 462 | * get the value of the expression and convert it |
| | | 463 | * to the type of the switch expression |
| | | 464 | */ |
| | | 465 | v = constant(tn, 1); |
| | | 466 | (void) memset(&nv, 0, sizeof nv); |
| | | 467 | cvtcon(CASE, 0, ci->c_swtype, &nv, v); |
| | | 468 | free(v); |
| | | 469 | |
| | | 470 | /* look if we had this value already */ |
| | | 471 | for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) { |
| | | 472 | if (cl->cl_val.v_quad == nv.v_quad) |
| | | 473 | break; |
| | | 474 | } |
| | | 475 | if (cl != NULL && tspec_is_uint(nv.v_tspec)) { |
| | | 476 | /* duplicate case in switch: %lu */ |
| | | 477 | error(200, (u_long)nv.v_quad); |
| | | 478 | } else if (cl != NULL) { |
| | | 479 | /* duplicate case in switch: %ld */ |
| | | 480 | error(199, (long)nv.v_quad); |
| | | 481 | } else { |
467 | /* | | 482 | /* |
468 | * get the value of the expression and convert it | | 483 | * append the value to the list of |
469 | * to the type of the switch expression | | 484 | * case values |
470 | */ | | 485 | */ |
471 | v = constant(tn, 1); | | 486 | cl = xcalloc(1, sizeof (clst_t)); |
472 | (void) memset(&nv, 0, sizeof nv); | | 487 | cl->cl_val = nv; |
473 | cvtcon(CASE, 0, ci->c_swtype, &nv, v); | | 488 | cl->cl_next = ci->c_clst; |
474 | free(v); | | 489 | ci->c_clst = cl; |
475 | | | | |
476 | /* look if we had this value already */ | | | |
477 | for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) { | | | |
478 | if (cl->cl_val.v_quad == nv.v_quad) | | | |
479 | break; | | | |
480 | } | | | |
481 | if (cl != NULL && tspec_is_uint(nv.v_tspec)) { | | | |
482 | /* duplicate case in switch: %lu */ | | | |
483 | error(200, (u_long)nv.v_quad); | | | |
484 | } else if (cl != NULL) { | | | |
485 | /* duplicate case in switch: %ld */ | | | |
486 | error(199, (long)nv.v_quad); | | | |
487 | } else { | | | |
488 | /* | | | |
489 | * append the value to the list of | | | |
490 | * case values | | | |
491 | */ | | | |
492 | cl = xcalloc(1, sizeof (clst_t)); | | | |
493 | cl->cl_val = nv; | | | |
494 | cl->cl_next = ci->c_clst; | | | |
495 | ci->c_clst = cl; | | | |
496 | } | | | |
497 | } | | 490 | } |
498 | tfreeblk(); | | 491 | } |
499 | break; | | 492 | tfreeblk(); |
| | | 493 | |
| | | 494 | reached = 1; |
| | | 495 | } |
500 | | | 496 | |
501 | case T_DEFAULT: | | 497 | void |
| | | 498 | default_label(void) |
| | | 499 | { |
| | | 500 | cstk_t *ci; |
502 | | | 501 | |
503 | /* find the stack entry for the innermost switch statement */ | | 502 | /* find the stack entry for the innermost switch statement */ |
504 | for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) | | 503 | for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next) |
505 | continue; | | 504 | continue; |
506 | | | 505 | |
507 | if (ci == NULL) { | | 506 | if (ci == NULL) { |
508 | /* default outside switch */ | | 507 | /* default outside switch */ |
509 | error(201); | | 508 | error(201); |
510 | } else if (ci->c_default) { | | 509 | } else if (ci->c_default) { |
511 | /* duplicate default in switch */ | | 510 | /* duplicate default in switch */ |
512 | error(202); | | 511 | error(202); |
513 | } else { | | 512 | } else { |
514 | if (reached && !ftflg) { | | 513 | if (reached && !ftflg) { |
515 | if (hflag) | | 514 | if (hflag) |
516 | /* fallthrough on default statement */ | | 515 | /* fallthrough on default statement */ |
517 | warning(284); | | 516 | warning(284); |
518 | } | | | |
519 | ci->c_default = 1; | | | |
520 | } | | 517 | } |
521 | break; | | 518 | ci->c_default = 1; |
522 | }; | | 519 | } |
| | | 520 | |
523 | reached = 1; | | 521 | reached = 1; |
524 | } | | 522 | } |
525 | | | 523 | |
526 | static tnode_t * | | 524 | static tnode_t * |
527 | check_controlling_expression(tnode_t *tn) | | 525 | check_controlling_expression(tnode_t *tn) |
528 | { | | 526 | { |
529 | tspec_t t = tn->tn_type->t_tspec; | | 527 | tspec_t t = tn->tn_type->t_tspec; |
530 | | | 528 | |
531 | if (tn != NULL) | | 529 | if (tn != NULL) |
532 | tn = cconv(tn); | | 530 | tn = cconv(tn); |
533 | if (tn != NULL) | | 531 | if (tn != NULL) |
534 | tn = promote(NOOP, 0, tn); | | 532 | tn = promote(NOOP, 0, tn); |
535 | | | 533 | |