1%{ |
2/* $NetBSD: cgram.y,v 1.40 2008/04/25 17:18:24 christos Exp $ */ |
3 4/* 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 6 * Copyright (c) 1994, 1995 Jochen Pohl 7 * All Rights Reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions --- 19 unchanged lines hidden (view full) --- 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 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 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36#include <sys/cdefs.h> 37#if defined(__RCSID) && !defined(lint) |
38__RCSID("$NetBSD: cgram.y,v 1.40 2008/04/25 17:18:24 christos Exp $"); |
39#endif |
40__FBSDID("$FreeBSD: stable/10/usr.bin/xlint/lint1/cgram.y 281168 2015-04-06 19:56:27Z pfg $"); |
41 42#include <stdlib.h> 43#include <string.h> 44#include <limits.h> 45 46#include "lint1.h" 47 48/* --- 11 unchanged lines hidden (view full) --- 60int mblklev; 61 62/* 63 * Save the no-warns state and restore it to avoid the problem where 64 * if (expr) { stmt } / * NOLINT * / stmt; 65 */ 66static int onowarn = -1; 67 |
68static int toicon(tnode_t *, int); |
69static void idecl(sym_t *, int, sbuf_t *); 70static void ignuptorp(void); 71 72#ifdef DEBUG |
73static inline void CLRWFLGS(void); 74static inline void CLRWFLGS(void) |
75{ 76 printf("%s, %d: clear flags %s %d\n", curr_pos.p_file, 77 curr_pos.p_line, __FILE__, __LINE__); 78 clrwflgs(); 79 onowarn = -1; 80} 81 |
82static inline void SAVE(void); 83static inline void SAVE(void) |
84{ 85 if (onowarn != -1) 86 abort(); 87 printf("%s, %d: save flags %s %d = %d\n", curr_pos.p_file, 88 curr_pos.p_line, __FILE__, __LINE__, nowarn); 89 onowarn = nowarn; 90} 91 |
92static inline void RESTORE(void); 93static inline void RESTORE(void) |
94{ 95 if (onowarn != -1) { 96 nowarn = onowarn; 97 printf("%s, %d: restore flags %s %d = %d\n", curr_pos.p_file, 98 curr_pos.p_line, __FILE__, __LINE__, nowarn); 99 onowarn = -1; 100 } else 101 CLRWFLGS(); 102} 103#else 104#define CLRWFLGS() clrwflgs(), onowarn = -1 105#define SAVE() onowarn = nowarn 106#define RESTORE() (void)(onowarn == -1 ? (clrwflgs(), 0) : (nowarn = onowarn)) 107#endif 108%} 109 |
110%expect 1 111 |
112%union { 113 int y_int; 114 val_t *y_val; 115 sbuf_t *y_sb; 116 sym_t *y_sym; 117 op_t y_op; 118 scl_t y_scl; 119 tspec_t y_tspec; 120 tqual_t y_tqual; 121 type_t *y_type; 122 tnode_t *y_tnode; |
123 range_t y_range; |
124 strg_t *y_strg; 125 pqinf_t *y_pqinf; 126}; 127 128%token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPARN T_RPARN 129%token <y_op> T_STROP 130%token <y_op> T_UNOP 131%token <y_op> T_INCDEC --- 109 unchanged lines hidden (view full) --- 241%type <y_pqinf> type_qualifier 242%type <y_sym> identifier_list 243%type <y_sym> abs_decl 244%type <y_sym> direct_abs_decl 245%type <y_sym> vararg_parameter_type_list 246%type <y_sym> parameter_type_list 247%type <y_sym> parameter_declaration 248%type <y_tnode> expr |
249%type <y_tnode> expr_stmnt_val 250%type <y_tnode> expr_stmnt_list |
251%type <y_tnode> term 252%type <y_tnode> func_arg_list 253%type <y_op> point_or_arrow 254%type <y_type> type_name 255%type <y_sym> abstract_declaration 256%type <y_tnode> do_while_expr 257%type <y_tnode> opt_expr 258%type <y_strg> string 259%type <y_strg> string2 260%type <y_sb> opt_asm_or_symbolrename |
261%type <y_range> range 262%type <y_range> lorange |
263 264 265%% 266 267program: 268 /* empty */ { 269 if (sflag) { 270 /* empty translation unit */ --- 413 unchanged lines hidden (view full) --- 684 } 685 ; 686 687notype_member_decl: 688 notype_decl { 689 $$ = $1; 690 } 691 | notype_decl T_COLON constant { |
692 $$ = bitfield($1, toicon($3, 1)); |
693 } 694 | { 695 symtyp = FVFT; 696 } T_COLON constant { |
697 $$ = bitfield(NULL, toicon($3, 1)); |
698 } 699 ; 700 701type_member_decl: 702 type_decl { 703 $$ = $1; 704 } 705 | type_decl T_COLON constant { |
706 $$ = bitfield($1, toicon($3, 1)); |
707 } 708 | { 709 symtyp = FVFT; 710 } T_COLON constant { |
711 $$ = bitfield(NULL, toicon($3, 1)); |
712 } 713 ; 714 715enum_spec: 716 enum enum_tag { 717 $$ = mktag($2, ENUM, 0, 0); 718 } 719 | enum enum_tag { --- 66 unchanged lines hidden (view full) --- 786 } 787 ; 788 789enumerator: 790 ename { 791 $$ = ename($1, enumval, 1); 792 } 793 | ename T_ASSIGN constant { |
794 $$ = ename($1, toicon($3, 1), 0); |
795 } 796 ; 797 798ename: 799 identifier { 800 $$ = getsym($1); 801 } 802 ; --- 48 unchanged lines hidden (view full) --- 851 } 852 | T_LPARN type_decl T_RPARN { 853 $$ = $2; 854 } 855 | notype_direct_decl T_LBRACK T_RBRACK { 856 $$ = addarray($1, 0, 0); 857 } 858 | notype_direct_decl T_LBRACK constant T_RBRACK { |
859 $$ = addarray($1, 1, toicon($3, 0)); |
860 } 861 | notype_direct_decl param_list { 862 $$ = addfunc($1, $2); 863 popdecl(); 864 blklev--; 865 } 866 ; 867 --- 12 unchanged lines hidden (view full) --- 880 } 881 | T_LPARN type_decl T_RPARN { 882 $$ = $2; 883 } 884 | type_direct_decl T_LBRACK T_RBRACK { 885 $$ = addarray($1, 0, 0); 886 } 887 | type_direct_decl T_LBRACK constant T_RBRACK { |
888 $$ = addarray($1, 1, toicon($3, 0)); |
889 } 890 | type_direct_decl param_list { 891 $$ = addfunc($1, $2); 892 popdecl(); 893 blklev--; 894 } 895 ; 896 --- 19 unchanged lines hidden (view full) --- 916 } 917 | T_LPARN notype_param_decl T_RPARN { 918 $$ = $2; 919 } 920 | direct_param_decl T_LBRACK T_RBRACK { 921 $$ = addarray($1, 0, 0); 922 } 923 | direct_param_decl T_LBRACK constant T_RBRACK { |
924 $$ = addarray($1, 1, toicon($3, 0)); |
925 } 926 | direct_param_decl param_list { 927 $$ = addfunc($1, $2); 928 popdecl(); 929 blklev--; 930 } 931 ; 932 --- 12 unchanged lines hidden (view full) --- 945 } 946 | T_LPARN notype_param_decl T_RPARN { 947 $$ = $2; 948 } 949 | direct_notype_param_decl T_LBRACK T_RBRACK { 950 $$ = addarray($1, 0, 0); 951 } 952 | direct_notype_param_decl T_LBRACK constant T_RBRACK { |
953 $$ = addarray($1, 1, toicon($3, 0)); |
954 } 955 | direct_notype_param_decl param_list { 956 $$ = addfunc($1, $2); 957 popdecl(); 958 blklev--; 959 } 960 ; 961 --- 161 unchanged lines hidden (view full) --- 1123initializer: 1124 init_expr 1125 ; 1126 1127init_expr: 1128 expr %prec T_COMMA { 1129 mkinit($1); 1130 } |
1131 | init_by_name init_expr %prec T_COMMA |
1132 | init_lbrace init_expr_list init_rbrace 1133 | init_lbrace init_expr_list T_COMMA init_rbrace 1134 | error 1135 ; 1136 1137init_expr_list: 1138 init_expr %prec T_COMMA 1139 | init_expr_list T_COMMA init_expr 1140 ; 1141 |
1142lorange: 1143 constant T_ELLIPSE { 1144 $$.lo = toicon($1, 1); 1145 } 1146 ; 1147range: 1148 constant { 1149 $$.lo = toicon($1, 1); 1150 $$.hi = $$.lo + 1; 1151 } 1152 | lorange constant { 1153 $$.lo = $1.lo; 1154 $$.hi = toicon($2, 1); 1155 } 1156 ; 1157 1158init_by_name: 1159 T_LBRACK range T_RBRACK T_ASSIGN { 1160 if (!Sflag) 1161 warning(321); 1162 } 1163 | point identifier T_ASSIGN { 1164 if (!Sflag) 1165 warning(313); 1166 memberpush($2); 1167 } 1168 | identifier T_COLON { 1169 gnuism(315); 1170 memberpush($1); 1171 } 1172 ; 1173 |
1174init_lbrace: 1175 T_LBRACE { 1176 initlbr(); 1177 } 1178 ; 1179 1180init_rbrace: 1181 T_RBRACE { --- 40 unchanged lines hidden (view full) --- 1222direct_abs_decl: 1223 T_LPARN abs_decl T_RPARN { 1224 $$ = $2; 1225 } 1226 | T_LBRACK T_RBRACK { 1227 $$ = addarray(aname(), 0, 0); 1228 } 1229 | T_LBRACK constant T_RBRACK { |
1230 $$ = addarray(aname(), 1, toicon($2, 0)); |
1231 } 1232 | direct_abs_decl T_LBRACK T_RBRACK { 1233 $$ = addarray($1, 0, 0); 1234 } 1235 | direct_abs_decl T_LBRACK constant T_RBRACK { |
1236 $$ = addarray($1, 1, toicon($3, 0)); |
1237 } 1238 | abs_decl_param_list { 1239 $$ = addfunc(aname(), $1); 1240 popdecl(); 1241 blklev--; 1242 } 1243 | direct_abs_decl abs_decl_param_list { 1244 $$ = addfunc($1, $2); 1245 popdecl(); 1246 blklev--; 1247 } 1248 ; 1249 |
1250non_expr_stmnt: |
1251 labeled_stmnt |
1252 | comp_stmnt 1253 | selection_stmnt 1254 | iteration_stmnt 1255 | jump_stmnt { 1256 ftflg = 0; 1257 } 1258 | asm_stmnt |
1259 1260stmnt: 1261 expr_stmnt 1262 | non_expr_stmnt |
1263 ; 1264 1265labeled_stmnt: 1266 label stmnt 1267 ; 1268 1269label: 1270 identifier T_COLON { 1271 symtyp = FLAB; 1272 label(T_NAME, getsym($1), NULL); 1273 } 1274 | T_CASE constant T_COLON { 1275 label(T_CASE, NULL, $2); 1276 ftflg = 1; |
1277 } 1278 | T_CASE constant T_ELLIPSE constant T_COLON { 1279 /* XXX: We don't fill all cases */ 1280 label(T_CASE, NULL, $2); 1281 ftflg = 1; 1282 } |
1283 | T_DEFAULT T_COLON { 1284 label(T_DEFAULT, NULL, NULL); 1285 ftflg = 1; 1286 } 1287 ; 1288 1289comp_stmnt: |
1290 comp_stmnt_lbrace declaration_list opt_stmnt_list comp_stmnt_rbrace 1291 | comp_stmnt_lbrace opt_stmnt_list comp_stmnt_rbrace |
1292 ; 1293 |
1294comp_stmnt_lbrace: |
1295 T_LBRACE { 1296 blklev++; 1297 mblklev++; 1298 pushdecl(AUTO); 1299 } 1300 ; 1301 |
1302comp_stmnt_rbrace: |
1303 T_RBRACE { 1304 popdecl(); 1305 freeblk(); 1306 mblklev--; 1307 blklev--; 1308 ftflg = 0; 1309 } 1310 ; --- 8 unchanged lines hidden (view full) --- 1319 | stmnt_list stmnt { 1320 RESTORE(); 1321 } 1322 | stmnt_list error T_SEMI 1323 ; 1324 1325expr_stmnt: 1326 expr T_SEMI { |
1327 expr($1, 0, 0, 1); |
1328 ftflg = 0; 1329 } 1330 | T_SEMI { 1331 ftflg = 0; 1332 } 1333 ; 1334 |
1335/* 1336 * The following two productions are used to implement 1337 * ({ [[decl-list] stmt-list] }). 1338 * XXX: This is not well tested. 1339 */ 1340expr_stmnt_val: 1341 expr T_SEMI { 1342 /* XXX: We should really do that only on the last name */ 1343 if ($1->tn_op == NAME) 1344 $1->tn_sym->s_used = 1; 1345 $$ = $1; 1346 expr($1, 0, 0, 0); 1347 ftflg = 0; 1348 } 1349 | non_expr_stmnt { 1350 $$ = getnode(); 1351 $$->tn_type = gettyp(VOID); 1352 } 1353 ; 1354 1355expr_stmnt_list: 1356 expr_stmnt_val 1357 | expr_stmnt_list expr_stmnt_val { 1358 $$ = $2; 1359 } 1360 | expr_stmnt_list expr_stmnt_val 1361 ; 1362 |
1363selection_stmnt: 1364 if_without_else { 1365 SAVE(); 1366 if2(); 1367 if3(0); 1368 } 1369 | if_without_else T_ELSE { 1370 SAVE(); --- 225 unchanged lines hidden (view full) --- 1596 | T_CON { 1597 $$ = getcnode(gettyp($1->v_tspec), $1); 1598 } 1599 | T_LPARN expr T_RPARN { 1600 if ($2 != NULL) 1601 $2->tn_parn = 1; 1602 $$ = $2; 1603 } |
1604 | T_LPARN comp_stmnt_lbrace declaration_list expr_stmnt_list { 1605 blklev--; 1606 mblklev--; 1607 initsym = mktempsym(duptyp($4->tn_type)); 1608 mblklev++; 1609 blklev++; 1610 gnuism(320); 1611 } comp_stmnt_rbrace T_RPARN { 1612 $$ = getnnode(initsym, 0); 1613 } 1614 | T_LPARN comp_stmnt_lbrace expr_stmnt_list { 1615 blklev--; 1616 mblklev--; 1617 initsym = mktempsym($3->tn_type); 1618 mblklev++; 1619 blklev++; 1620 gnuism(320); 1621 } comp_stmnt_rbrace T_RPARN { 1622 $$ = getnnode(initsym, 0); 1623 } |
1624 | term T_INCDEC { 1625 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL); 1626 } 1627 | T_INCDEC term { 1628 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL); 1629 } 1630 | T_MULT term { 1631 $$ = build(STAR, $2, NULL); --- 39 unchanged lines hidden (view full) --- 1671 chkmisc($2, 0, 0, 0, 0, 0, 1); 1672 } 1673 | T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF { 1674 $$ = bldszof($3); 1675 } 1676 | T_LPARN type_name T_RPARN term %prec T_UNOP { 1677 $$ = cast($4, $2); 1678 } |
1679 | T_LPARN type_name T_RPARN %prec T_UNOP { 1680 sym_t *tmp = mktempsym($2); 1681 idecl(tmp, 1, NULL); 1682 } init_lbrace init_expr_list init_rbrace { 1683 if (!Sflag) 1684 gnuism(319); 1685 $$ = getnnode(initsym, 0); 1686 } |
1687 ; 1688 1689string: 1690 T_STRING { 1691 $$ = $1; 1692 } 1693 | T_STRING string2 { 1694 $$ = catstrg($1, $2); --- 24 unchanged lines hidden (view full) --- 1719 1720point_or_arrow: 1721 T_STROP { 1722 symtyp = FMOS; 1723 $$ = $1; 1724 } 1725 ; 1726 |
1727point: 1728 T_STROP { 1729 if ($1 != POINT) 1730 error(249); 1731 } 1732 ; 1733 |
1734identifier: 1735 T_NAME { 1736 $$ = $1; 1737 } 1738 | T_TYPENAME { 1739 $$ = $1; 1740 } 1741 ; 1742 1743%% 1744 1745/* ARGSUSED */ 1746int 1747yyerror(char *msg) 1748{ |
1749 error(249); 1750 if (++sytxerr >= 5) 1751 norecover(); 1752 return (0); 1753} 1754 1755static __inline int uq_gt(uint64_t, uint64_t); 1756static __inline int --- 18 unchanged lines hidden (view full) --- 1775 * as integer. 1776 * Is the node not constant or too large for int or of type float, 1777 * a warning will be printed. 1778 * 1779 * toicon() should be used only inside declarations. If it is used in 1780 * expressions, it frees the memory used for the expression. 1781 */ 1782static int |
1783toicon(tnode_t *tn, int required) |
1784{ 1785 int i; 1786 tspec_t t; 1787 val_t *v; 1788 |
1789 v = constant(tn, required); |
1790 1791 /* 1792 * Abstract declarations are used inside expression. To free 1793 * the memory would be a fatal error. 1794 */ 1795 if (dcs->d_ctx != ABSTRACT) 1796 tfreeblk(); 1797 --- 28 unchanged lines hidden (view full) --- 1826 1827 initerr = 0; 1828 initsym = decl; 1829 1830 switch (dcs->d_ctx) { 1831 case EXTERN: 1832 if (rename != NULL) { 1833 if (decl->s_rename != NULL) |
1834 LERROR("idecl()"); |
1835 1836 s = getlblk(1, rename->sb_len + 1); 1837 (void)memcpy(s, rename->sb_name, rename->sb_len + 1); 1838 decl->s_rename = s; 1839 freeyyv(&rename, T_NAME); 1840 } 1841 decl1ext(decl, initflg); 1842 break; --- 11 unchanged lines hidden (view full) --- 1854 /* symbol renaming can't be used on automatic variables */ 1855 error(311); 1856 freeyyv(&rename, T_NAME); 1857 break; 1858 } 1859 decl1loc(decl, initflg); 1860 break; 1861 default: |
1862 LERROR("idecl()"); |
1863 } 1864 1865 if (initflg && !initerr) 1866 prepinit(); 1867} 1868 1869/* 1870 * Discard all input tokens up to and including the next --- 23 unchanged lines hidden --- |