From: Stefan Kangas <stefankangas@gmail.com>
To: Po Lu <luangruo@yahoo.com>, emacs-devel@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>, Eli Zaretskii <eliz@gnu.org>
Subject: Re: master 37889523278: Add new `swap` macro and use it
Date: Sat, 6 Jan 2024 00:50:55 -0800 [thread overview]
Message-ID: <CADwFkmkcrEj4dpyeravpqBdQhOCVWQs9TZe9h1E7hn72=We_mQ@mail.gmail.com> (raw)
In-Reply-To: <87plye9ahs.fsf@yahoo.com>
[-- Attachment #1: Type: text/plain, Size: 1728 bytes --]
Po Lu <luangruo@yahoo.com> writes:
> Stefan Kangas <stefankangas@gmail.com> writes:
>
>> branch: master
>> commit 37889523278fe65733938fb11c3701898309961c
>> Author: Stefan Kangas <stefankangas@gmail.com>
>> Commit: Stefan Kangas <stefankangas@gmail.com>
>>
>> Add new `swap` macro and use it
>>
>> A `swap` macro prevents programming errors and is more concise.
>> It is a natural addition to our existing `min` and `max` macros.
>
> It's well known that a generic "swap" macro is impossible to express in
> Standard C for lack of a standard "typeof"-like expression. To
> compensate for this limitation, the macro must accept an argument
> identifying the type of the field where the temporary value is saved,
> which is more trouble than it's worth and reintroduces the type
> conversion problems such macros are intended to prevent.
>
> Neither typeof nor __typeof__ are portable by our standards, the latter
> being an extension compatible with the Standard enabled when GCC is
> configured to obey the standard to the letter, while the former is a
> keyword enabled in other situations.
Ouch, right. I was inspired by some other large project, let's leave it
unnamed, but their C chops were big enough that I let my guard down.
Unfortunately for me, they happen to support far fewer compilers than we
do. Sorry about that, and thanks for being on your toes.
I had a look at what Gnulib has, and came up with the attached patch.
It still lets us avoid the type conversion problems on GCC, clang and
reasonably recent versions of IBM C and sunc. I'm not sure it's worth
it though, so perhaps my original patch should just be reverted.
I copied in Paul just in case, and I'm hoping Eli will chime in too.
[-- Attachment #2: portable-swap.diff --]
[-- Type: text/x-diff, Size: 9228 bytes --]
diff --git a/lwlib/xlwmenu.c b/lwlib/xlwmenu.c
index a3d9474bed0..d6fa23841a3 100644
--- a/lwlib/xlwmenu.c
+++ b/lwlib/xlwmenu.c
@@ -671,7 +671,7 @@ draw_shadow_rectangle (XlwMenuWidget mw, Window window, int x, int y,
}
if (!erase_p && down_p)
- swap (top_gc, bottom_gc);
+ swap (GC, top_gc, bottom_gc);
/* Do draw (or erase) shadows */
points [0].x = x;
@@ -752,7 +752,7 @@ draw_shadow_rhombus (XlwMenuWidget mw, Window window, int x, int y,
}
if (!erase_p && down_p)
- swap (top_gc, bottom_gc);
+ swap (GC, top_gc, bottom_gc);
points [0].x = x;
points [0].y = y + height / 2;
diff --git a/src/androidterm.c b/src/androidterm.c
index 34734e63c37..851a066316a 100644
--- a/src/androidterm.c
+++ b/src/androidterm.c
@@ -5869,7 +5869,7 @@ android_get_surrounding_text (void *data)
bad input methods. */
if (request->end < request->start)
- swap (request->start, request->end);
+ swap (ptrdiff_t, request->start, request->end);
/* Retrieve the conversion region. */
diff --git a/src/buffer.c b/src/buffer.c
index 14c67224551..c03170e8c2d 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -3591,7 +3591,7 @@ DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
CHECK_FIXNUM_COERCE_MARKER (end);
if (XFIXNUM (beg) > XFIXNUM (end))
- swap (beg, end);
+ swap (Lisp_Object, beg, end);
ptrdiff_t obeg = clip_to_bounds (BUF_BEG (b), XFIXNUM (beg), BUF_Z (b));
ptrdiff_t oend = clip_to_bounds (obeg, XFIXNUM (end), BUF_Z (b));
@@ -3611,7 +3611,7 @@ DEFUN ("make-overlay", Fmake_overlay, Smake_overlay, 2, 5, 0,
modify_overlay (struct buffer *buf, ptrdiff_t start, ptrdiff_t end)
{
if (start > end)
- swap (start, end);
+ swap (ptrdiff_t, start, end);
BUF_COMPUTE_UNCHANGED (buf, start, end);
@@ -3651,7 +3651,7 @@ DEFUN ("move-overlay", Fmove_overlay, Smove_overlay, 3, 4, 0,
CHECK_FIXNUM_COERCE_MARKER (end);
if (XFIXNUM (beg) > XFIXNUM (end))
- swap (beg, end);
+ swap (Lisp_Object, beg, end);
specbind (Qinhibit_quit, Qt); /* FIXME: Why? */
diff --git a/src/dispnew.c b/src/dispnew.c
index 78ec3537a35..a3e6ca416db 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -649,7 +649,7 @@ reverse_rows (struct glyph_matrix *matrix, int start, int end)
int i, j;
for (i = start, j = end - 1; i < j; ++i, --j)
- swap (matrix->rows[i], matrix->rows[j]);
+ swap (struct glyph_row, matrix->rows[i], matrix->rows[j]);
}
@@ -975,7 +975,7 @@ swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
while (glyph_a < glyph_a_end)
{
- swap (*glyph_a, *glyph_b);
+ swap (struct glyph, *glyph_a, *glyph_b);
++glyph_a;
++glyph_b;
}
diff --git a/src/editfns.c b/src/editfns.c
index 2e455a2efed..f31d8f5aef3 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1753,7 +1753,7 @@ DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_subst
b = !NILP (start) ? fix_position (start) : BUF_BEGV (bp);
e = !NILP (end) ? fix_position (end) : BUF_ZV (bp);
if (b > e)
- swap (b, e);
+ swap (EMACS_INT, b, e);
if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp)))
args_out_of_range (start, end);
@@ -1808,7 +1808,7 @@ DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_
begp1 = !NILP (start1) ? fix_position (start1) : BUF_BEGV (bp1);
endp1 = !NILP (end1) ? fix_position (end1) : BUF_ZV (bp1);
if (begp1 > endp1)
- swap (begp1, endp1);
+ swap (EMACS_INT, begp1, endp1);
if (!(BUF_BEGV (bp1) <= begp1
&& begp1 <= endp1
@@ -1833,7 +1833,7 @@ DEFUN ("compare-buffer-substrings", Fcompare_buffer_substrings, Scompare_buffer_
begp2 = !NILP (start2) ? fix_position (start2) : BUF_BEGV (bp2);
endp2 = !NILP (end2) ? fix_position (end2) : BUF_ZV (bp2);
if (begp2 > endp2)
- swap (begp2, endp2);
+ swap (EMACS_INT, begp2, endp2);
if (!(BUF_BEGV (bp2) <= begp2
&& begp2 <= endp2
diff --git a/src/eval.c b/src/eval.c
index 6a866d6cc32..7d89c0b621e 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -2794,9 +2794,9 @@ DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure,
static Lisp_Object
run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args)
{
- swap (args[0], args[1]);
+ swap (Lisp_Object, args[0], args[1]);
Lisp_Object ret = Ffuncall (nargs, args);
- swap (args[1], args[0]);
+ swap (Lisp_Object, args[1], args[0]);
return ret;
}
diff --git a/src/fns.c b/src/fns.c
index c8adc5cb891..9e7f43b58e1 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -5674,7 +5674,7 @@ extract_data_from_object (Lisp_Object spec,
b = !NILP (start) ? fix_position (start) : BEGV;
e = !NILP (end) ? fix_position (end) : ZV;
if (b > e)
- swap (b, e);
+ swap (EMACS_INT, b, e);
if (!(BEGV <= b && e <= ZV))
args_out_of_range (start, end);
diff --git a/src/lisp.h b/src/lisp.h
index f96932ab0c1..a9782868157 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -68,8 +68,13 @@ #define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
/* Swap values of a and b. */
-#define swap(a, b) \
- do { typeof (a) __tmp; __tmp = (a); (a) = (b); (b) = __tmp; } while (0);
+#if _GL_HAVE___TYPEOF__
+#define swap(type, a, b) \
+ do { __typeof__ (a) __tmp; __tmp = (a); (a) = (b); (b) = __tmp; } while (0);
+#else
+#define swap(type, a, b) \
+ do { type __tmp; __tmp = (a); (a) = (b); (b) = __tmp; } while (0);
+#endif
/* Number of elements in an array. */
#define ARRAYELTS(arr) (sizeof (arr) / sizeof (arr)[0])
diff --git a/src/regex-emacs.c b/src/regex-emacs.c
index fdc2cc63445..afaf0d06bd1 100644
--- a/src/regex-emacs.c
+++ b/src/regex-emacs.c
@@ -2930,7 +2930,7 @@ forall_firstchar_1 (re_char *p, re_char *pend,
/* We have to check that both destinations are safe.
Arrange for `newp1` to be the smaller of the two. */
if (newp1 > newp2)
- swap (newp1, newp2);
+ swap (re_char *, newp1, newp2);
if (newp2 <= p_orig) /* Both destinations go backward! */
{
diff --git a/src/textconv.c b/src/textconv.c
index e0707522d7e..c8599f78a54 100644
--- a/src/textconv.c
+++ b/src/textconv.c
@@ -383,8 +383,8 @@ textconv_query (struct frame *f, struct textconv_callback_struct *query,
if (end < pos)
{
eassert (end_byte < pos_byte);
- swap (pos_byte, end_byte);
- swap (pos, end);
+ swap (ptrdiff_t, pos_byte, end_byte);
+ swap (ptrdiff_t, pos, end);
}
/* Return the string first. */
@@ -1903,7 +1903,7 @@ get_extracted_text (struct frame *f, ptrdiff_t n,
/* Sort start and end. */
if (start > end)
- swap (start, end);
+ swap (ptrdiff_t, start, end);
}
else
goto finish;
@@ -2002,7 +2002,7 @@ get_surrounding_text (struct frame *f, ptrdiff_t left,
/* Now sort start and end. */
if (end < start)
- swap (start, end)
+ swap (ptrdiff_t, start, end)
/* And subtract left and right. */
diff --git a/src/textprop.c b/src/textprop.c
index ec9435219ea..118a5831c10 100644
--- a/src/textprop.c
+++ b/src/textprop.c
@@ -142,7 +142,7 @@ validate_interval_range (Lisp_Object object, Lisp_Object *begin,
return NULL;
if (XFIXNUM (*begin) > XFIXNUM (*end))
- swap (*begin, *end);
+ swap (Lisp_Object, *begin, *end);
if (BUFFERP (object))
{
@@ -2196,7 +2196,7 @@ verify_interval_modification (struct buffer *buf,
return;
if (start > end)
- swap (start, end);
+ swap (ptrdiff_t, start, end);
/* For an insert operation, check the two chars around the position. */
if (start == end)
diff --git a/src/w32uniscribe.c b/src/w32uniscribe.c
index c417159cf9e..798a90bf594 100644
--- a/src/w32uniscribe.c
+++ b/src/w32uniscribe.c
@@ -768,7 +768,7 @@ #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \
BYTE data[2]; \
if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \
goto font_table_error; \
- swap (data[0], data[1]); \
+ swap (BYTE, data[0], data[1]); \
memcpy (PTR, data, 2); \
} while (0)
diff --git a/src/xfaces.c b/src/xfaces.c
index f79eb022e15..95ae3b4b1bc 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -1357,7 +1357,7 @@ load_face_colors (struct frame *f, struct face *face,
/* Swap colors if face is inverse-video. */
if (EQ (attrs[LFACE_INVERSE_INDEX], Qt))
- swap (fg, bg);
+ swap (Lisp_Object, fg, bg);
/* Check for support for foreground, not for background because
face_color_supported_p is smart enough to know that grays are
diff --git a/src/xterm.c b/src/xterm.c
index 0b83b0554b3..92e51577cec 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -1571,8 +1571,8 @@ #define SWAPCARD32(l) \
struct { unsigned t : 32; } bit32; \
char *tp = (char *) &bit32; \
bit32.t = l; \
- swap (tp[0], tp[3]); \
- swap (tp[1], tp[2]); \
+ swap (char, tp[0], tp[3]); \
+ swap (char, tp[1], tp[2]); \
l = bit32.t; \
}
@@ -1581,7 +1581,7 @@ #define SWAPCARD16(s) \
struct { unsigned t : 16; } bit16; \
char *tp = (char *) &bit16; \
bit16.t = s; \
- swap (tp[0], tp[1]); \
+ swap (char, tp[0], tp[1]); \
s = bit16.t; \
}
next prev parent reply other threads:[~2024-01-06 8:50 UTC|newest]
Thread overview: 80+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <170452579053.27998.16123231327386305897@vcs2.savannah.gnu.org>
[not found] ` <20240106072311.28B8FC0034E@vcs2.savannah.gnu.org>
2024-01-06 7:39 ` master 37889523278: Add new `swap` macro and use it Po Lu
2024-01-06 8:44 ` Eli Zaretskii
2024-01-06 8:45 ` Po Lu
2024-01-06 9:18 ` Stefan Kangas
2024-01-06 10:33 ` Po Lu
2024-01-06 11:30 ` Stefan Kangas
2024-01-06 13:13 ` Po Lu
2024-01-06 13:59 ` Eli Zaretskii
2024-01-06 14:41 ` Po Lu
2024-01-06 15:34 ` Eli Zaretskii
2024-01-07 1:39 ` Po Lu
2024-01-07 6:34 ` Stefan Kangas
2024-01-07 7:50 ` Po Lu
2024-01-07 8:33 ` Eli Zaretskii
2024-01-07 9:45 ` Stefan Kangas
2024-01-07 10:36 ` Po Lu
2024-01-07 11:34 ` Eli Zaretskii
2024-01-07 11:53 ` Po Lu
2024-01-07 14:37 ` Eli Zaretskii
2024-01-07 17:32 ` Stefan Kangas
2024-01-08 2:15 ` Po Lu
2024-01-07 7:09 ` Eli Zaretskii
2024-01-12 0:50 ` Gregory Heytings
2024-01-13 10:16 ` Stefan Kangas
2024-01-14 3:03 ` Richard Stallman
2024-01-14 5:14 ` Po Lu
2024-01-14 7:07 ` Eli Zaretskii
2024-01-14 8:05 ` Po Lu
2024-01-14 9:31 ` Eli Zaretskii
2024-01-15 1:32 ` Gregory Heytings
2024-01-15 12:41 ` Eli Zaretskii
2024-01-15 13:56 ` Po Lu
2024-01-15 14:13 ` Eli Zaretskii
2024-01-18 1:01 ` Gregory Heytings
2024-01-18 2:11 ` Po Lu
2024-01-27 1:26 ` Gregory Heytings
2024-01-27 2:58 ` Po Lu
2024-01-27 23:44 ` Stefan Kangas
2024-01-28 1:51 ` Po Lu
2024-01-28 2:35 ` Stefan Kangas
2024-01-28 4:17 ` Po Lu
2024-01-28 6:28 ` Eli Zaretskii
2024-01-28 2:22 ` Gregory Heytings
2024-01-28 4:03 ` Po Lu
2024-01-30 2:00 ` Gregory Heytings
2024-01-30 2:41 ` Po Lu
2024-01-28 6:40 ` Eli Zaretskii
2024-01-30 1:59 ` Gregory Heytings
2024-01-30 12:41 ` Eli Zaretskii
2024-01-18 6:22 ` Eli Zaretskii
2024-01-27 1:25 ` Gregory Heytings
2024-01-27 3:08 ` Po Lu
2024-01-27 7:19 ` Eli Zaretskii
2024-01-17 3:29 ` Richard Stallman
2024-01-17 10:16 ` Stefan Kangas
2024-01-17 11:15 ` Po Lu
2024-01-17 21:15 ` Stefan Kangas
2024-01-18 0:39 ` Po Lu
2024-01-18 19:17 ` Stefan Kangas
2024-01-19 1:10 ` Po Lu
2024-01-20 20:31 ` Stefan Kangas
2024-01-22 3:35 ` Richard Stallman
2024-01-22 4:48 ` Po Lu
2024-01-22 18:21 ` Dmitry Gutov
2024-01-23 0:50 ` Po Lu
2024-01-25 23:38 ` Stefan Kangas
2024-01-26 2:02 ` Po Lu
2024-01-26 7:53 ` Eli Zaretskii
2024-01-26 7:38 ` Eli Zaretskii
2024-01-19 3:32 ` Richard Stallman
2024-01-19 4:12 ` Po Lu
2024-01-27 1:27 ` Gregory Heytings
2024-01-27 23:56 ` Stefan Kangas
2024-01-28 2:23 ` Gregory Heytings
2024-01-13 8:02 ` Stefan Kangas
2024-01-13 9:14 ` Eli Zaretskii
2024-01-13 9:50 ` Mattias Engdegård
2024-01-06 8:50 ` Stefan Kangas [this message]
2024-01-06 9:09 ` Po Lu
2024-01-06 9:52 ` Andreas Schwab
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CADwFkmkcrEj4dpyeravpqBdQhOCVWQs9TZe9h1E7hn72=We_mQ@mail.gmail.com' \
--to=stefankangas@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=luangruo@yahoo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.