Fri Mar 26 17:44:52 2021 UTC ()
lint: rename pushdecl and popdecl to be more expressive

The previous names were highly ambiguous.  The 'decl' could have meant
'declaration', which would be the usual abbreviation.  It could also be
split into 'dec' and 'l', meaning 'declaration level', which would make
more sense in this particular context.

To avoid having to guess anything about these names, rename the
functions.  Instead of 'push' and 'pop', I renamed them to 'begin' and
'end' since these are the high-level operation that are of interest.
That the hierarchy of declaration levels is implemented as a stack is
nice to know but not as important to understand the whole situation.

No functional change.


(rillig)
diff -r1.201 -r1.202 src/usr.bin/xlint/lint1/cgram.y
diff -r1.159 -r1.160 src/usr.bin/xlint/lint1/decl.c
diff -r1.85 -r1.86 src/usr.bin/xlint/lint1/externs1.h

cvs diff -r1.201 -r1.202 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2021/03/26 16:05:19 1.201
+++ src/usr.bin/xlint/lint1/cgram.y 2021/03/26 17:44:52 1.202
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.201 2021/03/26 16:05:19 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.202 2021/03/26 17:44:52 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.201 2021/03/26 16:05:19 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.202 2021/03/26 17:44:52 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48 48
49/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -429,31 +429,31 @@ function_definition: /* C99 6.9.1 */ @@ -429,31 +429,31 @@ function_definition: /* C99 6.9.1 */
429 func_decl { 429 func_decl {
430 if ($1->s_type->t_tspec != FUNC) { 430 if ($1->s_type->t_tspec != FUNC) {
431 /* syntax error '%s' */ 431 /* syntax error '%s' */
432 error(249, yytext); 432 error(249, yytext);
433 YYERROR; 433 YYERROR;
434 } 434 }
435 if ($1->s_type->t_typedef) { 435 if ($1->s_type->t_typedef) {
436 /* ()-less function definition */ 436 /* ()-less function definition */
437 error(64); 437 error(64);
438 YYERROR; 438 YYERROR;
439 } 439 }
440 funcdef($1); 440 funcdef($1);
441 block_level++; 441 block_level++;
442 pushdecl(ARG); 442 begin_declaration_level(ARG);
443 if (lwarn == LWARN_NONE) 443 if (lwarn == LWARN_NONE)
444 $1->s_used = true; 444 $1->s_used = true;
445 } arg_declaration_list_opt { 445 } arg_declaration_list_opt {
446 popdecl(); 446 end_declaration_level();
447 block_level--; 447 block_level--;
448 check_func_lint_directives(); 448 check_func_lint_directives();
449 check_func_old_style_arguments(); 449 check_func_old_style_arguments();
450 pushctrl(0); 450 pushctrl(0);
451 } compound_statement { 451 } compound_statement {
452 funcend(); 452 funcend();
453 popctrl(0); 453 popctrl(0);
454 } 454 }
455 ; 455 ;
456 456
457func_decl: 457func_decl:
458 clrtyp deftyp notype_decl { 458 clrtyp deftyp notype_decl {
459 $$ = $3; 459 $$ = $3;
@@ -693,31 +693,31 @@ typespec: @@ -693,31 +693,31 @@ typespec:
693 | T_TYPENAME { 693 | T_TYPENAME {
694 $$ = getsym($1)->s_type; 694 $$ = getsym($1)->s_type;
695 } 695 }
696 ; 696 ;
697 697
698notype_typespec: 698notype_typespec:
699 T_TYPE { 699 T_TYPE {
700 $$ = gettyp($1); 700 $$ = gettyp($1);
701 } 701 }
702 | T_TYPEOF term { 702 | T_TYPEOF term {
703 $$ = $2->tn_type; 703 $$ = $2->tn_type;
704 } 704 }
705 | struct_spec { 705 | struct_spec {
706 popdecl(); 706 end_declaration_level();
707 $$ = $1; 707 $$ = $1;
708 } 708 }
709 | enum_spec { 709 | enum_spec {
710 popdecl(); 710 end_declaration_level();
711 $$ = $1; 711 $$ = $1;
712 } 712 }
713 ; 713 ;
714 714
715struct_spec: 715struct_spec:
716 struct struct_tag { 716 struct struct_tag {
717 /* 717 /*
718 * STDC requires that "struct a;" always introduces 718 * STDC requires that "struct a;" always introduces
719 * a new tag if "a" is not declared at current level 719 * a new tag if "a" is not declared at current level
720 * 720 *
721 * yychar is valid because otherwise the parser would not 721 * yychar is valid because otherwise the parser would not
722 * have been able to decide if it must shift or reduce 722 * have been able to decide if it must shift or reduce
723 */ 723 */
@@ -733,27 +733,27 @@ struct_spec: @@ -733,27 +733,27 @@ struct_spec:
733 } struct_declaration { 733 } struct_declaration {
734 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3); 734 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3);
735 } 735 }
736 | struct error { 736 | struct error {
737 symtyp = FVFT; 737 symtyp = FVFT;
738 $$ = gettyp(INT); 738 $$ = gettyp(INT);
739 } 739 }
740 ; 740 ;
741 741
742struct: 742struct:
743 struct type_attribute 743 struct type_attribute
744 | T_STRUCT_OR_UNION { 744 | T_STRUCT_OR_UNION {
745 symtyp = FTAG; 745 symtyp = FTAG;
746 pushdecl($1 == STRUCT ? MOS : MOU); 746 begin_declaration_level($1 == STRUCT ? MOS : MOU);
747 dcs->d_offset = 0; 747 dcs->d_offset = 0;
748 dcs->d_stralign = CHAR_SIZE; 748 dcs->d_stralign = CHAR_SIZE;
749 $$ = $1; 749 $$ = $1;
750 } 750 }
751 ; 751 ;
752 752
753struct_tag: 753struct_tag:
754 identifier { 754 identifier {
755 $$ = getsym($1); 755 $$ = getsym($1);
756 } 756 }
757 ; 757 ;
758 758
759struct_declaration: 759struct_declaration:
@@ -930,27 +930,27 @@ enum_spec: @@ -930,27 +930,27 @@ enum_spec:
930 dcs->d_tagtyp = mktag(NULL, ENUM, true, false); 930 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
931 } enum_declaration { 931 } enum_declaration {
932 $$ = complete_tag_enum(dcs->d_tagtyp, $3); 932 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
933 } 933 }
934 | enum error { 934 | enum error {
935 symtyp = FVFT; 935 symtyp = FVFT;
936 $$ = gettyp(INT); 936 $$ = gettyp(INT);
937 } 937 }
938 ; 938 ;
939 939
940enum: 940enum:
941 T_ENUM { 941 T_ENUM {
942 symtyp = FTAG; 942 symtyp = FTAG;
943 pushdecl(CTCONST); 943 begin_declaration_level(CTCONST);
944 } 944 }
945 ; 945 ;
946 946
947enum_tag: 947enum_tag:
948 identifier { 948 identifier {
949 $$ = getsym($1); 949 $$ = getsym($1);
950 } 950 }
951 ; 951 ;
952 952
953enum_declaration: 953enum_declaration:
954 enum_decl_lbrace enums_with_opt_comma T_RBRACE { 954 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
955 $$ = $2; 955 $$ = $2;
956 } 956 }
@@ -1062,27 +1062,27 @@ notype_direct_decl: @@ -1062,27 +1062,27 @@ notype_direct_decl:
1062 $$ = $2; 1062 $$ = $2;
1063 } 1063 }
1064 | type_attribute notype_direct_decl { 1064 | type_attribute notype_direct_decl {
1065 $$ = $2; 1065 $$ = $2;
1066 } 1066 }
1067 | notype_direct_decl T_LBRACK T_RBRACK { 1067 | notype_direct_decl T_LBRACK T_RBRACK {
1068 $$ = add_array($1, false, 0); 1068 $$ = add_array($1, false, 0);
1069 } 1069 }
1070 | notype_direct_decl T_LBRACK constant_expr T_RBRACK { 1070 | notype_direct_decl T_LBRACK constant_expr T_RBRACK {
1071 $$ = add_array($1, true, to_int_constant($3, false)); 1071 $$ = add_array($1, true, to_int_constant($3, false));
1072 } 1072 }
1073 | notype_direct_decl param_list opt_asm_or_symbolrename { 1073 | notype_direct_decl param_list opt_asm_or_symbolrename {
1074 $$ = add_function(symbolrename($1, $3), $2); 1074 $$ = add_function(symbolrename($1, $3), $2);
1075 popdecl(); 1075 end_declaration_level();
1076 block_level--; 1076 block_level--;
1077 } 1077 }
1078 | notype_direct_decl type_attribute_list 1078 | notype_direct_decl type_attribute_list
1079 ; 1079 ;
1080 1080
1081type_decl: 1081type_decl:
1082 type_direct_decl { 1082 type_direct_decl {
1083 $$ = $1; 1083 $$ = $1;
1084 } 1084 }
1085 | pointer type_direct_decl { 1085 | pointer type_direct_decl {
1086 $$ = add_pointer($2, $1); 1086 $$ = add_pointer($2, $1);
1087 } 1087 }
1088 ; 1088 ;
@@ -1095,27 +1095,27 @@ type_direct_decl: @@ -1095,27 +1095,27 @@ type_direct_decl:
1095 $$ = $2; 1095 $$ = $2;
1096 } 1096 }
1097 | type_attribute type_direct_decl { 1097 | type_attribute type_direct_decl {
1098 $$ = $2; 1098 $$ = $2;
1099 } 1099 }
1100 | type_direct_decl T_LBRACK T_RBRACK { 1100 | type_direct_decl T_LBRACK T_RBRACK {
1101 $$ = add_array($1, false, 0); 1101 $$ = add_array($1, false, 0);
1102 } 1102 }
1103 | type_direct_decl T_LBRACK constant_expr T_RBRACK { 1103 | type_direct_decl T_LBRACK constant_expr T_RBRACK {
1104 $$ = add_array($1, true, to_int_constant($3, false)); 1104 $$ = add_array($1, true, to_int_constant($3, false));
1105 } 1105 }
1106 | type_direct_decl param_list opt_asm_or_symbolrename { 1106 | type_direct_decl param_list opt_asm_or_symbolrename {
1107 $$ = add_function(symbolrename($1, $3), $2); 1107 $$ = add_function(symbolrename($1, $3), $2);
1108 popdecl(); 1108 end_declaration_level();
1109 block_level--; 1109 block_level--;
1110 } 1110 }
1111 | type_direct_decl type_attribute_list 1111 | type_direct_decl type_attribute_list
1112 ; 1112 ;
1113 1113
1114/* 1114/*
1115 * param_decl and notype_param_decl exist to avoid a conflict in 1115 * param_decl and notype_param_decl exist to avoid a conflict in
1116 * argument lists. A typename enclosed in parens should always be 1116 * argument lists. A typename enclosed in parens should always be
1117 * treated as a typename, not an argument. 1117 * treated as a typename, not an argument.
1118 * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));" 1118 * "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
1119 * not "typedef int a; f(int a);" 1119 * not "typedef int a; f(int a);"
1120 */ 1120 */
1121param_decl: 1121param_decl:
@@ -1135,27 +1135,27 @@ direct_param_decl: @@ -1135,27 +1135,27 @@ direct_param_decl:
1135 $$ = declarator_name(getsym($1)); 1135 $$ = declarator_name(getsym($1));
1136 } 1136 }
1137 | T_LPAREN notype_param_decl T_RPAREN { 1137 | T_LPAREN notype_param_decl T_RPAREN {
1138 $$ = $2; 1138 $$ = $2;
1139 } 1139 }
1140 | direct_param_decl T_LBRACK T_RBRACK { 1140 | direct_param_decl T_LBRACK T_RBRACK {
1141 $$ = add_array($1, false, 0); 1141 $$ = add_array($1, false, 0);
1142 } 1142 }
1143 | direct_param_decl T_LBRACK constant_expr T_RBRACK { 1143 | direct_param_decl T_LBRACK constant_expr T_RBRACK {
1144 $$ = add_array($1, true, to_int_constant($3, false)); 1144 $$ = add_array($1, true, to_int_constant($3, false));
1145 } 1145 }
1146 | direct_param_decl param_list opt_asm_or_symbolrename { 1146 | direct_param_decl param_list opt_asm_or_symbolrename {
1147 $$ = add_function(symbolrename($1, $3), $2); 1147 $$ = add_function(symbolrename($1, $3), $2);
1148 popdecl(); 1148 end_declaration_level();
1149 block_level--; 1149 block_level--;
1150 } 1150 }
1151 ; 1151 ;
1152 1152
1153notype_param_decl: 1153notype_param_decl:
1154 direct_notype_param_decl { 1154 direct_notype_param_decl {
1155 $$ = $1; 1155 $$ = $1;
1156 } 1156 }
1157 | pointer direct_notype_param_decl { 1157 | pointer direct_notype_param_decl {
1158 $$ = add_pointer($2, $1); 1158 $$ = add_pointer($2, $1);
1159 } 1159 }
1160 ; 1160 ;
1161 1161
@@ -1164,27 +1164,27 @@ direct_notype_param_decl: @@ -1164,27 +1164,27 @@ direct_notype_param_decl:
1164 $$ = declarator_name(getsym($1)); 1164 $$ = declarator_name(getsym($1));
1165 } 1165 }
1166 | T_LPAREN notype_param_decl T_RPAREN { 1166 | T_LPAREN notype_param_decl T_RPAREN {
1167 $$ = $2; 1167 $$ = $2;
1168 } 1168 }
1169 | direct_notype_param_decl T_LBRACK T_RBRACK { 1169 | direct_notype_param_decl T_LBRACK T_RBRACK {
1170 $$ = add_array($1, false, 0); 1170 $$ = add_array($1, false, 0);
1171 } 1171 }
1172 | direct_notype_param_decl T_LBRACK constant_expr T_RBRACK { 1172 | direct_notype_param_decl T_LBRACK constant_expr T_RBRACK {
1173 $$ = add_array($1, true, to_int_constant($3, false)); 1173 $$ = add_array($1, true, to_int_constant($3, false));
1174 } 1174 }
1175 | direct_notype_param_decl param_list opt_asm_or_symbolrename { 1175 | direct_notype_param_decl param_list opt_asm_or_symbolrename {
1176 $$ = add_function(symbolrename($1, $3), $2); 1176 $$ = add_function(symbolrename($1, $3), $2);
1177 popdecl(); 1177 end_declaration_level();
1178 block_level--; 1178 block_level--;
1179 } 1179 }
1180 ; 1180 ;
1181 1181
1182pointer: 1182pointer:
1183 asterisk { 1183 asterisk {
1184 $$ = $1; 1184 $$ = $1;
1185 } 1185 }
1186 | asterisk type_qualifier_list { 1186 | asterisk type_qualifier_list {
1187 $$ = merge_pointers_and_qualifiers($1, $2); 1187 $$ = merge_pointers_and_qualifiers($1, $2);
1188 } 1188 }
1189 | asterisk pointer { 1189 | asterisk pointer {
1190 $$ = merge_pointers_and_qualifiers($1, $2); 1190 $$ = merge_pointers_and_qualifiers($1, $2);
@@ -1226,27 +1226,27 @@ type_qualifier: @@ -1226,27 +1226,27 @@ type_qualifier:
1226 1226
1227param_list: 1227param_list:
1228 id_list_lparen identifier_list T_RPAREN { 1228 id_list_lparen identifier_list T_RPAREN {
1229 $$ = $2; 1229 $$ = $2;
1230 } 1230 }
1231 | abstract_decl_param_list { 1231 | abstract_decl_param_list {
1232 $$ = $1; 1232 $$ = $1;
1233 } 1233 }
1234 ; 1234 ;
1235 1235
1236id_list_lparen: 1236id_list_lparen:
1237 T_LPAREN { 1237 T_LPAREN {
1238 block_level++; 1238 block_level++;
1239 pushdecl(PROTO_ARG); 1239 begin_declaration_level(PROTO_ARG);
1240 } 1240 }
1241 ; 1241 ;
1242 1242
1243identifier_list: 1243identifier_list:
1244 T_NAME { 1244 T_NAME {
1245 $$ = old_style_function_name(getsym($1)); 1245 $$ = old_style_function_name(getsym($1));
1246 } 1246 }
1247 | identifier_list T_COMMA T_NAME { 1247 | identifier_list T_COMMA T_NAME {
1248 $$ = lnklst($1, old_style_function_name(getsym($3))); 1248 $$ = lnklst($1, old_style_function_name(getsym($3)));
1249 } 1249 }
1250 | identifier_list error { 1250 | identifier_list error {
1251 $$ = $1; 1251 $$ = $1;
1252 } 1252 }
@@ -1258,27 +1258,27 @@ abstract_decl_param_list: @@ -1258,27 +1258,27 @@ abstract_decl_param_list:
1258 } 1258 }
1259 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN { 1259 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN {
1260 dcs->d_proto = true; 1260 dcs->d_proto = true;
1261 $$ = $2; 1261 $$ = $2;
1262 } 1262 }
1263 | abstract_decl_lparen error T_RPAREN { 1263 | abstract_decl_lparen error T_RPAREN {
1264 $$ = NULL; 1264 $$ = NULL;
1265 } 1265 }
1266 ; 1266 ;
1267 1267
1268abstract_decl_lparen: 1268abstract_decl_lparen:
1269 T_LPAREN { 1269 T_LPAREN {
1270 block_level++; 1270 block_level++;
1271 pushdecl(PROTO_ARG); 1271 begin_declaration_level(PROTO_ARG);
1272 } 1272 }
1273 ; 1273 ;
1274 1274
1275vararg_parameter_type_list: 1275vararg_parameter_type_list:
1276 parameter_type_list { 1276 parameter_type_list {
1277 $$ = $1; 1277 $$ = $1;
1278 } 1278 }
1279 | parameter_type_list T_COMMA T_ELLIPSIS { 1279 | parameter_type_list T_COMMA T_ELLIPSIS {
1280 dcs->d_vararg = true; 1280 dcs->d_vararg = true;
1281 $$ = $1; 1281 $$ = $1;
1282 } 1282 }
1283 | T_ELLIPSIS { 1283 | T_ELLIPSIS {
1284 if (sflag) { 1284 if (sflag) {
@@ -1410,29 +1410,29 @@ init_lbrace: @@ -1410,29 +1410,29 @@ init_lbrace:
1410 T_LBRACE { 1410 T_LBRACE {
1411 init_lbrace(); 1411 init_lbrace();
1412 } 1412 }
1413 ; 1413 ;
1414 1414
1415init_rbrace: 1415init_rbrace:
1416 T_RBRACE { 1416 T_RBRACE {
1417 init_rbrace(); 1417 init_rbrace();
1418 } 1418 }
1419 ; 1419 ;
1420 1420
1421type_name: 1421type_name:
1422 { 1422 {
1423 pushdecl(ABSTRACT); 1423 begin_declaration_level(ABSTRACT);
1424 } abstract_declaration { 1424 } abstract_declaration {
1425 popdecl(); 1425 end_declaration_level();
1426 $$ = $2->s_type; 1426 $$ = $2->s_type;
1427 } 1427 }
1428 ; 1428 ;
1429 1429
1430abstract_declaration: 1430abstract_declaration:
1431 noclass_declmods deftyp { 1431 noclass_declmods deftyp {
1432 $$ = declare_1_abstract(abstract_name()); 1432 $$ = declare_1_abstract(abstract_name());
1433 } 1433 }
1434 | noclass_declspecs deftyp { 1434 | noclass_declspecs deftyp {
1435 $$ = declare_1_abstract(abstract_name()); 1435 $$ = declare_1_abstract(abstract_name());
1436 } 1436 }
1437 | noclass_declmods deftyp abstract_decl { 1437 | noclass_declmods deftyp abstract_decl {
1438 $$ = declare_1_abstract($3); 1438 $$ = declare_1_abstract($3);
@@ -1468,32 +1468,32 @@ direct_abstract_decl: @@ -1468,32 +1468,32 @@ direct_abstract_decl:
1468 $$ = add_array(abstract_name(), true, to_int_constant($2, false)); 1468 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
1469 } 1469 }
1470 | type_attribute direct_abstract_decl { 1470 | type_attribute direct_abstract_decl {
1471 $$ = $2; 1471 $$ = $2;
1472 } 1472 }
1473 | direct_abstract_decl T_LBRACK T_RBRACK { 1473 | direct_abstract_decl T_LBRACK T_RBRACK {
1474 $$ = add_array($1, false, 0); 1474 $$ = add_array($1, false, 0);
1475 } 1475 }
1476 | direct_abstract_decl T_LBRACK constant_expr T_RBRACK { 1476 | direct_abstract_decl T_LBRACK constant_expr T_RBRACK {
1477 $$ = add_array($1, true, to_int_constant($3, false)); 1477 $$ = add_array($1, true, to_int_constant($3, false));
1478 } 1478 }
1479 | abstract_decl_param_list opt_asm_or_symbolrename { 1479 | abstract_decl_param_list opt_asm_or_symbolrename {
1480 $$ = add_function(symbolrename(abstract_name(), $2), $1); 1480 $$ = add_function(symbolrename(abstract_name(), $2), $1);
1481 popdecl(); 1481 end_declaration_level();
1482 block_level--; 1482 block_level--;
1483 } 1483 }
1484 | direct_abstract_decl abstract_decl_param_list opt_asm_or_symbolrename { 1484 | direct_abstract_decl abstract_decl_param_list opt_asm_or_symbolrename {
1485 $$ = add_function(symbolrename($1, $3), $2); 1485 $$ = add_function(symbolrename($1, $3), $2);
1486 popdecl(); 1486 end_declaration_level();
1487 block_level--; 1487 block_level--;
1488 } 1488 }
1489 | direct_abstract_decl type_attribute_list 1489 | direct_abstract_decl type_attribute_list
1490 ; 1490 ;
1491 1491
1492non_expr_statement: 1492non_expr_statement:
1493 labeled_statement 1493 labeled_statement
1494 | compound_statement 1494 | compound_statement
1495 | selection_statement 1495 | selection_statement
1496 | iteration_statement 1496 | iteration_statement
1497 | jump_statement { 1497 | jump_statement {
1498 seen_fallthrough = false; 1498 seen_fallthrough = false;
1499 } 1499 }
@@ -1527,33 +1527,33 @@ label: @@ -1527,33 +1527,33 @@ label:
1527 seen_fallthrough = true; 1527 seen_fallthrough = true;
1528 } 1528 }
1529 ; 1529 ;
1530 1530
1531compound_statement: /* C99 6.8.2 */ 1531compound_statement: /* C99 6.8.2 */
1532 compound_statement_lbrace compound_statement_rbrace 1532 compound_statement_lbrace compound_statement_rbrace
1533 | compound_statement_lbrace block_item_list compound_statement_rbrace 1533 | compound_statement_lbrace block_item_list compound_statement_rbrace
1534 ; 1534 ;
1535 1535
1536compound_statement_lbrace: 1536compound_statement_lbrace:
1537 T_LBRACE { 1537 T_LBRACE {
1538 block_level++; 1538 block_level++;
1539 mem_block_level++; 1539 mem_block_level++;
1540 pushdecl(AUTO); 1540 begin_declaration_level(AUTO);
1541 } 1541 }
1542 ; 1542 ;
1543 1543
1544compound_statement_rbrace: 1544compound_statement_rbrace:
1545 T_RBRACE { 1545 T_RBRACE {
1546 popdecl(); 1546 end_declaration_level();
1547 freeblk(); 1547 freeblk();
1548 mem_block_level--; 1548 mem_block_level--;
1549 block_level--; 1549 block_level--;
1550 seen_fallthrough = false; 1550 seen_fallthrough = false;
1551 } 1551 }
1552 ; 1552 ;
1553 1553
1554block_item_list: 1554block_item_list:
1555 block_item 1555 block_item
1556 | block_item_list block_item { 1556 | block_item_list block_item {
1557 if (!Sflag && $1 && !$2) 1557 if (!Sflag && $1 && !$2)
1558 /* declarations after statements is a C99 feature */ 1558 /* declarations after statements is a C99 feature */
1559 c99ism(327); 1559 c99ism(327);
@@ -1686,59 +1686,59 @@ iteration_statement: /* C99 6.8.5 */ @@ -1686,59 +1686,59 @@ iteration_statement: /* C99 6.8.5 */
1686 while2(); 1686 while2();
1687 } 1687 }
1688 | do_statement do_while_expr { 1688 | do_statement do_while_expr {
1689 do2($2); 1689 do2($2);
1690 seen_fallthrough = false; 1690 seen_fallthrough = false;
1691 } 1691 }
1692 | do error { 1692 | do error {
1693 clear_warning_flags(); 1693 clear_warning_flags();
1694 do2(NULL); 1694 do2(NULL);
1695 } 1695 }
1696 | for_exprs statement { 1696 | for_exprs statement {
1697 clear_warning_flags(); 1697 clear_warning_flags();
1698 for2(); 1698 for2();
1699 popdecl(); 1699 end_declaration_level();
1700 block_level--; 1700 block_level--;
1701 } 1701 }
1702 | for_exprs error { 1702 | for_exprs error {
1703 clear_warning_flags(); 1703 clear_warning_flags();
1704 for2(); 1704 for2();
1705 popdecl(); 1705 end_declaration_level();
1706 block_level--; 1706 block_level--;
1707 } 1707 }
1708 ; 1708 ;
1709 1709
1710while_expr: 1710while_expr:
1711 T_WHILE T_LPAREN expr T_RPAREN { 1711 T_WHILE T_LPAREN expr T_RPAREN {
1712 while1($3); 1712 while1($3);
1713 clear_warning_flags(); 1713 clear_warning_flags();
1714 } 1714 }
1715 ; 1715 ;
1716 1716
1717do: 1717do:
1718 T_DO { 1718 T_DO {
1719 do1(); 1719 do1();
1720 } 1720 }
1721 ; 1721 ;
1722 1722
1723do_while_expr: 1723do_while_expr:
1724 T_WHILE T_LPAREN expr T_RPAREN T_SEMI { 1724 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1725 $$ = $3; 1725 $$ = $3;
1726 } 1726 }
1727 ; 1727 ;
1728 1728
1729for_start: 1729for_start:
1730 T_FOR T_LPAREN { 1730 T_FOR T_LPAREN {
1731 pushdecl(AUTO); 1731 begin_declaration_level(AUTO);
1732 block_level++; 1732 block_level++;
1733 } 1733 }
1734 ; 1734 ;
1735for_exprs: 1735for_exprs:
1736 for_start declaration_specifiers deftyp notype_init_decls T_SEMI 1736 for_start declaration_specifiers deftyp notype_init_decls T_SEMI
1737 opt_expr T_SEMI opt_expr T_RPAREN { 1737 opt_expr T_SEMI opt_expr T_RPAREN {
1738 /* variable declaration in for loop */ 1738 /* variable declaration in for loop */
1739 c99ism(325); 1739 c99ism(325);
1740 for1(NULL, $6, $8); 1740 for1(NULL, $6, $8);
1741 clear_warning_flags(); 1741 clear_warning_flags();
1742 } 1742 }
1743 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN { 1743 | for_start opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPAREN {
1744 for1($2, $4, $6); 1744 for1($2, $4, $6);

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

--- src/usr.bin/xlint/lint1/decl.c 2021/03/23 18:40:50 1.159
+++ src/usr.bin/xlint/lint1/decl.c 2021/03/26 17:44:52 1.160
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: decl.c,v 1.159 2021/03/23 18:40:50 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.160 2021/03/26 17:44:52 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.159 2021/03/23 18:40:50 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.160 2021/03/26 17:44:52 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;
@@ -578,51 +578,53 @@ add_qualifier(tqual_t q) @@ -578,51 +578,53 @@ add_qualifier(tqual_t q)
578 } 578 }
579 dcs->d_volatile = true; 579 dcs->d_volatile = true;
580 } else { 580 } else {
581 lint_assert(q == RESTRICT || q == THREAD); 581 lint_assert(q == RESTRICT || q == THREAD);
582 /* Silently ignore these qualifiers. */ 582 /* Silently ignore these qualifiers. */
583 } 583 }
584} 584}
585 585
586/* 586/*
587 * Go to the next declaration level (structs, nested structs, blocks, 587 * Go to the next declaration level (structs, nested structs, blocks,
588 * argument declaration lists ...) 588 * argument declaration lists ...)
589 */ 589 */
590void 590void
591pushdecl(scl_t sc) 591begin_declaration_level(scl_t sc)
592{ 592{
593 dinfo_t *di; 593 dinfo_t *di;
594 594
595 /* put a new element on the declaration stack */ 595 /* put a new element on the declaration stack */
596 di = xcalloc(1, sizeof (dinfo_t)); 596 di = xcalloc(1, sizeof (dinfo_t));
597 di->d_next = dcs; 597 di->d_next = dcs;
598 dcs = di; 598 dcs = di;
599 di->d_ctx = sc; 599 di->d_ctx = sc;
600 di->d_ldlsym = &di->d_dlsyms; 600 di->d_ldlsym = &di->d_dlsyms;
601 if (dflag) 601 if (dflag)
602 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc); 602 (void)printf("begin_declaration_level(%p %d)\n",
 603 dcs, (int)sc);
603 604
604} 605}
605 606
606/* 607/*
607 * Go back to previous declaration level 608 * Go back to previous declaration level
608 */ 609 */
609void 610void
610popdecl(void) 611end_declaration_level(void)
611{ 612{
612 dinfo_t *di; 613 dinfo_t *di;
613 614
614 if (dflag) 615 if (dflag)
615 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx); 616 (void)printf("end_declaration_level(%p %d)\n",
 617 dcs, (int)dcs->d_ctx);
616 618
617 lint_assert(dcs->d_next != NULL); 619 lint_assert(dcs->d_next != NULL);
618 di = dcs; 620 di = dcs;
619 dcs = di->d_next; 621 dcs = di->d_next;
620 switch (di->d_ctx) { 622 switch (di->d_ctx) {
621 case MOS: 623 case MOS:
622 case MOU: 624 case MOU:
623 case CTCONST: 625 case CTCONST:
624 /* 626 /*
625 * Symbols declared in (nested) structs or enums are 627 * Symbols declared in (nested) structs or enums are
626 * part of the next level (they are removed from the 628 * part of the next level (they are removed from the
627 * symbol table if the symbols of the outer level are 629 * symbol table if the symbols of the outer level are
628 * removed). 630 * removed).
@@ -1383,34 +1385,34 @@ add_function(sym_t *decl, sym_t *args) @@ -1383,34 +1385,34 @@ add_function(sym_t *decl, sym_t *args)
1383{ 1385{
1384 type_t **tpp, *tp; 1386 type_t **tpp, *tp;
1385 1387
1386 if (dcs->d_proto) { 1388 if (dcs->d_proto) {
1387 if (tflag) 1389 if (tflag)
1388 /* function prototypes are illegal in traditional C */ 1390 /* function prototypes are illegal in traditional C */
1389 warning(270); 1391 warning(270);
1390 args = new_style_function(decl, args); 1392 args = new_style_function(decl, args);
1391 } else { 1393 } else {
1392 old_style_function(decl, args); 1394 old_style_function(decl, args);
1393 } 1395 }
1394 1396
1395 /* 1397 /*
1396 * The symbols are removed from the symbol table by popdecl() after 1398 * The symbols are removed from the symbol table by
1397 * add_function(). To be able to restore them if this is a function 1399 * end_declaration_level after add_function. To be able to restore
1398 * definition, a pointer to the list of all symbols is stored in 1400 * them if this is a function definition, a pointer to the list of all
1399 * dcs->d_next->d_func_proto_syms. Also a list of the arguments 1401 * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1400 * (concatenated by s_next) is stored in dcs->d_next->d_func_args. 1402 * the arguments (concatenated by s_next) is stored in
1401 * (dcs->d_next must be used because *dcs is the declaration stack 1403 * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1402 * element created for the list of params and is removed after 1404 * the declaration stack element created for the list of params and is
1403 * add_function()) 1405 * removed after add_function.)
1404 */ 1406 */
1405 if (dcs->d_next->d_ctx == EXTERN && 1407 if (dcs->d_next->d_ctx == EXTERN &&
1406 decl->s_type == dcs->d_next->d_type) { 1408 decl->s_type == dcs->d_next->d_type) {
1407 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms; 1409 dcs->d_next->d_func_proto_syms = dcs->d_dlsyms;
1408 dcs->d_next->d_func_args = args; 1410 dcs->d_next->d_func_args = args;
1409 } 1411 }
1410 1412
1411 tpp = &decl->s_type; 1413 tpp = &decl->s_type;
1412 while (*tpp != NULL && *tpp != dcs->d_next->d_type) 1414 while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1413 tpp = &(*tpp)->t_subt; 1415 tpp = &(*tpp)->t_subt;
1414 if (*tpp == NULL) 1416 if (*tpp == NULL)
1415 return decl; 1417 return decl;
1416 1418
@@ -2885,27 +2887,27 @@ abstract_name(void) @@ -2885,27 +2887,27 @@ abstract_name(void)
2885 dcs->d_vararg = false; 2887 dcs->d_vararg = false;
2886 2888
2887 return sym; 2889 return sym;
2888} 2890}
2889 2891
2890/* 2892/*
2891 * Removes anything which has nothing to do on global level. 2893 * Removes anything which has nothing to do on global level.
2892 */ 2894 */
2893void 2895void
2894global_clean_up(void) 2896global_clean_up(void)
2895{ 2897{
2896 2898
2897 while (dcs->d_next != NULL) 2899 while (dcs->d_next != NULL)
2898 popdecl(); 2900 end_declaration_level();
2899 2901
2900 cleanup(); 2902 cleanup();
2901 block_level = 0; 2903 block_level = 0;
2902 mem_block_level = 0; 2904 mem_block_level = 0;
2903 2905
2904 /* 2906 /*
2905 * remove all information about pending lint directives without 2907 * remove all information about pending lint directives without
2906 * warnings. 2908 * warnings.
2907 */ 2909 */
2908 global_clean_up_decl(true); 2910 global_clean_up_decl(true);
2909} 2911}
2910 2912
2911/* 2913/*

cvs diff -r1.85 -r1.86 src/usr.bin/xlint/lint1/externs1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/externs1.h 2021/03/25 21:51:55 1.85
+++ src/usr.bin/xlint/lint1/externs1.h 2021/03/26 17:44:52 1.86
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: externs1.h,v 1.85 2021/03/25 21:51:55 rillig Exp $ */ 1/* $NetBSD: externs1.h,v 1.86 2021/03/26 17:44:52 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.
@@ -138,28 +138,28 @@ extern const char *unnamed; @@ -138,28 +138,28 @@ extern const char *unnamed;
138extern int enumval; 138extern int enumval;
139 139
140extern void initdecl(void); 140extern void initdecl(void);
141extern type_t *gettyp(tspec_t); 141extern type_t *gettyp(tspec_t);
142extern type_t *duptyp(const type_t *); 142extern type_t *duptyp(const type_t *);
143extern type_t *tduptyp(const type_t *); 143extern type_t *tduptyp(const type_t *);
144extern bool is_incomplete(const type_t *); 144extern bool is_incomplete(const type_t *);
145extern void setcomplete(type_t *, bool); 145extern void setcomplete(type_t *, bool);
146extern void add_storage_class(scl_t); 146extern void add_storage_class(scl_t);
147extern void add_type(type_t *); 147extern void add_type(type_t *);
148extern void add_qualifier(tqual_t); 148extern void add_qualifier(tqual_t);
149extern void addpacked(void); 149extern void addpacked(void);
150extern void add_attr_used(void); 150extern void add_attr_used(void);
151extern void pushdecl(scl_t); 151extern void begin_declaration_level(scl_t);
152extern void popdecl(void); 152extern void end_declaration_level(void);
153extern void setasm(void); 153extern void setasm(void);
154extern void clrtyp(void); 154extern void clrtyp(void);
155extern void deftyp(void); 155extern void deftyp(void);
156extern int length(const type_t *, const char *); 156extern int length(const type_t *, const char *);
157extern int alignment_in_bits(const type_t *); 157extern int alignment_in_bits(const type_t *);
158extern sym_t *lnklst(sym_t *, sym_t *); 158extern sym_t *lnklst(sym_t *, sym_t *);
159extern void check_type(sym_t *); 159extern void check_type(sym_t *);
160extern sym_t *declarator_1_struct_union(sym_t *); 160extern sym_t *declarator_1_struct_union(sym_t *);
161extern sym_t *bitfield(sym_t *, int); 161extern sym_t *bitfield(sym_t *, int);
162extern pqinf_t *merge_pointers_and_qualifiers(pqinf_t *, pqinf_t *); 162extern pqinf_t *merge_pointers_and_qualifiers(pqinf_t *, pqinf_t *);
163extern sym_t *add_pointer(sym_t *, pqinf_t *); 163extern sym_t *add_pointer(sym_t *, pqinf_t *);
164extern sym_t *add_array(sym_t *, bool, int); 164extern sym_t *add_array(sym_t *, bool, int);
165extern sym_t *add_function(sym_t *, sym_t *); 165extern sym_t *add_function(sym_t *, sym_t *);