summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-01-06 20:03:25 -0800
committerKaz Kylheku <kaz@kylheku.com>2022-01-06 20:03:25 -0800
commit07a0d613fceaf12fd7bf2f900223ac925908c76e (patch)
tree7d8ded9098d1e715af35b5cc8b6e7ea97097f543
parent5983e14cf97bd1302151ffc3dbc53451acdc87e9 (diff)
downloadtxr-07a0d613fceaf12fd7bf2f900223ac925908c76e.tar.gz
txr-07a0d613fceaf12fd7bf2f900223ac925908c76e.tar.bz2
txr-07a0d613fceaf12fd7bf2f900223ac925908c76e.zip
Casts have crept into the code not wrapped by macros.
It is against TXR coding conventions to use the C cast notation. The usage creeps into the code. To find instances of this, we must compile using GNU g++, and add -Wold-style-cast via EXTRA_FLAGS. * eval.c (prof_call): Use macro instead of cast. * ffi.c (pad_retval, ffi_varray_alloc, make_ffi_type_union, carray_dup, carray_replace, uint_carray, int_carray, put_carray, fill_carray): Likewise. * itypes.c (c_i64, c_u64): Likewise. * lib.c (cyr, chk_xalloc, spilt_str_keep, vector, cobj_register): Likewise. * linenoise.c (record_undo): Likewise. Also, drop one superfluous cast: wstrdup_fn returns wchar_t *. (flash, edit_insert, edit_insert_str): Use macro instead of cast. * mpi/mpi.c (s_mp_ispow2d): Likewise. * parser.c (lino_getch): Likewise. * rand.c (make_random_state, random_buf): Likewise. * stream.c (generic_get_line, do_parse_mode): Likewise. * struct.c (get_duplicate_supers, call_initfun_chain, call_postinitfun_chain): Likewise. * sysif.c (c_time): Likewise. * tree.c (tr_insert): Likewise.
-rw-r--r--eval.c4
-rw-r--r--ffi.c20
-rw-r--r--itypes.c9
-rw-r--r--lib.c10
-rw-r--r--linenoise/linenoise.c12
-rw-r--r--mpi/mpi.c2
-rw-r--r--parser.c2
-rw-r--r--rand.c24
-rw-r--r--stream.c6
-rw-r--r--struct.c6
-rw-r--r--sysif.c4
-rw-r--r--tree.c4
12 files changed, 56 insertions, 47 deletions
diff --git a/eval.c b/eval.c
index e07d5327..6f57a138 100644
--- a/eval.c
+++ b/eval.c
@@ -3071,11 +3071,11 @@ val prof_call(val (*fun)(mem_t *ctx), mem_t *ctx)
alloc_bytes_t delta_mlbytes = malloc_bytes - start_mlbytes;
alloc_bytes_t delta_gcbytes = gc_bytes - start_gcbytes;
#if SIZEOF_ALLOC_BYTES_T > SIZEOF_PTR
- val dmb = if3(delta_mlbytes <= (alloc_bytes_t) INT_PTR_MAX,
+ val dmb = if3(delta_mlbytes <= convert(alloc_bytes_t, INT_PTR_MAX),
unum(delta_mlbytes),
logior(ash(unum(delta_mlbytes >> 32), num_fast(32)),
unum(delta_mlbytes & 0xFFFFFFFF)));
- val dgc = if3(delta_gcbytes <= (alloc_bytes_t) INT_PTR_MAX,
+ val dgc = if3(delta_gcbytes <= convert(alloc_bytes_t, INT_PTR_MAX),
unum(delta_gcbytes),
logior(ash(unum(delta_gcbytes >> 32), num_fast(32)),
unum(delta_gcbytes & 0xFFFFFFFF)));
diff --git a/ffi.c b/ffi.c
index d39e5b42..3e8aaf5a 100644
--- a/ffi.c
+++ b/ffi.c
@@ -80,7 +80,7 @@
#define alignof(type) offsetof(struct {char x; type y;}, y)
#define pad_retval(size) (!(size) || convert(size_t, size) > sizeof (ffi_arg) \
- ? (size_t) (size) \
+ ? convert(size_t, size) \
: sizeof (ffi_arg))
#define min(a, b) ((a) < (b) ? (a) : (b))
@@ -470,7 +470,7 @@ static mem_t *ffi_varray_alloc(struct txr_ffi_type *tft, val obj, val self)
{
cnum dynsize = ffi_varray_dynsize(tft, obj, self);
size_t size = dynsize;
- if ((cnum) size != dynsize)
+ if (convert(cnum, size) != dynsize)
uw_throwf(error_s, lit("~a: array too large"), self, nao);
return chk_calloc(size, 1);
}
@@ -3580,9 +3580,9 @@ static val make_ffi_type_union(val syntax, val use_existing, val self)
if (biggest_size < size)
biggest_size = size;
} else {
- if (most_align < (ucnum) mtft->align)
+ if (most_align < convert(ucnum, mtft->align))
most_align = mtft->align;
- if (biggest_size < (ucnum) mtft->size)
+ if (biggest_size < convert(ucnum, mtft->size))
biggest_size = mtft->size;
}
}
@@ -5408,7 +5408,7 @@ val carray_dup(val carray)
self, carray, nao);
} else {
cnum elsize = scry->eltft->size;
- cnum size = (ucnum) scry->nelem * (ucnum) elsize;
+ cnum size = convert(ucnum, scry->nelem) * convert(ucnum, elsize);
mem_t *dup = chk_copy_obj(scry->data, scry->nelem * scry->eltft->size);
if (size < 0 || (elsize > 0 && size / elsize != scry->nelem))
@@ -5828,7 +5828,7 @@ val carray_replace(val carray, val values, val from, val to)
cnum tn = c_num(to, self);
struct txr_ffi_type *eltft = scry->eltft;
cnum elsize = eltft->size;
- cnum size = (ucnum) ln * (ucnum) elsize;
+ cnum size = convert(ucnum, ln) * convert(ucnum, elsize);
cnum vn = c_num(vlen, self);
cnum sn;
mem_t *ptr;
@@ -6074,7 +6074,7 @@ val uint_carray(val carray)
val self = lit("uint-carray");
struct carray *scry = carray_struct_checked(self, carray);
struct txr_ffi_type *etft = scry->eltft;
- ucnum size = (ucnum) etft->size * (ucnum) scry->nelem;
+ ucnum size = convert(ucnum, etft->size) * convert(ucnum, scry->nelem);
val ubn = make_bignum();
mp_err mpe = mp_read_unsigned_bin(mp(ubn), scry->data, size);
if (mpe != MP_OKAY)
@@ -6087,7 +6087,7 @@ val int_carray(val carray)
val self = lit("int-carray");
struct carray *scry = carray_struct_checked(self, carray);
struct txr_ffi_type *etft = scry->eltft;
- ucnum size = (ucnum) etft->size * (ucnum) scry->nelem;
+ ucnum size = convert(ucnum, etft->size) * convert(ucnum, scry->nelem);
ucnum bits = size * 8;
val ubn = make_bignum();
mp_err mpe = mp_read_unsigned_bin(mp(ubn), scry->data, size);
@@ -6101,7 +6101,7 @@ val put_carray(val carray, val offs, val stream)
val self = lit("put-carray");
struct carray *scry = carray_struct_checked(self, carray);
struct txr_ffi_type *etft = scry->eltft;
- ucnum size = (ucnum) etft->size * (ucnum) scry->nelem;
+ ucnum size = convert(ucnum, etft->size) * convert(ucnum, scry->nelem);
val buf = make_borrowed_buf(unum(size), scry->data);
val pos = default_arg(offs, zero);
val ret = put_buf(buf, pos, stream);
@@ -6114,7 +6114,7 @@ val fill_carray(val carray, val offs, val stream)
val self = lit("fill-carray");
struct carray *scry = carray_struct_checked(self, carray);
struct txr_ffi_type *etft = scry->eltft;
- ucnum size = (ucnum) etft->size * (ucnum) scry->nelem;
+ ucnum size = convert(ucnum, etft->size) * convert(ucnum, scry->nelem);
val buf = make_borrowed_buf(unum(size), scry->data);
val pos = default_arg(offs, zero);
val ret = fill_buf(buf, pos, stream);
diff --git a/itypes.c b/itypes.c
index a72444e8..6fc2d24f 100644
--- a/itypes.c
+++ b/itypes.c
@@ -121,18 +121,23 @@ u64_t c_u64(val n, val self)
i64_t c_i64(val n, val self)
{
dbl_cnum v = c_dbl_num(n);
- if (v < (- (dbl_cnum) 0x7FFFFFFFFFFFFFFF - 1) || v > (dbl_cnum) 0x7FFFFFFFFFFFFFFF)
+ if (v < (- convert(dbl_cnum, 0x7FFFFFFFFFFFFFFF) - 1) ||
+ v > convert(dbl_cnum, 0x7FFFFFFFFFFFFFFF))
+ {
uw_throwf(error_s, lit("~a: value ~s is out of signed 64 bit range"),
self, n, nao);
+ }
return v;
}
u64_t c_u64(val n, val self)
{
dbl_ucnum v = c_dbl_unum(n);
- if (v > (dbl_ucnum) 0xFFFFFFFFFFFFFFFF)
+ if (v > convert(dbl_ucnum, 0xFFFFFFFFFFFFFFFF))
+ {
uw_throwf(error_s, lit("~a: value ~s is out of unsigned 64 bit range"),
self, n, nao);
+ }
return v;
}
#else
diff --git a/lib.c b/lib.c
index 9cfcd286..6d75532d 100644
--- a/lib.c
+++ b/lib.c
@@ -1724,7 +1724,7 @@ val cyr(val addr, val obj)
mp_int *a = mp(addr);
if (!mp_isneg(a)) {
mp_size i, n = mp_count_bits(a);
- for (i = n - 2; i != (mp_size) -1; i--)
+ for (i = n - 2; i != convert(mp_size, -1); i--)
obj = if3(mp_bit(a, i) == MP_YES, car(obj), cdr(obj));
return obj;
}
@@ -4408,7 +4408,7 @@ mem_t *chk_xalloc(ucnum m, ucnum n, val self)
ucnum mn = m * n;
size_t size = mn;
- if ((m > 0 && mn / m != n) || (ucnum) size != mn)
+ if ((m > 0 && mn / m != n) || convert(ucnum, size) != mn)
uw_throwf(error_s, lit("~a: memory allocation size overflow"),
self, nao);
@@ -5764,7 +5764,7 @@ val split_str_keep(val str, val sep, val keep_sep)
for (;;) {
const wchar_t *psep = wcsstr(cstr, csep);
- size_t span = (psep != 0) ? (size_t) (psep - cstr) : wcslen(cstr);
+ size_t span = (psep != 0) ? convert(size_t, psep - cstr) : wcslen(cstr);
val piece = mkustring(num(span));
init_str(piece, cstr, self);
iter = list_collect(iter, piece);
@@ -8906,7 +8906,7 @@ val vector(val length, val initval)
unsigned i;
ucnum len = c_unum(length, self);
ucnum alloc_plus = len + 2;
- ucnum size = if3(alloc_plus > len, alloc_plus, (ucnum) -1);
+ ucnum size = if3(alloc_plus > len, alloc_plus, convert(ucnum, -1));
val *v = coerce(val *, chk_xalloc(size, sizeof *v, self));
val vec = make_obj();
vec->v.type = VEC;
@@ -9683,7 +9683,7 @@ val lazy_str_get_trailing_list(val lstr, val index)
struct cobj_class *cobj_register(val cls_sym)
{
- if ((size_t) (cobj_ptr - cobj_class) >= nelem(cobj_class))
+ if (convert(size_t, cobj_ptr - cobj_class) >= nelem(cobj_class))
internal_error("cobj array too small");
cobj_ptr->cls_sym = cls_sym;
return cobj_ptr++;
diff --git a/linenoise/linenoise.c b/linenoise/linenoise.c
index d3d6640b..badf0ae5 100644
--- a/linenoise/linenoise.c
+++ b/linenoise/linenoise.c
@@ -446,8 +446,10 @@ static void free_undo_stack(lino_t *l)
static void record_undo(lino_t *l)
{
- struct lino_undo *rec = (struct lino_undo *) lino_os.alloc_fn(sizeof *rec), *iter;
- wchar_t *data = (wchar_t *) lino_os.wstrdup_fn(l->data);
+ struct lino_undo *rec = coerce(struct lino_undo *,
+ lino_os.alloc_fn(sizeof *rec));
+ struct lino_undo *iter;
+ wchar_t *data = lino_os.wstrdup_fn(l->data);
int count;
if (rec == 0 || data == 0) {
@@ -1523,7 +1525,7 @@ static void flash(lino_t *l, int ch)
wchar_t on[2] = { ch };
const wchar_t *off = L"\b \b";
- if (l->dlen >= (int) nelem (l->data) - 1)
+ if (l->dlen >= convert(int, nelem (l->data)) - 1)
return;
for (i = 0; i < 2 && !cancel; i++) {
@@ -1612,7 +1614,7 @@ static void delete_sel(lino_t *l)
*
* On error writing to the terminal -1 is returned, otherwise 0. */
static int edit_insert(lino_t *l, wchar_t c) {
- if (l->dlen < (int) nelem(l->data) - 1) {
+ if (l->dlen < convert(int, nelem(l->data)) - 1) {
record_triv_undo(l);
delete_sel(l);
if (l->dpos == l->dlen) {
@@ -1671,7 +1673,7 @@ static int edit_insert(lino_t *l, wchar_t c) {
static int edit_insert_str(lino_t *l, const wchar_t *s, int nchar)
{
- if (l->dlen < (int) nelem (l->data) - nchar) {
+ if (l->dlen < convert(int, nelem (l->data)) - nchar) {
record_undo(l);
delete_sel(l);
diff --git a/mpi/mpi.c b/mpi/mpi.c
index 291f3d10..c8275189 100644
--- a/mpi/mpi.c
+++ b/mpi/mpi.c
@@ -4102,7 +4102,7 @@ int s_mp_ispow2d(mp_digit d)
return -1; /* not a power of two */
/* If d == 0, s_highest_bit returns 0, thus we return -1. */
- return (int) s_highest_bit(d) - 1;
+ return convert(int, s_highest_bit(d)) - 1;
}
/* Convert the given character to its digit value, in the given radix.
diff --git a/parser.c b/parser.c
index c30020fc..91d97fe7 100644
--- a/parser.c
+++ b/parser.c
@@ -1762,7 +1762,7 @@ static wint_t lino_getch(mem_t *stream_in)
stream = coerce(val, stream_in);
ch = get_char(stream);
- ret = if3(ch, (wint_t) c_num(ch, self), WEOF);
+ ret = if3(ch, convert(wint_t, c_num(ch, self)), WEOF);
uw_catch (sy, va) {
(void) sy;
diff --git a/rand.c b/rand.c
index 6202a74c..0461453f 100644
--- a/rand.c
+++ b/rand.c
@@ -200,10 +200,10 @@ val make_random_state(val seed, val warmup)
for (i = 0; i < 16; i++) {
if (len >= 4) {
- r->state[i] = (((rand32_t) data[0]) << 24 |
- ((rand32_t) data[1]) << 16 |
- ((rand32_t) data[2]) << 8 |
- ((rand32_t) data[3]));
+ r->state[i] = ((convert(rand32_t, data[0])) << 24 |
+ (convert(rand32_t, data[1])) << 16 |
+ (convert(rand32_t, data[2])) << 8 |
+ (convert(rand32_t, data[3])));
data += 4;
len -= 4;
} else if (len == 0) {
@@ -215,18 +215,18 @@ val make_random_state(val seed, val warmup)
len = 0;
break;
case 1:
- r->state[i] = (((rand32_t) data[0]) << 24);
+ r->state[i] = ((convert(rand32_t, data[0])) << 24);
len = 0;
break;
case 2:
- r->state[i] = (((rand32_t) data[0]) << 24 |
- ((rand32_t) data[1]) << 16);
+ r->state[i] = ((convert(rand32_t, data[0])) << 24 |
+ (convert(rand32_t, data[1])) << 16);
len = 0;
break;
case 3:
- r->state[i] = (((rand32_t) data[0]) << 24 |
- ((rand32_t) data[1]) << 16 |
- ((rand32_t) data[2]) << 8);
+ r->state[i] = ((convert(rand32_t, data[0])) << 24 |
+ (convert(rand32_t, data[1])) << 16 |
+ (convert(rand32_t, data[2])) << 8);
len = 0;
break;
}
@@ -455,10 +455,10 @@ val random_buf(val size, val state)
for (; sz >= 4; sz -= 4, data += 4) {
rand32_t rnd = rand32(r);
#if HAVE_LITTLE_ENDIAN
- *(rand32_t *) data = rnd;
+ *coerce(rand32_t *, data) = rnd;
#else
rnd = (0xFF00FF00U & rnd) >> 8 | (0x00FF00FFU & rnd) << 8;
- *(rand32_t *) data = (rnd << 16 | rnd >> 16);
+ *coerce(rand32_t *, data) = (rnd << 16 | rnd >> 16);
#endif
}
diff --git a/stream.c b/stream.c
index ebbb7e19..75d46537 100644
--- a/stream.c
+++ b/stream.c
@@ -843,7 +843,7 @@ val generic_get_line(val stream)
for (;;) {
val chr = ops->get_char(stream);
- wint_t ch = chr ? (wint_t) c_chr(chr) : WEOF;
+ wint_t ch = chr ? convert(wint_t, c_chr(chr)) : WEOF;
if (ch == WEOF && buf == 0)
break;
@@ -1530,13 +1530,13 @@ static struct stdio_mode do_parse_mode(val mode_str, struct stdio_mode m_dfl,
}
if (ms[1] != '(') {
- if (!isdigit((unsigned char) ms[1]) || !ms[2]) {
+ if (!isdigit(convert(unsigned char, ms[1])) || !ms[2]) {
m.malformed = 1;
return m;
}
m.redir[nredir][0] = ms[1] - '0';
- if (isdigit((unsigned char) ms[2])) {
+ if (isdigit(convert(unsigned char, ms[2]))) {
m.redir[nredir][1] = ms[2] - '0';
} else switch (ms[2]) {
case 'n': case 'x':
diff --git a/struct.c b/struct.c
index f2fb78a5..37d2439d 100644
--- a/struct.c
+++ b/struct.c
@@ -364,7 +364,7 @@ static val get_duplicate_supers(val supers, val self)
val super = us_car(iter);
struct struct_type *st = stype_handle(&super, self);
int pos = st->id % (sizeof bloom * CHAR_BIT);
- ucnum mask = (ucnum) 1 << pos;
+ ucnum mask = convert(ucnum, 1) << pos;
if ((mask & bloom) != 0) {
if (memq(super, all_supers) != iter && !memq(super, dup_supers)) {
@@ -709,7 +709,7 @@ static void call_initfun_chain(struct struct_type *st, val strct,
const int bits_ucnum = sizeof *seen * CHAR_BIT;
cnum index = i / bits_ucnum;
cnum bit = i % bits_ucnum;
- ucnum mask = (ucnum) 1 << bit;
+ ucnum mask = convert(ucnum, 1) << bit;
if ((seen[index] & mask) != 0)
return;
seen[index] |= mask;
@@ -736,7 +736,7 @@ static void call_postinitfun_chain(struct struct_type *st, val strct,
const int bits_ucnum = sizeof *seen * CHAR_BIT;
cnum index = i / bits_ucnum;
cnum bit = i % bits_ucnum;
- ucnum mask = (ucnum) 1 << bit;
+ ucnum mask = convert(ucnum, 1) << bit;
if ((seen[index] & mask) != 0)
return;
seen[index] |= mask;
diff --git a/sysif.c b/sysif.c
index be6e020a..c9c0ef35 100644
--- a/sysif.c
+++ b/sysif.c
@@ -1227,7 +1227,9 @@ static val exit_star_wrap(val status)
time_t c_time(val time, val self)
{
- return if3(convert(time_t, -1) > 0, (time_t) c_unum(time, self), (time_t) c_num(time, self));
+ return if3(convert(time_t, -1) > 0,
+ convert(time_t, c_unum(time, self)),
+ convert(time_t, c_num(time, self)));
}
val num_time(time_t time)
diff --git a/tree.c b/tree.c
index 0c803d8e..6450d6e9 100644
--- a/tree.c
+++ b/tree.c
@@ -381,7 +381,7 @@ static void tr_insert(val tree, struct tree *tr, struct tree_iter *ti,
set(mkloc(subtree->tn.left, subtree), node);
if (++tr->size > tr->max_size)
tr->max_size = tr->size;
- if (subtree->tn.right == nil && (((ucnum) 1) << dep) > tr->size) {
+ if (subtree->tn.right == nil && (convert(ucnum, 1) << dep) > tr->size) {
set(mkloc(ti->path[ti->depth++], ti->self), subtree);
tr_find_rebuild_scapegoat(tree, tr, ti, node, 1);
}
@@ -412,7 +412,7 @@ static void tr_insert(val tree, struct tree *tr, struct tree_iter *ti,
set(mkloc(subtree->tn.right, subtree), node);
if (++tr->size > tr->max_size)
tr->max_size = tr->size;
- if (subtree->tn.left == nil && (((ucnum) 1) << dep) > tr->size) {
+ if (subtree->tn.left == nil && (convert(ucnum, 1) << dep) > tr->size) {
set(mkloc(ti->path[ti->depth++], ti->self), subtree);
tr_find_rebuild_scapegoat(tree, tr, ti, node, 1);
}