| @@ -524,13 +524,163 @@ ST_FUNC void minp(void) · 523 unmodified lines |
| 524 | handle_stray(); | 524 | handle_stray(); |
| 525 | } | 525 | } |
| 526 | | 526 | |
| | 527 | static void pp_line(TCCState *s1, BufferedFile *f, int level) |
| | 528 | { |
| | 529 | if (s1->ppfp) { |
| | 530 | int d = f->line_num - f->line_ref; |
| | 531 | if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE |
| | 532 | || (level == 0 && f->line_ref && d < 8)) { |
| | 533 | while (d > 0) |
| | 534 | fputs("\n", s1->ppfp), --d; |
| | 535 | } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) { |
| | 536 | fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename); |
| | 537 | } else { |
| | 538 | fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, |
| | 539 | level > 0 ? " 1" : level < 0 ? " 2" : ""); |
| | 540 | } |
| | 541 | } |
| | 542 | f->line_ref = f->line_num; |
| | 543 | } |
| | 544 | |
| | 545 | static uint8_t *parse_print_line_comment(uint8_t *p) |
| | 546 | { |
| | 547 | int c; |
| | 548 | |
| | 549 | if ((file->line_num - file->line_ref) > 0) { |
| | 550 | fputc('\n', tcc_state->ppfp); |
| | 551 | file->line_ref++; |
| | 552 | pp_line(tcc_state, file, 0); |
| | 553 | } |
| | 554 | fputs("// ", tcc_state->ppfp); |
| | 555 | |
| | 556 | for(;;) { |
| | 557 | c = *p; |
| | 558 | redo: |
| | 559 | if (c == '\n' || c == CH_EOF) |
| | 560 | break; |
| | 561 | fputc(c, tcc_state->ppfp); |
| | 562 | if (c == '\\') { |
| | 563 | file->buf_ptr = p; |
| | 564 | c = handle_eob(); |
| | 565 | p = file->buf_ptr; |
| | 566 | if (c == '\\') { |
| | 567 | PEEKC_EOB(c, p); |
| | 568 | if (c == '\n') { |
| | 569 | file->line_num++; |
| | 570 | PEEKC_EOB(c, p); |
| | 571 | } else if (c == '\r') { |
| | 572 | PEEKC_EOB(c, p); |
| | 573 | if (c == '\n') { |
| | 574 | file->line_num++; |
| | 575 | PEEKC_EOB(c, p); |
| | 576 | } |
| | 577 | } |
| | 578 | } else { |
| | 579 | goto redo; |
| | 580 | } |
| | 581 | } else { |
| | 582 | p++; |
| | 583 | } |
| | 584 | } |
| | 585 | return p; |
| | 586 | } |
| | 587 | |
| | 588 | |
| | 589 | static uint8_t *parse_print_comment(uint8_t *p) |
| | 590 | { |
| | 591 | int c; |
| | 592 | |
| | 593 | if ((file->line_num - file->line_ref) > 0) { |
| | 594 | fputc('\n', tcc_state->ppfp); |
| | 595 | file->line_ref++; |
| | 596 | pp_line(tcc_state, file, 0); |
| | 597 | } |
| | 598 | fputs("/*", tcc_state->ppfp); |
| | 599 | |
| | 600 | for(;;) { |
| | 601 | /* fast skip loop */ |
| | 602 | for(;;) { |
| | 603 | c = *p; |
| | 604 | if (c == '\n' || c == '*' || c == '\\') |
| | 605 | break; |
| | 606 | fputc(c, tcc_state->ppfp); |
| | 607 | p++; |
| | 608 | } |
| | 609 | /* now we can handle all the cases */ |
| | 610 | if (c == '\n') { |
| | 611 | fputc(c, tcc_state->ppfp); |
| | 612 | file->line_num++; |
| | 613 | p++; |
| | 614 | } else if (c == '*') { |
| | 615 | fputc(c, tcc_state->ppfp); |
| | 616 | p++; |
| | 617 | for(;;) { |
| | 618 | c = *p; |
| | 619 | if (c == '*') { |
| | 620 | fputc(c, tcc_state->ppfp); |
| | 621 | p++; |
| | 622 | } else if (c == '/') { |
| | 623 | fputc(c, tcc_state->ppfp); |
| | 624 | goto end_of_comment; |
| | 625 | } else if (c == '\\') { |
| | 626 | file->buf_ptr = p; |
| | 627 | c = handle_eob(); |
| | 628 | p = file->buf_ptr; |
| | 629 | if (c == CH_EOF) |
| | 630 | tcc_error("unexpected end of file in comment"); |
| | 631 | if (c == '\\') { |
| | 632 | /* skip '\[\r]\n', otherwise just skip the stray */ |
| | 633 | while (c == '\\') { |
| | 634 | fputc(c, tcc_state->ppfp); |
| | 635 | PEEKC_EOB(c, p); |
| | 636 | if (c == '\n') { |
| | 637 | fputc(c, tcc_state->ppfp); |
| | 638 | file->line_num++; |
| | 639 | PEEKC_EOB(c, p); |
| | 640 | } else if (c == '\r') { |
| | 641 | PEEKC_EOB(c, p); |
| | 642 | if (c == '\n') { |
| | 643 | fputc(c, tcc_state->ppfp); |
| | 644 | file->line_num++; |
| | 645 | PEEKC_EOB(c, p); |
| | 646 | } |
| | 647 | } else { |
| | 648 | goto after_star; |
| | 649 | } |
| | 650 | } |
| | 651 | } |
| | 652 | } else { |
| | 653 | break; |
| | 654 | } |
| | 655 | } |
| | 656 | after_star: ; |
| | 657 | } else { |
| | 658 | /* stray, eob or eof */ |
| | 659 | file->buf_ptr = p; |
| | 660 | c = handle_eob(); |
| | 661 | p = file->buf_ptr; |
| | 662 | if (c == CH_EOF) |
| | 663 | tcc_error("unexpected end of file in comment"); |
| | 664 | if (c == '\\') { |
| | 665 | fputc(c, tcc_state->ppfp); |
| | 666 | p++; |
| | 667 | } |
| | 668 | } |
| | 669 | } |
| | 670 | end_of_comment: |
| | 671 | p++; |
| | 672 | file->line_ref = file->line_num; |
| | 673 | return p; |
| | 674 | } |
| 527 | | 675 | |
| 528 | /* single line C++ comments */ | 676 | /* single line C++ comments */ |
| 529 | static uint8_t *parse_line_comment(uint8_t *p) | 677 | static uint8_t *parse_line_comment(uint8_t *p, int skip) |
| 530 | { | 678 | { |
| 531 | int c; | 679 | int c; |
| 532 | | 680 | |
| 533 | p++; | 681 | p++; |
| | 682 | if (tcc_state->option_C && !skip) |
| | 683 | return parse_print_line_comment(p); |
| 534 | for(;;) { | 684 | for(;;) { |
| 535 | c = *p; | 685 | c = *p; |
| 536 | redo: | 686 | redo: |
| @@ -563,11 +713,13 @@ static uint8_t *parse_line_comment(uint8_t *p) · 26 unmodified lines |
| 563 | } | 713 | } |
| 564 | | 714 | |
| 565 | /* C comments */ | 715 | /* C comments */ |
| 566 | ST_FUNC uint8_t *parse_comment(uint8_t *p) | 716 | ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip) |
| 567 | { | 717 | { |
| 568 | int c; | 718 | int c; |
| 569 | | 719 | |
| 570 | p++; | 720 | p++; |
| | 721 | if (tcc_state->option_C && !skip) |
| | 722 | return parse_print_comment(p); |
| 571 | for(;;) { | 723 | for(;;) { |
| 572 | /* fast skip loop */ | 724 | /* fast skip loop */ |
| 573 | for(;;) { | 725 | for(;;) { |
| @@ -774,9 +926,9 @@ redo_start: · 200 unmodified lines |
| 774 | minp(); | 926 | minp(); |
| 775 | p = file->buf_ptr; | 927 | p = file->buf_ptr; |
| 776 | if (ch == '*') { | 928 | if (ch == '*') { |
| 777 | p = parse_comment(p); | 929 | p = parse_comment(p,1); |
| 778 | } else if (ch == '/') { | 930 | } else if (ch == '/') { |
| 779 | p = parse_line_comment(p); | 931 | p = parse_line_comment(p,1); |
| 780 | } | 932 | } |
| 781 | break; | 933 | break; |
| 782 | case '#': | 934 | case '#': |
| @@ -797,9 +949,9 @@ redo_start: · 14 unmodified lines |
| 797 | else if (tok == TOK_LINEFEED) | 949 | else if (tok == TOK_LINEFEED) |
| 798 | goto redo_start; | 950 | goto redo_start; |
| 799 | else if (parse_flags & PARSE_FLAG_ASM_FILE) | 951 | else if (parse_flags & PARSE_FLAG_ASM_FILE) |
| 800 | p = parse_line_comment(p); | 952 | p = parse_line_comment(p,0); |
| 801 | } else if (parse_flags & PARSE_FLAG_ASM_FILE) | 953 | } else if (parse_flags & PARSE_FLAG_ASM_FILE) |
| 802 | p = parse_line_comment(p); | 954 | p = parse_line_comment(p,0); |
| 803 | break; | 955 | break; |
| 804 | _default: | 956 | _default: |
| 805 | default: | 957 | default: |
| @@ -1072,24 +1224,6 @@ static int macro_is_equal(const int *a, const int *b) · 266 unmodified lines |
| 1072 | return !(*a || *b); | 1224 | return !(*a || *b); |
| 1073 | } | 1225 | } |
| 1074 | | 1226 | |
| 1075 | static void pp_line(TCCState *s1, BufferedFile *f, int level) | | |
| 1076 | { | | |
| 1077 | if (s1->ppfp) { | | |
| 1078 | int d = f->line_num - f->line_ref; | | |
| 1079 | if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE | | |
| 1080 | || (level == 0 && f->line_ref && d < 8)) { | | |
| 1081 | while (d > 0) | | |
| 1082 | fputs("\n", s1->ppfp), --d; | | |
| 1083 | } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) { | | |
| 1084 | fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename); | | |
| 1085 | } else { | | |
| 1086 | fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename, | | |
| 1087 | level > 0 ? " 1" : level < 0 ? " 2" : ""); | | |
| 1088 | } | | |
| 1089 | } | | |
| 1090 | f->line_ref = f->line_num; | | |
| 1091 | } | | |
| 1092 | | | |
| 1093 | static void tok_print(const char *msg, const int *str) | 1227 | static void tok_print(const char *msg, const int *str) |
| 1094 | { | 1228 | { |
| 1095 | FILE *pr = tcc_state->dffp; | 1229 | FILE *pr = tcc_state->dffp; |
| @@ -1848,7 +1982,7 @@ _line_num: · 752 unmodified lines |
| 1848 | goto ignore; | 1982 | goto ignore; |
| 1849 | tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc)); | 1983 | tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc)); |
| 1850 | ignore: | 1984 | ignore: |
| 1851 | file->buf_ptr = parse_line_comment(file->buf_ptr); | 1985 | file->buf_ptr = parse_line_comment(file->buf_ptr,0); |
| 1852 | goto the_end; | 1986 | goto the_end; |
| 1853 | } | 1987 | } |
| 1854 | /* ignore other preprocess commands or #! for C scripts */ | 1988 | /* ignore other preprocess commands or #! for C scripts */ |
| @@ -2444,7 +2578,7 @@ maybe_newline: · 589 unmodified lines |
| 2444 | tok = TOK_TWOSHARPS; | 2578 | tok = TOK_TWOSHARPS; |
| 2445 | } else { | 2579 | } else { |
| 2446 | if (parse_flags & PARSE_FLAG_ASM_FILE) { | 2580 | if (parse_flags & PARSE_FLAG_ASM_FILE) { |
| 2447 | p = parse_line_comment(p - 1); | 2581 | p = parse_line_comment(p - 1,0); |
| 2448 | goto redo_no_start; | 2582 | goto redo_no_start; |
| 2449 | } else { | 2583 | } else { |
| 2450 | tok = '#'; | 2584 | tok = '#'; |
| @@ -2711,13 +2845,17 @@ maybe_newline: · 260 unmodified lines |
| 2711 | case '/': | 2845 | case '/': |
| 2712 | PEEKC(c, p); | 2846 | PEEKC(c, p); |
| 2713 | if (c == '*') { | 2847 | if (c == '*') { |
| 2714 | p = parse_comment(p); | 2848 | p = parse_comment(p,0); |
| 2715 | /* comments replaced by a blank */ | 2849 | /* comments replaced by a blank */ |
| 2716 | tok = ' '; | 2850 | tok = ' '; |
| | 2851 | if (tcc_state->option_C) |
| | 2852 | goto redo_no_start; |
| 2717 | goto keep_tok_flags; | 2853 | goto keep_tok_flags; |
| 2718 | } else if (c == '/') { | 2854 | } else if (c == '/') { |
| 2719 | p = parse_line_comment(p); | 2855 | p = parse_line_comment(p,0); |
| 2720 | tok = ' '; | 2856 | tok = ' '; |
| | 2857 | if (tcc_state->option_C) |
| | 2858 | goto redo_no_start; |
| 2721 | goto keep_tok_flags; | 2859 | goto keep_tok_flags; |
| 2722 | } else if (c == '=') { | 2860 | } else if (c == '=') { |
| 2723 | p++; | 2861 | p++; |
| @@ -2939,10 +3077,10 @@ static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *w · 215 unmodified lines |
| 2939 | uint8_t *p = file->buf_ptr; | 3077 | uint8_t *p = file->buf_ptr; |
| 2940 | PEEKC(c, p); | 3078 | PEEKC(c, p); |
| 2941 | if (c == '*') { | 3079 | if (c == '*') { |
| 2942 | p = parse_comment(p); | 3080 | p = parse_comment(p,0); |
| 2943 | file->buf_ptr = p - 1; | 3081 | file->buf_ptr = p - 1; |
| 2944 | } else if (c == '/') { | 3082 | } else if (c == '/') { |
| 2945 | p = parse_line_comment(p); | 3083 | p = parse_line_comment(p,0); |
| 2946 | file->buf_ptr = p - 1; | 3084 | file->buf_ptr = p - 1; |
| 2947 | } else | 3085 | } else |
| 2948 | break; | 3086 | break; |