History

preprocessor oprtion -C (keep comments)

This is done by impression of the pcc -C option. Usual execution path and speed are not changed.

seyko committed 10 years ago

Commit c6dc7561 parent 16cbca25 files · +195 −48Browse files

Unified · Split

5 files
  1. libtcc.c +20 −0
  2. tcc.c +3 −15
  3. tcc.h +2 −1
  4. tccelf.c +1 −1
  5. tccpp.c +169 −31
libtcc.c+20−0
@@ -1604,6 +1604,21 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 1603 unmodified lines
1604{1604{
1605 s->output_type = output_type;1605 s->output_type = output_type;
1606 1606
1607 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
1608 if (!s->outfile) {
1609 s->ppfp = stdout;
1610 } else {
1611 s->ppfp = fopen(s->outfile, "w");
1612 if (!s->ppfp)
1613 tcc_error("could not write '%s'", s->outfile);
1614 }
1615 s->dffp = s->ppfp;
1616 if (s->dflag == 'M')
1617 s->ppfp = NULL;
1618 }
1619 if (s->option_C && !s->ppfp)
1620 s->option_C = 0;
1621
1607 if (!s->nostdinc) {1622 if (!s->nostdinc) {
1608 /* default include paths */1623 /* default include paths */
1609 /* -isystem paths have already been handled */1624 /* -isystem paths have already been handled */
@@ -1946,6 +1961,7 @@ enum { · 336 unmodified lines
1946 TCC_OPTION_b,1961 TCC_OPTION_b,
1947 TCC_OPTION_g,1962 TCC_OPTION_g,
1948 TCC_OPTION_c,1963 TCC_OPTION_c,
1964 TCC_OPTION_C,
1949 TCC_OPTION_dumpversion,1965 TCC_OPTION_dumpversion,
1950 TCC_OPTION_d,1966 TCC_OPTION_d,
1951 TCC_OPTION_float_abi,1967 TCC_OPTION_float_abi,
@@ -2003,6 +2019,7 @@ static const TCCOption tcc_options[] = { · 51 unmodified lines
2003#endif2019#endif
2004 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },2020 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2005 { "c", TCC_OPTION_c, 0 },2021 { "c", TCC_OPTION_c, 0 },
2022 { "C", TCC_OPTION_C, 0 },
2006 { "dumpversion", TCC_OPTION_dumpversion, 0},2023 { "dumpversion", TCC_OPTION_dumpversion, 0},
2007 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },2024 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
2008#ifdef TCC_TARGET_ARM2025#ifdef TCC_TARGET_ARM
@@ -2191,6 +2208,9 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) · 182 unmodified lines
2191 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);2208 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
2192 s->output_type = TCC_OUTPUT_OBJ;2209 s->output_type = TCC_OUTPUT_OBJ;
2193 break;2210 break;
2211 case TCC_OPTION_C:
2212 s->option_C = 1;
2213 break;
2194 case TCC_OPTION_d:2214 case TCC_OPTION_d:
2195 if (*optarg == 'D' || *optarg == 'M')2215 if (*optarg == 'D' || *optarg == 'M')
2196 s->dflag = *optarg;2216 s->dflag = *optarg;
tcc.c+3−15
@@ -98,8 +98,9 @@ static void help(void) · 97 unmodified lines
98 " -Dsym[=val] define 'sym' with value 'val'\n"98 " -Dsym[=val] define 'sym' with value 'val'\n"
99 " -Usym undefine 'sym'\n"99 " -Usym undefine 'sym'\n"
100 " -E preprocess only\n"100 " -E preprocess only\n"
101 " -P[1] no/alternative output of #line directives with -E\n"101 " -P[1] no/alternative output of #line directives\n"
102 " -d{D|M} dump defines (only with -E)\n"102 " -d{D|M} dump defines\n"
103 " -C keep comments\n"
103 "Linker options:\n"104 "Linker options:\n"
104 " -Ldir add library path 'dir'\n"105 " -Ldir add library path 'dir'\n"
105 " -llib link with dynamic or static library 'lib'\n"106 " -llib link with dynamic or static library 'lib'\n"
@@ -294,19 +295,6 @@ int main(int argc, char **argv) · 188 unmodified lines
294 }295 }
295 }296 }
296 297
297 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
298 if (!s->outfile) {
299 s->ppfp = stdout;
300 } else {
301 s->ppfp = fopen(s->outfile, "w");
302 if (!s->ppfp)
303 tcc_error("could not write '%s'", s->outfile);
304 }
305 s->dffp = s->ppfp;
306 if (s->dflag == 'M')
307 s->ppfp = NULL;
308 }
309
310 tcc_set_output_type(s, s->output_type);298 tcc_set_output_type(s, s->output_type);
311 299
312 /* compile or add each files or library */300 /* compile or add each files or library */
tcc.h+2−1
@@ -812,6 +812,7 @@ struct TCCState { · 811 unmodified lines
812 char *option_m; /* only -m32/-m64 handled */812 char *option_m; /* only -m32/-m64 handled */
813 int print_search_dirs; /* option */813 int print_search_dirs; /* option */
814 int option_r; /* option -r */814 int option_r; /* option -r */
815 int option_C; /* option -C, keep comments when -E */
815 int do_bench; /* option -bench */816 int do_bench; /* option -bench */
816 int gen_deps; /* option -MD */817 int gen_deps; /* option -MD */
817 char *deps_outfile; /* option -MF */818 char *deps_outfile; /* option -MF */
@@ -1406,7 +1407,7 @@ ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); · 588 unmodified lines
1406#ifndef TCC_TARGET_PE1407#ifndef TCC_TARGET_PE
1407ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);1408ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1408ST_FUNC int tcc_load_ldscript(TCCState *s1);1409ST_FUNC int tcc_load_ldscript(TCCState *s1);
1409ST_FUNC uint8_t *parse_comment(uint8_t *p);1410ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip);
1410ST_FUNC void minp(void);1411ST_FUNC void minp(void);
1411ST_INLN void inp(void);1412ST_INLN void inp(void);
1412ST_FUNC int handle_eob(void);1413ST_FUNC int handle_eob(void);
tccelf.c+1−1
@@ -3282,7 +3282,7 @@ static int ld_next(TCCState *s1, char *name, int name_size) · 3281 unmodified lines
3282 case '/':3282 case '/':
3283 minp();3283 minp();
3284 if (ch == '*') {3284 if (ch == '*') {
3285 file->buf_ptr = parse_comment(file->buf_ptr);3285 file->buf_ptr = parse_comment(file->buf_ptr,0);
3286 ch = file->buf_ptr[0];3286 ch = file->buf_ptr[0];
3287 goto redo;3287 goto redo;
3288 } else {3288 } else {
tccpp.c+169−31
@@ -524,13 +524,163 @@ ST_FUNC void minp(void) · 523 unmodified lines
524 handle_stray();524 handle_stray();
525}525}
526 526
527static 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
545static 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
589static 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 */
529static uint8_t *parse_line_comment(uint8_t *p)677static 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 */
566ST_FUNC uint8_t *parse_comment(uint8_t *p)716ST_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
1075static 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
1093static void tok_print(const char *msg, const int *str)1227static 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 } else3085 } else
2948 break;3086 break;

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