* Make more use of idx_t
@ 2020-12-05 10:57 Bruno Haible
2020-12-06 2:14 ` Paul Eggert
0 siblings, 1 reply; 5+ messages in thread
From: Bruno Haible @ 2020-12-05 10:57 UTC (permalink / raw
To: bug-gnulib
[-- Attachment #1: Type: text/plain, Size: 1937 bytes --]
Here are a couple of proposed patches, to make use of idx_t instead of ptrdiff_t
when the value is always nonnegative. This helps understanding the code.
2020-12-05 Bruno Haible <bruno@clisp.org>
filenamecat-tests: Use idx_t for nonnegative ptrdiff_t variables.
* tests/test-filenamecat.c: Include idx.h.
(main): Mark prefixlen as nonnegative.
* modules/filenamecat-tests (Depends-on): Add idx.
2020-12-05 Bruno Haible <bruno@clisp.org>
time_rz: Use idx_t for nonnegative ptrdiff_t variables.
* lib/time_rz.c: Include idx.h.
(save_abbr): Mark zone_size as nonnegative.
* modules/time_rz (Depends-on): Add idx.
2020-12-05 Bruno Haible <bruno@clisp.org>
parse-datetime: Use idx_t for nonnegative ptrdiff_t variables.
* lib/parse-datetime.y: Include idx.h.
(textint): Mark digits as nonnegative.
(parser_control): Mark dates_seen, days_seen, local_zones_seen,
dsts_seen, times_seen, zones_seen as nonnegative.
(lookup_word): Mark wordlen as nonnegative.
(yylex): Mark count as nonnegative.
(parse_datetime2): Mark tzsize as nonnegative.
* modules/parse-datetime (Depends-on): Add idx.
2020-12-05 Bruno Haible <bruno@clisp.org>
fnmatch: Use idx_t for nonnegative ptrdiff_t variables.
* lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly.
* lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as
nonnegative.
* modules/fnmatch (Depends-on): Add idx.
2020-12-05 Bruno Haible <bruno@clisp.org>
c-stack: Use idx_t for nonnegative ptrdiff_t variables.
* lib/c-stack.c: Include idx.h.
(die): Mark buflen as nonnegative.
* modules/c-stack (Depends-on): Add idx.
2020-12-05 Bruno Haible <bruno@clisp.org>
backupfile: Use idx_t for nonnegative ptrdiff_t variables.
* lib/backupfile.c: Include idx.h.
(numbered_backup): Mark base_offset as nonnegative.
(backupfile_internal): Likewise.
* modules/backup-rename (Depends-on): Add idx.
* modules/backupfile (Depends-on): Likewise.
[-- Attachment #2: 0001-backupfile-Use-idx_t-for-nonnegative-ptrdiff_t-varia.patch --]
[-- Type: text/x-patch, Size: 3046 bytes --]
From 873bdd8d5275e41f107daed3efcae16084f69d95 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:43:25 +0100
Subject: [PATCH 1/6] backupfile: Use idx_t for nonnegative ptrdiff_t
variables.
* lib/backupfile.c: Include idx.h.
(numbered_backup): Mark base_offset as nonnegative.
(backupfile_internal): Likewise.
* modules/backup-rename (Depends-on): Add idx.
* modules/backupfile (Depends-on): Likewise.
---
ChangeLog | 9 +++++++++
lib/backupfile.c | 17 +++++++++--------
modules/backup-rename | 1 +
modules/backupfile | 1 +
4 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 3de6368..3281f74 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
+ backupfile: Use idx_t for nonnegative ptrdiff_t variables.
+ * lib/backupfile.c: Include idx.h.
+ (numbered_backup): Mark base_offset as nonnegative.
+ (backupfile_internal): Likewise.
+ * modules/backup-rename (Depends-on): Add idx.
+ * modules/backupfile (Depends-on): Likewise.
+
2020-12-04 Bruno Haible <bruno@clisp.org>
utime: Fix a test failure on macOS 10.13.
diff --git a/lib/backupfile.c b/lib/backupfile.c
index 2a88248..dfc29e8 100644
--- a/lib/backupfile.c
+++ b/lib/backupfile.c
@@ -22,12 +22,6 @@
#include "backup-internal.h"
-#include "attribute.h"
-#include "basename-lgpl.h"
-#include "opendirat.h"
-#include "renameatu.h"
-#include "xalloc-oversized.h"
-
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
@@ -36,6 +30,13 @@
#include <string.h>
#include <unistd.h>
+#include "attribute.h"
+#include "basename-lgpl.h"
+#include "idx.h"
+#include "opendirat.h"
+#include "renameatu.h"
+#include "xalloc-oversized.h"
+
#ifndef _D_EXACT_NAMLEN
# define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
#endif
@@ -198,7 +199,7 @@ enum numbered_backup_result
static enum numbered_backup_result
numbered_backup (int dir_fd, char **buffer, size_t buffer_size, size_t filelen,
- ptrdiff_t base_offset, DIR **dirpp, int *pnew_fd)
+ idx_t base_offset, DIR **dirpp, int *pnew_fd)
{
enum numbered_backup_result result = BACKUP_IS_NEW;
DIR *dirp = *dirpp;
@@ -307,7 +308,7 @@ char *
backupfile_internal (int dir_fd, char const *file,
enum backup_type backup_type, bool rename)
{
- ptrdiff_t base_offset = last_component (file) - file;
+ idx_t base_offset = last_component (file) - file;
size_t filelen = base_offset + strlen (file + base_offset);
if (! simple_backup_suffix)
diff --git a/modules/backup-rename b/modules/backup-rename
index cdc96e6..d4bd339 100644
--- a/modules/backup-rename
+++ b/modules/backup-rename
@@ -17,6 +17,7 @@ closedir
d-ino
dirent-safer
fcntl
+idx
memcmp
opendirat
readdir
diff --git a/modules/backupfile b/modules/backupfile
index 2ca1e54..fd15ec5 100644
--- a/modules/backupfile
+++ b/modules/backupfile
@@ -17,6 +17,7 @@ closedir
d-ino
dirent-safer
fcntl
+idx
memcmp
opendirat
readdir
--
2.7.4
[-- Attachment #3: 0002-c-stack-Use-idx_t-for-nonnegative-ptrdiff_t-variable.patch --]
[-- Type: text/x-patch, Size: 2235 bytes --]
From 0eec786d489ebaed246d6dbdc48c131742c90e42 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:45:15 +0100
Subject: [PATCH 2/6] c-stack: Use idx_t for nonnegative ptrdiff_t variables.
* lib/c-stack.c: Include idx.h.
(die): Mark buflen as nonnegative.
* modules/c-stack (Depends-on): Add idx.
---
ChangeLog | 7 +++++++
lib/c-stack.c | 10 ++++++----
modules/c-stack | 1 +
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 3281f74..6d32664 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2020-12-05 Bruno Haible <bruno@clisp.org>
+ c-stack: Use idx_t for nonnegative ptrdiff_t variables.
+ * lib/c-stack.c: Include idx.h.
+ (die): Mark buflen as nonnegative.
+ * modules/c-stack (Depends-on): Add idx.
+
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
backupfile: Use idx_t for nonnegative ptrdiff_t variables.
* lib/backupfile.c: Include idx.h.
(numbered_backup): Mark base_offset as nonnegative.
diff --git a/lib/c-stack.c b/lib/c-stack.c
index 3aea16a..c5bb74a 100644
--- a/lib/c-stack.c
+++ b/lib/c-stack.c
@@ -37,9 +37,6 @@
#include "c-stack.h"
-#include "gettext.h"
-#define _(msgid) gettext (msgid)
-
#include <errno.h>
#include <inttypes.h>
@@ -64,6 +61,11 @@ typedef struct sigaltstack stack_t;
# include <stdio.h>
#endif
+#include "idx.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
/* Use libsigsegv only if needed; kernels like Solaris can detect
stack overflow without the overhead of an external library. */
#define USE_LIBSIGSEGV (!HAVE_XSI_STACK_OVERFLOW_HEURISTIC && HAVE_LIBSIGSEGV)
@@ -134,7 +136,7 @@ die (int signo)
size_t messagelen = strlen (message);
static char const separator[] = {':', ' '};
char buf[sizeof alternate_signal_stack / 16 + sizeof separator];
- ptrdiff_t buflen;
+ idx_t buflen;
if (prognamelen + messagelen < sizeof buf - sizeof separator)
{
char *p = mempcpy (buf, progname, prognamelen);
diff --git a/modules/c-stack b/modules/c-stack
index 77cf6aa..4c80f31 100644
--- a/modules/c-stack
+++ b/modules/c-stack
@@ -14,6 +14,7 @@ exitfail
getprogname
gettext-h
havelib
+idx
ignore-value
intprops
inttypes
--
2.7.4
[-- Attachment #4: 0003-fnmatch-Use-idx_t-for-nonnegative-ptrdiff_t-variable.patch --]
[-- Type: text/x-patch, Size: 3610 bytes --]
From 1e75316d5a581ad87d659bd6f4c22153d8640cc0 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:47:51 +0100
Subject: [PATCH 3/6] fnmatch: Use idx_t for nonnegative ptrdiff_t variables.
* lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly.
* lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as
nonnegative.
* modules/fnmatch (Depends-on): Add idx.
---
ChangeLog | 8 ++++++++
lib/fnmatch.c | 6 ++++++
lib/fnmatch_loop.c | 6 +++---
modules/fnmatch | 1 +
4 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 6d32664..797f6d0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
2020-12-05 Bruno Haible <bruno@clisp.org>
+ fnmatch: Use idx_t for nonnegative ptrdiff_t variables.
+ * lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly.
+ * lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as
+ nonnegative.
+ * modules/fnmatch (Depends-on): Add idx.
+
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
c-stack: Use idx_t for nonnegative ptrdiff_t variables.
* lib/c-stack.c: Include idx.h.
(die): Mark buflen as nonnegative.
diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 3014428..c80809e 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -76,6 +76,12 @@ extern int fnmatch (const char *pattern, const char *string, int flags);
#include <intprops.h>
#include <flexmember.h>
+#ifdef _LIBC
+typedef ptrdiff_t idx_t;
+#else
+# include "idx.h"
+#endif
+
/* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
#define NO_LEADING_PERIOD(flags) \
((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
diff --git a/lib/fnmatch_loop.c b/lib/fnmatch_loop.c
index d9be16f..c533107 100644
--- a/lib/fnmatch_loop.c
+++ b/lib/fnmatch_loop.c
@@ -1036,9 +1036,9 @@ EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end,
struct patternlist *newp; \
size_t plen = (opt == L_('?') || opt == L_('@') \
? pattern_len : (p - startp + 1UL)); \
- ptrdiff_t slen = FLEXSIZEOF (struct patternlist, str, 0); \
- ptrdiff_t new_used = alloca_used + slen; \
- ptrdiff_t plensize; \
+ idx_t slen = FLEXSIZEOF (struct patternlist, str, 0); \
+ idx_t new_used = alloca_used + slen; \
+ idx_t plensize; \
if (INT_MULTIPLY_WRAPV (plen, sizeof (CHAR), &plensize) \
|| INT_ADD_WRAPV (new_used, plensize, &new_used)) \
{ \
diff --git a/modules/fnmatch b/modules/fnmatch
index f547670..2cb69a7 100644
--- a/modules/fnmatch
+++ b/modules/fnmatch
@@ -14,6 +14,7 @@ attribute [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
btowc [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
builtin-expect [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
flexmember [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
+idx [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
intprops [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
isblank [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
iswctype [test $HAVE_FNMATCH = 0 || test $REPLACE_FNMATCH = 1]
--
2.7.4
[-- Attachment #5: 0004-parse-datetime-Use-idx_t-for-nonnegative-ptrdiff_t-v.patch --]
[-- Type: text/x-patch, Size: 3544 bytes --]
From 08d7f5cb9d2d087d17541256868219384693b645 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:51:23 +0100
Subject: [PATCH 4/6] parse-datetime: Use idx_t for nonnegative ptrdiff_t
variables.
* lib/parse-datetime.y: Include idx.h.
(textint): Mark digits as nonnegative.
(parser_control): Mark dates_seen, days_seen, local_zones_seen,
dsts_seen, times_seen, zones_seen as nonnegative.
(lookup_word): Mark wordlen as nonnegative.
(yylex): Mark count as nonnegative.
(parse_datetime2): Mark tzsize as nonnegative.
* modules/parse-datetime (Depends-on): Add idx.
---
ChangeLog | 12 ++++++++++++
lib/parse-datetime.y | 21 +++++++++++----------
modules/parse-datetime | 1 +
3 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 797f6d0..57dea1d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
2020-12-05 Bruno Haible <bruno@clisp.org>
+ parse-datetime: Use idx_t for nonnegative ptrdiff_t variables.
+ * lib/parse-datetime.y: Include idx.h.
+ (textint): Mark digits as nonnegative.
+ (parser_control): Mark dates_seen, days_seen, local_zones_seen,
+ dsts_seen, times_seen, zones_seen as nonnegative.
+ (lookup_word): Mark wordlen as nonnegative.
+ (yylex): Mark count as nonnegative.
+ (parse_datetime2): Mark tzsize as nonnegative.
+ * modules/parse-datetime (Depends-on): Add idx.
+
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
fnmatch: Use idx_t for nonnegative ptrdiff_t variables.
* lib/fnmatch.c: Include idx.h. In glibc, define idx_t directly.
* lib/fnmatch_loop.c (EXT): Mark slen, new_used, plensize as
diff --git a/lib/parse-datetime.y b/lib/parse-datetime.y
index e8ed691..2b56db4 100644
--- a/lib/parse-datetime.y
+++ b/lib/parse-datetime.y
@@ -35,6 +35,7 @@
#include "parse-datetime.h"
+#include "idx.h"
#include "intprops.h"
#include "timespec.h"
#include "verify.h"
@@ -139,7 +140,7 @@ typedef struct
{
bool negative;
intmax_t value;
- ptrdiff_t digits;
+ idx_t digits;
} textint;
/* An entry in the lexical lookup table. */
@@ -212,12 +213,12 @@ typedef struct
/* Presence or counts of nonterminals of various flavors parsed so far. */
bool timespec_seen;
bool rels_seen;
- ptrdiff_t dates_seen;
- ptrdiff_t days_seen;
- ptrdiff_t local_zones_seen;
- ptrdiff_t dsts_seen;
- ptrdiff_t times_seen;
- ptrdiff_t zones_seen;
+ idx_t dates_seen;
+ idx_t days_seen;
+ idx_t local_zones_seen;
+ idx_t dsts_seen;
+ idx_t times_seen;
+ idx_t zones_seen;
bool year_seen;
/* Print debugging output to stderr. */
@@ -1334,7 +1335,7 @@ lookup_word (parser_control const *pc, char *word)
{
char *p;
char *q;
- ptrdiff_t wordlen;
+ idx_t wordlen;
table const *tp;
bool period_found;
bool abbrev;
@@ -1514,7 +1515,7 @@ yylex (union YYSTYPE *lvalp, parser_control *pc)
if (c != '(')
return to_uchar (*pc->input++);
- ptrdiff_t count = 0;
+ idx_t count = 0;
do
{
c = *pc->input++;
@@ -1750,7 +1751,7 @@ parse_datetime2 (struct timespec *result, char const *p,
if (strncmp (p, "TZ=\"", 4) == 0)
{
char const *tzbase = p + 4;
- ptrdiff_t tzsize = 1;
+ idx_t tzsize = 1;
char const *s;
for (s = tzbase; *s; s++, tzsize++)
diff --git a/modules/parse-datetime b/modules/parse-datetime
index a16b90f..820bc6c 100644
--- a/modules/parse-datetime
+++ b/modules/parse-datetime
@@ -15,6 +15,7 @@ c-ctype
stdbool
gettime
gettext-h
+idx
intprops
inttypes
mktime
--
2.7.4
[-- Attachment #6: 0005-time_rz-Use-idx_t-for-nonnegative-ptrdiff_t-variable.patch --]
[-- Type: text/x-patch, Size: 2122 bytes --]
From f09bd3c4dfa248da10dbec6ac5708f61239dca09 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:53:00 +0100
Subject: [PATCH 5/6] time_rz: Use idx_t for nonnegative ptrdiff_t variables.
* lib/time_rz.c: Include idx.h.
(save_abbr): Mark zone_size as nonnegative.
* modules/time_rz (Depends-on): Add idx.
---
ChangeLog | 7 +++++++
lib/time_rz.c | 3 ++-
modules/time_rz | 1 +
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 57dea1d..9c0fef9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2020-12-05 Bruno Haible <bruno@clisp.org>
+ time_rz: Use idx_t for nonnegative ptrdiff_t variables.
+ * lib/time_rz.c: Include idx.h.
+ (save_abbr): Mark zone_size as nonnegative.
+ * modules/time_rz (Depends-on): Add idx.
+
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
parse-datetime: Use idx_t for nonnegative ptrdiff_t variables.
* lib/parse-datetime.y: Include idx.h.
(textint): Mark digits as nonnegative.
diff --git a/lib/time_rz.c b/lib/time_rz.c
index a33b807..6e70ca9 100644
--- a/lib/time_rz.c
+++ b/lib/time_rz.c
@@ -33,6 +33,7 @@
#include <string.h>
#include "flexmember.h"
+#include "idx.h"
#include "time-internal.h"
/* The approximate size to use for small allocation requests. This is
@@ -120,7 +121,7 @@ save_abbr (timezone_t tz, struct tm *tm)
{
if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
{
- ptrdiff_t zone_size = strlen (zone) + 1;
+ idx_t zone_size = strlen (zone) + 1;
if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
extend_abbrs (zone_copy, zone, zone_size);
else
diff --git a/modules/time_rz b/modules/time_rz
index 699a61d..6a2b23c 100644
--- a/modules/time_rz
+++ b/modules/time_rz
@@ -22,6 +22,7 @@ c99
extensions
time
flexmember [test $HAVE_TIMEZONE_T = 0]
+idx [test $HAVE_TIMEZONE_T = 0]
setenv [test $HAVE_TIMEZONE_T = 0]
stdbool [test $HAVE_TIMEZONE_T = 0]
time_r [test $HAVE_TIMEZONE_T = 0]
--
2.7.4
[-- Attachment #7: 0006-filenamecat-tests-Use-idx_t-for-nonnegative-ptrdiff_.patch --]
[-- Type: text/x-patch, Size: 2043 bytes --]
From fe6ed193c3eb8bf99aa6440158bbd4f7ae8122f0 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 Dec 2020 11:54:20 +0100
Subject: [PATCH 6/6] filenamecat-tests: Use idx_t for nonnegative ptrdiff_t
variables.
* tests/test-filenamecat.c: Include idx.h.
(main): Mark prefixlen as nonnegative.
* modules/filenamecat-tests (Depends-on): Add idx.
---
ChangeLog | 7 +++++++
modules/filenamecat-tests | 1 +
tests/test-filenamecat.c | 4 +++-
3 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 9c0fef9..5364c15 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2020-12-05 Bruno Haible <bruno@clisp.org>
+ filenamecat-tests: Use idx_t for nonnegative ptrdiff_t variables.
+ * tests/test-filenamecat.c: Include idx.h.
+ (main): Mark prefixlen as nonnegative.
+ * modules/filenamecat-tests (Depends-on): Add idx.
+
+2020-12-05 Bruno Haible <bruno@clisp.org>
+
time_rz: Use idx_t for nonnegative ptrdiff_t variables.
* lib/time_rz.c: Include idx.h.
(save_abbr): Mark zone_size as nonnegative.
diff --git a/modules/filenamecat-tests b/modules/filenamecat-tests
index 14d3c17..7b2a98f 100644
--- a/modules/filenamecat-tests
+++ b/modules/filenamecat-tests
@@ -2,6 +2,7 @@ Files:
tests/test-filenamecat.c
Depends-on:
+idx
stdbool
configure.ac:
diff --git a/tests/test-filenamecat.c b/tests/test-filenamecat.c
index a6f236e..40259dc 100644
--- a/tests/test-filenamecat.c
+++ b/tests/test-filenamecat.c
@@ -21,6 +21,8 @@
#include "filenamecat.h"
+#include "idx.h"
+
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
@@ -54,7 +56,7 @@ main (int argc _GL_UNUSED, char *argv[])
char *base_in_result;
char const *const *t = tests[i];
char *res = file_name_concat (t[0], t[1], &base_in_result);
- ptrdiff_t prefixlen = base_in_result - res;
+ idx_t prefixlen = base_in_result - res;
size_t t0len = strlen (t[0]);
size_t reslen = strlen (res);
if (strcmp (res, t[2]) != 0)
--
2.7.4
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: Make more use of idx_t
2020-12-05 10:57 Make more use of idx_t Bruno Haible
@ 2020-12-06 2:14 ` Paul Eggert
2020-12-06 11:32 ` Bruno Haible
0 siblings, 1 reply; 5+ messages in thread
From: Paul Eggert @ 2020-12-06 2:14 UTC (permalink / raw
To: Bruno Haible; +Cc: bug-gnulib
Those all look good to me.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Make more use of idx_t
2020-12-06 2:14 ` Paul Eggert
@ 2020-12-06 11:32 ` Bruno Haible
2020-12-17 9:52 ` Paul Eggert
0 siblings, 1 reply; 5+ messages in thread
From: Bruno Haible @ 2020-12-06 11:32 UTC (permalink / raw
To: Paul Eggert; +Cc: bug-gnulib
Paul Eggert wrote:
> Those all look good to me.
OK, I pushed them.
Bruno
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Make more use of idx_t
2020-12-06 11:32 ` Bruno Haible
@ 2020-12-17 9:52 ` Paul Eggert
2020-12-17 23:17 ` Bruno Haible
0 siblings, 1 reply; 5+ messages in thread
From: Paul Eggert @ 2020-12-17 9:52 UTC (permalink / raw
To: Bruno Haible; +Cc: bug-gnulib
[-- Attachment #1: Type: text/plain, Size: 1022 bytes --]
On 12/6/20 3:32 AM, Bruno Haible wrote:
> Paul Eggert wrote:
>> Those all look good to me.
>
> OK, I pushed them.
When I looked into this a bit more when hacking on canonicalize-lgpl, I
realized I spoke too soon. First, there's some cruft left over from when
idx_t could be unsigned. More important, defining IDX_WIDTH to be
PTRDIFF_WIDTH - 1 is confusing as this makes idx_t different from all other
signed types in that IDX_WIDTH does not count the sign bit. I can see why it
doesn't count the sign bit (the sign bit is always zero) but I can also see
why it should (shift counts are limited by the *_WIDTH macros). If we're
going to have a macro that stands for PTRDIFF_WIDTH - 1 it'd probably be
better to use a different naming convention, to avoid that confusion.
However, nobody's using any such macro now and possibly we'll never need it
so in the meantime I merely substituted a comment for the #define in the
attached patch.
Hope this is OK; further comments welcome of course.
[-- Attachment #2: 0001-idx-simplify-IDX_MAX-remove-IDX_WIDTH.patch --]
[-- Type: text/x-patch, Size: 3725 bytes --]
From 03d1588fca2bffe92b08bb4f4ba69273f4843431 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Wed, 16 Dec 2020 23:52:24 -0800
Subject: [PATCH] idx: simplify IDX_MAX, remove IDX_WIDTH
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* lib/idx.h (IDX_MAX): Simplify by removing obsolete reference
to UNSIGNED_IDX_T.
(IDX_WIDTH): Remove, since it’s not used and its value
arguably should be PTRDIFF_WIDTH anyway.
---
ChangeLog | 8 ++++++++
lib/idx.h | 33 ++++++++++++++-------------------
2 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d50539adc..445e2c074 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2020-12-17 Paul Eggert <eggert@cs.ucla.edu>
+
+ idx: simplify IDX_MAX, remove IDX_WIDTH
+ * lib/idx.h (IDX_MAX): Simplify by removing obsolete reference
+ to UNSIGNED_IDX_T.
+ (IDX_WIDTH): Remove, since it’s not used and its value
+ arguably should be PTRDIFF_WIDTH anyway.
+
2020-12-16 Bruno Haible <bruno@clisp.org>
posix_spawn_file_actions_addfchdir-tests: Rename test.
diff --git a/lib/idx.h b/lib/idx.h
index 0095467dc..ad7d31a2b 100644
--- a/lib/idx.h
+++ b/lib/idx.h
@@ -21,11 +21,12 @@
/* Get ptrdiff_t. */
#include <stddef.h>
-/* Get PTRDIFF_WIDTH. */
+/* Get PTRDIFF_MAX. */
#include <stdint.h>
/* The type 'idx_t' holds an (array) index or an (object) size.
- Its implementation is a signed integer type, capable of holding the values
+ Its implementation promotes to a signed integer type,
+ which can hold the values
0..2^63-1 (on 64-bit platforms) or
0..2^31-1 (on 32-bit platforms).
@@ -87,32 +88,26 @@
or to the C standard. Several programming languages (Ada, Haskell,
Common Lisp, Pascal) already have range types. Such range types may
help producing good code and good warnings. The type 'idx_t' could
- then be typedef'ed to a (signed!) range type. */
+ then be typedef'ed to a range type that is signed after promotion. */
-#if 0
-/* In the future, idx_t could be typedef'ed to a signed range type. */
-/* Note: The clang "extended integer types", supported in Clang 11 or newer
+/* In the future, idx_t could be typedef'ed to a signed range type.
+ The clang "extended integer types", supported in Clang 11 or newer
<https://clang.llvm.org/docs/LanguageExtensions.html#extended-integer-types>,
are a special case of range types. However, these types don't support binary
operators with plain integer types (e.g. expressions such as x > 1).
Therefore, they don't behave like signed types (and not like unsigned types
either). So, we cannot use them here. */
-typedef <some_range_type> idx_t;
-#else
-/* Use the signed type 'ptrdiff_t' by default. */
+
+/* Use the signed type 'ptrdiff_t'. */
/* Note: ISO C does not mandate that 'size_t' and 'ptrdiff_t' have the same
- size, but it is so an all platforms we have seen since 1990. */
+ size, but it is so on all platforms we have seen since 1990. */
typedef ptrdiff_t idx_t;
-#endif
/* IDX_MAX is the maximum value of an idx_t. */
-#if defined UNSIGNED_IDX_T
-# define IDX_MAX SIZE_MAX
-#else
-# define IDX_MAX PTRDIFF_MAX
-#endif
-
-/* IDX_WIDTH is the number of bits in an idx_t (31 or 63). */
-#define IDX_WIDTH (PTRDIFF_WIDTH - 1)
+#define IDX_MAX PTRDIFF_MAX
+
+/* So far no need has been found for an IDX_WIDTH macro.
+ Perhaps there should be another macro IDX_VALUE_BITS that does not
+ count the sign bit and is therefore one less than PTRDIFF_WIDTH. */
#endif /* _IDX_H */
--
2.27.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: Make more use of idx_t
2020-12-17 9:52 ` Paul Eggert
@ 2020-12-17 23:17 ` Bruno Haible
0 siblings, 0 replies; 5+ messages in thread
From: Bruno Haible @ 2020-12-17 23:17 UTC (permalink / raw
To: Paul Eggert; +Cc: bug-gnulib
Paul Eggert wrote:
> If we're
> going to have a macro that stands for PTRDIFF_WIDTH - 1 it'd probably be
> better to use a different naming convention, to avoid that confusion.
Makes sense. Yes, IDX_VALUE_BITS would be a better name than IDX_WIDTH.
Bruno
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-12-17 23:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-05 10:57 Make more use of idx_t Bruno Haible
2020-12-06 2:14 ` Paul Eggert
2020-12-06 11:32 ` Bruno Haible
2020-12-17 9:52 ` Paul Eggert
2020-12-17 23:17 ` Bruno Haible
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).