# HG changeset patch # User Thinker K.F. Li # Date 1283952916 -28800 # Node ID 7fec19e274118bb4fc14aac4565cc8106477bbfc # Parent 165781cb4cdd2d2f5283a907c0d5c4d60f28bea0 Add more comment diff -r 165781cb4cdd -r 7fec19e27411 src/cospy.c --- a/src/cospy.c Wed Sep 08 21:29:30 2010 +0800 +++ b/src/cospy.c Wed Sep 08 21:35:16 2010 +0800 @@ -10,6 +10,10 @@ int plugin_is_GPL_compatible; +/*! \brief Parse a tree of type and return respective string. + * + * \return a string for the type. It must be free through ggc_free(). + */ static char * parse_type(tree type_node) { tree type_v; @@ -22,6 +26,7 @@ int type_str_len; int i, ptr_i; + /* Collect pointers */ type_v = type_node; while(TREE_CODE(type_v) == POINTER_TYPE) { if(TREE_READONLY(type_v)) @@ -30,21 +35,6 @@ lvl++; } - type_decl = TYPE_NAME(type_v); - type_name = DECL_NAME(type_decl); - base_type_name = IDENTIFIER_POINTER(type_name); - - type_str_len = lvl + strlen(base_type_name) + const_cnt * 5; /* "const" */ - if(TREE_READONLY(type_v)) - type_str_len += 6; /* "const " */ - type_str = (char *)ggc_alloc(type_str_len + 1); - - type_str[0] = 0; - if(TREE_READONLY(type_v)) - strcpy(type_str, "const "); - - strcat(type_str, base_type_name); - pointers = (tree *)ggc_alloc(sizeof(tree) * lvl); type_v = type_node; for(ptr_i = 0; ptr_i < lvl; ptr_i++) { @@ -52,6 +42,24 @@ type_v = TREE_TYPE(type_v); } + /* Get name of base type */ + type_decl = TYPE_NAME(type_v); + type_name = DECL_NAME(type_decl); + base_type_name = IDENTIFIER_POINTER(type_name); + + /* Compute total length of string of full type */ + type_str_len = lvl + strlen(base_type_name) + const_cnt * 5; /* "const" */ + if(TREE_READONLY(type_v)) + type_str_len += 6; /* "const " */ + type_str = (char *)ggc_alloc(type_str_len + 1); + + /* modify const for base type */ + type_str[0] = 0; + if(TREE_READONLY(type_v)) + strcpy(type_str, "const "); + strcat(type_str, base_type_name); + + /* Add pointers and const modifications after base type */ i = strlen(type_str); for(ptr_i = lvl - 1; ptr_i >= 0; ptr_i--) { type_v = pointers[ptr_i];