History

A gcc preprocessor option -dD added With this option on a defines are included into the output (inside comments). This will allow to debug a problems like:

In file included from math.c:8: In file included from /usr/include/math.h:43: /usr/include/bits/nan.h:52: warning: NAN redefined

seyko committed 11 years ago

Commit b7b9f9f1 parent 50cdccf4 files · +84 −22Browse files

Unified · Split

4 files
  1. libtcc.c +10 −0
  2. tcc.c +3 −0
  3. tcc.h +2 −0
  4. tccpp.c +69 −22
libtcc.c+10−0
@@ -1656,6 +1656,7 @@ enum { · 1655 unmodified lines
16561656 TCC_OPTION_b,
16571657 TCC_OPTION_g,
16581658 TCC_OPTION_c,
1659+ TCC_OPTION_d,
16591660 TCC_OPTION_float_abi,
16601661 TCC_OPTION_static,
16611662 TCC_OPTION_shared,
@@ -1709,6 +1710,7 @@ static const TCCOption tcc_options[] = { · 47 unmodified lines
17091710 #endif
17101711 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
17111712 { "c", TCC_OPTION_c, 0 },
1713+ { "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
17121714 #ifdef TCC_TARGET_ARM
17131715 { "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG },
17141716 #endif
@@ -1848,6 +1850,14 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 133 unmodified lines
18481850 tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
18491851 s->output_type = TCC_OUTPUT_OBJ;
18501852 break;
1853+ case TCC_OPTION_d:
1854+ if (*optarg != 'D') {
1855+ if (s->warn_unsupported)
1856+ goto unsupported_option;
1857+ tcc_error("invalid option -- '%s'", r);
1858+ }
1859+ s->dflag = 1;
1860+ break;
18511861 #ifdef TCC_TARGET_ARM
18521862 case TCC_OPTION_float_abi:
18531863 /* tcc doesn't support soft float yet */
tcc.c+3−0
@@ -47,6 +47,7 @@ static void help(void) · 46 unmodified lines
4747 " -Usym undefine 'sym'\n"
4848 " -P do not output a #line directive\n"
4949 " -P1 use a #line directive in output instead of the gcc style\n"
50+ " -dD put a define directive in the output (inside a comment)\n"
5051 "Linker options:\n"
5152 " -Ldir add library path 'dir'\n"
5253 " -llib link with dynamic or static library 'lib'\n"
@@ -307,6 +308,8 @@ int main(int argc, char **argv) · 254 unmodified lines
307308 start_time = getclock_us();
308309
309310 tcc_set_output_type(s, s->output_type);
311+ if (s->output_type == TCC_OUTPUT_PREPROCESS)
312+ print_defines();
310313
311314 /* compile or add each files or library */
312315 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
tcc.h+2−0
@@ -661,6 +661,7 @@ struct TCCState { · 660 unmodified lines
661661 LINE_MACRO_OUTPUT_FORMAT_NONE,
662662 LINE_MACRO_OUTPUT_FORMAT_STD,
663663 } Pflag;
664+ int dflag; /* for keeping a -dD value */
664665
665666 /* for -MD/-MF: collected dependencies for this compilation */
666667 char **target_deps;
@@ -1155,6 +1156,7 @@ ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg); · 488 unmodified lines
11551156 ST_FUNC void define_undef(Sym *s);
11561157 ST_INLN Sym *define_find(int v);
11571158 ST_FUNC void free_defines(Sym *b);
1159+void print_defines(void);
11581160 ST_FUNC Sym *label_find(int v);
11591161 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
11601162 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
tccpp.c+69−22
@@ -1049,6 +1049,55 @@ static int macro_is_equal(const int *a, const int *b) · 1048 unmodified lines
10491049 return !(*a || *b);
10501050 }
10511051
1052+static void define_print(Sym *s, int is_undef)
1053+{
1054+ int c, t;
1055+ CValue cval;
1056+ const int *str;
1057+ Sym *arg;
1058+
1059+ if (tcc_state->dflag == 0 || !s || !tcc_state->ppfp)
1060+ return;
1061+
1062+ if (file) {
1063+ c = file->line_num - file->line_ref - 1;
1064+ if (c > 0) {
1065+ while (c--)
1066+ fputs("\n", tcc_state->ppfp);
1067+ file->line_ref = file->line_num;
1068+ }
1069+ }
1070+
1071+ if (is_undef) {
1072+ fprintf(tcc_state->ppfp, "// #undef %s\n", get_tok_str(s->v, NULL));
1073+ return;
1074+ }
1075+
1076+ fprintf(tcc_state->ppfp, "// #define %s", get_tok_str(s->v, NULL));
1077+ arg = s->next;
1078+ if (arg) {
1079+ char *sep = "(";
1080+ while (arg) {
1081+ fprintf(tcc_state->ppfp, "%s%s", sep, get_tok_str(arg->v & ~SYM_FIELD, NULL));
1082+ sep = ",";
1083+ arg = arg->next;
1084+ }
1085+ fprintf(tcc_state->ppfp, ")");
1086+ }
1087+
1088+ str = s->d;
1089+ if (str)
1090+ fprintf(tcc_state->ppfp, " ");
1091+
1092+ while (str) {
1093+ TOK_GET(&t, &str, &cval);
1094+ if (!t)
1095+ break;
1096+ fprintf(tcc_state->ppfp, "%s", get_tok_str(t, &cval));
1097+ }
1098+ fprintf(tcc_state->ppfp, "\n");
1099+}
1100+
10521101 /* defines handling */
10531102 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
10541103 {
@@ -1062,6 +1111,7 @@ ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg) · 7 unmodified lines
10621111 s->d = str;
10631112 s->next = first_arg;
10641113 table_ident[v - TOK_IDENT]->sym_define = s;
1114+ define_print(s, 0);
10651115 }
10661116
10671117 /* undefined a define symbol. Its name is just set to zero */
@@ -1069,8 +1119,10 @@ ST_FUNC void define_undef(Sym *s) · 1 unmodified line
10691119 {
10701120 int v;
10711121 v = s->v;
1072− if (v >= TOK_IDENT && v < tok_ident)
1122+ if (v >= TOK_IDENT && v < tok_ident) {
1123+ define_print(s, 1);
10731124 table_ident[v - TOK_IDENT]->sym_define = NULL;
1125+ }
10741126 s->v = 0;
10751127 }
10761128
@@ -1103,6 +1155,22 @@ ST_FUNC void free_defines(Sym *b) · 26 unmodified lines
11031155 define_stack = b;
11041156 }
11051157
1158+void print_defines(void)
1159+{
1160+ Sym *top, *s;
1161+ int v;
1162+
1163+ top = define_stack;
1164+ while (top) {
1165+ v = top->v;
1166+ if (v >= TOK_IDENT && v < tok_ident) {
1167+ s = table_ident[v - TOK_IDENT]->sym_define;
1168+ define_print(s, 0);
1169+ }
1170+ top = top->prev;
1171+ }
1172+}
1173+
11061174 /* label lookup */
11071175 ST_FUNC Sym *label_find(int v)
11081176 {
@@ -1192,23 +1260,6 @@ static int expr_preprocess(void) · 83 unmodified lines
11921260 return c != 0;
11931261 }
11941262
1195−#if defined(PARSE_DEBUG) || defined(PP_DEBUG)
1196−static void tok_print(int *str)
1197−{
1198− int t;
1199− CValue cval;
1200−
1201− printf("<");
1202− while (1) {
1203− TOK_GET(&t, &str, &cval);
1204− if (!t)
1205− break;
1206− printf("%s", get_tok_str(t, &cval));
1207− }
1208− printf(">\n");
1209−}
1210−#endif
1211−
12121263 /* parse after #define */
12131264 ST_FUNC void parse_define(void)
12141265 {
@@ -1279,10 +1330,6 @@ ST_FUNC void parse_define(void) · 64 unmodified lines
12791330 if (spc == 1)
12801331 --str.len; /* remove trailing space */
12811332 tok_str_add(&str, 0);
1282−#ifdef PP_DEBUG
1283− printf("define %s %d: ", get_tok_str(v, NULL), t);
1284− tok_print(str.str);
1285−#endif
12861333 define_push(v, t, str.str, first);
12871334 }
12881335

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