From dce16548a55bdc8559a4983b7b69d9be971b1c0d Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Mon, 18 Dec 2023 14:18:59 +0100 Subject: [PATCH v7 2/3] Make some macros that used cell work with new foreach macros This modifies foreach_delete_current and foreach_current_index to work with the new foreach macros that don't require a ListCell * variable. --- src/include/nodes/pg_list.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/include/nodes/pg_list.h b/src/include/nodes/pg_list.h index e73e399f59d..7755f726604 100644 --- a/src/include/nodes/pg_list.h +++ b/src/include/nodes/pg_list.h @@ -383,24 +383,25 @@ lnext(const List *l, const ListCell *c) * delete the current list element from the List associated with a * surrounding foreach() loop, returning the new List pointer. * - * This is equivalent to list_delete_cell(), but it also adjusts the foreach - * loop's state so that no list elements will be missed. Do not delete - * elements from an active foreach loop's list in any other way! + * This is similar to list_delete_cell(), but it also works when using + * foreach_xyz macros that don't require passing in a "ListCell *". + * Furthermore it adjusts the foreach loop's state so that no list elements + * will be missed. Do not delete elements from an active foreach loop's list in + * any other way! */ -#define foreach_delete_current(lst, cell) \ - (cell##__state.i--, \ - (List *) (cell##__state.l = list_delete_cell(lst, cell))) +#define foreach_delete_current(lst, var_or_cell) \ + ((List *) (var_or_cell##__state.l = list_delete_nth_cell(lst, var_or_cell##__state.i--))) /* * foreach_current_index - * get the zero-based list index of a surrounding foreach() loop's - * current element; pass the name of the "ListCell *" iterator variable. + * current element; pass the name of the iterator variable. * * Beware of using this after foreach_delete_current(); the value will be * out of sync for the rest of the current loop iteration. Anyway, since * you just deleted the current element, the value is pretty meaningless. */ -#define foreach_current_index(cell) (cell##__state.i) +#define foreach_current_index(var_or_cell) (var_or_cell##__state.i) /* * for_each_from - -- 2.34.1