parse_print_line_comment() and parse_print_comment() are combined and made more simply: * don't worry about speed with -E option * don't handle straya in comments Do we need to handle strays in regular parse_line_comment() and parse_comment() ?
Unified · Split
| @@ -542,134 +542,51 @@ static void pp_line(TCCState *s1, BufferedFile *f, int level) · 541 unmodified lines | |||
| 542 | f->line_ref = f->line_num; | 542 | f->line_ref = f->line_num; |
| 543 | } | 543 | } |
| 544 | 544 | ||
| 545 | static uint8_t *parse_print_line_comment(uint8_t *p) | 545 | static uint8_t *parse_print_comment(uint8_t *p, int is_line_comment) |
| 546 | { | 546 | { |
| 547 | int c; | 547 | int c, star_count; |
| 548 | FILE *ppfp = tcc_state->ppfp; | ||
| 548 | 549 | ||
| 549 | if ((file->line_num - file->line_ref) > 0) { | 550 | if ((file->line_num - file->line_ref) > 0) { |
| 550 | fputc('\n', tcc_state->ppfp); | 551 | fputc('\n', ppfp); |
| 551 | file->line_ref++; | 552 | file->line_ref++; |
| 552 | pp_line(tcc_state, file, 0); | 553 | pp_line(tcc_state, file, 0); |
| 553 | } | 554 | } |
| 554 | fputs("// ", tcc_state->ppfp); | 555 | if (is_line_comment) |
| 556 | fputs("//", ppfp); else | ||
| 557 | fputs("/*", ppfp); | ||
| 555 | 558 | ||
| 559 | star_count = 0; | ||
| 556 | for(;;) { | 560 | for(;;) { |
| 557 | c = *p; | 561 | c = *p; |
| 558 | redo: | ||
| 559 | if (c == '\n' || c == CH_EOF) | ||
| 560 | break; | ||
| 561 | fputc(c, tcc_state->ppfp); | ||
| 562 | if (c == '\\') { | 562 | if (c == '\\') { |
| 563 | file->buf_ptr = p; | 563 | file->buf_ptr = p; |
| 564 | c = handle_eob(); | 564 | c = handle_eob(); |
| 565 | p = file->buf_ptr; | 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 | } | 566 | } |
| 584 | } | 567 | if (c == CH_EOF) { |
| 585 | return p; | 568 | if (is_line_comment) break; |
| 586 | } | 569 | tcc_error("unexpected end of file in comment"); |
| 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 | } | 570 | } |
| 609 | /* now we can handle all the cases */ | 571 | if (c == '*') |
| 610 | if (c == '\n') { | 572 | star_count++; |
| 611 | fputc(c, tcc_state->ppfp); | 573 | else { |
| 612 | file->line_num++; | 574 | if ((c == '/') && star_count && !is_line_comment) |
| 613 | p++; | 575 | break; |
| 614 | } else if (c == '*') { | 576 | star_count = 0; |
| 615 | fputc(c, tcc_state->ppfp); | 577 | if (c == '\n') { |
| 616 | p++; | 578 | if (is_line_comment) break; |
| 617 | for(;;) { | 579 | file->line_num++; |
| 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 | } | 580 | } |
| 668 | } | 581 | } |
| 582 | fputc(c, ppfp); | ||
| 583 | p++; | ||
| 584 | } | ||
| 585 | if (!is_line_comment) { | ||
| 586 | fputc('/', ppfp); | ||
| 587 | p++; | ||
| 588 | file->line_ref = file->line_num; | ||
| 669 | } | 589 | } |
| 670 | end_of_comment: | ||
| 671 | p++; | ||
| 672 | file->line_ref = file->line_num; | ||
| 673 | return p; | 590 | return p; |
| 674 | } | 591 | } |
| 675 | 592 | ||
| @@ -680,7 +597,7 @@ static uint8_t *parse_line_comment(uint8_t *p, int skip) · 4 unmodified lines | |||
| 680 | 597 | ||
| 681 | p++; | 598 | p++; |
| 682 | if (tcc_state->option_C && !skip) | 599 | if (tcc_state->option_C && !skip) |
| 683 | return parse_print_line_comment(p); | 600 | return parse_print_comment(p, 1); |
| 684 | for(;;) { | 601 | for(;;) { |
| 685 | c = *p; | 602 | c = *p; |
| 686 | redo: | 603 | redo: |
| @@ -719,7 +636,7 @@ ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip) · 32 unmodified lines | |||
| 719 | 636 | ||
| 720 | p++; | 637 | p++; |
| 721 | if (tcc_state->option_C && !skip) | 638 | if (tcc_state->option_C && !skip) |
| 722 | return parse_print_comment(p); | 639 | return parse_print_comment(p, 0); |
| 723 | for(;;) { | 640 | for(;;) { |
| 724 | /* fast skip loop */ | 641 | /* fast skip loop */ |
| 725 | for(;;) { | 642 | for(;;) { |