diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2016-03-29 20:05:34 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2016-03-29 20:05:34 -0700 |
commit | f87d813b6339efe1f7b96028cce0df2d96bae507 (patch) | |
tree | d065882cba2cce83dbaa3afd517665c7bf6f122d | |
parent | 32572f8c5c2a25cae3686762b29455e2c6e1a2ce (diff) | |
download | txr-f87d813b6339efe1f7b96028cce0df2d96bae507.tar.gz txr-f87d813b6339efe1f7b96028cce0df2d96bae507.tar.bz2 txr-f87d813b6339efe1f7b96028cce0df2d96bae507.zip |
Replace all stray C style casts with macros.
* gc.c (gc_report_copies): C style casts found in this
function.
* linenoise.c (strip_qual, convert, coerce): Copy and paste
the macros here.
(record_undo, compare_completions, lino_add_completion,
history_search, ab_append, sync_data_to_buf,
refresh_singleline, screen_rows, refresh_multiline,
find_nearest_paren, paren_jump, yank_sel,
edit_move_matching_paren, edit, lino_make, lino_copy,
lino_hist_add, lino_hist_set_max_len): C style casts
replaced.
* mpi/mpi-types.h (MP_DIGIT_BIT, MP_DIGIT_MAX, MP_WORD_BIT,
MP_WORD_MAX, RADIX): C style casts replaced.
* mpi/mpi.c (convert, coerce): Copy and paste the macros here.
(mp_init_size, mp_init_copy, mp_copy, mp_set_int, mp_div_d,
mp_bit, mp_to_double, mp_to_signed_bin, mp_to_unsigned_bin,
mp_to_unsigned_buf, mp_toradix_case, mp_grow, s_mp_set_bit,
s_mp_mod_2d, s_mp_mul_2d, s_mp_div_2d, s_mp_mul_d, s_mp_mul,
s_mp_sqr, s_mp_div, s_mp_2expt, s_mp_todigit): C style
casts replaced.
* mpi/mplogic (convert): Macro copy and pasted here.
(mpl_num_set, mpl_num_clear): C style casts replaced.
* parser.c (provide_completions): Likewise.
* signal.c (small_sigfillset): Likewise.
* stream.c (stdio_truncate, test_set_indent_mode,
set_indent_mode): Likewise.
-rw-r--r-- | gc.c | 3 | ||||
-rw-r--r-- | linenoise/linenoise.c | 79 | ||||
-rw-r--r-- | mpi/mpi-types.h | 10 | ||||
-rw-r--r-- | mpi/mpi.c | 69 | ||||
-rw-r--r-- | mpi/mplogic.c | 10 | ||||
-rw-r--r-- | parser.c | 4 | ||||
-rw-r--r-- | signal.c | 2 | ||||
-rw-r--r-- | stream.c | 6 |
8 files changed, 107 insertions, 76 deletions
@@ -931,6 +931,7 @@ void gc_report_copies(val *pvar) for (; pvar < gc_stack_bottom; pvar++) { if (*pvar == obj) printf("%p found at %p (offset %d)\n", - (void *) obj, (void *) pvar, (int) (pvar - opvar)); + convert(void *, obj), convert(void *, pvar), + convert(int, pvar - opvar)); } } diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c index a78e4c10..fd843212 100644 --- a/linenoise/linenoise.c +++ b/linenoise/linenoise.c @@ -60,6 +60,15 @@ #endif #include "linenoise.h" +#ifdef __cplusplus +#define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR)) +#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) +#define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR)) +#else +#define convert(TYPE, EXPR) ((TYPE) (EXPR)) +#define coerce(TYPE, EXPR) ((TYPE) (EXPR)) +#endif + #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 #define LINENOISE_MAX_LINE 1024 #define LINENOISE_MAX_DISP (LINENOISE_MAX_LINE * 8) @@ -312,8 +321,10 @@ static void free_undo_stack(lino_t *l) static void record_undo(lino_t *l) { - struct lino_undo *rec = (struct lino_undo *) chk_malloc(sizeof *rec), *iter; - char *data = (char *) chk_strdup_utf8(l->data); + struct lino_undo *rec = coerce(struct lino_undo *, + chk_malloc(sizeof *rec)); + struct lino_undo *iter; + char *data = coerce(char *, chk_strdup_utf8(l->data)); int count; if (rec == 0 || data == 0) { @@ -433,8 +444,8 @@ static void sync_data_to_buf(lino_t *l); static int compare_completions(const void *larg, const void *rarg) { - const char **lelem = (const char **) larg; - const char **relem = (const char **) rarg; + const char * const *lelem = convert(const char * const *, larg); + const char * const *relem = convert(const char * const *, rarg); const char *lstr = *lelem, *rstr = *relem; size_t llen = strlen(lstr); size_t rlen = strlen(rstr); @@ -547,11 +558,11 @@ void lino_add_completion(lino_completions_t *lc, const char *str) { size_t len = strlen(str); char *copy, **cvec; - copy = (char *) chk_malloc(len+1); + copy = coerce(char *, chk_malloc(len+1)); if (copy == NULL) return; memcpy(copy,str,len+1); - cvec = (char **) chk_realloc((mem_t *) lc->cvec, - (lc->len+1) * sizeof *cvec); + cvec = coerce(char **, chk_realloc(coerce(mem_t *, lc->cvec), + (lc->len+1) * sizeof *cvec)); if (cvec == NULL) { free(copy); return; @@ -629,7 +640,7 @@ static int history_search(lino_t *l) if (c < 32) continue; verbatim: - if (hl >= (int) sizeof hpat) + if (hl >= convert(int, sizeof hpat)) break; hpat[hl++] = c; /* fallthrough */ @@ -727,7 +738,8 @@ static void ab_init(struct abuf *ab) { } static void ab_append(struct abuf *ab, const char *s, int len) { - char *ns = (char *) chk_realloc((mem_t *) ab->b,ab->len+len); + char *ns = coerce(char *, + chk_realloc(coerce(mem_t *, ab->b), ab->len+len)); if (ns == NULL) return; memcpy(ns+ab->len,s,len); @@ -751,7 +763,7 @@ static void sync_data_to_buf(lino_t *l) l->prompt); } - while (bptr - l->buf < (ptrdiff_t) sizeof l->buf - 1) { + while (bptr - l->buf < convert(ptrdiff_t, sizeof l->buf) - 1) { size_t dpos = dptr - l->data; size_t pos = bptr - l->buf; @@ -866,7 +878,7 @@ static void refresh_singleline(lino_t *l) { snprintf(seq,64,"\x1b[0K"); ab_append(&ab,seq,strlen(seq)); /* Move cursor to original position. */ - snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen)); + snprintf(seq,64,"\r\x1b[%dC", convert(int, pos + plen)); ab_append(&ab,seq,strlen(seq)); if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ ab_free(&ab); @@ -884,7 +896,7 @@ static struct row_values screen_rows(const char *str, size_t pos, int cols) for (col = 0; ; str++) { int ch = *str; - int atpos = (str - start == (ptrdiff_t) pos); + int atpos = (str - start == convert(ptrdiff_t, pos)); switch (ch) { case '\n': @@ -935,13 +947,14 @@ static void refresh_multiline(lino_t *l) { struct abuf ab; /* Update maxrows if needed. */ - if (rows > (int)l->maxrows) l->maxrows = rows; + if (rows > convert(int, l->maxrows)) + l->maxrows = rows; /* First step: clear all the lines used before. To do so start by * going to the last row. */ ab_init(&ab); if (oldmaxrows - l->oldrow > 0) { - snprintf(seq,64,"\x1b[%dB", oldmaxrows - (int) l->oldrow); + snprintf(seq,64,"\x1b[%dB", oldmaxrows - convert(int, l->oldrow)); ab_append(&ab,seq,strlen(seq)); } @@ -984,7 +997,7 @@ static void refresh_multiline(lino_t *l) { if (nrow > rows) { ab_append(&ab, "\r\n", 2); rows++; - if (rows > (int) l->maxrows) + if (rows > convert(int, l->maxrows)) l->maxrows = rows; } @@ -1120,10 +1133,10 @@ static size_t find_nearest_paren(const char *s, size_t i) { static const char *ope = "([{"; static const char *clo = ")]}"; - size_t pre = (size_t) -1, nxt = (size_t) -1; + size_t pre = convert(size_t, -1), nxt = convert(size_t, -1); size_t j; - for (j = i; j != (size_t) -1; j--) { + for (j = i; j != convert(size_t, -1); j--) { if (s[j] && (strchr(ope, s[j]) || strchr(clo, s[j]))) { pre = j; break; @@ -1137,10 +1150,10 @@ static size_t find_nearest_paren(const char *s, size_t i) } } - if (pre == (size_t) -1) + if (pre == convert(size_t, -1)) return nxt; - if (nxt == (size_t) -1) + if (nxt == convert(size_t, -1)) return pre; if (i - pre > nxt - i) @@ -1185,10 +1198,10 @@ static void paren_jump(lino_t *l) { size_t pos = scan_rev(l->data, l->dpos - 1); - if (pos == (size_t) -1) + if (pos == convert(size_t, -1)) pos = scan_fwd(l->data, l->dpos - 1); - if (pos != (size_t) -1) { + if (pos != convert(size_t, -1)) { size_t dp = l->dpos; l->dpos = pos; refresh_line(l); @@ -1225,7 +1238,7 @@ static void yank_sel(lino_t *l) if (end - sel > 0) { free(l->clip); - l->clip = (char *) chk_malloc(end - sel + 1); + l->clip = coerce(char *, chk_malloc(end - sel + 1)); memcpy(l->clip, l->data + sel, end - sel); l->clip[end - sel] = 0; l->dpos = sel; @@ -1374,14 +1387,14 @@ static void edit_move_matching_paren(lino_t *l) { size_t p = find_nearest_paren(l->data, l->dpos); - if (p != (size_t) -1) { + if (p != convert(size_t, -1)) { size_t fw = scan_fwd(l->data, p); size_t re = scan_rev(l->data, p); - if (fw != (size_t) -1) { + if (fw != convert(size_t, -1)) { l->dpos = fw; l->need_refresh = 1; - } else if (re != (size_t) -1) { + } else if (re != convert(size_t, -1)) { l->dpos = re; l->need_refresh = 1; } else { @@ -1705,7 +1718,7 @@ static int edit(lino_t *l, const char *prompt) } if (nread <= 0) { - ret = l->len ? (int) l->len : -1; + ret = l->len ? convert(int, l->len) : -1; goto out; } @@ -1753,12 +1766,12 @@ static int edit(lino_t *l, const char *prompt) extend_num = 1; for (; extend_num--; word_end = word_start) { - while (word_end > prev_line && isspace((unsigned char) word_end[-1])) + while (word_end > prev_line && isspace(convert(unsigned char, word_end[-1]))) word_end--; word_start = word_end; - while (word_start > prev_line && !isspace((unsigned char) word_start[-1])) + while (word_start > prev_line && !isspace(convert(unsigned char, word_start[-1]))) word_start--; if (extend_num == 0) @@ -1831,7 +1844,7 @@ static int edit(lino_t *l, const char *prompt) l->save_hist_idx = l->history_index; goto out; default: - if (isdigit((unsigned char) c)) { + if (isdigit(convert(unsigned char, c))) { if (extend_num < 0) extend_num = 0; extend_num %= 100; @@ -2195,7 +2208,7 @@ static void unlink_from_list(lino_t *ls) lino_t *lino_make(int ifd, int ofd) { - lino_t *ls = (lino_t *) chk_malloc(sizeof *ls); + lino_t *ls = coerce(lino_t *, chk_malloc(sizeof *ls)); if (ls) { memset(ls, 0, sizeof *ls); @@ -2211,7 +2224,7 @@ lino_t *lino_make(int ifd, int ofd) lino_t *lino_copy(lino_t *le) { - lino_t *ls = (lino_t *) chk_malloc(sizeof *ls); + lino_t *ls = coerce(lino_t *, chk_malloc(sizeof *ls)); if (ls != 0) { *ls = *le; @@ -2305,7 +2318,7 @@ int lino_hist_add(lino_t *ls, const char *line) { /* Initialization on first call. */ if (ls->history == NULL) { size_t size = ls->history_max_len * sizeof *ls->history; - ls->history = (char **) chk_malloc(size); + ls->history = coerce(char **, chk_malloc(size)); if (ls->history == NULL) return 0; memset(ls->history, 0, size); } @@ -2339,7 +2352,7 @@ int lino_hist_set_max_len(lino_t *ls, int len) { if (ls->history) { int tocopy = ls->history_len; - nsv = (char **) chk_malloc(len * sizeof *nsv); + nsv = coerce(char **, chk_malloc(len * sizeof *nsv)); if (nsv == NULL) return 0; /* If we can't copy everything, free the elements we'll not use. */ diff --git a/mpi/mpi-types.h b/mpi/mpi-types.h index d8d1a718..eb59084f 100644 --- a/mpi/mpi-types.h +++ b/mpi/mpi-types.h @@ -48,9 +48,9 @@ typedef int mp_err; #error Failure to configure MPI types on this target platform #endif -#define MP_DIGIT_BIT ((int) (CHAR_BIT*sizeof(mp_digit))) -#define MP_DIGIT_MAX ((mp_digit) -1) -#define MP_WORD_BIT ((int) (CHAR_BIT*sizeof(mp_word))) -#define MP_WORD_MAX ((mp_word) -1) +#define MP_DIGIT_BIT convert(int, CHAR_BIT*sizeof(mp_digit)) +#define MP_DIGIT_MAX convert(mp_digit, -1) +#define MP_WORD_BIT convert(int, CHAR_BIT*sizeof(mp_word)) +#define MP_WORD_MAX convert(mp_word, -1) -#define RADIX (((mp_word) MP_DIGIT_MAX) + 1) +#define RADIX (convert(mp_word, MP_DIGIT_MAX) + 1) @@ -26,6 +26,14 @@ #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) +#ifdef __cplusplus +#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) +#define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR)) +#else +#define convert(TYPE, EXPR) ((TYPE) (EXPR)) +#define coerce(TYPE, EXPR) ((TYPE) (EXPR)) +#endif + typedef unsigned char mem_t; extern mem_t *chk_calloc(size_t n, size_t size); @@ -292,7 +300,8 @@ mp_err mp_init_size(mp_int *mp, mp_size prec) { ARGCHK(mp != NULL && prec > 0, MP_BADARG); - if((DIGITS(mp) = (mp_digit *) s_mp_alloc(prec, sizeof(mp_digit))) == NULL) + if ((DIGITS(mp) = coerce(mp_digit *, + s_mp_alloc(prec, sizeof (mp_digit)))) == NULL) return MP_MEM; SIGN(mp) = MP_ZPOS; @@ -322,7 +331,8 @@ mp_err mp_init_copy(mp_int *mp, mp_int *from) if(mp == from) return MP_OKAY; - if((DIGITS(mp) = (mp_digit *) s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL) + if ((DIGITS(mp) = coerce(mp_digit *, + s_mp_alloc(USED(from), sizeof (mp_digit)))) == NULL) return MP_MEM; s_mp_copy(DIGITS(from), DIGITS(mp), USED(from)); @@ -368,7 +378,8 @@ mp_err mp_copy(mp_int *from, mp_int *to) s_mp_copy(DIGITS(from), DIGITS(to), USED(from)); } else { - if((tmp = (mp_digit *) s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL) + if((tmp = coerce(mp_digit *, + s_mp_alloc(USED(from), sizeof (mp_digit)))) == NULL) return MP_MEM; s_mp_copy(DIGITS(from), tmp, USED(from)); @@ -517,8 +528,8 @@ mp_err mp_set_int(mp_int *mp, long z) if((res = s_mp_mul_2d(mp, CHAR_BIT)) != MP_OKAY) return res; - res = s_mp_add_d(mp, - (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX)); + res = s_mp_add_d(mp, + convert(mp_digit, ((v >> (ix * CHAR_BIT)) & UCHAR_MAX))); if(res != MP_OKAY) return res; @@ -793,7 +804,7 @@ mp_err mp_div_d(mp_int *a, mp_digit d, mp_int *q, mp_digit *r) if((pow = s_mp_ispow2d(d)) >= 0) { mp_digit mask; - mask = ((mp_digit) 1 << pow) - 1; + mask = (convert(mp_digit, 1) << pow) - 1; rem = DIGIT(a, 0) & mask; if(q) { @@ -2772,7 +2783,7 @@ mp_err mp_bit(mp_int *a, mp_digit bit) mp_err res; int a_neg = ISNEG(a); int digit = bit / MP_DIGIT_BIT; - mp_digit mask = ((mp_digit) 1 << (bit % MP_DIGIT_BIT)); + mp_digit mask = convert(mp_digit, 1) << (bit % MP_DIGIT_BIT); if (a_neg) { mp_init(&tmp); @@ -2796,9 +2807,9 @@ mp_err mp_to_double(mp_int *mp, double *d) if (!mult) mult = pow(2.0, MP_DIGIT_BIT); - for (ix = (int) used - 2; ix >= 0; ix--) { + for (ix = convert(int, used) - 2; ix >= 0; ix--) { out = out * mult; - out += (double) dp[ix]; + out += convert(double, dp[ix]); } if (SIGN(mp) == MP_NEG) @@ -2888,7 +2899,7 @@ mp_err mp_to_signed_bin(mp_int *mp, unsigned char *str) ARGCHK(mp != NULL && str != NULL, MP_BADARG); /* Caller responsible for allocating enough memory (use mp_raw_size(mp)) */ - str[0] = (char)SIGN(mp); + str[0] = convert(char, SIGN(mp)); return mp_to_unsigned_bin(mp, str + 1); @@ -2978,7 +2989,7 @@ mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str) int ix; d = *dp; - for(ix = 0; ix < (int) sizeof(mp_digit); ++ix) { + for(ix = 0; ix < convert(int, sizeof(mp_digit)); ++ix) { *spos = d & UCHAR_MAX; d >>= CHAR_BIT; ++spos; @@ -3021,7 +3032,7 @@ mp_err mp_to_unsigned_buf(mp_int *mp, unsigned char *str, int size) int ix; mp_digit d = *dp; - for (ix = 0; ix < (int) sizeof(mp_digit); ++ix) { + for (ix = 0; ix < convert(int, sizeof(mp_digit)); ++ix) { if (dp + 1 == end && d == 0) break; ARGCHK(spos >= str, MP_RANGE); @@ -3162,7 +3173,7 @@ mp_err mp_toradix_case(mp_int *mp, unsigned char *str, int radix, int low) mp_err res; mp_int tmp; mp_sign sgn; - mp_digit rem, rdx = (mp_digit)radix; + mp_digit rem, rdx = convert(mp_digit, radix); char ch; if((res = mp_init_copy(&tmp, mp)) != MP_OKAY) @@ -3271,7 +3282,7 @@ mp_err s_mp_grow(mp_int *mp, mp_size min) /* Set min to next nearest default precision block size */ min = ((min + (s_mp_defprec - 1)) / s_mp_defprec) * s_mp_defprec; - if((tmp = (mp_digit *) s_mp_alloc(min, sizeof(mp_digit))) == NULL) + if ((tmp = coerce(mp_digit *, s_mp_alloc(min, sizeof (mp_digit)))) == NULL) return MP_MEM; s_mp_copy(DIGITS(mp), tmp, USED(mp)); @@ -3629,7 +3640,7 @@ mp_err s_mp_set_bit(mp_int *a, int bit) if ((res = s_mp_pad(a, nd)) != MP_OKAY) return res; - DIGIT(a, nd - 1) |= ((mp_digit) 1 << nbit); + DIGIT(a, nd - 1) |= (convert(mp_digit, 1) << nbit); return MP_OKAY; } @@ -3789,11 +3800,11 @@ void s_mp_mod_2d(mp_int *mp, mp_digit d) int ix; mp_digit dmask, *dp = DIGITS(mp); - if((int) ndig >= USED(mp)) + if (convert(int, ndig) >= USED(mp)) return; /* Flush all the bits above 2^d in its digit */ - dmask = ((mp_digit) 1 << nbit) - 1; + dmask = (convert(mp_digit, 1) << nbit) - 1; dp[ndig] &= dmask; /* Flush all digits above the one with 2^d in it */ @@ -3826,7 +3837,7 @@ mp_err s_mp_mul_2d(mp_int *mp, mp_digit d) dp = DIGITS(mp); used = USED(mp); d %= DIGIT_BIT; - mask = ((mp_digit) 1 << d) - 1; + mask = (convert(mp_digit, 1) << d) - 1; /* If the shift requires another digit, make sure we've got one to work with */ @@ -3874,7 +3885,7 @@ void s_mp_div_2d(mp_int *mp, mp_digit d) s_mp_rshd(mp, d / DIGIT_BIT); d %= DIGIT_BIT; - mask = ((mp_digit) 1 << d) - 1; + mask = (convert(mp_digit, 1) << d) - 1; save = 0; for(ix = USED(mp) - 1; ix >= 0; ix--) { @@ -4018,7 +4029,7 @@ mp_err s_mp_mul_d(mp_int *a, mp_digit d) unless absolutely necessary. */ max = USED(a); - w = dp[max - 1] * (mp_word) d; + w = dp[max - 1] * convert(mp_word, d); if(CARRYOUT(w) != 0) { if((res = s_mp_pad(a, max + 1)) != MP_OKAY) return res; @@ -4026,7 +4037,7 @@ mp_err s_mp_mul_d(mp_int *a, mp_digit d) } for(ix = 0; ix < max; ix++) { - w = (dp[ix] * (mp_word) d) + k; + w = dp[ix] * convert(mp_word, d) + k; dp[ix] = ACCUM(w); k = CARRYOUT(w); } @@ -4243,7 +4254,7 @@ mp_err s_mp_mul(mp_int *a, mp_int *b) pa = DIGITS(a); for(jx = 0; jx < ua; ++jx, ++pa) { pt = pbt + ix + jx; - w = *pb * (mp_word) *pa + k + *pt; + w = *pb * convert(mp_word, *pa) + k + *pt; *pt = ACCUM(w); k = CARRYOUT(w); } @@ -4325,7 +4336,7 @@ mp_err s_mp_sqr(mp_int *a) if(*pa1 == 0) continue; - w = DIGIT(&tmp, ix + ix) + (*pa1 * (mp_word) *pa1); + w = DIGIT(&tmp, ix + ix) + *pa1 * convert(mp_word, *pa1); pbt[ix + ix] = ACCUM(w); k = CARRYOUT(w); @@ -4347,7 +4358,7 @@ mp_err s_mp_sqr(mp_int *a) pt = pbt + ix + jx; /* Compute the multiplicative step */ - w = *pa1 * (mp_word) *pa2; + w = *pa1 * convert(mp_word, *pa2); /* If w is more than half MP_WORD_MAX, the doubling will overflow, and we need to record a carry out into the next @@ -4391,7 +4402,7 @@ mp_err s_mp_sqr(mp_int *a) */ kx = 1; while(k) { - k = (mp_word) pbt[ix + jx + kx] + 1; + k = convert(mp_word, pbt[ix + jx + kx]) + 1; pbt[ix + jx + kx] = ACCUM(k); k = CARRYOUT(k); ++kx; @@ -4432,8 +4443,8 @@ mp_err s_mp_div(mp_int *a, mp_int *b) /* Shortcut if b is power of two */ if((ix = s_mp_ispow2(b)) >= 0) { mp_copy(a, b); /* need this for remainder */ - s_mp_div_2d(a, (mp_digit)ix); - s_mp_mod_2d(b, (mp_digit)ix); + s_mp_div_2d(a, convert(mp_digit, ix)); + s_mp_mod_2d(b, convert(mp_digit, ix)); return MP_OKAY; } @@ -4553,7 +4564,7 @@ mp_err s_mp_2expt(mp_int *a, mp_digit k) if((res = s_mp_pad(a, dig + 1)) != MP_OKAY) return res; - DIGIT(a, dig) |= ((mp_digit) 1 << bit); + DIGIT(a, dig) |= (convert(mp_digit, 1) << bit); return MP_OKAY; @@ -4819,7 +4830,7 @@ char s_mp_todigit(int val, int r, int low) */ int s_mp_outlen(int bits, int r) { - return (int)((double)bits * LOG_V_2(r) + 0.5); + return convert(int, convert(double, bits) * LOG_V_2(r) + 0.5); } /* end s_mp_outlen() */ diff --git a/mpi/mplogic.c b/mpi/mplogic.c index eb44b866..63baeef9 100644 --- a/mpi/mplogic.c +++ b/mpi/mplogic.c @@ -16,6 +16,12 @@ #endif #include <stdlib.h> +#ifdef __cplusplus +#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR)) +#else +#define convert(TYPE, EXPR) ((TYPE) (EXPR)) +#endif + /* Some things from the guts of the MPI library we make use of... */ extern mp_err s_mp_lshd(mp_int *mp, mp_size p); extern void s_mp_rshd(mp_int *mp, mp_size p); @@ -293,7 +299,7 @@ mp_err mpl_num_set(mp_int *a, int *num) for(ix = 0; ix < USED(a); ix++) { cur = DIGIT(a, ix); - for(db = 0; db < (int) sizeof(mp_digit); db++) { + for(db = 0; db < convert(int, sizeof(mp_digit)); db++) { reg = (cur >> (CHAR_BIT * db)) & UCHAR_MAX; nset += bitc[reg]; @@ -322,7 +328,7 @@ mp_err mpl_num_clear(mp_int *a, int *num) for(ix = 0; ix < USED(a); ix++) { cur = DIGIT(a, ix); - for(db = 0; db < (int) sizeof(mp_digit); db++) { + for(db = 0; db < convert(int, sizeof(mp_digit)); db++) { reg = (cur >> (CHAR_BIT * db)) & UCHAR_MAX; nset += bitc[UCHAR_MAX - reg]; @@ -501,7 +501,7 @@ static void provide_completions(const char *data, if (!ptr) return; - while ((isalnum((unsigned char) *ptr) || strchr(gly, *ptr)) && + while ((isalnum(convert(unsigned char, *ptr)) || strchr(gly, *ptr)) && (sym = ptr) && ptr > data) ptr--; @@ -516,7 +516,7 @@ static void provide_completions(const char *data, } else { ptr--; - while ((isalnum((unsigned char) *ptr) || strchr(gly, *ptr)) && + while ((isalnum(convert(unsigned char, *ptr)) || strchr(gly, *ptr)) && (pkg = ptr) && ptr > data) ptr--; @@ -248,7 +248,7 @@ static void teardown_alt_stack(void) static void small_sigfillset(small_sigset_t *ss) { - ss->set = (unsigned int) -1; + ss->set = convert(unsigned int, -1); } val set_sig_handler(val signo, val lambda) @@ -757,7 +757,7 @@ static val stdio_truncate(val stream, val len) int (*truncfun)(int, long) = chsize; #endif - if ((cnum) (trunc_off_t) l != l) + if (convert(cnum, convert(trunc_off_t, l)) != l) uw_throwf(error_s, lit("truncate-stream: ~s is too large"), len, nao); return (h->f != 0 && truncfun(fileno(h->f), l) == 0) @@ -3254,7 +3254,7 @@ val test_set_indent_mode(val stream, val compare, val mode) cobj_handle(stream, stream_s)); val oldval = num_fast(s->indent_mode); if (oldval == compare) - s->indent_mode = (enum indent_mode) c_num(mode); + s->indent_mode = convert(enum indent_mode, c_num(mode)); return oldval; } @@ -3263,7 +3263,7 @@ val set_indent_mode(val stream, val mode) struct strm_base *s = coerce(struct strm_base *, cobj_handle(stream, stream_s)); val oldval = num_fast(s->indent_mode); - s->indent_mode = (enum indent_mode) c_num(mode); + s->indent_mode = convert(enum indent_mode, c_num(mode)); return oldval; } |