| @@ -1049,6 +1049,55 @@ static int macro_is_equal(const int *a, const int *b) · 1048 unmodified lines |
| 1049 | 1049 | return !(*a || *b); |
| 1050 | 1050 | } |
| 1051 | 1051 | |
| 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 | + |
| 1052 | 1101 | /* defines handling */ |
| 1053 | 1102 | ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg) |
| 1054 | 1103 | { |
| @@ -1062,6 +1111,7 @@ ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg) · 7 unmodified lines |
| 1062 | 1111 | s->d = str; |
| 1063 | 1112 | s->next = first_arg; |
| 1064 | 1113 | table_ident[v - TOK_IDENT]->sym_define = s; |
| 1114 | + define_print(s, 0); |
| 1065 | 1115 | } |
| 1066 | 1116 | |
| 1067 | 1117 | /* 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 |
| 1069 | 1119 | { |
| 1070 | 1120 | int v; |
| 1071 | 1121 | v = s->v; |
| 1072 | | − if (v >= TOK_IDENT && v < tok_ident) |
| 1122 | + if (v >= TOK_IDENT && v < tok_ident) { |
| 1123 | + define_print(s, 1); |
| 1073 | 1124 | table_ident[v - TOK_IDENT]->sym_define = NULL; |
| 1125 | + } |
| 1074 | 1126 | s->v = 0; |
| 1075 | 1127 | } |
| 1076 | 1128 | |
| @@ -1103,6 +1155,22 @@ ST_FUNC void free_defines(Sym *b) · 26 unmodified lines |
| 1103 | 1155 | define_stack = b; |
| 1104 | 1156 | } |
| 1105 | 1157 | |
| 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 | + |
| 1106 | 1174 | /* label lookup */ |
| 1107 | 1175 | ST_FUNC Sym *label_find(int v) |
| 1108 | 1176 | { |
| @@ -1192,23 +1260,6 @@ static int expr_preprocess(void) · 83 unmodified lines |
| 1192 | 1260 | return c != 0; |
| 1193 | 1261 | } |
| 1194 | 1262 | |
| 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 | | − |
| 1212 | 1263 | /* parse after #define */ |
| 1213 | 1264 | ST_FUNC void parse_define(void) |
| 1214 | 1265 | { |
| @@ -1279,10 +1330,6 @@ ST_FUNC void parse_define(void) · 64 unmodified lines |
| 1279 | 1330 | if (spc == 1) |
| 1280 | 1331 | --str.len; /* remove trailing space */ |
| 1281 | 1332 | 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 |
| 1286 | 1333 | define_push(v, t, str.str, first); |
| 1287 | 1334 | } |
| 1288 | 1335 | |