History

simplify -C printing

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() ?

seyko committed 10 years ago

Commit 587aace1 parent e010b131 file · +29 −112Browse files

Unified · Split

tccpp.c+29−112
@@ -542,134 +542,51 @@ static void pp_line(TCCState *s1, BufferedFile *f, int level) · 541 unmodified lines
542542 f->line_ref = f->line_num;
543543 }
544544
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)
546546 {
547− int c;
547+ int c, star_count;
548+ FILE *ppfp = tcc_state->ppfp;
548549
549550 if ((file->line_num - file->line_ref) > 0) {
550− fputc('\n', tcc_state->ppfp);
551+ fputc('\n', ppfp);
551552 file->line_ref++;
552553 pp_line(tcc_state, file, 0);
553554 }
554− fputs("// ", tcc_state->ppfp);
555+ if (is_line_comment)
556+ fputs("//", ppfp); else
557+ fputs("/*", ppfp);
555558
559+ star_count = 0;
556560 for(;;) {
557561 c = *p;
558− redo:
559− if (c == '\n' || c == CH_EOF)
560− break;
561− fputc(c, tcc_state->ppfp);
562562 if (c == '\\') {
563563 file->buf_ptr = p;
564564 c = handle_eob();
565565 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++;
583566 }
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");
608570 }
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++;
667580 }
668581 }
582+ fputc(c, ppfp);
583+ p++;
584+ }
585+ if (!is_line_comment) {
586+ fputc('/', ppfp);
587+ p++;
588+ file->line_ref = file->line_num;
669589 }
670− end_of_comment:
671− p++;
672− file->line_ref = file->line_num;
673590 return p;
674591 }
675592
@@ -680,7 +597,7 @@ static uint8_t *parse_line_comment(uint8_t *p, int skip) · 4 unmodified lines
680597
681598 p++;
682599 if (tcc_state->option_C && !skip)
683− return parse_print_line_comment(p);
600+ return parse_print_comment(p, 1);
684601 for(;;) {
685602 c = *p;
686603 redo:
@@ -719,7 +636,7 @@ ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip) · 32 unmodified lines
719636
720637 p++;
721638 if (tcc_state->option_C && !skip)
722− return parse_print_comment(p);
639+ return parse_print_comment(p, 0);
723640 for(;;) {
724641 /* fast skip loop */
725642 for(;;) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t