From 90e1270b2d9ee9222ef05612fe62747c340a2cd2 Mon Sep 17 00:00:00 2001 From: Christopher Li Date: Wed, 15 Sep 2010 15:33:45 -0700 Subject: Fixup and cleanup modifier_string() function. The change started from Bernd Petrovitsch's patch but get mostly rewritten using accociatived array. It fix the modifier_string() function and check the string length for the string. Signed-off-by: Christopher Li Signed-off-by: Bernd Petrovitsch --- show-parse.c | 64 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/show-parse.c b/show-parse.c index d7b502d..c97debe 100644 --- a/show-parse.c +++ b/show-parse.c @@ -95,29 +95,55 @@ void debug_symbol(struct symbol *sym) const char *modifier_string(unsigned long mod) { static char buffer[100]; - char *p = buffer; - const char *res,**ptr, *names[] = { - "auto", "register", "static", "extern", - "const", "volatile", "[signed]", "[unsigned]", - "[char]", "[short]", "[long]", "[long long]", - "[typedef]", "[structof]", "[unionof]", "[enum]", - "[typeof]", "[attribute]", "inline", "[addressable]", - "[nocast]", "[noderef]", "[accessed]", "[toplevel]", - "[label]", "[assigned]", "[type]", "[safe]", - "[usertype]", "[force]", "[explicitly-signed]", - NULL + int len = 0; + int i; + struct mod_name { + unsigned long mod; + const char *name; + } *m; + + static struct mod_name mod_names[] = { + {MOD_AUTO, "auto"}, + {MOD_REGISTER, "register"}, + {MOD_STATIC, "static"}, + {MOD_EXTERN, "extern"}, + {MOD_CONST, "const"}, + {MOD_VOLATILE, "volatile"}, + {MOD_SIGNED, "[signed]"}, + {MOD_UNSIGNED, "[unsigned]"}, + {MOD_CHAR, "[char]"}, + {MOD_SHORT, "[short]"}, + {MOD_LONG, "[long]"}, + {MOD_LONGLONG, "[long long]"}, + {MOD_LONGLONGLONG, "[long long long]"}, + {MOD_TYPEDEF, "[typedef]"}, + {MOD_TLS, "[tls]"}, + {MOD_INLINE, "inline"}, + {MOD_ADDRESSABLE, "[addressable]"}, + {MOD_NOCAST, "[nocast]"}, + {MOD_NODEREF, "[noderef]"}, + {MOD_ACCESSED, "[accessed]"}, + {MOD_TOPLEVEL, "[toplevel]"}, + {MOD_ASSIGNED, "[assigned]"}, + {MOD_TYPE, "[type]"}, + {MOD_SAFE, "[safe]"}, + {MOD_USERTYPE, "[usertype]"}, + {MOD_NORETURN, "[noreturn]"}, + {MOD_EXPLICITLY_SIGNED, "[explicitly-signed]"}, + {MOD_BITWISE, "[bitwise]"}, }; - ptr = names; - while ((res = *ptr++) != NULL) { - if (mod & 1) { + + for (i = 0; i < ARRAY_SIZE(mod_names); i++) { + m = mod_names + i; + if (mod & m->mod) { char c; - while ((c = *res++) != '\0') - *p++ = c; - *p++ = ' '; + const char *name = m->name; + while ((c = *name++) != '\0' && len + 2 < sizeof buffer) + buffer[len++] = c; + buffer[len++] = ' '; } - mod >>= 1; } - *p = 0; + buffer[len] = 0; return buffer; } -- cgit v1.2.3-65-gdbad