Coverage Report

Created: 2025-11-11 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/ast_preprocess.c
Line
Count
Source
1
/* AST pre-processing */
2
#include "Python.h"
3
#include "pycore_ast.h"           // _PyAST_GetDocString()
4
#include "pycore_c_array.h"       // _Py_CArray_EnsureCapacity()
5
#include "pycore_format.h"        // F_LJUST
6
#include "pycore_runtime.h"       // _Py_STR()
7
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
8
9
10
/* See PEP 765 */
11
typedef struct {
12
    bool in_finally;
13
    bool in_funcdef;
14
    bool in_loop;
15
} ControlFlowInFinallyContext;
16
17
typedef struct {
18
    PyObject *filename;
19
    int optimize;
20
    int ff_features;
21
    int syntax_check_only;
22
    int enable_warnings;
23
24
    _Py_c_array_t cf_finally;       /* context for PEP 765 check */
25
    int cf_finally_used;
26
} _PyASTPreprocessState;
27
28
637k
#define ENTER_RECURSIVE() \
29
637k
if (Py_EnterRecursiveCall(" during compilation")) { \
30
0
    return 0; \
31
0
}
32
33
637k
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
34
35
static ControlFlowInFinallyContext*
36
get_cf_finally_top(_PyASTPreprocessState *state)
37
19.0k
{
38
19.0k
    int idx = state->cf_finally_used;
39
19.0k
    return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
40
19.0k
}
41
42
static int
43
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
44
11.9k
{
45
11.9k
    if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
46
0
        return 0;
47
0
    }
48
49
11.9k
    state->cf_finally_used++;
50
11.9k
    ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
51
52
11.9k
    ctx->in_finally = finally;
53
11.9k
    ctx->in_funcdef = funcdef;
54
11.9k
    ctx->in_loop = loop;
55
11.9k
    return 1;
56
11.9k
}
57
58
static void
59
pop_cf_context(_PyASTPreprocessState *state)
60
11.9k
{
61
11.9k
    assert(state->cf_finally_used > 0);
62
11.9k
    state->cf_finally_used--;
63
11.9k
}
64
65
static int
66
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state)
67
0
{
68
0
    PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw);
69
0
    if (msg == NULL) {
70
0
        return 0;
71
0
    }
72
0
    int ret = _PyErr_EmitSyntaxWarning(msg, state->filename, n->lineno,
73
0
                                       n->col_offset + 1, n->end_lineno,
74
0
                                       n->end_col_offset + 1);
75
0
    Py_DECREF(msg);
76
0
    return ret < 0 ? 0 : 1;
77
0
}
78
79
static int
80
before_return(_PyASTPreprocessState *state, stmt_ty node_)
81
6.81k
{
82
6.81k
    if (state->enable_warnings && state->cf_finally_used > 0) {
83
6.34k
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
84
6.34k
        if (ctx->in_finally && ! ctx->in_funcdef) {
85
0
            if (!control_flow_in_finally_warning("return", node_, state)) {
86
0
                return 0;
87
0
            }
88
0
        }
89
6.34k
    }
90
6.81k
    return 1;
91
6.81k
}
92
93
static int
94
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
95
1.19k
{
96
1.19k
    if (state->enable_warnings && state->cf_finally_used > 0) {
97
697
        ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
98
697
        if (ctx->in_finally && ! ctx->in_loop) {
99
0
            if (!control_flow_in_finally_warning(kw, node_, state)) {
100
0
                return 0;
101
0
            }
102
0
        }
103
697
    }
104
1.19k
    return 1;
105
1.19k
}
106
107
#define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \
108
11.9k
    if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \
109
0
        return 0; \
110
0
    }
111
112
11.9k
#define POP_CONTEXT(S) pop_cf_context(S)
113
114
2.18k
#define BEFORE_FINALLY(S, N)    PUSH_CONTEXT((S), (N), true, false, false)
115
2.18k
#define AFTER_FINALLY(S)        POP_CONTEXT(S)
116
7.54k
#define BEFORE_FUNC_BODY(S, N)  PUSH_CONTEXT((S), (N), false, true, false)
117
7.54k
#define AFTER_FUNC_BODY(S)      POP_CONTEXT(S)
118
2.23k
#define BEFORE_LOOP_BODY(S, N)  PUSH_CONTEXT((S), (N), false, false, true)
119
2.23k
#define AFTER_LOOP_BODY(S)      POP_CONTEXT(S)
120
121
#define BEFORE_RETURN(S, N) \
122
6.81k
    if (!before_return((S), (N))) { \
123
0
        return 0; \
124
0
    }
125
126
#define BEFORE_LOOP_EXIT(S, N, KW) \
127
1.19k
    if (!before_loop_exit((S), (N), (KW))) { \
128
0
        return 0; \
129
0
    }
130
131
static int
132
make_const(expr_ty node, PyObject *val, PyArena *arena)
133
0
{
134
    // Even if no new value was calculated, make_const may still
135
    // need to clear an error (e.g. for division by zero)
136
0
    if (val == NULL) {
137
0
        if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
138
0
            return 0;
139
0
        }
140
0
        PyErr_Clear();
141
0
        return 1;
142
0
    }
143
0
    if (_PyArena_AddPyObject(arena, val) < 0) {
144
0
        Py_DECREF(val);
145
0
        return 0;
146
0
    }
147
0
    node->kind = Constant_kind;
148
0
    node->v.Constant.kind = NULL;
149
0
    node->v.Constant.value = val;
150
0
    return 1;
151
0
}
152
153
507
#define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
154
155
static int
156
has_starred(asdl_expr_seq *elts)
157
628
{
158
628
    Py_ssize_t n = asdl_seq_LEN(elts);
159
1.91k
    for (Py_ssize_t i = 0; i < n; i++) {
160
1.29k
        expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
161
1.29k
        if (e->kind == Starred_kind) {
162
0
            return 1;
163
0
        }
164
1.29k
    }
165
628
    return 0;
166
628
}
167
168
static expr_ty
169
parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena)
170
1.68k
{
171
1.68k
    const void *data = PyUnicode_DATA(fmt);
172
1.68k
    int kind = PyUnicode_KIND(fmt);
173
1.68k
    Py_ssize_t size = PyUnicode_GET_LENGTH(fmt);
174
1.68k
    Py_ssize_t start, pos;
175
1.68k
    int has_percents = 0;
176
1.68k
    start = pos = *ppos;
177
12.2k
    while (pos < size) {
178
11.6k
        if (PyUnicode_READ(kind, data, pos) != '%') {
179
10.5k
            pos++;
180
10.5k
        }
181
1.17k
        else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') {
182
4
            has_percents = 1;
183
4
            pos += 2;
184
4
        }
185
1.17k
        else {
186
1.17k
            break;
187
1.17k
        }
188
11.6k
    }
189
1.68k
    *ppos = pos;
190
1.68k
    if (pos == start) {
191
701
        return NULL;
192
701
    }
193
980
    PyObject *str = PyUnicode_Substring(fmt, start, pos);
194
    /* str = str.replace('%%', '%') */
195
980
    if (str && has_percents) {
196
3
        _Py_DECLARE_STR(dbl_percent, "%%");
197
3
        Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent),
198
3
                                         _Py_LATIN1_CHR('%'), -1));
199
3
    }
200
980
    if (!str) {
201
0
        return NULL;
202
0
    }
203
204
980
    if (_PyArena_AddPyObject(arena, str) < 0) {
205
0
        Py_DECREF(str);
206
0
        return NULL;
207
0
    }
208
980
    return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
209
980
}
210
211
62
#define MAXDIGITS 3
212
213
static int
214
simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos,
215
                        int *spec, int *flags, int *width, int *prec)
216
1.17k
{
217
1.17k
    Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt);
218
1.17k
    Py_UCS4 ch;
219
220
1.27k
#define NEXTC do {                      \
221
1.27k
    if (pos >= len) {                   \
222
0
        return 0;                       \
223
0
    }                                   \
224
1.27k
    ch = PyUnicode_READ_CHAR(fmt, pos); \
225
1.27k
    pos++;                              \
226
1.27k
} while (0)
227
228
1.17k
    *flags = 0;
229
1.22k
    while (1) {
230
1.22k
        NEXTC;
231
1.22k
        switch (ch) {
232
9
            case '-': *flags |= F_LJUST; continue;
233
0
            case '+': *flags |= F_SIGN; continue;
234
0
            case ' ': *flags |= F_BLANK; continue;
235
22
            case '#': *flags |= F_ALT; continue;
236
20
            case '0': *flags |= F_ZERO; continue;
237
1.22k
        }
238
1.17k
        break;
239
1.22k
    }
240
1.17k
    if ('0' <= ch && ch <= '9') {
241
30
        *width = 0;
242
30
        int digits = 0;
243
75
        while ('0' <= ch && ch <= '9') {
244
48
            *width = *width * 10 + (ch - '0');
245
48
            NEXTC;
246
48
            if (++digits >= MAXDIGITS) {
247
3
                return 0;
248
3
            }
249
48
        }
250
30
    }
251
252
1.17k
    if (ch == '.') {
253
1
        NEXTC;
254
1
        *prec = 0;
255
1
        if ('0' <= ch && ch <= '9') {
256
1
            int digits = 0;
257
3
            while ('0' <= ch && ch <= '9') {
258
2
                *prec = *prec * 10 + (ch - '0');
259
2
                NEXTC;
260
2
                if (++digits >= MAXDIGITS) {
261
0
                    return 0;
262
0
                }
263
2
            }
264
1
        }
265
1
    }
266
1.17k
    *spec = ch;
267
1.17k
    *ppos = pos;
268
1.17k
    return 1;
269
270
1.17k
#undef NEXTC
271
1.17k
}
272
273
static expr_ty
274
parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena)
275
1.17k
{
276
1.17k
    int spec, flags, width = -1, prec = -1;
277
1.17k
    if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) {
278
        // Unsupported format.
279
3
        return NULL;
280
3
    }
281
1.17k
    if (spec == 's' || spec == 'r' || spec == 'a') {
282
1.05k
        char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf;
283
1.05k
        if (!(flags & F_LJUST) && width > 0) {
284
2
            *p++ = '>';
285
2
        }
286
1.05k
        if (width >= 0) {
287
11
            p += snprintf(p, MAXDIGITS + 1, "%d", width);
288
11
        }
289
1.05k
        if (prec >= 0) {
290
1
            p += snprintf(p, MAXDIGITS + 2, ".%d", prec);
291
1
        }
292
1.05k
        expr_ty format_spec = NULL;
293
1.05k
        if (p != buf) {
294
12
            PyObject *str = PyUnicode_FromString(buf);
295
12
            if (str == NULL) {
296
0
                return NULL;
297
0
            }
298
12
            if (_PyArena_AddPyObject(arena, str) < 0) {
299
0
                Py_DECREF(str);
300
0
                return NULL;
301
0
            }
302
12
            format_spec = _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
303
12
            if (format_spec == NULL) {
304
0
                return NULL;
305
0
            }
306
12
        }
307
1.05k
        return _PyAST_FormattedValue(arg, spec, format_spec,
308
1.05k
                                     arg->lineno, arg->col_offset,
309
1.05k
                                     arg->end_lineno, arg->end_col_offset,
310
1.05k
                                     arena);
311
1.05k
    }
312
    // Unsupported format.
313
118
    return NULL;
314
1.17k
}
315
316
static int
317
optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena)
318
628
{
319
628
    Py_ssize_t pos = 0;
320
628
    Py_ssize_t cnt = 0;
321
628
    asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena);
322
628
    if (!seq) {
323
0
        return 0;
324
0
    }
325
628
    seq->size = 0;
326
327
1.68k
    while (1) {
328
1.68k
        expr_ty lit = parse_literal(fmt, &pos, arena);
329
1.68k
        if (lit) {
330
980
            asdl_seq_SET(seq, seq->size++, lit);
331
980
        }
332
701
        else if (PyErr_Occurred()) {
333
0
            return 0;
334
0
        }
335
336
1.68k
        if (pos >= PyUnicode_GET_LENGTH(fmt)) {
337
507
            break;
338
507
        }
339
1.17k
        if (cnt >= asdl_seq_LEN(elts)) {
340
            // More format units than items.
341
0
            return 1;
342
0
        }
343
1.17k
        assert(PyUnicode_READ_CHAR(fmt, pos) == '%');
344
1.17k
        pos++;
345
1.17k
        expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena);
346
1.17k
        cnt++;
347
1.17k
        if (!expr) {
348
121
            return !PyErr_Occurred();
349
121
        }
350
1.05k
        asdl_seq_SET(seq, seq->size++, expr);
351
1.05k
    }
352
507
    if (cnt < asdl_seq_LEN(elts)) {
353
        // More items than format units.
354
0
        return 1;
355
0
    }
356
507
    expr_ty res = _PyAST_JoinedStr(seq,
357
507
                                   node->lineno, node->col_offset,
358
507
                                   node->end_lineno, node->end_col_offset,
359
507
                                   arena);
360
507
    if (!res) {
361
0
        return 0;
362
0
    }
363
507
    COPY_NODE(node, res);
364
//     PySys_FormatStderr("format = %R\n", fmt);
365
507
    return 1;
366
507
}
367
368
static int
369
fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state)
370
28.3k
{
371
28.3k
    if (state->syntax_check_only) {
372
22.9k
        return 1;
373
22.9k
    }
374
5.35k
    expr_ty lhs, rhs;
375
5.35k
    lhs = node->v.BinOp.left;
376
5.35k
    rhs = node->v.BinOp.right;
377
5.35k
    if (lhs->kind != Constant_kind) {
378
3.68k
        return 1;
379
3.68k
    }
380
1.67k
    PyObject *lv = lhs->v.Constant.value;
381
382
1.67k
    if (node->v.BinOp.op == Mod &&
383
1.16k
        rhs->kind == Tuple_kind &&
384
1.67k
        PyUnicode_Check(lv) &&
385
628
        !has_starred(rhs->v.Tuple.elts))
386
628
    {
387
628
        return optimize_format(node, lv, rhs->v.Tuple.elts, arena);
388
628
    }
389
390
1.04k
    return 1;
391
1.67k
}
392
393
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
394
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
395
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
396
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
397
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
398
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
399
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
400
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
401
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
402
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
403
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
404
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
405
406
#define CALL(FUNC, TYPE, ARG) \
407
384k
    if (!FUNC((ARG), ctx_, state)) \
408
384k
        return 0;
409
410
#define CALL_OPT(FUNC, TYPE, ARG) \
411
100k
    if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \
412
100k
        return 0;
413
414
250k
#define CALL_SEQ(FUNC, TYPE, ARG) { \
415
250k
    Py_ssize_t i; \
416
250k
    asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \
417
577k
    for (i = 0; i < asdl_seq_LEN(seq); i++) { \
418
327k
        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
419
327k
        if (elt != NULL && !FUNC(elt, ctx_, state)) \
420
327k
            return 0; \
421
327k
    } \
422
250k
}
423
424
425
static int
426
stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
427
0
{
428
0
    if (idx >= asdl_seq_LEN(stmts)) {
429
0
        return 0;
430
0
    }
431
0
    for (Py_ssize_t i = idx; i < asdl_seq_LEN(stmts) - 1; i++) {
432
0
        stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, i+1);
433
0
        asdl_seq_SET(stmts, i, st);
434
0
    }
435
0
    stmts->size--;
436
0
    return 1;
437
0
}
438
439
static int
440
remove_docstring(asdl_stmt_seq *stmts, Py_ssize_t idx, PyArena *ctx_)
441
0
{
442
0
    assert(_PyAST_GetDocString(stmts) != NULL);
443
    // In case there's just the docstring in the body, replace it with `pass`
444
    // keyword, so body won't be empty.
445
0
    if (asdl_seq_LEN(stmts) == 1) {
446
0
        stmt_ty docstring = (stmt_ty)asdl_seq_GET(stmts, 0);
447
0
        stmt_ty pass = _PyAST_Pass(
448
0
            docstring->lineno, docstring->col_offset,
449
            // we know that `pass` always takes 4 chars and a single line,
450
            // while docstring can span on multiple lines
451
0
            docstring->lineno, docstring->col_offset + 4,
452
0
            ctx_
453
0
        );
454
0
        if (pass == NULL) {
455
0
            return 0;
456
0
        }
457
0
        asdl_seq_SET(stmts, 0, pass);
458
0
        return 1;
459
0
    }
460
    // In case there are more than 1 body items, just remove the docstring.
461
0
    return stmt_seq_remove_item(stmts, idx);
462
0
}
463
464
static int
465
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state)
466
15.9k
{
467
15.9k
    int docstring = _PyAST_GetDocString(stmts) != NULL;
468
15.9k
    if (docstring && (state->optimize >= 2)) {
469
        /* remove the docstring */
470
0
        if (!remove_docstring(stmts, 0, ctx_)) {
471
0
            return 0;
472
0
        }
473
0
        docstring = 0;
474
0
    }
475
15.9k
    CALL_SEQ(astfold_stmt, stmt, stmts);
476
15.9k
    if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
477
0
        stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
478
0
        asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
479
0
        if (!values) {
480
0
            return 0;
481
0
        }
482
0
        asdl_seq_SET(values, 0, st->v.Expr.value);
483
0
        expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset,
484
0
                                        st->end_lineno, st->end_col_offset,
485
0
                                        ctx_);
486
0
        if (!expr) {
487
0
            return 0;
488
0
        }
489
0
        st->v.Expr.value = expr;
490
0
    }
491
15.9k
    return 1;
492
15.9k
}
493
494
static int
495
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
496
6.64k
{
497
6.64k
    switch (node_->kind) {
498
6.56k
    case Module_kind:
499
6.56k
        CALL(astfold_body, asdl_seq, node_->v.Module.body);
500
6.56k
        break;
501
0
    case Interactive_kind:
502
0
        CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
503
0
        break;
504
75
    case Expression_kind:
505
75
        CALL(astfold_expr, expr_ty, node_->v.Expression.body);
506
75
        break;
507
    // The following top level nodes don't participate in constant folding
508
0
    case FunctionType_kind:
509
0
        break;
510
    // No default case, so the compiler will emit a warning if new top level
511
    // compilation nodes are added without being handled here
512
6.64k
    }
513
6.64k
    return 1;
514
6.64k
}
515
516
static int
517
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
518
519k
{
519
519k
    ENTER_RECURSIVE();
520
519k
    switch (node_->kind) {
521
2.77k
    case BoolOp_kind:
522
2.77k
        CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
523
2.77k
        break;
524
28.3k
    case BinOp_kind:
525
28.3k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
526
28.3k
        CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
527
28.3k
        CALL(fold_binop, expr_ty, node_);
528
28.3k
        break;
529
78.8k
    case UnaryOp_kind:
530
78.8k
        CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
531
78.8k
        break;
532
1.09k
    case Lambda_kind:
533
1.09k
        CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
534
1.09k
        CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
535
1.09k
        break;
536
474
    case IfExp_kind:
537
474
        CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
538
474
        CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
539
474
        CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
540
474
        break;
541
1.65k
    case Dict_kind:
542
1.65k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
543
1.65k
        CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
544
1.65k
        break;
545
482
    case Set_kind:
546
482
        CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
547
482
        break;
548
558
    case ListComp_kind:
549
558
        CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
550
558
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
551
558
        break;
552
176
    case SetComp_kind:
553
176
        CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
554
176
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
555
176
        break;
556
609
    case DictComp_kind:
557
609
        CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
558
609
        CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
559
609
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
560
609
        break;
561
667
    case GeneratorExp_kind:
562
667
        CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
563
667
        CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
564
667
        break;
565
219
    case Await_kind:
566
219
        CALL(astfold_expr, expr_ty, node_->v.Await.value);
567
219
        break;
568
813
    case Yield_kind:
569
813
        CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
570
813
        break;
571
109
    case YieldFrom_kind:
572
109
        CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
573
109
        break;
574
9.95k
    case Compare_kind:
575
9.95k
        CALL(astfold_expr, expr_ty, node_->v.Compare.left);
576
9.95k
        CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
577
9.95k
        break;
578
31.3k
    case Call_kind:
579
31.3k
        CALL(astfold_expr, expr_ty, node_->v.Call.func);
580
31.3k
        CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
581
31.3k
        CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
582
31.3k
        break;
583
12.5k
    case FormattedValue_kind:
584
12.5k
        CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
585
12.5k
        CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
586
12.5k
        break;
587
1.52k
    case Interpolation_kind:
588
1.52k
        CALL(astfold_expr, expr_ty, node_->v.Interpolation.value);
589
1.52k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec);
590
1.52k
        break;
591
7.09k
    case JoinedStr_kind:
592
7.09k
        CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
593
7.09k
        break;
594
295
    case TemplateStr_kind:
595
295
        CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values);
596
295
        break;
597
29.6k
    case Attribute_kind:
598
29.6k
        CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
599
29.6k
        break;
600
5.37k
    case Subscript_kind:
601
5.37k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
602
5.37k
        CALL(astfold_expr, expr_ty, node_->v.Subscript.slice);
603
5.37k
        break;
604
936
    case Starred_kind:
605
936
        CALL(astfold_expr, expr_ty, node_->v.Starred.value);
606
936
        break;
607
3.30k
    case Slice_kind:
608
3.30k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
609
3.30k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
610
3.30k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
611
3.30k
        break;
612
2.91k
    case List_kind:
613
2.91k
        CALL_SEQ(astfold_expr, expr, node_->v.List.elts);
614
2.91k
        break;
615
12.3k
    case Tuple_kind:
616
12.3k
        CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
617
12.3k
        break;
618
169k
    case Name_kind:
619
169k
        if (state->syntax_check_only) {
620
63.0k
            break;
621
63.0k
        }
622
106k
        if (node_->v.Name.ctx == Load &&
623
87.9k
                _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
624
0
            LEAVE_RECURSIVE();
625
0
            return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
626
0
        }
627
106k
        break;
628
106k
    case NamedExpr_kind:
629
311
        CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
630
311
        break;
631
115k
    case Constant_kind:
632
        // Already a constant, nothing further to do
633
115k
        break;
634
    // No default case, so the compiler will emit a warning if new expression
635
    // kinds are added without being handled here
636
519k
    }
637
519k
    LEAVE_RECURSIVE();
638
519k
    return 1;
639
519k
}
640
641
static int
642
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
643
5.34k
{
644
5.34k
    CALL(astfold_expr, expr_ty, node_->value);
645
5.34k
    return 1;
646
5.34k
}
647
648
static int
649
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
650
2.11k
{
651
2.11k
    CALL(astfold_expr, expr_ty, node_->target);
652
2.11k
    CALL(astfold_expr, expr_ty, node_->iter);
653
2.11k
    CALL_SEQ(astfold_expr, expr, node_->ifs);
654
2.11k
    return 1;
655
2.11k
}
656
657
static int
658
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
659
8.63k
{
660
8.63k
    CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
661
8.63k
    CALL_SEQ(astfold_arg, arg, node_->args);
662
8.63k
    CALL_OPT(astfold_arg, arg_ty, node_->vararg);
663
8.63k
    CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
664
8.63k
    CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
665
8.63k
    CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
666
8.63k
    CALL_SEQ(astfold_expr, expr, node_->defaults);
667
8.63k
    return 1;
668
8.63k
}
669
670
static int
671
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
672
22.0k
{
673
22.0k
    if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
674
21.1k
        CALL_OPT(astfold_expr, expr_ty, node_->annotation);
675
21.1k
    }
676
22.0k
    return 1;
677
22.0k
}
678
679
static int
680
astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
681
103k
{
682
103k
    ENTER_RECURSIVE();
683
103k
    switch (node_->kind) {
684
7.35k
    case FunctionDef_kind: {
685
7.35k
        CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
686
7.35k
        CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
687
7.35k
        BEFORE_FUNC_BODY(state, node_);
688
7.35k
        CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
689
7.35k
        AFTER_FUNC_BODY(state);
690
7.35k
        CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
691
7.35k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
692
7.05k
            CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
693
7.05k
        }
694
7.35k
        break;
695
7.35k
    }
696
7.35k
    case AsyncFunctionDef_kind: {
697
193
        CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params);
698
193
        CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
699
193
        BEFORE_FUNC_BODY(state, node_);
700
193
        CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
701
193
        AFTER_FUNC_BODY(state);
702
193
        CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
703
193
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
704
175
            CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
705
175
        }
706
193
        break;
707
193
    }
708
1.79k
    case ClassDef_kind:
709
1.79k
        CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params);
710
1.79k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
711
1.79k
        CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
712
1.79k
        CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
713
1.79k
        CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
714
1.79k
        break;
715
6.81k
    case Return_kind:
716
6.81k
        BEFORE_RETURN(state, node_);
717
6.81k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
718
6.81k
        break;
719
594
    case Delete_kind:
720
594
        CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
721
594
        break;
722
17.7k
    case Assign_kind:
723
17.7k
        CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
724
17.7k
        CALL(astfold_expr, expr_ty, node_->v.Assign.value);
725
17.7k
        break;
726
1.14k
    case AugAssign_kind:
727
1.14k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
728
1.14k
        CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
729
1.14k
        break;
730
1.13k
    case AnnAssign_kind:
731
1.13k
        CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target);
732
1.13k
        if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
733
1.04k
            CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation);
734
1.04k
        }
735
1.13k
        CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
736
1.13k
        break;
737
74
    case TypeAlias_kind:
738
74
        CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
739
74
        CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params);
740
74
        CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
741
74
        break;
742
1.48k
    case For_kind: {
743
1.48k
        CALL(astfold_expr, expr_ty, node_->v.For.target);
744
1.48k
        CALL(astfold_expr, expr_ty, node_->v.For.iter);
745
1.48k
        BEFORE_LOOP_BODY(state, node_);
746
1.48k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
747
1.48k
        AFTER_LOOP_BODY(state);
748
1.48k
        CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
749
1.48k
        break;
750
1.48k
    }
751
72
    case AsyncFor_kind: {
752
72
        CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
753
72
        CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
754
72
        BEFORE_LOOP_BODY(state, node_);
755
72
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body);
756
72
        AFTER_LOOP_BODY(state);
757
72
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse);
758
72
        break;
759
72
    }
760
680
    case While_kind: {
761
680
        CALL(astfold_expr, expr_ty, node_->v.While.test);
762
680
        BEFORE_LOOP_BODY(state, node_);
763
680
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
764
680
        AFTER_LOOP_BODY(state);
765
680
        CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
766
680
        break;
767
680
    }
768
10.3k
    case If_kind:
769
10.3k
        CALL(astfold_expr, expr_ty, node_->v.If.test);
770
10.3k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
771
10.3k
        CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
772
10.3k
        break;
773
335
    case With_kind:
774
335
        CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
775
335
        CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
776
335
        break;
777
222
    case AsyncWith_kind:
778
222
        CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items);
779
222
        CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body);
780
222
        break;
781
3.09k
    case Raise_kind:
782
3.09k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
783
3.09k
        CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
784
3.09k
        break;
785
1.75k
    case Try_kind: {
786
1.75k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
787
1.75k
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
788
1.75k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
789
1.75k
        BEFORE_FINALLY(state, node_);
790
1.75k
        CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
791
1.75k
        AFTER_FINALLY(state);
792
1.75k
        break;
793
1.75k
    }
794
431
    case TryStar_kind: {
795
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body);
796
431
        CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers);
797
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse);
798
431
        BEFORE_FINALLY(state, node_);
799
431
        CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody);
800
431
        AFTER_FINALLY(state);
801
431
        break;
802
431
    }
803
459
    case Assert_kind:
804
459
        CALL(astfold_expr, expr_ty, node_->v.Assert.test);
805
459
        CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
806
459
        break;
807
41.9k
    case Expr_kind:
808
41.9k
        CALL(astfold_expr, expr_ty, node_->v.Expr.value);
809
41.9k
        break;
810
268
    case Match_kind:
811
268
        CALL(astfold_expr, expr_ty, node_->v.Match.subject);
812
268
        CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases);
813
268
        break;
814
681
    case Break_kind:
815
681
        BEFORE_LOOP_EXIT(state, node_, "break");
816
681
        break;
817
514
    case Continue_kind:
818
514
        BEFORE_LOOP_EXIT(state, node_, "continue");
819
514
        break;
820
    // The following statements don't contain any subexpressions to be folded
821
1.51k
    case Import_kind:
822
2.85k
    case ImportFrom_kind:
823
3.22k
    case Global_kind:
824
3.44k
    case Nonlocal_kind:
825
4.01k
    case Pass_kind:
826
4.01k
        break;
827
    // No default case, so the compiler will emit a warning if new statement
828
    // kinds are added without being handled here
829
103k
    }
830
103k
    LEAVE_RECURSIVE();
831
103k
    return 1;
832
103k
}
833
834
static int
835
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
836
2.36k
{
837
2.36k
    switch (node_->kind) {
838
2.36k
    case ExceptHandler_kind:
839
2.36k
        CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
840
2.36k
        CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
841
2.36k
        break;
842
    // No default case, so the compiler will emit a warning if new handler
843
    // kinds are added without being handled here
844
2.36k
    }
845
2.36k
    return 1;
846
2.36k
}
847
848
static int
849
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
850
1.82k
{
851
1.82k
    CALL(astfold_expr, expr_ty, node_->context_expr);
852
1.82k
    CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
853
1.82k
    return 1;
854
1.82k
}
855
856
static int
857
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
858
649
{
859
649
    if (state->syntax_check_only) {
860
636
        return 1;
861
636
    }
862
13
    switch (node->kind)
863
13
    {
864
0
        case UnaryOp_kind:
865
0
        {
866
0
            if (node->v.UnaryOp.op == USub &&
867
0
                node->v.UnaryOp.operand->kind == Constant_kind)
868
0
            {
869
0
                PyObject *operand = node->v.UnaryOp.operand->v.Constant.value;
870
0
                PyObject *folded = PyNumber_Negative(operand);
871
0
                return make_const(node, folded, ctx_);
872
0
            }
873
0
            break;
874
0
        }
875
0
        case BinOp_kind:
876
0
        {
877
0
            operator_ty op = node->v.BinOp.op;
878
0
            if ((op == Add || op == Sub) &&
879
0
                node->v.BinOp.right->kind == Constant_kind)
880
0
            {
881
0
                CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.left);
882
0
                if (node->v.BinOp.left->kind == Constant_kind) {
883
0
                    PyObject *left = node->v.BinOp.left->v.Constant.value;
884
0
                    PyObject *right = node->v.BinOp.right->v.Constant.value;
885
0
                    PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right);
886
0
                    return make_const(node, folded, ctx_);
887
0
                }
888
0
            }
889
0
            break;
890
0
        }
891
13
        default:
892
13
            break;
893
13
    }
894
13
    return 1;
895
13
}
896
897
static int
898
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
899
14.2k
{
900
    // Currently, this is really only used to form complex/negative numeric
901
    // constants in MatchValue and MatchMapping nodes
902
    // We still recurse into all subexpressions and subpatterns anyway
903
14.2k
    ENTER_RECURSIVE();
904
14.2k
    switch (node_->kind) {
905
520
        case MatchValue_kind:
906
520
            CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value);
907
520
            break;
908
306
        case MatchSingleton_kind:
909
306
            break;
910
495
        case MatchSequence_kind:
911
495
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns);
912
495
            break;
913
403
        case MatchMapping_kind:
914
403
            CALL_SEQ(fold_const_match_patterns, expr, node_->v.MatchMapping.keys);
915
403
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns);
916
403
            break;
917
1.18k
        case MatchClass_kind:
918
1.18k
            CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls);
919
1.18k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns);
920
1.18k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns);
921
1.18k
            break;
922
291
        case MatchStar_kind:
923
291
            break;
924
8.45k
        case MatchAs_kind:
925
8.45k
            if (node_->v.MatchAs.pattern) {
926
78
                CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
927
78
            }
928
8.45k
            break;
929
8.45k
        case MatchOr_kind:
930
2.57k
            CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns);
931
2.57k
            break;
932
    // No default case, so the compiler will emit a warning if new pattern
933
    // kinds are added without being handled here
934
14.2k
    }
935
14.2k
    LEAVE_RECURSIVE();
936
14.2k
    return 1;
937
14.2k
}
938
939
static int
940
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
941
763
{
942
763
    CALL(astfold_pattern, expr_ty, node_->pattern);
943
763
    CALL_OPT(astfold_expr, expr_ty, node_->guard);
944
763
    CALL_SEQ(astfold_stmt, stmt, node_->body);
945
763
    return 1;
946
763
}
947
948
static int
949
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
950
5.90k
{
951
5.90k
    switch (node_->kind) {
952
4.80k
        case TypeVar_kind:
953
4.80k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
954
4.80k
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
955
4.80k
            break;
956
433
        case ParamSpec_kind:
957
433
            CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value);
958
433
            break;
959
670
        case TypeVarTuple_kind:
960
670
            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value);
961
670
            break;
962
5.90k
    }
963
5.90k
    return 1;
964
5.90k
}
965
966
#undef CALL
967
#undef CALL_OPT
968
#undef CALL_SEQ
969
970
int
971
_PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
972
                  int ff_features, int syntax_check_only, int enable_warnings)
973
6.64k
{
974
6.64k
    _PyASTPreprocessState state;
975
6.64k
    memset(&state, 0, sizeof(_PyASTPreprocessState));
976
6.64k
    state.filename = filename;
977
6.64k
    state.optimize = optimize;
978
6.64k
    state.ff_features = ff_features;
979
6.64k
    state.syntax_check_only = syntax_check_only;
980
6.64k
    state.enable_warnings = enable_warnings;
981
6.64k
    if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) {
982
0
        return -1;
983
0
    }
984
985
6.64k
    int ret = astfold_mod(mod, arena, &state);
986
6.64k
    assert(ret || PyErr_Occurred());
987
988
6.64k
    _Py_CArray_Fini(&state.cf_finally);
989
6.64k
    return ret;
990
6.64k
}