Use debug printer instead direct output, fix some size_t related warnings.
[libfirm] / ir / libcore / lc_printf.c
index c8a552c..1ac0cfa 100644 (file)
@@ -144,7 +144,7 @@ static int dispatch_snprintf(char *buf, size_t len, const char *fmt,
        int res = 0;
 
        switch (lc_arg_type) {
-#define LC_ARG_TYPE(type,name) \
+#define LC_ARG_TYPE(type,name,va_type) \
                case lc_arg_type_ ## name: res = snprintf(buf, len, fmt, val->v_ ## name); break;
 #include "lc_printf_arg_types.def"
 #undef LC_ARG_TYPE
@@ -172,6 +172,45 @@ static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
        strncpy(mod, occ->modifier, sizeof(mod) - 1);
        mod[LC_MIN(sizeof(mod) - 1, occ->modifier_length)] = '\0';
 
+#ifdef _MSC_VER
+       /* work-around for buggy mscrt not supporting z, j,  and t modifier */
+       if (occ->modifier_length == 1) {
+               if (mod[0] == 'z') {
+                       if (sizeof(size_t) == sizeof(int))
+                               mod[0] = '\0';
+                       if (sizeof(size_t) == sizeof(__int64)) {
+                               mod[0] = 'I';
+                               mod[1] = '6';
+                               mod[2] = '4';
+                               mod[3] = '\0';
+                       }
+               } else if (mod[0] == 't') {
+                       if (sizeof(ptrdiff_t) == sizeof(int))
+                               mod[0] = '\0';
+                       if (sizeof(ptrdiff_t) == sizeof(__int64)) {
+                               mod[0] = 'I';
+                               mod[1] = '6';
+                               mod[2] = '4';
+                               mod[3] = '\0';
+                       }
+               } else if (mod[0] == 'j') {
+                       if (sizeof(intmax_t) == sizeof(int))
+                               mod[0] = '\0';
+                       if (sizeof(intmax_t) == sizeof(__int64)) {
+                               mod[0] = 'I';
+                               mod[1] = '6';
+                               mod[2] = '4';
+                               mod[3] = '\0';
+                       }
+               }
+       } else if (occ->modifier_length == 2) {
+               if (mod[0] == 'h' && mod[1] == 'h') {
+                       /* no support for char in mscrt, but we can safely ignore it
+                        * because the size is handled by the argument reader code */
+                       mod[0] = '\0';
+               }
+       }
+#endif
        snprintf(buf, len, "%%%s%s%s%s%s%s%s%s%c",
                        occ->flag_space ? "#" : "",
                        occ->flag_hash ? "#" : "",
@@ -189,20 +228,20 @@ static char *make_fmt(char *buf, size_t len, const lc_arg_occ_t *occ)
  */
 static int std_get_lc_arg_type(const lc_arg_occ_t *occ)
 {
-       int modlen = occ->modifier_length;
+       size_t modlen = occ->modifier_length;
 
        /* check, if the type can be derived from the modifier */
        if (modlen > 0) {
                const char *mod = occ->modifier;
                switch (mod[0]) {
+                       case 'h':
+                               return modlen > 1 && mod[1] == 'h' ? lc_arg_type_char : lc_arg_type_short;
                        case 'l':
                                return modlen > 1 && mod[1] == 'l' ? lc_arg_type_long_long : lc_arg_type_long;
 #define TYPE_CASE(letter,type) case letter: return lc_arg_type_ ## type;
-#if 0
                        TYPE_CASE('j', intmax_t);
                        TYPE_CASE('z', size_t);
                        TYPE_CASE('t', ptrdiff_t);
-#endif
                        TYPE_CASE('L', long_double);
 #undef TYPE_CASE
                }
@@ -241,7 +280,7 @@ static int std_emit(lc_appendable_t *app, const lc_arg_occ_t *occ, const lc_arg_
                case 'n':
                        {
                                int *num = (int*)val->v_ptr;
-                               *num = app->written;
+                               *num = (int)app->written;
                        }
                        break;
 
@@ -387,7 +426,7 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                                        for (ch = *s; ch != '}' && ch != '\0'; ch = *++s);
 
                                        if (s - named) {
-                                               int n = s - named;
+                                               size_t n = s - named;
                                                char *name;
                                                lc_arg_t tmp;
 
@@ -452,7 +491,8 @@ int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
 
                        /* Store the value according to argument information */
                        switch (occ.lc_arg_type) {
-#define LC_ARG_TYPE(type,name) case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, type); break;
+#define LC_ARG_TYPE(type,name,va_type) \
+                       case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, va_type); break;
 #include "lc_printf_arg_types.def"
 #undef LC_ARG_TYPE
                        }