From: Stefan Kangas Date: Sat, 6 Jan 2024 07:22:08 +0000 (+0100) Subject: Add new `swap` macro and use it X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=37889523278fe65733938fb11c3701898309961c;p=emacs.git 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. * src/lisp.h (swap): New macro. * lwlib/xlwmenu.c (draw_shadow_rectangle, draw_shadow_rhombus): * src/androidterm.c (android_get_surrounding_text): * src/buffer.c (Fmake_overlay, modify_overlay, Fmove_overlay): * src/dispnew.c (swap_glyphs_in_rows, reverse_rows): * src/editfns.c (Finsert_buffer_substring) (Fcompare_buffer_substrings): * src/eval.c (run_hook_wrapped_funcall): * src/fns.c (extract_data_from_object): * src/regex-emacs.c (forall_firstchar_1): * src/textconv.c (textconv_query, get_extracted_text) (get_surrounding_text): * src/textprop.c (validate_interval_range) (verify_interval_modification): * src/w32uniscribe.c (OTF_INT16_VAL): * src/xfaces.c (load_face_colors): * src/xterm.c (SWAPCARD32): Prefer using above macro to open-coding. --- diff --git a/lwlib/xlwmenu.c b/lwlib/xlwmenu.c index 0f8f94b803c..a3d9474bed0 100644 --- a/lwlib/xlwmenu.c +++ b/lwlib/xlwmenu.c @@ -671,12 +671,7 @@ draw_shadow_rectangle (XlwMenuWidget mw, Window window, int x, int y, } if (!erase_p && down_p) - { - GC temp; - temp = top_gc; - top_gc = bottom_gc; - bottom_gc = temp; - } + swap (top_gc, bottom_gc); /* Do draw (or erase) shadows */ points [0].x = x; @@ -757,12 +752,7 @@ draw_shadow_rhombus (XlwMenuWidget mw, Window window, int x, int y, } if (!erase_p && down_p) - { - GC temp; - temp = top_gc; - top_gc = bottom_gc; - bottom_gc = temp; - } + swap (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 2e4ee64f390..34734e63c37 100644 --- a/src/androidterm.c +++ b/src/androidterm.c @@ -5849,7 +5849,6 @@ android_get_surrounding_text (void *data) { struct android_get_surrounding_text_context *request; struct frame *f; - ptrdiff_t temp; request = data; @@ -5870,11 +5869,7 @@ android_get_surrounding_text (void *data) bad input methods. */ if (request->end < request->start) - { - temp = request->start; - request->start = request->end; - request->end = temp; - } + swap (request->start, request->end); /* Retrieve the conversion region. */ diff --git a/src/buffer.c b/src/buffer.c index 352aca8ddfd..14c67224551 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -3591,10 +3591,7 @@ for the rear of the overlay advance when text is inserted there CHECK_FIXNUM_COERCE_MARKER (end); if (XFIXNUM (beg) > XFIXNUM (end)) - { - Lisp_Object temp; - temp = beg; beg = end; end = temp; - } + swap (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)); @@ -3614,11 +3611,7 @@ static void modify_overlay (struct buffer *buf, ptrdiff_t start, ptrdiff_t end) { if (start > end) - { - ptrdiff_t temp = start; - start = end; - end = temp; - } + swap (start, end); BUF_COMPUTE_UNCHANGED (buf, start, end); @@ -3658,10 +3651,7 @@ buffer. */) CHECK_FIXNUM_COERCE_MARKER (end); if (XFIXNUM (beg) > XFIXNUM (end)) - { - Lisp_Object temp; - temp = beg; beg = end; end = temp; - } + swap (beg, end); specbind (Qinhibit_quit, Qt); /* FIXME: Why? */ diff --git a/src/dispnew.c b/src/dispnew.c index d0f259eef6c..78ec3537a35 100644 --- a/src/dispnew.c +++ b/src/dispnew.c @@ -649,14 +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) - { - /* Non-ISO HP/UX compiler doesn't like auto struct - initialization. */ - struct glyph_row temp; - temp = matrix->rows[i]; - matrix->rows[i] = matrix->rows[j]; - matrix->rows[j] = temp; - } + swap (matrix->rows[i], matrix->rows[j]); } @@ -966,9 +959,7 @@ increment_row_positions (struct glyph_row *row, static void swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b) { - int area; - - for (area = 0; area < LAST_AREA; ++area) + for (int area = 0; area < LAST_AREA; ++area) { /* Number of glyphs to swap. */ int max_used = max (a->used[area], b->used[area]); @@ -984,12 +975,7 @@ swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b) while (glyph_a < glyph_a_end) { - /* Non-ISO HP/UX compiler doesn't like auto struct - initialization. */ - struct glyph temp; - temp = *glyph_a; - *glyph_a = *glyph_b; - *glyph_b = temp; + swap (*glyph_a, *glyph_b); ++glyph_a; ++glyph_b; } diff --git a/src/editfns.c b/src/editfns.c index f3b3cfb7243..2e455a2efed 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -1739,7 +1739,7 @@ versa, strings are converted from unibyte to multibyte or vice versa using `string-make-multibyte' or `string-make-unibyte', which see. */) (Lisp_Object buffer, Lisp_Object start, Lisp_Object end) { - register EMACS_INT b, e, temp; + register EMACS_INT b, e; register struct buffer *bp, *obuf; Lisp_Object buf; @@ -1753,7 +1753,7 @@ using `string-make-multibyte' or `string-make-unibyte', which see. */) b = !NILP (start) ? fix_position (start) : BUF_BEGV (bp); e = !NILP (end) ? fix_position (end) : BUF_ZV (bp); if (b > e) - temp = b, b = e, e = temp; + swap (b, e); if (!(BUF_BEGV (bp) <= b && e <= BUF_ZV (bp))) args_out_of_range (start, end); @@ -1782,7 +1782,7 @@ The value of `case-fold-search' in the current buffer determines whether case is significant or ignored. */) (Lisp_Object buffer1, Lisp_Object start1, Lisp_Object end1, Lisp_Object buffer2, Lisp_Object start2, Lisp_Object end2) { - register EMACS_INT begp1, endp1, begp2, endp2, temp; + register EMACS_INT begp1, endp1, begp2, endp2; register struct buffer *bp1, *bp2; register Lisp_Object trt = (!NILP (Vcase_fold_search) @@ -1808,7 +1808,7 @@ determines whether case is significant or ignored. */) begp1 = !NILP (start1) ? fix_position (start1) : BUF_BEGV (bp1); endp1 = !NILP (end1) ? fix_position (end1) : BUF_ZV (bp1); if (begp1 > endp1) - temp = begp1, begp1 = endp1, endp1 = temp; + swap (begp1, endp1); if (!(BUF_BEGV (bp1) <= begp1 && begp1 <= endp1 @@ -1833,7 +1833,7 @@ determines whether case is significant or ignored. */) begp2 = !NILP (start2) ? fix_position (start2) : BUF_BEGV (bp2); endp2 = !NILP (end2) ? fix_position (end2) : BUF_ZV (bp2); if (begp2 > endp2) - temp = begp2, begp2 = endp2, endp2 = temp; + swap (begp2, endp2); if (!(BUF_BEGV (bp2) <= begp2 && begp2 <= endp2 diff --git a/src/eval.c b/src/eval.c index 94f6d8e31f8..6a866d6cc32 100644 --- a/src/eval.c +++ b/src/eval.c @@ -2794,12 +2794,9 @@ usage: (run-hook-with-args-until-failure HOOK &rest ARGS) */) static Lisp_Object run_hook_wrapped_funcall (ptrdiff_t nargs, Lisp_Object *args) { - Lisp_Object tmp = args[0], ret; - args[0] = args[1]; - args[1] = tmp; - ret = Ffuncall (nargs, args); - args[1] = args[0]; - args[0] = tmp; + swap (args[0], args[1]); + Lisp_Object ret = Ffuncall (nargs, args); + swap (args[1], args[0]); return ret; } diff --git a/src/fns.c b/src/fns.c index c03aea02397..c8adc5cb891 100644 --- a/src/fns.c +++ b/src/fns.c @@ -5674,11 +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) - { - EMACS_INT temp = b; - b = e; - e = temp; - } + swap (b, e); if (!(BEGV <= b && e <= ZV)) args_out_of_range (start, end); diff --git a/src/lisp.h b/src/lisp.h index 44f69892c6f..f96932ab0c1 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -67,6 +67,10 @@ INLINE_HEADER_BEGIN #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); + /* 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 6aa6f4f9b34..fdc2cc63445 100644 --- a/src/regex-emacs.c +++ b/src/regex-emacs.c @@ -2839,7 +2839,7 @@ forall_firstchar_1 (re_char *p, re_char *pend, while (true) { - re_char *newp1, *newp2, *tmp; + re_char *newp1, *newp2; re_char *p_orig = p; int offset; @@ -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) - (tmp = newp1, newp1 = newp2, newp2 = tmp); + swap (newp1, newp2); if (newp2 <= p_orig) /* Both destinations go backward! */ { diff --git a/src/textconv.c b/src/textconv.c index 2a7b0ed330d..e0707522d7e 100644 --- a/src/textconv.c +++ b/src/textconv.c @@ -176,7 +176,7 @@ textconv_query (struct frame *f, struct textconv_callback_struct *query, { specpdl_ref count; ptrdiff_t pos, pos_byte, end, end_byte, start; - ptrdiff_t temp, temp1, mark; + ptrdiff_t mark; char *buffer; struct window *w; @@ -383,12 +383,8 @@ textconv_query (struct frame *f, struct textconv_callback_struct *query, if (end < pos) { eassert (end_byte < pos_byte); - temp = pos_byte; - temp1 = pos; - pos_byte = end_byte; - pos = end; - end = temp1; - end_byte = temp; + swap (pos_byte, end_byte); + swap (pos, end); } /* Return the string first. */ @@ -1905,15 +1901,9 @@ get_extracted_text (struct frame *f, ptrdiff_t n, start = marker_position (BVAR (current_buffer, mark)); end = PT; - /* Sort start and end. start_byte is used to hold a - temporary value. */ - + /* Sort start and end. */ if (start > end) - { - start_byte = end; - end = start; - start = start_byte; - } + swap (start, end); } else goto finish; @@ -1979,7 +1969,7 @@ get_surrounding_text (struct frame *f, ptrdiff_t left, ptrdiff_t *end_return) { specpdl_ref count; - ptrdiff_t start, end, start_byte, end_byte, mark, temp; + ptrdiff_t start, end, start_byte, end_byte, mark; char *buffer; if (!WINDOW_LIVE_P (f->old_selected_window)) @@ -2012,11 +2002,7 @@ get_surrounding_text (struct frame *f, ptrdiff_t left, /* Now sort start and end. */ if (end < start) - { - temp = start; - start = end; - end = temp; - } + swap (start, end) /* And subtract left and right. */ diff --git a/src/textprop.c b/src/textprop.c index 7d9aae0d2c5..ec9435219ea 100644 --- a/src/textprop.c +++ b/src/textprop.c @@ -142,12 +142,7 @@ validate_interval_range (Lisp_Object object, Lisp_Object *begin, return NULL; if (XFIXNUM (*begin) > XFIXNUM (*end)) - { - Lisp_Object n; - n = *begin; - *begin = *end; - *end = n; - } + swap (*begin, *end); if (BUFFERP (object)) { @@ -2201,11 +2196,7 @@ verify_interval_modification (struct buffer *buf, return; if (start > end) - { - ptrdiff_t temp = start; - start = end; - end = temp; - } + swap (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 a73c0de06f9..c417159cf9e 100644 --- a/src/w32uniscribe.c +++ b/src/w32uniscribe.c @@ -765,10 +765,10 @@ add_opentype_font_name_to_list (ENUMLOGFONTEX *logical_font, #define OTF_INT16_VAL(TABLE, OFFSET, PTR) \ do { \ - BYTE temp, data[2]; \ + BYTE data[2]; \ if (GetFontData (context, TABLE, OFFSET, data, 2) != 2) \ goto font_table_error; \ - temp = data[0], data[0] = data[1], data[1] = temp; \ + swap (data[0], data[1]); \ memcpy (PTR, data, 2); \ } while (0) diff --git a/src/xfaces.c b/src/xfaces.c index e30c2fac70c..f79eb022e15 100644 --- a/src/xfaces.c +++ b/src/xfaces.c @@ -1357,12 +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)) - { - Lisp_Object tmp; - tmp = fg; - fg = bg; - bg = tmp; - } + swap (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 1f398b2e39a..0b83b0554b3 100644 --- a/src/xterm.c +++ b/src/xterm.c @@ -1569,19 +1569,19 @@ typedef enum xm_byte_order #define SWAPCARD32(l) \ { \ struct { unsigned t : 32; } bit32; \ - char n, *tp = (char *) &bit32; \ + char *tp = (char *) &bit32; \ bit32.t = l; \ - n = tp[0]; tp[0] = tp[3]; tp[3] = n; \ - n = tp[1]; tp[1] = tp[2]; tp[2] = n; \ + swap (tp[0], tp[3]); \ + swap (tp[1], tp[2]); \ l = bit32.t; \ } #define SWAPCARD16(s) \ { \ struct { unsigned t : 16; } bit16; \ - char n, *tp = (char *) &bit16; \ + char *tp = (char *) &bit16; \ bit16.t = s; \ - n = tp[0]; tp[0] = tp[1]; tp[1] = n; \ + swap (tp[0], tp[1]); \ s = bit16.t; \ }