aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 15:13:21 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 15:13:21 -0300
commitf2206b2abe848f65956fa48da338c2bfac599e4a (patch)
tree94c1a856ec06846ad7485648ccafb429b5ca1b9b
parent0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b (diff)
downloadlua-f2206b2abe848f65956fa48da338c2bfac599e4a.tar.gz
lua-f2206b2abe848f65956fa48da338c2bfac599e4a.tar.bz2
lua-f2206b2abe848f65956fa48da338c2bfac599e4a.zip
'-Wconversion' extended to all options of Lua numbers
-rw-r--r--llimits.h4
-rw-r--r--lmem.c6
-rw-r--r--lmem.h2
-rw-r--r--lstrlib.c9
-rw-r--r--ltablib.c32
-rw-r--r--ltests.c28
-rw-r--r--lutf8lib.c6
-rw-r--r--testes/strings.lua2
8 files changed, 50 insertions, 39 deletions
diff --git a/llimits.h b/llimits.h
index e7da009b..d7ae065b 100644
--- a/llimits.h
+++ b/llimits.h
@@ -163,13 +163,15 @@ typedef LUAI_UACINT l_uacInt;
163*/ 163*/
164#define ct_diff2sz(df) ((size_t)(df)) 164#define ct_diff2sz(df) ((size_t)(df))
165 165
166/* ptrdiff_t to lua_Integer */
167#define ct_diff2S(df) cast_st2S(ct_diff2sz(df))
168
166/* 169/*
167** Special type equivalent to '(void*)' for functions (to suppress some 170** Special type equivalent to '(void*)' for functions (to suppress some
168** warnings when converting function pointers) 171** warnings when converting function pointers)
169*/ 172*/
170typedef void (*voidf)(void); 173typedef void (*voidf)(void);
171 174
172
173/* 175/*
174** Macro to convert pointer-to-void* to pointer-to-function. This cast 176** Macro to convert pointer-to-void* to pointer-to-function. This cast
175** is undefined according to ISO C, but POSIX assumes that it works. 177** is undefined according to ISO C, but POSIX assumes that it works.
diff --git a/lmem.c b/lmem.c
index d02c9fdc..52a4f999 100644
--- a/lmem.c
+++ b/lmem.c
@@ -126,10 +126,10 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
126** error. 126** error.
127*/ 127*/
128void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, 128void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
129 int final_n, int size_elem) { 129 int final_n, unsigned size_elem) {
130 void *newblock; 130 void *newblock;
131 size_t oldsize = cast_sizet((*size) * size_elem); 131 size_t oldsize = cast_sizet(*size) * size_elem;
132 size_t newsize = cast_sizet(final_n * size_elem); 132 size_t newsize = cast_sizet(final_n) * size_elem;
133 lua_assert(newsize <= oldsize); 133 lua_assert(newsize <= oldsize);
134 newblock = luaM_saferealloc_(L, block, oldsize, newsize); 134 newblock = luaM_saferealloc_(L, block, oldsize, newsize);
135 *size = final_n; 135 *size = final_n;
diff --git a/lmem.h b/lmem.h
index aa306105..204ce3bc 100644
--- a/lmem.h
+++ b/lmem.h
@@ -88,7 +88,7 @@ LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
88 int *size, unsigned size_elem, int limit, 88 int *size, unsigned size_elem, int limit,
89 const char *what); 89 const char *what);
90LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, 90LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem,
91 int final_n, int size_elem); 91 int final_n, unsigned size_elem);
92LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); 92LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag);
93 93
94#endif 94#endif
diff --git a/lstrlib.c b/lstrlib.c
index e9421c27..321d6a0b 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -704,7 +704,8 @@ static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s,
704 if (l_unlikely(capl == CAP_UNFINISHED)) 704 if (l_unlikely(capl == CAP_UNFINISHED))
705 luaL_error(ms->L, "unfinished capture"); 705 luaL_error(ms->L, "unfinished capture");
706 else if (capl == CAP_POSITION) 706 else if (capl == CAP_POSITION)
707 lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); 707 lua_pushinteger(ms->L,
708 ct_diff2S(ms->capture[i].init - ms->src_init) + 1);
708 return capl; 709 return capl;
709 } 710 }
710} 711}
@@ -775,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) {
775 /* do a plain search */ 776 /* do a plain search */
776 const char *s2 = lmemfind(s + init, ls - init, p, lp); 777 const char *s2 = lmemfind(s + init, ls - init, p, lp);
777 if (s2) { 778 if (s2) {
778 lua_pushinteger(L, (s2 - s) + 1); 779 lua_pushinteger(L, ct_diff2S(s2 - s) + 1);
779 lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); 780 lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp));
780 return 2; 781 return 2;
781 } 782 }
@@ -793,8 +794,8 @@ static int str_find_aux (lua_State *L, int find) {
793 reprepstate(&ms); 794 reprepstate(&ms);
794 if ((res=match(&ms, s1, p)) != NULL) { 795 if ((res=match(&ms, s1, p)) != NULL) {
795 if (find) { 796 if (find) {
796 lua_pushinteger(L, (s1 - s) + 1); /* start */ 797 lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */
797 lua_pushinteger(L, res - s); /* end */ 798 lua_pushinteger(L, ct_diff2S(res - s)); /* end */
798 return push_captures(&ms, NULL, 0) + 2; 799 return push_captures(&ms, NULL, 0) + 2;
799 } 800 }
800 else 801 else
diff --git a/ltablib.c b/ltablib.c
index 538d585d..4db3768a 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -231,10 +231,18 @@ static int tunpack (lua_State *L) {
231*/ 231*/
232 232
233 233
234/* type for array indices */ 234/*
235** Type for array indices. These indices are always limited by INT_MAX,
236** so it is safe to cast them to lua_Integer even for Lua 32 bits.
237*/
235typedef unsigned int IdxT; 238typedef unsigned int IdxT;
236 239
237 240
241/* Versions of lua_seti/lua_geti specialized for IdxT */
242#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx))
243#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx))
244
245
238/* 246/*
239** Produce a "random" 'unsigned int' to randomize pivot choice. This 247** Produce a "random" 'unsigned int' to randomize pivot choice. This
240** macro is used only when 'sort' detects a big imbalance in the result 248** macro is used only when 'sort' detects a big imbalance in the result
@@ -251,8 +259,8 @@ typedef unsigned int IdxT;
251 259
252 260
253static void set2 (lua_State *L, IdxT i, IdxT j) { 261static void set2 (lua_State *L, IdxT i, IdxT j) {
254 lua_seti(L, 1, i); 262 seti(L, 1, i);
255 lua_seti(L, 1, j); 263 seti(L, 1, j);
256} 264}
257 265
258 266
@@ -289,14 +297,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
289 /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ 297 /* loop invariant: a[lo .. i] <= P <= a[j .. up] */
290 for (;;) { 298 for (;;) {
291 /* next loop: repeat ++i while a[i] < P */ 299 /* next loop: repeat ++i while a[i] < P */
292 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { 300 while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) {
293 if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ 301 if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
294 luaL_error(L, "invalid order function for sorting"); 302 luaL_error(L, "invalid order function for sorting");
295 lua_pop(L, 1); /* remove a[i] */ 303 lua_pop(L, 1); /* remove a[i] */
296 } 304 }
297 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ 305 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
298 /* next loop: repeat --j while P < a[j] */ 306 /* next loop: repeat --j while P < a[j] */
299 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { 307 while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) {
300 if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ 308 if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
301 luaL_error(L, "invalid order function for sorting"); 309 luaL_error(L, "invalid order function for sorting");
302 lua_pop(L, 1); /* remove a[j] */ 310 lua_pop(L, 1); /* remove a[j] */
@@ -335,8 +343,8 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
335 IdxT p; /* Pivot index */ 343 IdxT p; /* Pivot index */
336 IdxT n; /* to be used later */ 344 IdxT n; /* to be used later */
337 /* sort elements 'lo', 'p', and 'up' */ 345 /* sort elements 'lo', 'p', and 'up' */
338 lua_geti(L, 1, lo); 346 geti(L, 1, lo);
339 lua_geti(L, 1, up); 347 geti(L, 1, up);
340 if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ 348 if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
341 set2(L, lo, up); /* swap a[lo] - a[up] */ 349 set2(L, lo, up); /* swap a[lo] - a[up] */
342 else 350 else
@@ -347,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
347 p = (lo + up)/2; /* middle element is a good pivot */ 355 p = (lo + up)/2; /* middle element is a good pivot */
348 else /* for larger intervals, it is worth a random pivot */ 356 else /* for larger intervals, it is worth a random pivot */
349 p = choosePivot(lo, up, rnd); 357 p = choosePivot(lo, up, rnd);
350 lua_geti(L, 1, p); 358 geti(L, 1, p);
351 lua_geti(L, 1, lo); 359 geti(L, 1, lo);
352 if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ 360 if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
353 set2(L, p, lo); /* swap a[p] - a[lo] */ 361 set2(L, p, lo); /* swap a[p] - a[lo] */
354 else { 362 else {
355 lua_pop(L, 1); /* remove a[lo] */ 363 lua_pop(L, 1); /* remove a[lo] */
356 lua_geti(L, 1, up); 364 geti(L, 1, up);
357 if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ 365 if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
358 set2(L, p, up); /* swap a[up] - a[p] */ 366 set2(L, p, up); /* swap a[up] - a[p] */
359 else 367 else
@@ -361,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
361 } 369 }
362 if (up - lo == 2) /* only 3 elements? */ 370 if (up - lo == 2) /* only 3 elements? */
363 return; /* already sorted */ 371 return; /* already sorted */
364 lua_geti(L, 1, p); /* get middle element (Pivot) */ 372 geti(L, 1, p); /* get middle element (Pivot) */
365 lua_pushvalue(L, -1); /* push Pivot */ 373 lua_pushvalue(L, -1); /* push Pivot */
366 lua_geti(L, 1, up - 1); /* push a[up - 1] */ 374 geti(L, 1, up - 1); /* push a[up - 1] */
367 set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ 375 set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
368 p = partition(L, lo, up); 376 p = partition(L, lo, up);
369 /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ 377 /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
diff --git a/ltests.c b/ltests.c
index 8a6b4065..7d134e2d 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1040,14 +1040,14 @@ static int table_query (lua_State *L) {
1040 1040
1041static int query_GCparams (lua_State *L) { 1041static int query_GCparams (lua_State *L) {
1042 global_State *g = G(L); 1042 global_State *g = G(L);
1043 lua_pushinteger(L, gettotalobjs(g)); 1043 lua_pushinteger(L, cast(lua_Integer, gettotalobjs(g)));
1044 lua_pushinteger(L, g->GCdebt); 1044 lua_pushinteger(L, cast(lua_Integer, g->GCdebt));
1045 lua_pushinteger(L, applygcparam(g, MINORMUL, 100)); 1045 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMUL, 100)));
1046 lua_pushinteger(L, applygcparam(g, MAJORMINOR, 100)); 1046 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MAJORMINOR, 100)));
1047 lua_pushinteger(L, applygcparam(g, MINORMAJOR, 100)); 1047 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMAJOR, 100)));
1048 lua_pushinteger(L, applygcparam(g, PAUSE, 100)); 1048 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, PAUSE, 100)));
1049 lua_pushinteger(L, applygcparam(g, STEPMUL, 100)); 1049 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPMUL, 100)));
1050 lua_pushinteger(L, applygcparam(g, STEPSIZE, 100)); 1050 lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPSIZE, 100)));
1051 return 8; 1051 return 8;
1052} 1052}
1053 1053
@@ -1062,7 +1062,7 @@ static int test_codeparam (lua_State *L) {
1062static int test_applyparam (lua_State *L) { 1062static int test_applyparam (lua_State *L) {
1063 lua_Integer p = luaL_checkinteger(L, 1); 1063 lua_Integer p = luaL_checkinteger(L, 1);
1064 lua_Integer x = luaL_checkinteger(L, 2); 1064 lua_Integer x = luaL_checkinteger(L, 2);
1065 lua_pushinteger(L, luaO_applyparam(cast_byte(p), x)); 1065 lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x)));
1066 return 1; 1066 return 1;
1067} 1067}
1068 1068
@@ -1162,7 +1162,7 @@ static int pushuserdata (lua_State *L) {
1162 1162
1163 1163
1164static int udataval (lua_State *L) { 1164static int udataval (lua_State *L) {
1165 lua_pushinteger(L, cast(long, lua_touserdata(L, 1))); 1165 lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1))));
1166 return 1; 1166 return 1;
1167} 1167}
1168 1168
@@ -1199,7 +1199,7 @@ static int num2int (lua_State *L) {
1199 1199
1200 1200
1201static int makeseed (lua_State *L) { 1201static int makeseed (lua_State *L) {
1202 lua_pushinteger(L, luaL_makeseed(L)); 1202 lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L)));
1203 return 1; 1203 return 1;
1204} 1204}
1205 1205
@@ -1486,7 +1486,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1486 const char *inst = getstring; 1486 const char *inst = getstring;
1487 if EQ("") return 0; 1487 if EQ("") return 0;
1488 else if EQ("absindex") { 1488 else if EQ("absindex") {
1489 lua_pushnumber(L1, lua_absindex(L1, getindex)); 1489 lua_pushinteger(L1, lua_absindex(L1, getindex));
1490 } 1490 }
1491 else if EQ("append") { 1491 else if EQ("append") {
1492 int t = getindex; 1492 int t = getindex;
@@ -1538,7 +1538,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
1538 } 1538 }
1539 else if EQ("func2num") { 1539 else if EQ("func2num") {
1540 lua_CFunction func = lua_tocfunction(L1, getindex); 1540 lua_CFunction func = lua_tocfunction(L1, getindex);
1541 lua_pushinteger(L1, cast(lua_Integer, func)); 1541 lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func)));
1542 } 1542 }
1543 else if EQ("getfield") { 1543 else if EQ("getfield") {
1544 int t = getindex; 1544 int t = getindex;
@@ -1901,7 +1901,7 @@ static int Cfunc (lua_State *L) {
1901static int Cfunck (lua_State *L, int status, lua_KContext ctx) { 1901static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
1902 lua_pushstring(L, statcodes[status]); 1902 lua_pushstring(L, statcodes[status]);
1903 lua_setglobal(L, "status"); 1903 lua_setglobal(L, "status");
1904 lua_pushinteger(L, ctx); 1904 lua_pushinteger(L, cast(lua_Integer, ctx));
1905 lua_setglobal(L, "ctx"); 1905 lua_setglobal(L, "ctx");
1906 return runC(L, L, lua_tostring(L, cast_int(ctx))); 1906 return runC(L, L, lua_tostring(L, cast_int(ctx)));
1907} 1907}
diff --git a/lutf8lib.c b/lutf8lib.c
index 6dfdd1f4..04bbfa56 100644
--- a/lutf8lib.c
+++ b/lutf8lib.c
@@ -103,7 +103,7 @@ static int utflen (lua_State *L) {
103 lua_pushinteger(L, posi + 1); /* ... and current position */ 103 lua_pushinteger(L, posi + 1); /* ... and current position */
104 return 2; 104 return 2;
105 } 105 }
106 posi = s1 - s; 106 posi = ct_diff2S(s1 - s);
107 n++; 107 n++;
108 } 108 }
109 lua_pushinteger(L, n); 109 lua_pushinteger(L, n);
@@ -137,7 +137,7 @@ static int codepoint (lua_State *L) {
137 s = utf8_decode(s, &code, !lax); 137 s = utf8_decode(s, &code, !lax);
138 if (s == NULL) 138 if (s == NULL)
139 return luaL_error(L, MSGInvalid); 139 return luaL_error(L, MSGInvalid);
140 lua_pushinteger(L, code); 140 lua_pushinteger(L, l_castU2S(code));
141 n++; 141 n++;
142 } 142 }
143 return n; 143 return n;
@@ -240,7 +240,7 @@ static int iter_aux (lua_State *L, int strict) {
240 if (next == NULL || iscontp(next)) 240 if (next == NULL || iscontp(next))
241 return luaL_error(L, MSGInvalid); 241 return luaL_error(L, MSGInvalid);
242 lua_pushinteger(L, l_castU2S(n + 1)); 242 lua_pushinteger(L, l_castU2S(n + 1));
243 lua_pushinteger(L, code); 243 lua_pushinteger(L, l_castU2S(code));
244 return 2; 244 return 2;
245 } 245 }
246} 246}
diff --git a/testes/strings.lua b/testes/strings.lua
index a0204309..9bb52b35 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -111,7 +111,7 @@ assert(string.rep('', 10) == '')
111 111
112do 112do
113 checkerror("too large", string.rep, 'aa', math.maxinteger); 113 checkerror("too large", string.rep, 'aa', math.maxinteger);
114 checkerror("too large", string.rep, 'a', math.maxinteger/2, ',') 114 checkerror("too large", string.rep, 'a', math.maxinteger, ',')
115end 115end
116 116
117-- repetitions with separator 117-- repetitions with separator