1 %{ 2 /* $NetBSD: scan.l,v 1.27 2002/02/05 03:04:29 thorpej 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 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Jochen Pohl for 20 * The NetBSD Project. 21 * 4. The name of the author may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 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 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: scan.l,v 1.27 2002/02/05 03:04:29 thorpej Exp $"); 39 #endif 40 41 #include <stdlib.h> 42 #include <string.h> 43 #include <limits.h> 44 #include <float.h> 45 #include <ctype.h> 46 #include <errno.h> 47 #include <math.h> 48 49 #include "lint1.h" 50 #include "cgram.h" 51 52 #define CHAR_MASK (~(~0 << CHAR_BIT)) 53 #define YY_NO_UNPUT 54 55 /* Current position (its also updated when an included file is parsed) */ 56 pos_t curr_pos = { 1, "", 0 }; 57 58 /* 59 * Current position in C source (not updated when an included file is 60 * parsed). 61 */ 62 pos_t csrc_pos = { 1, "", 0 }; 63 64 static void incline(void); 65 static void badchar(int); 66 static sbuf_t *allocsb(void); 67 static void freesb(sbuf_t *); 68 static int inpc(void); 69 static int hash(const char *); 70 static sym_t *search(sbuf_t *); 71 static int name(void); 72 static int keyw(sym_t *); 73 static int icon(int); 74 static int fcon(void); 75 static int operator(int, op_t); 76 static int ccon(void); 77 static int wccon(void); 78 static int getescc(int); 79 static void directive(void); 80 static void comment(void); 81 static void slashslashcomment(void); 82 static int string(void); 83 static int wcstrg(void); 84 85 %} 86 87 L [_A-Za-z] 88 D [0-9] 89 NZD [1-9] 90 OD [0-7] 91 HD [0-9A-Fa-f] 92 EX ([eE][+-]?[0-9]+) 93 94 %% 95 96 {L}({L}|{D})* return (name()); 97 0{OD}*[lLuU]* return (icon(8)); 98 {NZD}{D}*[lLuU]* return (icon(10)); 99 0[xX]{HD}+[lLuU]* return (icon(16)); 100 {D}+\.{D}*{EX}?[fFlL]? | 101 {D}+{EX}[fFlL]? | 102 \.{D}+{EX}?[fFlL]? return (fcon()); 103 "=" return (operator(T_ASSIGN, ASSIGN)); 104 "*=" return (operator(T_OPASS, MULASS)); 105 "/=" return (operator(T_OPASS, DIVASS)); 106 "%=" return (operator(T_OPASS, MODASS)); 107 "+=" return (operator(T_OPASS, ADDASS)); 108 "-=" return (operator(T_OPASS, SUBASS)); 109 "<<=" return (operator(T_OPASS, SHLASS)); 110 ">>=" return (operator(T_OPASS, SHRASS)); 111 "&=" return (operator(T_OPASS, ANDASS)); 112 "^=" return (operator(T_OPASS, XORASS)); 113 "|=" return (operator(T_OPASS, ORASS)); 114 "||" return (operator(T_LOGOR, LOGOR)); 115 "&&" return (operator(T_LOGAND, LOGAND)); 116 "|" return (operator(T_OR, OR)); 117 "&" return (operator(T_AND, AND)); 118 "^" return (operator(T_XOR, XOR)); 119 "==" return (operator(T_EQOP, EQ)); 120 "!=" return (operator(T_EQOP, NE)); 121 "<" return (operator(T_RELOP, LT)); 122 ">" return (operator(T_RELOP, GT)); 123 "<=" return (operator(T_RELOP, LE)); 124 ">=" return (operator(T_RELOP, GE)); 125 "<<" return (operator(T_SHFTOP, SHL)); 126 ">>" return (operator(T_SHFTOP, SHR)); 127 "++" return (operator(T_INCDEC, INC)); 128 "--" return (operator(T_INCDEC, DEC)); 129 "->" return (operator(T_STROP, ARROW)); 130 "." return (operator(T_STROP, POINT)); 131 "+" return (operator(T_ADDOP, PLUS)); 132 "-" return (operator(T_ADDOP, MINUS)); 133 "*" return (operator(T_MULT, MULT)); 134 "/" return (operator(T_DIVOP, DIV)); 135 "%" return (operator(T_DIVOP, MOD)); 136 "!" return (operator(T_UNOP, NOT)); 137 "~" return (operator(T_UNOP, COMPL)); 138 "\"" return (string()); 139 "L\"" return (wcstrg()); 140 ";" return (T_SEMI); 141 "{" return (T_LBRACE); 142 "}" return (T_RBRACE); 143 "," return (T_COMMA); 144 ":" return (T_COLON); 145 "?" return (T_QUEST); 146 "[" return (T_LBRACK); 147 "]" return (T_RBRACK); 148 "(" return (T_LPARN); 149 ")" return (T_RPARN); 150 "..." return (T_ELLIPSE); 151 "'" return (ccon()); 152 "L'" return (wccon()); 153 ^#.*$ directive(); 154 \n incline(); 155 \t|" "|\f|\v ; 156 "/*" comment(); 157 "//" slashslashcomment(); 158 . badchar(yytext[0]); 159 160 %% 161 162 static void 163 incline(void) 164 { 165 curr_pos.p_line++; 166 curr_pos.p_uniq = 0; 167 if (curr_pos.p_file == csrc_pos.p_file) { 168 csrc_pos.p_line++; 169 csrc_pos.p_uniq = 0; 170 } 171 } 172 173 static void 174 badchar(int c) 175 { 176 177 /* unknown character \%o */ 178 error(250, c); 179 } 180 181 /* 182 * Keywords. 183 * During initialisation they are written to the symbol table. 184 */ 185 static struct kwtab { 186 const char *kw_name; /* keyword */ 187 int kw_token; /* token returned by yylex() */ 188 scl_t kw_scl; /* storage class if kw_token T_SCLASS */ 189 tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */ 190 tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */ 191 u_int kw_stdc : 1; /* STDC keyword */ 192 u_int kw_gcc : 1; /* GCC keyword */ 193 } kwtab[] = { 194 { "asm", T_ASM, 0, 0, 0, 0, 1 }, 195 { "__asm", T_ASM, 0, 0, 0, 0, 0 }, 196 { "__asm__", T_ASM, 0, 0, 0, 0, 0 }, 197 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 }, 198 { "break", T_BREAK, 0, 0, 0, 0, 0 }, 199 { "case", T_CASE, 0, 0, 0, 0, 0 }, 200 { "char", T_TYPE, 0, CHAR, 0, 0, 0 }, 201 { "const", T_QUAL, 0, 0, CONST, 1, 0 }, 202 { "__const__", T_QUAL, 0, 0, CONST, 0, 0 }, 203 { "__const", T_QUAL, 0, 0, CONST, 0, 0 }, 204 { "continue", T_CONTINUE, 0, 0, 0, 0, 0 }, 205 { "default", T_DEFAULT, 0, 0, 0, 0, 0 }, 206 { "do", T_DO, 0, 0, 0, 0, 0 }, 207 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 }, 208 { "else", T_ELSE, 0, 0, 0, 0, 0 }, 209 { "enum", T_ENUM, 0, 0, 0, 0, 0 }, 210 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 }, 211 { "float", T_TYPE, 0, FLOAT, 0, 0, 0 }, 212 { "for", T_FOR, 0, 0, 0, 0, 0 }, 213 { "goto", T_GOTO, 0, 0, 0, 0, 0 }, 214 { "if", T_IF, 0, 0, 0, 0, 0 }, 215 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 }, 216 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 }, 217 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 }, 218 { "int", T_TYPE, 0, INT, 0, 0, 0 }, 219 { "__symbolrename", T_SYMBOLRENAME, 0, 0, 0, 0, 0 }, 220 { "long", T_TYPE, 0, LONG, 0, 0, 0 }, 221 { "register", T_SCLASS, REG, 0, 0, 0, 0 }, 222 { "return", T_RETURN, 0, 0, 0, 0, 0 }, 223 { "short", T_TYPE, 0, SHORT, 0, 0, 0 }, 224 { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 }, 225 { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 }, 226 { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 }, 227 { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 }, 228 { "static", T_SCLASS, STATIC, 0, 0, 0, 0 }, 229 { "struct", T_SOU, 0, STRUCT, 0, 0, 0 }, 230 { "switch", T_SWITCH, 0, 0, 0, 0, 0 }, 231 { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 }, 232 { "union", T_SOU, 0, UNION, 0, 0, 0 }, 233 { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 }, 234 { "void", T_TYPE, 0, VOID, 0, 0, 0 }, 235 { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 }, 236 { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 237 { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 238 { "while", T_WHILE, 0, 0, 0, 0, 0 }, 239 { NULL, 0, 0, 0, 0, 0, 0 } 240 }; 241 242 /* Symbol table */ 243 static sym_t *symtab[HSHSIZ1]; 244 245 /* bit i of the entry with index i is set */ 246 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT]; 247 248 /* least significant i bits are set in the entry with index i */ 249 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1]; 250 251 /* least significant i bits are not set in the entry with index i */ 252 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1]; 253 254 /* free list for sbuf structures */ 255 static sbuf_t *sbfrlst; 256 257 /* Typ of next expected symbol */ 258 symt_t symtyp; 259 260 261 /* 262 * All keywords are written to the symbol table. This saves us looking 263 * in a extra table for each name we found. 264 */ 265 void 266 initscan(void) 267 { 268 struct kwtab *kw; 269 sym_t *sym; 270 int h, i; 271 uint64_t uq; 272 273 for (kw = kwtab; kw->kw_name != NULL; kw++) { 274 if (kw->kw_stdc && tflag) 275 continue; 276 if (kw->kw_gcc && !gflag) 277 continue; 278 sym = getblk(sizeof (sym_t)); 279 sym->s_name = kw->kw_name; 280 sym->s_keyw = 1; 281 sym->s_value.v_quad = kw->kw_token; 282 if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) { 283 sym->s_tspec = kw->kw_tspec; 284 } else if (kw->kw_token == T_SCLASS) { 285 sym->s_scl = kw->kw_scl; 286 } else if (kw->kw_token == T_QUAL) { 287 sym->s_tqual = kw->kw_tqual; 288 } 289 h = hash(sym->s_name); 290 if ((sym->s_link = symtab[h]) != NULL) 291 symtab[h]->s_rlink = &sym->s_link; 292 (symtab[h] = sym)->s_rlink = &symtab[h]; 293 } 294 295 /* initialize bit-masks for quads */ 296 for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) { 297 qbmasks[i] = (uint64_t)1 << i; 298 uq = ~(uint64_t)0 << i; 299 qumasks[i] = uq; 300 qlmasks[i] = ~uq; 301 } 302 qumasks[i] = 0; 303 qlmasks[i] = ~(uint64_t)0; 304 } 305 306 /* 307 * Get a free sbuf structure, if possible from the free list 308 */ 309 static sbuf_t * 310 allocsb(void) 311 { 312 sbuf_t *sb; 313 314 if ((sb = sbfrlst) != NULL) { 315 sbfrlst = sb->sb_nxt; 316 } else { 317 sb = xmalloc(sizeof (sbuf_t)); 318 } 319 (void)memset(sb, 0, sizeof (sb)); 320 return (sb); 321 } 322 323 /* 324 * Put a sbuf structure to the free list 325 */ 326 static void 327 freesb(sbuf_t *sb) 328 { 329 330 sb->sb_nxt = sbfrlst; 331 sbfrlst = sb; 332 } 333 334 /* 335 * Read a character and ensure that it is positive (except EOF). 336 * Increment line count(s) if necessary. 337 */ 338 static int 339 inpc(void) 340 { 341 int c; 342 343 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n') 344 incline(); 345 return (c); 346 } 347 348 static int 349 hash(const char *s) 350 { 351 u_int v; 352 const u_char *us; 353 354 v = 0; 355 for (us = (const u_char *)s; *us != '\0'; us++) { 356 v = (v << sizeof (v)) + *us; 357 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 358 } 359 return (v % HSHSIZ1); 360 } 361 362 /* 363 * Lex has found a letter followed by zero or more letters or digits. 364 * It looks for a symbol in the symbol table with the same name. This 365 * symbol must either be a keyword or a symbol of the type required by 366 * symtyp (label, member, tag, ...). 367 * 368 * If it is a keyword, the token is returned. In some cases it is described 369 * more deeply by data written to yylval. 370 * 371 * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct 372 * is stored in yylval. This struct contains the name of the symbol, it's 373 * length and hash value. If there is already a symbol of the same name 374 * and type in the symbol table, the sbuf struct also contains a pointer 375 * to the symbol table entry. 376 */ 377 static int 378 name(void) 379 { 380 char *s; 381 sbuf_t *sb; 382 sym_t *sym; 383 int tok; 384 385 sb = allocsb(); 386 sb->sb_name = yytext; 387 sb->sb_len = yyleng; 388 sb->sb_hash = hash(yytext); 389 390 if ((sym = search(sb)) != NULL && sym->s_keyw) { 391 freesb(sb); 392 return (keyw(sym)); 393 } 394 395 sb->sb_sym = sym; 396 397 if (sym != NULL) { 398 if (blklev < sym->s_blklev) 399 lerror("name() 1"); 400 sb->sb_name = sym->s_name; 401 sb->sb_len = strlen(sym->s_name); 402 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 403 } else { 404 s = getblk(yyleng + 1); 405 (void)memcpy(s, yytext, yyleng + 1); 406 sb->sb_name = s; 407 sb->sb_len = yyleng; 408 tok = T_NAME; 409 } 410 411 yylval.y_sb = sb; 412 return (tok); 413 } 414 415 static sym_t * 416 search(sbuf_t *sb) 417 { 418 sym_t *sym; 419 420 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) { 421 if (strcmp(sym->s_name, sb->sb_name) == 0) { 422 if (sym->s_keyw || sym->s_kind == symtyp) 423 return (sym); 424 } 425 } 426 427 return (NULL); 428 } 429 430 static int 431 keyw(sym_t *sym) 432 { 433 int t; 434 435 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) { 436 yylval.y_scl = sym->s_scl; 437 } else if (t == T_TYPE || t == T_SOU) { 438 yylval.y_tspec = sym->s_tspec; 439 } else if (t == T_QUAL) { 440 yylval.y_tqual = sym->s_tqual; 441 } 442 return (t); 443 } 444 445 /* 446 * Convert a string representing an integer into internal representation. 447 * The value is returned in yylval. icon() (and yylex()) returns T_CON. 448 */ 449 static int 450 icon(int base) 451 { 452 int l_suffix, u_suffix; 453 int len; 454 const char *cp; 455 char c, *eptr; 456 tspec_t typ; 457 u_long ul = 0; 458 uint64_t uq = 0; 459 int ansiu; 460 static tspec_t contypes[2][3] = { 461 { INT, LONG, QUAD }, 462 { UINT, ULONG, UQUAD } 463 }; 464 465 cp = yytext; 466 len = yyleng; 467 468 /* skip 0x */ 469 if (base == 16) { 470 cp += 2; 471 len -= 2; 472 } 473 474 /* read suffixes */ 475 l_suffix = u_suffix = 0; 476 for ( ; ; ) { 477 if ((c = cp[len - 1]) == 'l' || c == 'L') { 478 l_suffix++; 479 } else if (c == 'u' || c == 'U') { 480 u_suffix++; 481 } else { 482 break; 483 } 484 len--; 485 } 486 if (l_suffix > 2 || u_suffix > 1) { 487 /* malformed integer constant */ 488 warning(251); 489 if (l_suffix > 2) 490 l_suffix = 2; 491 if (u_suffix > 1) 492 u_suffix = 1; 493 } 494 if (tflag && u_suffix != 0) { 495 /* suffix U is illegal in traditional C */ 496 warning(97); 497 } 498 typ = contypes[u_suffix][l_suffix]; 499 500 errno = 0; 501 if (l_suffix < 2) { 502 ul = strtoul(cp, &eptr, base); 503 } else { 504 uq = strtouq(cp, &eptr, base); 505 } 506 if (eptr != cp + len) 507 lerror("icon() 1"); 508 if (errno != 0) 509 /* integer constant out of range */ 510 warning(252); 511 512 /* 513 * If the value is to big for the current type, we must choose 514 * another type. 515 */ 516 ansiu = 0; 517 switch (typ) { 518 case INT: 519 if (ul <= INT_MAX) { 520 /* ok */ 521 } else if (ul <= (unsigned)UINT_MAX && base != 10) { 522 typ = UINT; 523 } else if (ul <= LONG_MAX) { 524 typ = LONG; 525 } else { 526 typ = ULONG; 527 } 528 if (typ == UINT || typ == ULONG) { 529 if (tflag) { 530 typ = LONG; 531 } else if (!sflag) { 532 /* 533 * Remember that the constant is unsigned 534 * only in ANSI C 535 */ 536 ansiu = 1; 537 } 538 } 539 break; 540 case UINT: 541 if (ul > (u_int)UINT_MAX) 542 typ = ULONG; 543 break; 544 case LONG: 545 if (ul > LONG_MAX && !tflag) { 546 typ = ULONG; 547 if (!sflag) 548 ansiu = 1; 549 } 550 break; 551 case QUAD: 552 if (uq > QUAD_MAX && !tflag) { 553 typ = UQUAD; 554 if (!sflag) 555 ansiu = 1; 556 } 557 break; 558 /* LINTED (enumeration values not handled in switch) */ 559 case STRUCT: 560 case VOID: 561 case LDOUBLE: 562 case FUNC: 563 case ARRAY: 564 case PTR: 565 case ENUM: 566 case UNION: 567 case SIGNED: 568 case NOTSPEC: 569 case DOUBLE: 570 case FLOAT: 571 case UQUAD: 572 case ULONG: 573 case USHORT: 574 case SHORT: 575 case UCHAR: 576 case SCHAR: 577 case CHAR: 578 case UNSIGN: 579 break; 580 581 case NTSPEC: /* this value unused */ 582 break; 583 } 584 585 if (typ != QUAD && typ != UQUAD) { 586 if (isutyp(typ)) { 587 uq = ul; 588 } else { 589 uq = (int64_t)(long)ul; 590 } 591 } 592 593 uq = (uint64_t)xsign((int64_t)uq, typ, -1); 594 595 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ; 596 yylval.y_val->v_ansiu = ansiu; 597 yylval.y_val->v_quad = (int64_t)uq; 598 599 return (T_CON); 600 } 601 602 /* 603 * Returns 1 if t is a signed type and the value is negative. 604 * 605 * len is the number of significant bits. If len is -1, len is set 606 * to the width of type t. 607 */ 608 int 609 sign(int64_t q, tspec_t t, int len) 610 { 611 612 if (t == PTR || isutyp(t)) 613 return (0); 614 return (msb(q, t, len)); 615 } 616 617 int 618 msb(int64_t q, tspec_t t, int len) 619 { 620 621 if (len <= 0) 622 len = size(t); 623 return ((q & qbmasks[len - 1]) != 0); 624 } 625 626 /* 627 * Extends the sign of q. 628 */ 629 int64_t 630 xsign(int64_t q, tspec_t t, int len) 631 { 632 633 if (len <= 0) 634 len = size(t); 635 636 if (t == PTR || isutyp(t) || !sign(q, t, len)) { 637 q &= qlmasks[len]; 638 } else { 639 q |= qumasks[len]; 640 } 641 return (q); 642 } 643 644 /* 645 * Convert a string representing a floating point value into its interal 646 * representation. Type and value are returned in yylval. fcon() 647 * (and yylex()) returns T_CON. 648 * XXX Currently it is not possible to convert constants of type 649 * long double which are greater than DBL_MAX. 650 */ 651 static int 652 fcon(void) 653 { 654 const char *cp; 655 int len; 656 tspec_t typ; 657 char c, *eptr; 658 double d; 659 float f = 0; 660 661 cp = yytext; 662 len = yyleng; 663 664 if ((c = cp[len - 1]) == 'f' || c == 'F') { 665 typ = FLOAT; 666 len--; 667 } else if (c == 'l' || c == 'L') { 668 typ = LDOUBLE; 669 len--; 670 } else { 671 typ = DOUBLE; 672 } 673 674 if (tflag && typ != DOUBLE) { 675 /* suffixes F and L are illegal in traditional C */ 676 warning(98); 677 } 678 679 errno = 0; 680 d = strtod(cp, &eptr); 681 if (eptr != cp + len) 682 lerror("fcon() 1"); 683 if (errno != 0) 684 /* floating-point constant out of range */ 685 warning(248); 686 687 if (typ == FLOAT) { 688 f = (float)d; 689 if (!finite(f)) { 690 /* floating-point constant out of range */ 691 warning(248); 692 f = f > 0 ? FLT_MAX : -FLT_MAX; 693 } 694 } 695 696 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ; 697 if (typ == FLOAT) { 698 yylval.y_val->v_ldbl = f; 699 } else { 700 yylval.y_val->v_ldbl = d; 701 } 702 703 return (T_CON); 704 } 705 706 static int 707 operator(int t, op_t o) 708 { 709 710 yylval.y_op = o; 711 return (t); 712 } 713 714 /* 715 * Called if lex found a leading \'. 716 */ 717 static int 718 ccon(void) 719 { 720 int n, val, c; 721 char cv; 722 723 n = 0; 724 val = 0; 725 while ((c = getescc('\'')) >= 0) { 726 val = (val << CHAR_BIT) + c; 727 n++; 728 } 729 if (c == -2) { 730 /* unterminated character constant */ 731 error(253); 732 } else { 733 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) { 734 /* too many characters in character constant */ 735 error(71); 736 } else if (n > 1) { 737 /* multi-character character constant */ 738 warning(294); 739 } else if (n == 0) { 740 /* empty character constant */ 741 error(73); 742 } 743 } 744 if (n == 1) { 745 cv = (char)val; 746 val = cv; 747 } 748 749 yylval.y_val = xcalloc(1, sizeof (val_t)); 750 yylval.y_val->v_tspec = INT; 751 yylval.y_val->v_quad = val; 752 753 return (T_CON); 754 } 755 756 /* 757 * Called if lex found a leading L\' 758 */ 759 static int 760 wccon(void) 761 { 762 static char buf[MB_LEN_MAX + 1]; 763 int i, c; 764 wchar_t wc; 765 766 i = 0; 767 while ((c = getescc('\'')) >= 0) { 768 if (i < MB_CUR_MAX) 769 buf[i] = (char)c; 770 i++; 771 } 772 773 wc = 0; 774 775 if (c == -2) { 776 /* unterminated character constant */ 777 error(253); 778 } else if (c == 0) { 779 /* empty character constant */ 780 error(73); 781 } else { 782 if (i > MB_CUR_MAX) { 783 i = MB_CUR_MAX; 784 /* too many characters in character constant */ 785 error(71); 786 } else { 787 buf[i] = '\0'; 788 (void)mbtowc(NULL, NULL, 0); 789 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0) 790 /* invalid multibyte character */ 791 error(291); 792 } 793 } 794 795 yylval.y_val = xcalloc(1, sizeof (val_t)); 796 yylval.y_val->v_tspec = WCHAR; 797 yylval.y_val->v_quad = wc; 798 799 return (T_CON); 800 } 801 802 /* 803 * Read a character which is part of a character constant or of a string 804 * and handle escapes. 805 * 806 * The Argument is the character which delimits the character constant or 807 * string. 808 * 809 * Returns -1 if the end of the character constant or string is reached, 810 * -2 if the EOF is reached, and the character otherwise. 811 */ 812 static int 813 getescc(int d) 814 { 815 static int pbc = -1; 816 int n, c, v; 817 818 if (pbc == -1) { 819 c = inpc(); 820 } else { 821 c = pbc; 822 pbc = -1; 823 } 824 if (c == d) 825 return (-1); 826 switch (c) { 827 case '\n': 828 if (tflag) { 829 /* newline in string or char constant */ 830 error(254); 831 return (-2); 832 } 833 return (c); 834 case EOF: 835 return (-2); 836 case '\\': 837 switch (c = inpc()) { 838 case '"': 839 if (tflag && d == '\'') 840 /* \" inside character constant undef. ... */ 841 warning(262); 842 return ('"'); 843 case '\'': 844 return ('\''); 845 case '?': 846 if (tflag) 847 /* \? undefined in traditional C */ 848 warning(263); 849 return ('?'); 850 case '\\': 851 return ('\\'); 852 case 'a': 853 if (tflag) 854 /* \a undefined in traditional C */ 855 warning(81); 856 return ('\a'); 857 case 'b': 858 return ('\b'); 859 case 'f': 860 return ('\f'); 861 case 'n': 862 return ('\n'); 863 case 'r': 864 return ('\r'); 865 case 't': 866 return ('\t'); 867 case 'v': 868 if (tflag) 869 /* \v undefined in traditional C */ 870 warning(264); 871 return ('\v'); 872 case '8': case '9': 873 /* bad octal digit %c */ 874 warning(77, c); 875 /* FALLTHROUGH */ 876 case '0': case '1': case '2': case '3': 877 case '4': case '5': case '6': case '7': 878 n = 3; 879 v = 0; 880 do { 881 v = (v << 3) + (c - '0'); 882 c = inpc(); 883 } while (--n && isdigit(c) && (tflag || c <= '7')); 884 if (tflag && n > 0 && isdigit(c)) 885 /* bad octal digit %c */ 886 warning(77, c); 887 pbc = c; 888 if (v > UCHAR_MAX) { 889 /* character escape does not fit in char. */ 890 warning(76); 891 v &= CHAR_MASK; 892 } 893 return (v); 894 case 'x': 895 if (tflag) 896 /* \x undefined in traditional C */ 897 warning(82); 898 v = 0; 899 n = 0; 900 while ((c = inpc()) >= 0 && isxdigit(c)) { 901 c = isdigit(c) ? 902 c - '0' : toupper(c) - 'A' + 10; 903 v = (v << 4) + c; 904 if (n >= 0) { 905 if ((v & ~CHAR_MASK) != 0) { 906 /* overflow in hex escape */ 907 warning(75); 908 n = -1; 909 } else { 910 n++; 911 } 912 } 913 } 914 pbc = c; 915 if (n == 0) { 916 /* no hex digits follow \x */ 917 error(74); 918 } if (n == -1) { 919 v &= CHAR_MASK; 920 } 921 return (v); 922 case '\n': 923 return (getescc(d)); 924 case EOF: 925 return (-2); 926 default: 927 if (isprint(c)) { 928 /* dubious escape \%c */ 929 warning(79, c); 930 } else { 931 /* dubious escape \%o */ 932 warning(80, c); 933 } 934 } 935 } 936 return (c); 937 } 938 939 /* 940 * Called for preprocessor directives. Currently implemented are: 941 * # lineno 942 * # lineno "filename" 943 */ 944 static void 945 directive(void) 946 { 947 const char *cp, *fn; 948 char c, *eptr; 949 size_t fnl; 950 long ln; 951 static int first = 1; 952 953 /* Go to first non-whitespace after # */ 954 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) 955 continue; 956 957 if (!isdigit((unsigned char)c)) { 958 error: 959 /* undefined or invalid # directive */ 960 warning(255); 961 return; 962 } 963 ln = strtol(--cp, &eptr, 10); 964 if (cp == eptr) 965 goto error; 966 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0') 967 goto error; 968 while ((c = *cp++) == ' ' || c == '\t') 969 continue; 970 if (c != '\0') { 971 if (c != '"') 972 goto error; 973 fn = cp; 974 while ((c = *cp) != '"' && c != '\0') 975 cp++; 976 if (c != '"') 977 goto error; 978 if ((fnl = cp++ - fn) > PATH_MAX) 979 goto error; 980 while ((c = *cp++) == ' ' || c == '\t') 981 continue; 982 #if 0 983 if (c != '\0') 984 warning("extra character(s) after directive"); 985 #endif 986 987 /* empty string means stdin */ 988 if (fnl == 0) { 989 fn = "{standard input}"; 990 fnl = 16; /* strlen (fn) */ 991 } 992 curr_pos.p_file = fnnalloc(fn, fnl); 993 /* 994 * If this is the first directive, the name is the name 995 * of the C source file as specified at the command line. 996 * It is written to the output file. 997 */ 998 if (first) { 999 csrc_pos.p_file = curr_pos.p_file; 1000 outsrc(curr_pos.p_file); 1001 first = 0; 1002 } 1003 } 1004 curr_pos.p_line = (int)ln - 1; 1005 curr_pos.p_uniq = 0; 1006 if (curr_pos.p_file == csrc_pos.p_file) { 1007 csrc_pos.p_line = (int)ln - 1; 1008 csrc_pos.p_uniq = 0; 1009 } 1010 } 1011 1012 /* 1013 * Handle lint comments. Following comments are currently understood: 1014 * ARGSUSEDn 1015 * BITFIELDTYPE 1016 * CONSTCOND CONSTANTCOND CONSTANTCONDITION 1017 * FALLTHRU FALLTHROUGH 1018 * LINTLIBRARY 1019 * LINTED NOSTRICT 1020 * LONGLONG 1021 * NOTREACHED 1022 * PRINTFLIKEn 1023 * PROTOLIB 1024 * SCANFLIKEn 1025 * VARARGSn 1026 * If one of this comments is recognized, the arguments, if any, are 1027 * parsed and a function which handles this comment is called. 1028 */ 1029 static void 1030 comment(void) 1031 { 1032 int c, lc; 1033 static struct { 1034 const char *keywd; 1035 int arg; 1036 void (*func)(int); 1037 } keywtab[] = { 1038 { "ARGSUSED", 1, argsused }, 1039 { "BITFIELDTYPE", 0, bitfieldtype }, 1040 { "CONSTCOND", 0, constcond }, 1041 { "CONSTANTCOND", 0, constcond }, 1042 { "CONSTANTCONDITION", 0, constcond }, 1043 { "FALLTHRU", 0, fallthru }, 1044 { "FALLTHROUGH", 0, fallthru }, 1045 { "LINTLIBRARY", 0, lintlib }, 1046 { "LINTED", 0, linted }, 1047 { "LONGLONG", 0, longlong }, 1048 { "NOSTRICT", 0, linted }, 1049 { "NOTREACHED", 0, notreach }, 1050 { "PRINTFLIKE", 1, printflike }, 1051 { "PROTOLIB", 1, protolib }, 1052 { "SCANFLIKE", 1, scanflike }, 1053 { "VARARGS", 1, varargs }, 1054 }; 1055 char keywd[32]; 1056 char arg[32]; 1057 int l, i, a; 1058 int eoc; 1059 1060 eoc = 0; 1061 1062 /* Skip white spaces after the start of the comment */ 1063 while ((c = inpc()) != EOF && isspace(c)) 1064 continue; 1065 1066 /* Read the potential keyword to keywd */ 1067 l = 0; 1068 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) { 1069 keywd[l++] = (char)c; 1070 c = inpc(); 1071 } 1072 keywd[l] = '\0'; 1073 1074 /* look for the keyword */ 1075 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) { 1076 if (strcmp(keywtab[i].keywd, keywd) == 0) 1077 break; 1078 } 1079 if (i == sizeof (keywtab) / sizeof (keywtab[0])) 1080 goto skip_rest; 1081 1082 /* skip white spaces after the keyword */ 1083 while (c != EOF && isspace(c)) 1084 c = inpc(); 1085 1086 /* read the argument, if the keyword accepts one and there is one */ 1087 l = 0; 1088 if (keywtab[i].arg) { 1089 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) { 1090 arg[l++] = (char)c; 1091 c = inpc(); 1092 } 1093 } 1094 arg[l] = '\0'; 1095 a = l != 0 ? atoi(arg) : -1; 1096 1097 /* skip white spaces after the argument */ 1098 while (c != EOF && isspace(c)) 1099 c = inpc(); 1100 1101 if (c != '*' || (c = inpc()) != '/') { 1102 if (keywtab[i].func != linted) 1103 /* extra characters in lint comment */ 1104 warning(257); 1105 } else { 1106 /* 1107 * remember that we have already found the end of the 1108 * comment 1109 */ 1110 eoc = 1; 1111 } 1112 1113 if (keywtab[i].func != NULL) 1114 (*keywtab[i].func)(a); 1115 1116 skip_rest: 1117 while (!eoc) { 1118 lc = c; 1119 if ((c = inpc()) == EOF) { 1120 /* unterminated comment */ 1121 error(256); 1122 break; 1123 } 1124 if (lc == '*' && c == '/') 1125 eoc = 1; 1126 } 1127 } 1128 1129 /* 1130 * Handle // style comments 1131 */ 1132 static void 1133 slashslashcomment(void) 1134 { 1135 int c; 1136 1137 if (sflag < 2 && !gflag) 1138 /* // comments only supported in C99 */ 1139 (void)gnuism(312, tflag ? "traditional" : "ANSI"); 1140 1141 while ((c = inpc()) != EOF && c != '\n') 1142 continue; 1143 } 1144 1145 /* 1146 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND. 1147 * clrwflgs() is called after function definitions and global and 1148 * local declarations and definitions. It is also called between 1149 * the controlling expression and the body of control statements 1150 * (if, switch, for, while). 1151 */ 1152 void 1153 clrwflgs(void) 1154 { 1155 1156 nowarn = 0; 1157 quadflg = 0; 1158 ccflg = 0; 1159 } 1160 1161 /* 1162 * Strings are stored in a dynamically alloceted buffer and passed 1163 * in yylval.y_xstrg to the parser. The parser or the routines called 1164 * by the parser are responsible for freeing this buffer. 1165 */ 1166 static int 1167 string(void) 1168 { 1169 u_char *s; 1170 int c; 1171 size_t len, max; 1172 strg_t *strg; 1173 1174 s = xmalloc(max = 64); 1175 1176 len = 0; 1177 while ((c = getescc('"')) >= 0) { 1178 /* +1 to reserve space for a trailing NUL character */ 1179 if (len + 1 == max) 1180 s = xrealloc(s, max *= 2); 1181 s[len++] = (char)c; 1182 } 1183 s[len] = '\0'; 1184 if (c == -2) 1185 /* unterminated string constant */ 1186 error(258); 1187 1188 strg = xcalloc(1, sizeof (strg_t)); 1189 strg->st_tspec = CHAR; 1190 strg->st_len = len; 1191 strg->st_cp = s; 1192 1193 yylval.y_strg = strg; 1194 return (T_STRING); 1195 } 1196 1197 static int 1198 wcstrg(void) 1199 { 1200 char *s; 1201 int c, i, n, wi; 1202 size_t len, max, wlen; 1203 wchar_t *ws; 1204 strg_t *strg; 1205 1206 s = xmalloc(max = 64); 1207 len = 0; 1208 while ((c = getescc('"')) >= 0) { 1209 /* +1 to save space for a trailing NUL character */ 1210 if (len + 1 >= max) 1211 s = xrealloc(s, max *= 2); 1212 s[len++] = (char)c; 1213 } 1214 s[len] = '\0'; 1215 if (c == -2) 1216 /* unterminated string constant */ 1217 error(258); 1218 1219 /* get length of wide character string */ 1220 (void)mblen(NULL, 0); 1221 for (i = 0, wlen = 0; i < len; i += n, wlen++) { 1222 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) { 1223 /* invalid multibyte character */ 1224 error(291); 1225 break; 1226 } 1227 if (n == 0) 1228 n = 1; 1229 } 1230 1231 ws = xmalloc((wlen + 1) * sizeof (wchar_t)); 1232 1233 /* convert from multibyte to wide char */ 1234 (void)mbtowc(NULL, NULL, 0); 1235 for (i = 0, wi = 0; i < len; i += n, wi++) { 1236 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1) 1237 break; 1238 if (n == 0) 1239 n = 1; 1240 } 1241 ws[wi] = 0; 1242 free(s); 1243 1244 strg = xcalloc(1, sizeof (strg_t)); 1245 strg->st_tspec = WCHAR; 1246 strg->st_len = wlen; 1247 strg->st_wcp = ws; 1248 1249 yylval.y_strg = strg; 1250 return (T_STRING); 1251 } 1252 1253 /* 1254 * As noted above the scanner does not create new symbol table entries 1255 * for symbols it cannot find in the symbol table. This is to avoid 1256 * putting undeclared symbols into the symbol table if a syntax error 1257 * occurs. 1258 * 1259 * getsym() is called as soon as it is probably ok to put the symbol to 1260 * the symbol table. This does not mean that it is not possible that 1261 * symbols are put to the symbol table which are than not completely 1262 * declared due to syntax errors. To avoid too many problems in this 1263 * case symbols get type int in getsym(). 1264 * 1265 * XXX calls to getsym() should be delayed until decl1*() is called 1266 */ 1267 sym_t * 1268 getsym(sbuf_t *sb) 1269 { 1270 dinfo_t *di; 1271 char *s; 1272 sym_t *sym; 1273 1274 sym = sb->sb_sym; 1275 1276 /* 1277 * During member declaration it is possible that name() looked 1278 * for symbols of type FVFT, although it should have looked for 1279 * symbols of type FTAG. Same can happen for labels. Both cases 1280 * are compensated here. 1281 */ 1282 if (symtyp == FMOS || symtyp == FLAB) { 1283 if (sym == NULL || sym->s_kind == FVFT) 1284 sym = search(sb); 1285 } 1286 1287 if (sym != NULL) { 1288 if (sym->s_kind != symtyp) 1289 lerror("storesym() 1"); 1290 symtyp = FVFT; 1291 freesb(sb); 1292 return (sym); 1293 } 1294 1295 /* create a new symbol table entry */ 1296 1297 /* labels must always be allocated at level 1 (outhermost block) */ 1298 if (symtyp == FLAB) { 1299 sym = getlblk(1, sizeof (sym_t)); 1300 s = getlblk(1, sb->sb_len + 1); 1301 (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 1302 sym->s_name = s; 1303 sym->s_blklev = 1; 1304 di = dcs; 1305 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL) 1306 di = di->d_nxt; 1307 if (di->d_ctx != AUTO) 1308 lerror("storesym() 2"); 1309 } else { 1310 sym = getblk(sizeof (sym_t)); 1311 sym->s_name = sb->sb_name; 1312 sym->s_blklev = blklev; 1313 di = dcs; 1314 } 1315 1316 UNIQUE_CURR_POS(sym->s_dpos); 1317 if ((sym->s_kind = symtyp) != FLAB) 1318 sym->s_type = gettyp(INT); 1319 1320 symtyp = FVFT; 1321 1322 if ((sym->s_link = symtab[sb->sb_hash]) != NULL) 1323 symtab[sb->sb_hash]->s_rlink = &sym->s_link; 1324 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash]; 1325 1326 *di->d_ldlsym = sym; 1327 di->d_ldlsym = &sym->s_dlnxt; 1328 1329 freesb(sb); 1330 return (sym); 1331 } 1332 1333 /* 1334 * Remove a symbol forever from the symbol table. s_blklev 1335 * is set to -1 to avoid that the symbol will later be put 1336 * back to the symbol table. 1337 */ 1338 void 1339 rmsym(sym_t *sym) 1340 { 1341 1342 if ((*sym->s_rlink = sym->s_link) != NULL) 1343 sym->s_link->s_rlink = sym->s_rlink; 1344 sym->s_blklev = -1; 1345 sym->s_link = NULL; 1346 } 1347 1348 /* 1349 * Remove a list of symbols declared at one level from the symbol 1350 * table. 1351 */ 1352 void 1353 rmsyms(sym_t *syms) 1354 { 1355 sym_t *sym; 1356 1357 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) { 1358 if (sym->s_blklev != -1) { 1359 if ((*sym->s_rlink = sym->s_link) != NULL) 1360 sym->s_link->s_rlink = sym->s_rlink; 1361 sym->s_link = NULL; 1362 sym->s_rlink = NULL; 1363 } 1364 } 1365 } 1366 1367 /* 1368 * Put a symbol into the symbol table 1369 */ 1370 void 1371 inssym(int bl, sym_t *sym) 1372 { 1373 int h; 1374 1375 h = hash(sym->s_name); 1376 if ((sym->s_link = symtab[h]) != NULL) 1377 symtab[h]->s_rlink = &sym->s_link; 1378 (symtab[h] = sym)->s_rlink = &symtab[h]; 1379 sym->s_blklev = bl; 1380 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev) 1381 lerror("inssym()"); 1382 } 1383 1384 /* 1385 * Called at level 0 after syntax errors 1386 * Removes all symbols which are not declared at level 0 from the 1387 * symbol table. Also frees all memory which is not associated with 1388 * level 0. 1389 */ 1390 void 1391 cleanup(void) 1392 { 1393 sym_t *sym, *nsym; 1394 int i; 1395 1396 for (i = 0; i < HSHSIZ1; i++) { 1397 for (sym = symtab[i]; sym != NULL; sym = nsym) { 1398 nsym = sym->s_link; 1399 if (sym->s_blklev >= 1) { 1400 if ((*sym->s_rlink = nsym) != NULL) 1401 nsym->s_rlink = sym->s_rlink; 1402 } 1403 } 1404 } 1405 1406 for (i = mblklev; i > 0; i--) 1407 freelblk(i); 1408 } 1409 1410 /* 1411 * Create a new symbol with the name of an existing symbol. 1412 */ 1413 sym_t * 1414 pushdown(sym_t *sym) 1415 { 1416 int h; 1417 sym_t *nsym; 1418 1419 h = hash(sym->s_name); 1420 nsym = getblk(sizeof (sym_t)); 1421 if (sym->s_blklev > blklev) 1422 lerror("pushdown()"); 1423 nsym->s_name = sym->s_name; 1424 UNIQUE_CURR_POS(nsym->s_dpos); 1425 nsym->s_kind = sym->s_kind; 1426 nsym->s_blklev = blklev; 1427 1428 if ((nsym->s_link = symtab[h]) != NULL) 1429 symtab[h]->s_rlink = &nsym->s_link; 1430 (symtab[h] = nsym)->s_rlink = &symtab[h]; 1431 1432 *dcs->d_ldlsym = nsym; 1433 dcs->d_ldlsym = &nsym->s_dlnxt; 1434 1435 return (nsym); 1436 } 1437 1438 /* 1439 * Free any dynamically allocated memory referenced by 1440 * the value stack or yylval. 1441 * The type of information in yylval is described by tok. 1442 */ 1443 void 1444 freeyyv(void *sp, int tok) 1445 { 1446 if (tok == T_NAME || tok == T_TYPENAME) { 1447 sbuf_t *sb = *(sbuf_t **)sp; 1448 freesb(sb); 1449 } else if (tok == T_CON) { 1450 val_t *val = *(val_t **)sp; 1451 free(val); 1452 } else if (tok == T_STRING) { 1453 strg_t *strg = *(strg_t **)sp; 1454 if (strg->st_tspec == CHAR) { 1455 free(strg->st_cp); 1456 } else if (strg->st_tspec == WCHAR) { 1457 free(strg->st_wcp); 1458 } else { 1459 lerror("fryylv() 1"); 1460 } 1461 free(strg); 1462 } 1463 } 1464