1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

Add macros for looping through a List without a ListCell.

Many foreach loops only use the ListCell pointer to retrieve the
content of the cell, like so:

    ListCell   *lc;

    foreach(lc, mylist)
    {
        int         myint = lfirst_int(lc);

        ...
    }

This commit adds a few convenience macros that automatically
declare the loop variable and retrieve the current cell's contents.
This allows us to rewrite the previous loop like this:

    foreach_int(myint, mylist)
    {
        ...
    }

This commit also adjusts a few existing loops in order to add
coverage for the new/adjusted macros.  There is presently no plan
to bulk update all foreach loops, as that could introduce a
significant amount of back-patching pain.  Instead, these macros
are primarily intended for use in new code.

Author: Jelte Fennema-Nio
Reviewed-by: David Rowley, Alvaro Herrera, Vignesh C, Tom Lane
Discussion: https://postgr.es/m/CAGECzQSwXKnxGwW1_Q5JE%2B8Ja20kyAbhBHO04vVrQsLcDciwXA%40mail.gmail.com
This commit is contained in:
Nathan Bossart
2024-01-04 16:09:34 -06:00
parent 5e8674dc83
commit 14dd0f27d7
5 changed files with 71 additions and 26 deletions

View File

@ -216,7 +216,6 @@ ExecInitQual(List *qual, PlanState *parent)
ExprState *state;
ExprEvalStep scratch = {0};
List *adjust_jumps = NIL;
ListCell *lc;
/* short-circuit (here and in ExecQual) for empty restriction list */
if (qual == NIL)
@ -250,10 +249,8 @@ ExecInitQual(List *qual, PlanState *parent)
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
foreach(lc, qual)
foreach_ptr(Expr, node, qual)
{
Expr *node = (Expr *) lfirst(lc);
/* first evaluate expression */
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
@ -265,9 +262,9 @@ ExecInitQual(List *qual, PlanState *parent)
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
foreach_int(jump, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
ExprEvalStep *as = &state->steps[jump];
Assert(as->opcode == EEOP_QUAL);
Assert(as->d.qualexpr.jumpdone == -1);