History

tccpp: cleanup options -dD -dM, remove -C

The lexer is for reading files, not for writing.

grischka committed 10 years ago

Commit fe845cf1 parent caebbc35 files · +198 −246Browse files

Unified · Split

5 files
  1. libtcc.c +14 −36
  2. tcc.c +2 −3
  3. tcc.h +1 −4
  4. tccelf.c +1 −1
  5. tccpp.c +180 −202
libtcc.c+14−36
@@ -1132,18 +1132,14 @@ LIBTCCAPI TCCState *tcc_new(void) · 1131 unmodified lines
11321132 tcc_define_symbol(s, "__linux", NULL);
11331133 # endif
11341134 # if defined(__FreeBSD__)
1135−# define str(s) #s
1136− tcc_define_symbol(s, "__FreeBSD__", str( __FreeBSD__));
1137−# undef str
1135+ tcc_define_symbol(s, "__FreeBSD__", "__FreeBSD__");
11381136 # endif
11391137 # if defined(__FreeBSD_kernel__)
11401138 tcc_define_symbol(s, "__FreeBSD_kernel__", NULL);
11411139 # endif
11421140 #endif
11431141 # if defined(__NetBSD__)
1144−# define str(s) #s
1145− tcc_define_symbol(s, "__NetBSD__", str( __NetBSD__));
1146−# undef str
1142+ tcc_define_symbol(s, "__NetBSD__", "__NetBSD__");
11471143 # endif
11481144
11491145 /* TinyCC & gcc defines */
@@ -1227,8 +1223,6 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 77 unmodified lines
12271223 /* close a preprocessor output */
12281224 if (s1->ppfp && s1->ppfp != stdout)
12291225 fclose(s1->ppfp);
1230− if (s1->dffp && s1->dffp != s1->ppfp)
1231− fclose(s1->dffp);
12321226
12331227 /* free all sections */
12341228 for(i = 1; i < s1->nb_sections; i++)
@@ -1614,12 +1608,7 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 379 unmodified lines
16141608 if (!s->ppfp)
16151609 tcc_error("could not write '%s'", s->outfile);
16161610 }
1617− s->dffp = s->ppfp;
1618− if (s->dflag == 'M')
1619− s->ppfp = NULL;
16201611 }
1621− if (s->option_C && !s->ppfp)
1622− s->option_C = 0;
16231612
16241613 if (!s->nostdinc) {
16251614 /* default include paths */
@@ -1670,7 +1659,6 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 44 unmodified lines
16701659 tcc_add_crt(s, "crti.o");
16711660 }
16721661 #endif
1673−
16741662 #ifdef CONFIG_TCC_BCHECK
16751663 if (s->do_bounds_check && (output_type == TCC_OUTPUT_EXE))
16761664 {
@@ -1681,12 +1669,8 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) · 4 unmodified lines
16811669 put_extern_sym(sym, NULL, 0, 0);
16821670 }
16831671 #endif
1684−
16851672 if (s->normalize_inc_dirs)
16861673 tcc_normalize_inc_dirs(s);
1687− if (s->output_type == TCC_OUTPUT_PREPROCESS)
1688− print_defines();
1689−
16901674 return 0;
16911675 }
16921676
@@ -1963,7 +1947,6 @@ enum { · 270 unmodified lines
19631947 TCC_OPTION_b,
19641948 TCC_OPTION_g,
19651949 TCC_OPTION_c,
1966− TCC_OPTION_C,
19671950 TCC_OPTION_dumpversion,
19681951 TCC_OPTION_d,
19691952 TCC_OPTION_float_abi,
@@ -2021,7 +2004,6 @@ static const TCCOption tcc_options[] = { · 51 unmodified lines
20212004 #endif
20222005 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
20232006 { "c", TCC_OPTION_c, 0 },
2024− { "C", TCC_OPTION_C, 0 },
20252007 { "dumpversion", TCC_OPTION_dumpversion, 0},
20262008 { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
20272009 #ifdef TCC_TARGET_ARM
@@ -2210,17 +2192,15 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) · 182 unmodified lines
22102192 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
22112193 s->output_type = TCC_OUTPUT_OBJ;
22122194 break;
2213− case TCC_OPTION_C:
2214− s->option_C = 1;
2215− break;
22162195 case TCC_OPTION_d:
2217− if (*optarg == 'D' || *optarg == 'M')
2218− s->dflag = *optarg;
2219− else {
2220− if (s->warn_unsupported)
2221− goto unsupported_option;
2222− tcc_error("invalid option -- '%s'", r);
2223− }
2196+ if (*optarg == 'D')
2197+ s->dflag = 3;
2198+ else if (*optarg == 'M')
2199+ s->dflag = 7;
2200+ else if (*optarg == 'b')
2201+ s->dflag = 8;
2202+ else
2203+ goto unsupported_option;
22242204 break;
22252205 #ifdef TCC_TARGET_ARM
22262206 case TCC_OPTION_float_abi:
@@ -2310,12 +2290,11 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) · 83 unmodified lines
23102290 do ++s->verbose; while (*optarg++ == 'v');
23112291 break;
23122292 case TCC_OPTION_f:
2313− if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
2293+ if (tcc_set_flag(s, optarg, 1) < 0)
23142294 goto unsupported_option;
23152295 break;
23162296 case TCC_OPTION_W:
2317− if (tcc_set_warning(s, optarg, 1) < 0 &&
2318− s->warn_unsupported)
2297+ if (tcc_set_warning(s, optarg, 1) < 0)
23192298 goto unsupported_option;
23202299 break;
23212300 case TCC_OPTION_w:
@@ -2378,10 +2357,9 @@ ST_FUNC int tcc_parse_args1(TCCState *s, int argc, char **argv) · 56 unmodified lines
23782357 /* ignored */
23792358 break;
23802359 default:
2381− if (s->warn_unsupported) {
2382− unsupported_option:
2360+unsupported_option:
2361+ if (s->warn_unsupported)
23832362 tcc_warning("unsupported option '%s'", r);
2384− }
23852363 break;
23862364 }
23872365 }
tcc.c+2−3
@@ -98,9 +98,8 @@ 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\n"
102− " -d{D|M} dump defines\n"
103− " -C keep comments\n"
101+ " -P[1] no / alternative #line output with -E\n"
102+ " -dD -dM output #define directives with -E\n"
104103 "Linker options:\n"
105104 " -Ldir add library path 'dir'\n"
106105 " -llib link with dynamic or static library 'lib'\n"
tcc.h+1−4
@@ -775,7 +775,6 @@ struct TCCState { · 774 unmodified lines
775775 LINE_MACRO_OUTPUT_FORMAT_STD
776776 } Pflag; /* -P switch */
777777 char dflag; /* -dX value */
778− FILE *dffp;
779778
780779 /* for -MD/-MF: collected dependencies for this compilation */
781780 char **target_deps;
@@ -858,7 +857,6 @@ struct TCCState { · 76 unmodified lines
858857 char *option_m; /* only -m32/-m64 handled */
859858 int print_search_dirs; /* option */
860859 int option_r; /* option -r */
861− int option_C; /* option -C, keep comments when -E */
862860 int do_bench; /* option -bench */
863861 int gen_deps; /* option -MD */
864862 char *deps_outfile; /* option -MF */
@@ -1296,7 +1294,6 @@ ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg · 431 unmodified lines
12961294 ST_FUNC void define_undef(Sym *s);
12971295 ST_INLN Sym *define_find(int v);
12981296 ST_FUNC void free_defines(Sym *b);
1299−ST_FUNC void print_defines(void);
13001297 ST_FUNC Sym *label_find(int v);
13011298 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
13021299 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
@@ -1453,7 +1450,7 @@ ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); · 150 unmodified lines
14531450 #ifndef TCC_TARGET_PE
14541451 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
14551452 ST_FUNC int tcc_load_ldscript(TCCState *s1);
1456−ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip);
1453+ST_FUNC uint8_t *parse_comment(uint8_t *p);
14571454 ST_FUNC void minp(void);
14581455 ST_INLN void inp(void);
14591456 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,0);
3285+ file->buf_ptr = parse_comment(file->buf_ptr);
32863286 ch = file->buf_ptr[0];
32873287 goto redo;
32883288 } else {
tccpp.c+180−202
@@ -49,6 +49,9 @@ static char token_buf[STRING_MAX_SIZE + 1]; · 48 unmodified lines
4949 static CString cstr_buf;
5050 static TokenString tokstr_buf;
5151 static unsigned char isidnum_table[256 - CH_EOF];
52+static int pp_debug_tok, pp_debug_symv;
53+static void tok_print(const char *msg, const int *str);
54+
5255 /* isidnum_table flags: */
5356 #define IS_SPC 1
5457 #define IS_ID 2
@@ -129,6 +132,21 @@ ST_FUNC void end_macro(void) · 74 unmodified lines
129132 }
130133 }
131134
135+ST_FUNC char *trimfront(char *p)
136+{
137+ while (*p && (unsigned char)*p <= ' ')
138+ ++p;
139+ return p;
140+}
141+
142+ST_FUNC char *trimback(char *a, char *e)
143+{
144+ while (e > a && (unsigned char)e[-1] <= ' ')
145+ --e;
146+ *e = 0;;
147+ return a;
148+}
149+
132150 /* ------------------------------------------------------------------------- */
133151 /* Custom allocator for tiny objects */
134152
@@ -675,80 +693,12 @@ ST_FUNC void minp(void) · 540 unmodified lines
675693 handle_stray();
676694 }
677695
678−static void pp_line(TCCState *s1, BufferedFile *f, int level)
679−{
680− if (s1->ppfp) {
681− int d = f->line_num - f->line_ref;
682− if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE
683− || (level == 0 && f->line_ref && d < 8)) {
684− while (d > 0)
685− fputs("\n", s1->ppfp), --d;
686− } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
687− fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
688− } else {
689− fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
690− level > 0 ? " 1" : level < 0 ? " 2" : "");
691− }
692− }
693− f->line_ref = f->line_num;
694−}
695−
696−static uint8_t *parse_print_comment(uint8_t *p, int is_line_comment)
697−{
698− int c, star_count;
699− FILE *ppfp = tcc_state->ppfp;
700−
701− if ((file->line_num - file->line_ref) > 0) {
702− fputc('\n', ppfp);
703− file->line_ref++;
704− pp_line(tcc_state, file, 0);
705− }
706− if (is_line_comment)
707− fputs("//", ppfp); else
708− fputs("/*", ppfp);
709−
710− star_count = 0;
711− for(;;) {
712− c = *p;
713− if (c == '\\') {
714− file->buf_ptr = p;
715− c = handle_eob();
716− p = file->buf_ptr;
717− }
718− if (c == CH_EOF) {
719− if (is_line_comment) break;
720− tcc_error("unexpected end of file in comment");
721− }
722− if (c == '*')
723− star_count++;
724− else {
725− if ((c == '/') && star_count && !is_line_comment)
726− break;
727− star_count = 0;
728− if (c == '\n') {
729− if (is_line_comment) break;
730− file->line_num++;
731− }
732− }
733− fputc(c, ppfp);
734− p++;
735− }
736− if (!is_line_comment) {
737− fputc('/', ppfp);
738− p++;
739− file->line_ref = file->line_num;
740− }
741− return p;
742−}
743−
744696 /* single line C++ comments */
745−static uint8_t *parse_line_comment(uint8_t *p, int skip)
697+static uint8_t *parse_line_comment(uint8_t *p)
746698 {
747699 int c;
748700
749701 p++;
750− if (tcc_state->option_C && !skip)
751− return parse_print_comment(p, 1);
752702 for(;;) {
753703 c = *p;
754704 redo:
@@ -781,13 +731,11 @@ static uint8_t *parse_line_comment(uint8_t *p, int skip) · 26 unmodified lines
781731 }
782732
783733 /* C comments */
784−ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip)
734+ST_FUNC uint8_t *parse_comment(uint8_t *p)
785735 {
786736 int c;
787737
788738 p++;
789− if (tcc_state->option_C && !skip)
790− return parse_print_comment(p, 0);
791739 for(;;) {
792740 /* fast skip loop */
793741 for(;;) {
@@ -994,9 +942,9 @@ redo_start: · 200 unmodified lines
994942 minp();
995943 p = file->buf_ptr;
996944 if (ch == '*') {
997− p = parse_comment(p,1);
945+ p = parse_comment(p);
998946 } else if (ch == '/') {
999− p = parse_line_comment(p,1);
947+ p = parse_line_comment(p);
1000948 }
1001949 break;
1002950 case '#':
@@ -1017,9 +965,9 @@ redo_start: · 14 unmodified lines
1017965 else if (tok == TOK_LINEFEED)
1018966 goto redo_start;
1019967 else if (parse_flags & PARSE_FLAG_ASM_FILE)
1020− p = parse_line_comment(p,0);
968+ p = parse_line_comment(p);
1021969 } else if (parse_flags & PARSE_FLAG_ASM_FILE)
1022− p = parse_line_comment(p,0);
970+ p = parse_line_comment(p);
1023971 break;
1024972 _default:
1025973 default:
@@ -1287,6 +1235,10 @@ static int macro_is_equal(const int *a, const int *b) · 261 unmodified lines
12871235 {
12881236 CValue cv;
12891237 int t;
1238+
1239+ if (!a || !b)
1240+ return 1;
1241+
12901242 while (*a && *b) {
12911243 /* first time preallocate static cstr_buf, next time only reset position to start */
12921244 cstr_reset(&cstr_buf);
@@ -1299,106 +1251,25 @@ static int macro_is_equal(const int *a, const int *b) · 6 unmodified lines
12991251 return !(*a || *b);
13001252 }
13011253
1302−static void tok_print(const char *msg, const int *str)
1303−{
1304− FILE *pr = tcc_state->dffp;
1305− int t;
1306− CValue cval;
1307−
1308− fprintf(pr, "%s ", msg);
1309− while (str) {
1310− TOK_GET(&t, &str, &cval);
1311− if (!t)
1312− break;
1313− fprintf(pr,"%s", get_tok_str(t, &cval));
1314− }
1315− fprintf(pr, "\n");
1316−}
1317−
1318−static int define_print_prepared(Sym *s)
1319−{
1320− if (!s || !tcc_state->dffp || tcc_state->dflag == 0)
1321− return 0;
1322−
1323− if (s->v < TOK_IDENT || s->v >= tok_ident)
1324− return 0;
1325−
1326− if (file && tcc_state->dflag == 'D') {
1327− file->line_num--;
1328− pp_line(tcc_state, file, 0);
1329− file->line_ref = ++file->line_num;
1330− }
1331− return 1;
1332−}
1333−
1334−static void define_print(int v)
1335−{
1336− FILE *pr = tcc_state->dffp;
1337− Sym *s, *a;
1338−
1339− s = define_find(v);
1340− if (define_print_prepared(s) == 0)
1341− return;
1342−
1343− fprintf(pr, "// #define %s", get_tok_str(v, NULL));
1344− if (s->type.t == MACRO_FUNC) {
1345− a = s->next;
1346− fprintf(pr,"(");
1347− if (a)
1348− for (;;) {
1349− fprintf(pr,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
1350− if (!(a = a->next))
1351− break;
1352− fprintf(pr,",");
1353− }
1354− fprintf(pr,")");
1355− }
1356− tok_print("", s->d);
1357−}
1358−
1359−static void undef_print(int v)
1360−{
1361− FILE *pr = tcc_state->dffp;
1362− Sym *s;
1363−
1364− s = define_find(v);
1365− if (define_print_prepared(s) == 0)
1366− return;
1367−
1368− fprintf(pr, "// #undef %s\n", get_tok_str(s->v, NULL));
1369−}
1370−
1371−ST_FUNC void print_defines(void)
1372−{
1373− Sym *top = define_stack;
1374− while (top) {
1375− define_print(top->v);
1376− top = top->prev;
1377− }
1378−}
1379−
13801254 /* defines handling */
13811255 ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg)
13821256 {
1383− Sym *s;
1384−
1385− if (str) {
1386− s = define_find(v);
1387− if (s && !macro_is_equal(s->d, str->str))
1388− tcc_warning("%s redefined", get_tok_str(v, NULL));
1389− }
1257+ Sym *s, *o;
13901258
1259+ o = define_find(v);
13911260 s = sym_push2(&define_stack, v, macro_type, 0);
13921261 s->d = str ? tok_str_dup(str) : NULL;
13931262 s->next = first_arg;
13941263 table_ident[v - TOK_IDENT]->sym_define = s;
1264+
1265+ if (o && !macro_is_equal(o->d, s->d))
1266+ tcc_warning("%s redefined", get_tok_str(v, NULL));
13951267 }
13961268
13971269 /* undefined a define symbol. Its name is just set to zero */
13981270 ST_FUNC void define_undef(Sym *s)
13991271 {
14001272 int v = s->v;
1401− undef_print(v);
14021273 if (v >= TOK_IDENT && v < tok_ident)
14031274 table_ident[v - TOK_IDENT]->sym_define = NULL;
14041275 }
@@ -1602,7 +1473,6 @@ ST_FUNC void parse_define(void) · 197 unmodified lines
16021473 bad_twosharp:
16031474 tcc_error("'##' cannot appear at either end of macro");
16041475 define_push(v, t, &tokstr_buf, first);
1605− define_print(v);
16061476 }
16071477
16081478 static inline int hash_cached_include(const char *filename)
@@ -1688,6 +1558,7 @@ static void pragma_parse(TCCState *s1) · 79 unmodified lines
16881558 table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
16891559 else
16901560 tcc_warning("unbalanced #pragma pop_macro");
1561+ pp_debug_tok = t, pp_debug_symv = v;
16911562
16921563 } else if (tok == TOK_once) {
16931564 char buf1[sizeof(file->filename) + sizeof(ONCE_PREFIX)];
@@ -1788,11 +1659,15 @@ ST_FUNC void preprocess(int is_bof) · 94 unmodified lines
17881659 redo:
17891660 switch(tok) {
17901661 case TOK_DEFINE:
1662+ pp_debug_tok = tok;
17911663 next_nomacro();
1664+ pp_debug_symv = tok;
17921665 parse_define();
17931666 break;
17941667 case TOK_UNDEF:
1668+ pp_debug_tok = tok;
17951669 next_nomacro();
1670+ pp_debug_symv = tok;
17961671 s = define_find(tok);
17971672 /* undefine symbol by putting an invalid name */
17981673 if (s)
@@ -2059,7 +1934,7 @@ _line_num: · 260 unmodified lines
20591934 goto ignore;
20601935 tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
20611936 ignore:
2062− file->buf_ptr = parse_line_comment(file->buf_ptr,0);
1937+ file->buf_ptr = parse_line_comment(file->buf_ptr);
20631938 goto the_end;
20641939 }
20651940 /* ignore other preprocess commands or #! for C scripts */
@@ -2655,7 +2530,7 @@ maybe_newline: · 589 unmodified lines
26552530 tok = TOK_TWOSHARPS;
26562531 } else {
26572532 if (parse_flags & PARSE_FLAG_ASM_FILE) {
2658− p = parse_line_comment(p - 1,0);
2533+ p = parse_line_comment(p - 1);
26592534 goto redo_no_start;
26602535 } else {
26612536 tok = '#';
@@ -2918,17 +2793,13 @@ maybe_newline: · 256 unmodified lines
29182793 case '/':
29192794 PEEKC(c, p);
29202795 if (c == '*') {
2921− p = parse_comment(p,0);
2796+ p = parse_comment(p);
29222797 /* comments replaced by a blank */
29232798 tok = ' ';
2924− if (tcc_state->option_C)
2925− goto redo_no_start;
29262799 goto keep_tok_flags;
29272800 } else if (c == '/') {
2928− p = parse_line_comment(p,0);
2801+ p = parse_line_comment(p);
29292802 tok = ' ';
2930− if (tcc_state->option_C)
2931− goto redo_no_start;
29322803 goto keep_tok_flags;
29332804 } else if (c == '=') {
29342805 p++;
@@ -3150,10 +3021,10 @@ static int next_argstream(Sym **nested_list, int can_read_stream, TokenString *w · 215 unmodified lines
31503021 uint8_t *p = file->buf_ptr;
31513022 PEEKC(c, p);
31523023 if (c == '*') {
3153− p = parse_comment(p,0);
3024+ p = parse_comment(p);
31543025 file->buf_ptr = p - 1;
31553026 } else if (c == '/') {
3156− p = parse_line_comment(p,0);
3027+ p = parse_line_comment(p);
31573028 file->buf_ptr = p - 1;
31583029 } else
31593030 break;
@@ -3570,8 +3441,6 @@ ST_INLN void unget_tok(int last_tok) · 410 unmodified lines
35703441 tok = last_tok;
35713442 }
35723443
3573−/* better than nothing, but needs extension to handle '-E' option
3574− correctly too */
35753444 ST_FUNC void preprocess_init(TCCState *s1)
35763445 {
35773446 s1->include_stack_ptr = s1->include_stack;
@@ -3585,8 +3454,7 @@ ST_FUNC void preprocess_init(TCCState *s1) · 7 unmodified lines
35853454 s1->pack_stack_ptr = s1->pack_stack;
35863455
35873456 isidnum_table['$' - CH_EOF] =
3588− tcc_state->dollars_in_identifiers ? IS_ID : 0;
3589−
3457+ s1->dollars_in_identifiers ? IS_ID : 0;
35903458 isidnum_table['.' - CH_EOF] =
35913459 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
35923460 }
@@ -3665,11 +3533,122 @@ ST_FUNC void preprocess_delete(void) · 72 unmodified lines
36653533 cstr_alloc = NULL;
36663534 }
36673535
3536+/* ------------------------------------------------------------------------- */
3537+/* tcc -E [-P[1]] [-dD} support */
3538+
3539+static void tok_print(const char *msg, const int *str)
3540+{
3541+ FILE *fp;
3542+ int t;
3543+ CValue cval;
3544+
3545+ fp = tcc_state->ppfp;
3546+ if (!fp || !tcc_state->dflag)
3547+ fp = stdout;
3548+
3549+ fprintf(fp, "%s ", msg);
3550+ while (str) {
3551+ TOK_GET(&t, &str, &cval);
3552+ if (!t)
3553+ break;
3554+ fprintf(fp,"%s", get_tok_str(t, &cval));
3555+ }
3556+ fprintf(fp, "\n");
3557+}
3558+
3559+static void pp_line(TCCState *s1, BufferedFile *f, int level)
3560+{
3561+ int d = f->line_num - f->line_ref;
3562+
3563+ if (s1->dflag & 4)
3564+ return;
3565+
3566+ if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
3567+ if (level == 0 && f->line_ref && d) {
3568+ d = 1;
3569+ goto simple;
3570+ }
3571+ } else if (level == 0 && f->line_ref && d < 8) {
3572+simple:
3573+ while (d > 0)
3574+ fputs("\n", s1->ppfp), --d;
3575+ } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
3576+ fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
3577+ } else {
3578+ fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
3579+ level > 0 ? " 1" : level < 0 ? " 2" : "");
3580+ }
3581+ f->line_ref = f->line_num;
3582+}
3583+
3584+static void define_print(TCCState *s1, int v)
3585+{
3586+ FILE *fp;
3587+ Sym *s;
3588+
3589+ s = define_find(v);
3590+ if (NULL == s || NULL == s->d)
3591+ return;
3592+
3593+ fp = s1->ppfp;
3594+ fprintf(fp, "#define %s", get_tok_str(v, NULL));
3595+ if (s->type.t == MACRO_FUNC) {
3596+ Sym *a = s->next;
3597+ fprintf(fp,"(");
3598+ if (a)
3599+ for (;;) {
3600+ fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
3601+ if (!(a = a->next))
3602+ break;
3603+ fprintf(fp,",");
3604+ }
3605+ fprintf(fp,")");
3606+ }
3607+ tok_print("", s->d);
3608+}
3609+
3610+static void pp_debug_defines(TCCState *s1)
3611+{
3612+ int v, t;
3613+ const char *vs;
3614+ FILE *fp;
3615+
3616+ t = pp_debug_tok;
3617+ if (t == 0)
3618+ return;
3619+
3620+ file->line_num--;
3621+ pp_line(s1, file, 0);
3622+ file->line_ref = ++file->line_num;
3623+
3624+ fp = s1->ppfp;
3625+ v = pp_debug_symv;
3626+ vs = get_tok_str(v, NULL);
3627+ if (t == TOK_DEFINE) {
3628+ define_print(s1, v);
3629+ } else if (t == TOK_UNDEF) {
3630+ fprintf(fp, "#undef %s\n", vs);
3631+ } else if (t == TOK_push_macro) {
3632+ fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
3633+ } else if (t == TOK_pop_macro) {
3634+ fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
3635+ }
3636+ pp_debug_tok = 0;
3637+}
3638+
3639+static void pp_debug_builtins(TCCState *s1)
3640+{
3641+ int v;
3642+ for (v = TOK_IDENT; v < tok_ident; ++v)
3643+ define_print(s1, v);
3644+}
3645+
36683646 /* Preprocess the current file */
36693647 ST_FUNC int tcc_preprocess(TCCState *s1)
36703648 {
36713649 BufferedFile **iptr;
36723650 int token_seen, spcs, level;
3651+ Sym *define_start;
36733652
36743653 preprocess_init(s1);
36753654 ch = file->buf_ptr[0];
@@ -3680,12 +3659,19 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 4 unmodified lines
36803659 | PARSE_FLAG_SPACES
36813660 | PARSE_FLAG_ACCEPT_STRAYS
36823661 ;
3662+ define_start = define_stack;
36833663
36843664 #ifdef PP_BENCH
3665+ /* for PP benchmarks */
36853666 do next(); while (tok != TOK_EOF); return 0;
36863667 #endif
36873668
3688− token_seen = spcs = 0;
3669+ if (s1->dflag & 1) {
3670+ pp_debug_builtins(s1);
3671+ s1->dflag &= ~1;
3672+ }
3673+
3674+ token_seen = TOK_LINEFEED, spcs = 0;
36893675 pp_line(s1, file, 0);
36903676
36913677 for (;;) {
@@ -3700,7 +3686,13 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 8 unmodified lines
37003686 pp_line(s1, file, level);
37013687 }
37023688
3703− if (0 == token_seen) {
3689+ if (s1->dflag) {
3690+ pp_debug_defines(s1);
3691+ if (s1->dflag & 4)
3692+ continue;
3693+ }
3694+
3695+ if (token_seen == TOK_LINEFEED) {
37043696 if (tok == ' ') {
37053697 ++spcs;
37063698 continue;
@@ -3710,33 +3702,19 @@ ST_FUNC int tcc_preprocess(TCCState *s1) · 3 unmodified lines
37103702 continue;
37113703 }
37123704 pp_line(s1, file, 0);
3713− while (s1->ppfp && spcs > 0)
3714− fputs(" ", s1->ppfp), --spcs;
3715− spcs = 0;
3716− token_seen = 1;
3717−
37183705 } else if (tok == TOK_LINEFEED) {
37193706 ++file->line_ref;
3720− token_seen = 0;
37213707 }
3722− if (s1->ppfp)
3723− fputs(get_tok_str(tok, &tokc), s1->ppfp);
3724− }
37253708
3726− return 0;
3727−}
3709+ while (spcs)
3710+ fputs(" ", s1->ppfp), --spcs;
3711+ fputs(get_tok_str(tok, &tokc), s1->ppfp);
37283712
3729−ST_FUNC char *trimfront(char *p)
3730−{
3731− while (*p && (unsigned char)*p <= ' ')
3732− ++p;
3733− return p;
3713+ token_seen = tok;
3714+ }
3715+ /* reset define stack, but keep -D and built-ins */
3716+ free_defines(define_start);
3717+ return 0;
37343718 }
37353719
3736−ST_FUNC char *trimback(char *a, char *e)
3737−{
3738− while (e > a && (unsigned char)e[-1] <= ' ')
3739− --e;
3740− *e = 0;;
3741− return a;
3742−}
3720+/* ------------------------------------------------------------------------- */

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