comparison lwcc/preproc.c @ 305:54f213c8fb81 ccdev

Various bugfixes and output tuning Tuned output of preprocessor to include line markers similar to the ones added by the gcc preprocessor. Also, many fixes for various bits of dumbosity leading to misbehaviour and crashing.
author William Astle <lost@l-w.ca>
date Wed, 18 Sep 2013 19:17:52 -0600
parents d85d173ba120
children b08787e5b9f3
comparison
equal deleted inserted replaced
304:d85d173ba120 305:54f213c8fb81
38 38
39 39
40 struct token *preproc_next_processed_token(struct preproc_info *pp) 40 struct token *preproc_next_processed_token(struct preproc_info *pp)
41 { 41 {
42 struct token *ct; 42 struct token *ct;
43 43
44 again: 44 again:
45 ct = preproc_next_token(pp); 45 ct = preproc_next_token(pp);
46 if (ct -> ttype == TOK_EOF) 46 if (ct -> ttype == TOK_EOF)
47 return ct; 47 return ct;
48 if (ct -> ttype == TOK_EOL) 48 if (ct -> ttype == TOK_EOL)
49 {
49 pp -> ppeolseen = 1; 50 pp -> ppeolseen = 1;
50 51 return ct;
52 }
53
51 if (ct -> ttype == TOK_HASH && pp -> ppeolseen == 1) 54 if (ct -> ttype == TOK_HASH && pp -> ppeolseen == 1)
52 { 55 {
53 // preprocessor directive 56 // preprocessor directive
54 process_directive(pp); 57 process_directive(pp);
58 goto again;
55 } 59 }
56 // if we're in a false section, don't return the token; keep scanning 60 // if we're in a false section, don't return the token; keep scanning
57 if (pp -> skip_level) 61 if (pp -> skip_level)
58 goto again; 62 goto again;
59 63
106 110
107 static void check_eol(struct preproc_info *pp) 111 static void check_eol(struct preproc_info *pp)
108 { 112 {
109 struct token *t; 113 struct token *t;
110 114
111 t = preproc_next_token(pp); 115 t = preproc_next_token_nws(pp);
112 if (t -> ttype != TOK_EOL) 116 if (t -> ttype != TOK_EOL)
113 preproc_throw_warning(pp, "Extra text after preprocessor directive"); 117 preproc_throw_warning(pp, "Extra text after preprocessor directive");
114 skip_eol(pp); 118 skip_eol(pp);
115 } 119 }
116 120
342 while (nargs > 0) 346 while (nargs > 0)
343 lw_free(arglist[--nargs]); 347 lw_free(arglist[--nargs]);
344 lw_free(arglist); 348 lw_free(arglist);
345 return; 349 return;
346 } 350 }
347 351
352 tl = token_list_create();
348 for (;;) 353 for (;;)
349 { 354 {
350 ct = preproc_next_token(pp); 355 ct = preproc_next_token(pp);
351 if (ct -> ttype == TOK_EOL) 356 if (ct -> ttype == TOK_EOL)
352 break; 357 break;
457 struct strbuf *strbuf; 462 struct strbuf *strbuf;
458 int i; 463 int i;
459 struct preproc_info *fs; 464 struct preproc_info *fs;
460 465
461 ct = preproc_next_token_nws(pp); 466 ct = preproc_next_token_nws(pp);
462 if (ct -> ttype == TOK_STRING) 467 if (ct -> ttype == TOK_STR_LIT)
463 { 468 {
464 usrinc: 469 usrinc:
465 sys = strlen(ct -> strval); 470 sys = strlen(ct -> strval);
466 fn = lw_alloc(sys - 1); 471 fn = lw_alloc(sys - 1);
467 memcpy(fn, ct -> strval + 1, sys - 2); 472 memcpy(fn, ct -> strval + 1, sys - 2);
468 fn[sys - 1] = 0; 473 fn[sys - 2] = 0;
469 sys = 0; 474 sys = 0;
470 goto doinc; 475 goto doinc;
471 } 476 }
472 else if (ct -> ttype == TOK_LT) 477 else if (ct -> ttype == TOK_LT)
473 { 478 {
474 strbuf = strbuf_new(); 479 strbuf = strbuf_new();
475 for (;;) 480 for (;;)
476 { 481 {
477 ct = preproc_next_token(pp); 482 int c;
478 if (ct -> ttype == TOK_GT) 483 c = preproc_lex_fetch_byte(pp);
479 break; 484 if (c == CPP_EOL)
480 if (ct -> ttype == TOK_EOL) 485 {
481 { 486 preproc_lex_unfetch_byte(pp, c);
482 preproc_throw_error(pp, "Bad #include"); 487 preproc_throw_error(pp, "Bad #include");
483 lw_free(strbuf_end(strbuf)); 488 lw_free(strbuf_end(strbuf));
484 return; 489 break;
485 } 490 }
486 for (i = 0; ct -> strval[i]; ct++) 491 if (c == '>')
487 { 492 break;
488 strbuf_add(strbuf, ct -> strval[i]); 493 strbuf_add(strbuf, c);
489 }
490 } 494 }
491 ct = preproc_next_token_nws(pp); 495 ct = preproc_next_token_nws(pp);
492 if (ct -> ttype != TOK_EOL) 496 if (ct -> ttype != TOK_EOL)
493 { 497 {
494 preproc_throw_error(pp, "Bad #include"); 498 preproc_throw_error(pp, "Bad #include");
503 else 507 else
504 { 508 {
505 preproc_unget_token(pp, ct); 509 preproc_unget_token(pp, ct);
506 // computed include 510 // computed include
507 ct = preproc_next_processed_token_nws(pp); 511 ct = preproc_next_processed_token_nws(pp);
508 if (ct -> ttype == TOK_STRING) 512 if (ct -> ttype == TOK_STR_LIT)
509 goto usrinc; 513 goto usrinc;
510 else if (ct -> ttype == TOK_LT) 514 else if (ct -> ttype == TOK_LT)
511 { 515 {
512 strbuf = strbuf_new(); 516 strbuf = strbuf_new();
513 for (;;) 517 for (;;)
548 doinc: 552 doinc:
549 // fn = preproc_find_file(pp, fn, sys); 553 // fn = preproc_find_file(pp, fn, sys);
550 fp = fopen(fn, "rb"); 554 fp = fopen(fn, "rb");
551 if (!fp) 555 if (!fp)
552 { 556 {
553 preproc_throw_error(pp, "Cannot open #include file - this is fatal"); 557 preproc_throw_error(pp, "Cannot open #include file %s - this is fatal", fn);
554 exit(1); 558 exit(1);
555 } 559 }
556 560
557 /* save the current include file state, etc. */ 561 /* save the current include file state, etc. */
558 fs = lw_alloc(sizeof(struct preproc_info)); 562 fs = lw_alloc(sizeof(struct preproc_info));
559 *fs = *pp; 563 *fs = *pp;
560 fs -> n = pp -> filestack; 564 fs -> n = pp -> filestack;
565 pp -> curtok = NULL;
561 pp -> filestack = fs; 566 pp -> filestack = fs;
562 pp -> fn = fn; 567 pp -> fn = fn;
563 pp -> fp = fp; 568 pp -> fp = fp;
564 pp -> ra = CPP_NOUNG; 569 pp -> ra = CPP_NOUNG;
565 pp -> ppeolseen = 1; 570 pp -> ppeolseen = 1;
566 pp -> eolstate = 0; 571 pp -> eolstate = 0;
567 pp -> lineno = 0; 572 pp -> lineno = 1;
568 pp -> column = 0; 573 pp -> column = 0;
569 pp -> qseen = 0; 574 pp -> qseen = 0;
570 pp -> ungetbufl = 0; 575 pp -> ungetbufl = 0;
571 pp -> ungetbufs = 0; 576 pp -> ungetbufs = 0;
572 pp -> ungetbuf = NULL; 577 pp -> ungetbuf = NULL;
575 pp -> nlseen = 0; 580 pp -> nlseen = 0;
576 pp -> skip_level = 0; 581 pp -> skip_level = 0;
577 pp -> found_level = 0; 582 pp -> found_level = 0;
578 pp -> else_level = 0; 583 pp -> else_level = 0;
579 pp -> else_skip_level = 0; 584 pp -> else_skip_level = 0;
580 585 pp -> tokqueue = NULL;
581 // now get on with processing 586 // now get on with processing
582 } 587 }
583 588
584 static void dir_line(struct preproc_info *pp) 589 static void dir_line(struct preproc_info *pp)
585 { 590 {
610 if (ct -> ttype == TOK_EOL) 615 if (ct -> ttype == TOK_EOL)
611 { 616 {
612 pp -> lineno = lineno; 617 pp -> lineno = lineno;
613 return; 618 return;
614 } 619 }
615 if (ct -> ttype != TOK_STRING) 620 if (ct -> ttype != TOK_STR_LIT)
616 { 621 {
617 preproc_throw_error(pp, "Bad #line"); 622 preproc_throw_error(pp, "Bad #line");
618 skip_eol(pp); 623 skip_eol(pp);
619 return; 624 return;
620 } 625 }
918 } 923 }
919 924
920 static long eval_expr(struct preproc_info *pp) 925 static long eval_expr(struct preproc_info *pp)
921 { 926 {
922 long rv; 927 long rv;
928 struct token *t;
923 929
924 rv = eval_expr_real(pp, 0); 930 rv = eval_expr_real(pp, 0);
925 if (pp -> curtok -> ttype != TOK_EOL) 931 t = preproc_next_token_nws(pp);
932 if (t -> ttype != TOK_EOL)
926 { 933 {
927 preproc_throw_error(pp, "Bad expression"); 934 preproc_throw_error(pp, "Bad expression");
928 skip_eol(pp); 935 skip_eol(pp);
929 } 936 }
930 return rv; 937 return rv;
1089 { 1096 {
1090 strbuf_add(s, ' '); 1097 strbuf_add(s, ' ');
1091 } 1098 }
1092 for (ws = 0; tl -> strval[ws]; ws++) 1099 for (ws = 0; tl -> strval[ws]; ws++)
1093 { 1100 {
1094 if (tl -> ttype == TOK_STRING || tl -> ttype == TOK_CHR_LIT) 1101 if (tl -> ttype == TOK_STR_LIT || tl -> ttype == TOK_CHR_LIT)
1095 { 1102 {
1096 if (tl -> strval[ws] == '"' || tl -> strval[ws] == '\\') 1103 if (tl -> strval[ws] == '"' || tl -> strval[ws] == '\\')
1097 strbuf_add(s, '\\'); 1104 strbuf_add(s, '\\');
1098 } 1105 }
1099 } 1106 }
1365 if (i != -1) 1372 if (i != -1)
1366 { 1373 {
1367 repl = 1; 1374 repl = 1;
1368 tstr = stringify(arglist[i]); 1375 tstr = stringify(arglist[i]);
1369 token_list_remove(t -> next); 1376 token_list_remove(t -> next);
1370 token_list_insert(expand_list, t, token_create(TOK_STRING, tstr, t -> lineno, t -> column, t -> fn)); 1377 token_list_insert(expand_list, t, token_create(TOK_STR_LIT, tstr, t -> lineno, t -> column, t -> fn));
1371 token_list_remove(t); 1378 token_list_remove(t);
1372 lw_free(tstr); 1379 lw_free(tstr);
1373 break; 1380 break;
1374 } 1381 }
1375 } 1382 }
1376 } 1383 }
1384 repl = 1;
1377 } 1385 }
1378 1386
1379 1387
1380 // scan for concatenation and handle it 1388 // scan for concatenation and handle it
1381 1389