Thu Jul 15 23:42:50 2021 UTC ()
lint: replace switch statement in dcs_merge_declaration_specifiers

Grouping the rules by their abstract type took a lot of visual space.
Instead, move each of the rules from C11 6.7.2 into its own if
statement, so that the rules almost read like in the standard.

No functional change.


(rillig)
diff -r1.204 -r1.205 src/usr.bin/xlint/lint1/decl.c

cvs diff -r1.204 -r1.205 src/usr.bin/xlint/lint1/decl.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/decl.c 2021/07/15 23:07:05 1.204
+++ src/usr.bin/xlint/lint1/decl.c 2021/07/15 23:42:49 1.205
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: decl.c,v 1.204 2021/07/15 23:07:05 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: decl.c,v 1.204 2021/07/15 23:07:05 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $");
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51const char *unnamed = "<unnamed>"; 51const char *unnamed = "<unnamed>";
52 52
53/* shared type structures for arithmetic types and void */ 53/* shared type structures for arithmetic types and void */
54static type_t *typetab; 54static type_t *typetab;
@@ -736,27 +736,31 @@ dcs_adjust_storage_class(void) @@ -736,27 +736,31 @@ dcs_adjust_storage_class(void)
736 /* illegal storage class */ 736 /* illegal storage class */
737 error(8); 737 error(8);
738 dcs->d_scl = NOSCL; 738 dcs->d_scl = NOSCL;
739 } 739 }
740 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) { 740 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
741 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) { 741 if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
742 /* only register valid as formal parameter storage... */ 742 /* only register valid as formal parameter storage... */
743 error(9); 743 error(9);
744 dcs->d_scl = NOSCL; 744 dcs->d_scl = NOSCL;
745 } 745 }
746 } 746 }
747} 747}
748 748
749/* Merge the declaration specifiers from dcs into dcs->d_type. */ 749/*
 750 * Merge the declaration specifiers from dcs into dcs->d_type.
 751 *
 752 * See C99 6.7.2 "Type specifiers".
 753 */
750static void 754static void
751dcs_merge_declaration_specifiers(void) 755dcs_merge_declaration_specifiers(void)
752{ 756{
753 tspec_t t, s, l, c; 757 tspec_t t, s, l, c;
754 type_t *tp; 758 type_t *tp;
755 759
756 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */ 760 t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
757 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */ 761 c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
758 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */ 762 s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
759 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */ 763 l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
760 tp = dcs->d_type; 764 tp = dcs->d_type;
761 765
762#ifdef DEBUG 766#ifdef DEBUG
@@ -766,75 +770,54 @@ dcs_merge_declaration_specifiers(void) @@ -766,75 +770,54 @@ dcs_merge_declaration_specifiers(void)
766 tp == NULL) 770 tp == NULL)
767 dcs->d_notyp = true; 771 dcs->d_notyp = true;
768 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && 772 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
769 tp == NULL) 773 tp == NULL)
770 t = c; 774 t = c;
771 775
772 if (tp != NULL) { 776 if (tp != NULL) {
773 lint_assert(t == NOTSPEC); 777 lint_assert(t == NOTSPEC);
774 lint_assert(s == NOTSPEC); 778 lint_assert(s == NOTSPEC);
775 lint_assert(l == NOTSPEC); 779 lint_assert(l == NOTSPEC);
776 return; 780 return;
777 } 781 }
778 782
779 switch (t) { 783 if (t == NOTSPEC)
780 case BOOL: 
781 break; 
782 case NOTSPEC: 
783 t = INT; 784 t = INT;
784 /* FALLTHROUGH */ 785 if (s == NOTSPEC && t == INT)
785 case INT: 786 s = SIGNED;
786 if (s == NOTSPEC) 787 if (l != NOTSPEC && t == CHAR) {
787 s = SIGNED; 788 dcs->d_terr = true;
788 break; 789 l = NOTSPEC;
789 case CHAR: 790 }
790 if (l != NOTSPEC) { 791 if (l == LONG && t == FLOAT) {
791 dcs->d_terr = true; 792 l = NOTSPEC;
792 l = NOTSPEC; 793 t = DOUBLE;
793 } 794 if (!tflag)
794 break; 795 /* use 'double' instead of 'long float' */
795 case FLOAT: 796 warning(6);
796 if (l == LONG) { 797 }
797 l = NOTSPEC; 798 if ((l == LONG && t == DOUBLE) || t == LDOUBLE) {
798 t = DOUBLE; 
799 if (!tflag) 
800 /* use 'double' instead of 'long float' */ 
801 warning(6); 
802 } 
803 break; 
804 case DOUBLE: 
805 if (l != LONG) 
806 break; 
807 /* FALLTHROUGH */ 
808 case LDOUBLE: 
809 l = NOTSPEC; 799 l = NOTSPEC;
810 t = LDOUBLE; 800 t = LDOUBLE;
811 if (tflag) 
812 /* 'long double' is illegal in traditional C */ 
813 warning(266); 
814 break; 
815 case DCOMPLEX: 
816 if (l == LONG) { 
817 l = NOTSPEC; 
818 t = LCOMPLEX; 
819 } 
820 break; 
821 case VOID: 
822 case FCOMPLEX: 
823 case LCOMPLEX: 
824 break; 
825 default: 
826 lint_assert(is_integer(t)); 
827 } 801 }
 802 if (t == LDOUBLE && tflag) {
 803 /* 'long double' is illegal in traditional C */
 804 warning(266);
 805 }
 806 if (l == LONG && t == DCOMPLEX) {
 807 l = NOTSPEC;
 808 t = LCOMPLEX;
 809 }
 810
828 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 811 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
829 dcs->d_terr = true; 812 dcs->d_terr = true;
830 l = s = NOTSPEC; 813 l = s = NOTSPEC;
831 } 814 }
832 if (l != NOTSPEC) 815 if (l != NOTSPEC)
833 t = l; 816 t = l;
834 dcs->d_type = gettyp(merge_type_specifiers(t, s)); 817 dcs->d_type = gettyp(merge_type_specifiers(t, s));
835} 818}
836 819
837/* 820/*
838 * Create a type structure from the information gathered in 821 * Create a type structure from the information gathered in
839 * the declaration stack. 822 * the declaration stack.
840 * Complain about storage classes which are not possible in current 823 * Complain about storage classes which are not possible in current