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
16041604 {
16051605 s->output_type = output_type;
16061606
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+
16071622 if (!s->nostdinc) {
16081623 /* default include paths */
16091624 /* -isystem paths have already been handled */
@@ -1946,6 +1961,7 @@ enum { · 336 unmodified lines
19461961 TCC_OPTION_b,
19471962 TCC_OPTION_g,
19481963 TCC_OPTION_c,
1964+ TCC_OPTION_C,
19491965 TCC_OPTION_dumpversion,
19501966 TCC_OPTION_d,
19511967 TCC_OPTION_float_abi,
@@ -2003,6 +2019,7 @@ static const TCCOption tcc_options[] = { · 51 unmodified lines
20032019 #endif
20042020 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
20052021 { "c", TCC_OPTION_c, 0 },
2022+ { "C", TCC_OPTION_C, 0 },
20062023 { "dumpversion", TCC_OPTION_dumpversion, 0},
20072024 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
20082025 #ifdef TCC_TARGET_ARM
@@ -2191,6 +2208,9 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) · 182 unmodified lines
21912208 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
21922209 s->output_type = TCC_OUTPUT_OBJ;
21932210 break;
2211+ case TCC_OPTION_C:
2212+ s->option_C = 1;
2213+ break;
21942214 case TCC_OPTION_d:
21952215 if (*optarg == 'D' || *optarg == 'M')
21962216 s->dflag = *optarg;
tcc.c+3−15
@@ -98,8 +98,9 @@ static void help(void) · 97 unmodified lines
9898 " -Dsym[=val] define 'sym' with value 'val'\n"
9999 " -Usym undefine 'sym'\n"
100100 " -E preprocess only\n"
101− " -P[1] no/alternative output of #line directives with -E\n"
102− " -d{D|M} dump defines (only with -E)\n"
101+ " -P[1] no/alternative output of #line directives\n"
102+ " -d{D|M} dump defines\n"
103+ " -C keep comments\n"
103104 "Linker options:\n"
104105 " -Ldir add library path 'dir'\n"
105106 " -llib link with dynamic or static library 'lib'\n"
@@ -294,19 +295,6 @@ int main(int argc, char **argv) · 188 unmodified lines
294295 }
295296 }
296297
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−
310298 tcc_set_output_type(s, s->output_type);
311299
312300 /* compile or add each files or library */
tcc.h+2−1
@@ -812,6 +812,7 @@ struct TCCState { · 811 unmodified lines
812812 char *option_m; /* only -m32/-m64 handled */
813813 int print_search_dirs; /* option */
814814 int option_r; /* option -r */
815+ int option_C; /* option -C, keep comments when -E */
815816 int do_bench; /* option -bench */
816817 int gen_deps; /* option -MD */
817818 char *deps_outfile; /* option -MF */
@@ -1406,7 +1407,7 @@ ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); · 588 unmodified lines
14061407 #ifndef TCC_TARGET_PE
14071408 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
14081409 ST_FUNC int tcc_load_ldscript(TCCState *s1);
1409−ST_FUNC uint8_t *parse_comment(uint8_t *p);
1410+ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip);
14101411 ST_FUNC void minp(void);
14111412 ST_INLN void inp(void);
14121413 ST_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
32823282 case '/':
32833283 minp();
32843284 if (ch == '*') {
3285− file->buf_ptr = parse_comment(file->buf_ptr);
3285+ file->buf_ptr = parse_comment(file->buf_ptr,0);
32863286 ch = file->buf_ptr[0];
32873287 goto redo;
32883288 } else {
tccpp.c+169−31
@@ -524,13 +524,163 @@ ST_FUNC void minp(void) · 523 unmodified lines
524524 handle_stray();
525525 }
526526
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+}
527675
528676 /* 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)
530678 {
531679 int c;
532680
533681 p++;
682+ if (tcc_state->option_C && !skip)
683+ return parse_print_line_comment(p);
534684 for(;;) {
535685 c = *p;
536686 redo:
@@ -563,11 +713,13 @@ static uint8_t *parse_line_comment(uint8_t *p) · 26 unmodified lines
563713 }
564714
565715 /* C comments */
566−ST_FUNC uint8_t *parse_comment(uint8_t *p)
716+ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip)
567717 {
568718 int c;
569−
719+
570720 p++;
721+ if (tcc_state->option_C && !skip)
722+ return parse_print_comment(p);
571723 for(;;) {
572724 /* fast skip loop */
573725 for(;;) {
@@ -774,9 +926,9 @@ redo_start: · 200 unmodified lines
774926 minp();
775927 p = file->buf_ptr;
776928 if (ch == '*') {
777− p = parse_comment(p);
929+ p = parse_comment(p,1);
778930 } else if (ch == '/') {
779− p = parse_line_comment(p);
931+ p = parse_line_comment(p,1);
780932 }
781933 break;
782934 case '#':
@@ -797,9 +949,9 @@ redo_start: · 14 unmodified lines
797949 else if (tok == TOK_LINEFEED)
798950 goto redo_start;
799951 else if (parse_flags & PARSE_FLAG_ASM_FILE)
800− p = parse_line_comment(p);
952+ p = parse_line_comment(p,0);
801953 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
802− p = parse_line_comment(p);
954+ p = parse_line_comment(p,0);
803955 break;
804956 _default:
805957 default:
@@ -1072,24 +1224,6 @@ static int macro_is_equal(const int *a, const int *b) · 266 unmodified lines
10721224 return !(*a || *b);
10731225 }
10741226
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−
10931227 static void tok_print(const char *msg, const int *str)
10941228 {
10951229 FILE *pr = tcc_state->dffp;
@@ -1848,7 +1982,7 @@ _line_num: · 752 unmodified lines
18481982 goto ignore;
18491983 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
18501984 ignore:
1851− file->buf_ptr = parse_line_comment(file->buf_ptr);
1985+ file->buf_ptr = parse_line_comment(file->buf_ptr,0);
18521986 goto the_end;
18531987 }
18541988 /* ignore other preprocess commands or #! for C scripts */
@@ -2444,7 +2578,7 @@ maybe_newline: · 589 unmodified lines
24442578 tok = TOK_TWOSHARPS;
24452579 } else {
24462580 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2447− p = parse_line_comment(p - 1);
2581+ p = parse_line_comment(p - 1,0);
24482582 goto redo_no_start;
24492583 } else {
24502584 tok = '#';
@@ -2711,13 +2845,17 @@ maybe_newline: · 260 unmodified lines
27112845 case '/':
27122846 PEEKC(c, p);
27132847 if (c == '*') {
2714− p = parse_comment(p);
2848+ p = parse_comment(p,0);
27152849 /* comments replaced by a blank */
27162850 tok = ' ';
2851+ if (tcc_state->option_C)
2852+ goto redo_no_start;
27172853 goto keep_tok_flags;
27182854 } else if (c == '/') {
2719− p = parse_line_comment(p);
2855+ p = parse_line_comment(p,0);
27202856 tok = ' ';
2857+ if (tcc_state->option_C)
2858+ goto redo_no_start;
27212859 goto keep_tok_flags;
27222860 } else if (c == '=') {
27232861 p++;
@@ -2939,10 +3077,10 @@ static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *w · 215 unmodified lines
29393077 uint8_t *p = file->buf_ptr;
29403078 PEEKC(c, p);
29413079 if (c == '*') {
2942− p = parse_comment(p);
3080+ p = parse_comment(p,0);
29433081 file->buf_ptr = p - 1;
29443082 } else if (c == '/') {
2945− p = parse_line_comment(p);
3083+ p = parse_line_comment(p,0);
29463084 file->buf_ptr = p - 1;
29473085 } else
29483086 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