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 | 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 | 550 | if ((file->line_num - file->line_ref) > 0) { |
| 550 | − fputc('\n', tcc_state->ppfp); | |
| 551 | + fputc('\n', ppfp); | |
| 551 | 552 | file->line_ref++; |
| 552 | 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 | 560 | for(;;) { |
| 557 | 561 | c = *p; |
| 558 | − redo: | |
| 559 | − if (c == '\n' || c == CH_EOF) | |
| 560 | − break; | |
| 561 | − fputc(c, tcc_state->ppfp); | |
| 562 | 562 | if (c == '\\') { |
| 563 | 563 | file->buf_ptr = p; |
| 564 | 564 | c = handle_eob(); |
| 565 | 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 | − } | |
| 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++; | |
| 567 | + if (c == CH_EOF) { | |
| 568 | + if (is_line_comment) break; | |
| 569 | + tcc_error("unexpected end of file in comment"); | |
| 608 | 570 | } |
| 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++; | |
| 571 | + if (c == '*') | |
| 572 | + star_count++; | |
| 573 | + else { | |
| 574 | + if ((c == '/') && star_count && !is_line_comment) | |
| 575 | + break; | |
| 576 | + star_count = 0; | |
| 577 | + if (c == '\n') { | |
| 578 | + if (is_line_comment) break; | |
| 579 | + file->line_num++; | |
| 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 | 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 | 598 | p++; |
| 682 | 599 | if (tcc_state->option_C && !skip) |
| 683 | − return parse_print_line_comment(p); | |
| 600 | + return parse_print_comment(p, 1); | |
| 684 | 601 | for(;;) { |
| 685 | 602 | c = *p; |
| 686 | 603 | redo: |
| @@ -719,7 +636,7 @@ ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip) · 32 unmodified lines | ||
| 719 | 636 | |
| 720 | 637 | p++; |
| 721 | 638 | if (tcc_state->option_C && !skip) |
| 722 | − return parse_print_comment(p); | |
| 639 | + return parse_print_comment(p, 0); | |
| 723 | 640 | for(;;) { |
| 724 | 641 | /* fast skip loop */ |
| 725 | 642 | for(;;) { |