From 4c20744d669982153209773e206c7a1f4ff28689 Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 7 Apr 2015 03:19:20 +0300 Subject: [PATCH] translate: check xrefs before adding to header otherwise lots of conflicting symbols on project merge --- tools/translate.c | 84 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 7 deletions(-) diff --git a/tools/translate.c b/tools/translate.c index bec6d07..a55e6d1 100644 --- a/tools/translate.c +++ b/tools/translate.c @@ -7379,7 +7379,78 @@ static char *next_word_s(char *w, size_t wsize, char *s) return s + i; } -static void scan_variables(FILE *fasm) +static int cmpstringp(const void *p1, const void *p2) +{ + return strcmp(*(char * const *)p1, *(char * const *)p2); +} + +static int is_xref_needed(char *p, char **rlist, int rlist_len) +{ + char *p2; + + p = sskip(p); + if (strstr(p, "...")) + // unable to determine, assume needed + return 1; + + if (*p == '.') // .text, .data, ... + // ref from other data or non-function -> no + return 0; + + p2 = strpbrk(p, "+:\r\n\x18"); + if (p2 != NULL) + *p2 = 0; + if (bsearch(&p, rlist, rlist_len, sizeof(rlist[0]), cmpstringp)) + // referenced from removed code + return 0; + + return 1; +} + +static int xrefs_show_need(FILE *fasm, char *p, + char **rlist, int rlist_len) +{ + int found_need = 0; + char line[256]; + long pos; + + p = strrchr(p, ';'); + if (p != NULL && *p == ';' && IS_START(p + 2, "DATA XREF: ")) { + p += 13; + if (is_xref_needed(p, rlist, rlist_len)) + return 1; + } + + pos = ftell(fasm); + while (1) + { + if (!my_fgets(line, sizeof(line), fasm)) + break; + // non-first line is always indented + if (!my_isblank(line[0])) + break; + + // should be no content, just comment + p = sskip(line); + if (*p != ';') + break; + + p = strrchr(p, ';'); + p += 2; + // it's printed once, but no harm to check again + if (IS_START(p, "DATA XREF: ")) + p += 11; + + if (is_xref_needed(p, rlist, rlist_len)) { + found_need = 1; + break; + } + } + fseek(fasm, pos, SEEK_SET); + return found_need; +} + +static void scan_variables(FILE *fasm, char **rlist, int rlist_len) { struct scanned_var *var; char line[256] = { 0, }; @@ -7448,6 +7519,10 @@ static void scan_variables(FILE *fasm) break; } + // check refs comment(s) + if (!xrefs_show_need(fasm, p, rlist, rlist_len)) + continue; + if ((hg_var_cnt & 0xff) == 0) { hg_vars = realloc(hg_vars, sizeof(hg_vars[0]) * (hg_var_cnt + 0x100)); @@ -7546,11 +7621,6 @@ static int cmp_chunks(const void *p1, const void *p2) return strcmp(c1->name, c2->name); } -static int cmpstringp(const void *p1, const void *p2) -{ - return strcmp(*(char * const *)p1, *(char * const *)p2); -} - static void scan_ahead(FILE *fasm) { char words[2][256]; @@ -7748,7 +7818,7 @@ int main(int argc, char *argv[]) } if (g_header_mode) - scan_variables(fasm); + scan_variables(fasm, rlist, rlist_len); while (my_fgets(line, sizeof(line), fasm)) { -- 2.39.5