Coverage Report

Created: 2025-11-02 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/lexer/state.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_pystate.h"
3
#include "pycore_token.h"
4
#include "errcode.h"
5
6
#include "state.h"
7
8
/* Never change this */
9
21.2k
#define TABSIZE 8
10
11
/* Create and initialize a new tok_state structure */
12
struct tok_state *
13
_PyTokenizer_tok_new(void)
14
21.2k
{
15
21.2k
    struct tok_state *tok = (struct tok_state *)PyMem_Calloc(
16
21.2k
                                            1,
17
21.2k
                                            sizeof(struct tok_state));
18
21.2k
    if (tok == NULL)
19
0
        return NULL;
20
21.2k
    tok->buf = tok->cur = tok->inp = NULL;
21
21.2k
    tok->fp_interactive = 0;
22
21.2k
    tok->interactive_src_start = NULL;
23
21.2k
    tok->interactive_src_end = NULL;
24
21.2k
    tok->start = NULL;
25
21.2k
    tok->end = NULL;
26
21.2k
    tok->done = E_OK;
27
21.2k
    tok->fp = NULL;
28
21.2k
    tok->input = NULL;
29
21.2k
    tok->tabsize = TABSIZE;
30
21.2k
    tok->indent = 0;
31
21.2k
    tok->indstack[0] = 0;
32
21.2k
    tok->atbol = 1;
33
21.2k
    tok->pendin = 0;
34
21.2k
    tok->prompt = tok->nextprompt = NULL;
35
21.2k
    tok->lineno = 0;
36
21.2k
    tok->starting_col_offset = -1;
37
21.2k
    tok->col_offset = -1;
38
21.2k
    tok->level = 0;
39
21.2k
    tok->altindstack[0] = 0;
40
21.2k
    tok->decoding_state = STATE_INIT;
41
21.2k
    tok->decoding_erred = 0;
42
21.2k
    tok->enc = NULL;
43
21.2k
    tok->encoding = NULL;
44
21.2k
    tok->cont_line = 0;
45
21.2k
    tok->filename = NULL;
46
21.2k
    tok->decoding_readline = NULL;
47
21.2k
    tok->decoding_buffer = NULL;
48
21.2k
    tok->readline = NULL;
49
21.2k
    tok->type_comments = 0;
50
21.2k
    tok->interactive_underflow = IUNDERFLOW_NORMAL;
51
21.2k
    tok->underflow = NULL;
52
21.2k
    tok->str = NULL;
53
21.2k
    tok->report_warnings = 1;
54
21.2k
    tok->tok_extra_tokens = 0;
55
21.2k
    tok->comment_newline = 0;
56
21.2k
    tok->implicit_newline = 0;
57
21.2k
    tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .quote='\0', .quote_size = 0, .in_debug=0};
58
21.2k
    tok->tok_mode_stack_index = 0;
59
#ifdef Py_DEBUG
60
    tok->debug = _Py_GetConfig()->parser_debug;
61
#endif
62
21.2k
    return tok;
63
21.2k
}
64
65
static void
66
free_fstring_expressions(struct tok_state *tok)
67
21.2k
{
68
21.2k
    int index;
69
21.2k
    tokenizer_mode *mode;
70
71
46.5k
    for (index = tok->tok_mode_stack_index; index >= 0; --index) {
72
25.3k
        mode = &(tok->tok_mode_stack[index]);
73
25.3k
        if (mode->last_expr_buffer != NULL) {
74
3.93k
            PyMem_Free(mode->last_expr_buffer);
75
3.93k
            mode->last_expr_buffer = NULL;
76
3.93k
            mode->last_expr_size = 0;
77
3.93k
            mode->last_expr_end = -1;
78
3.93k
            mode->in_format_spec = 0;
79
3.93k
        }
80
25.3k
    }
81
21.2k
}
82
83
/* Free a tok_state structure */
84
void
85
_PyTokenizer_Free(struct tok_state *tok)
86
21.2k
{
87
21.2k
    if (tok->encoding != NULL) {
88
4.40k
        PyMem_Free(tok->encoding);
89
4.40k
    }
90
21.2k
    Py_XDECREF(tok->decoding_readline);
91
21.2k
    Py_XDECREF(tok->decoding_buffer);
92
21.2k
    Py_XDECREF(tok->readline);
93
21.2k
    Py_XDECREF(tok->filename);
94
21.2k
    if ((tok->readline != NULL || tok->fp != NULL ) && tok->buf != NULL) {
95
0
        PyMem_Free(tok->buf);
96
0
    }
97
21.2k
    if (tok->input) {
98
21.2k
        PyMem_Free(tok->input);
99
21.2k
    }
100
21.2k
    if (tok->interactive_src_start != NULL) {
101
0
        PyMem_Free(tok->interactive_src_start);
102
0
    }
103
21.2k
    free_fstring_expressions(tok);
104
21.2k
    PyMem_Free(tok);
105
21.2k
}
106
107
void
108
7.99k
_PyToken_Free(struct token *token) {
109
7.99k
    Py_XDECREF(token->metadata);
110
7.99k
}
111
112
void
113
1.56M
_PyToken_Init(struct token *token) {
114
1.56M
    token->metadata = NULL;
115
1.56M
}
116
117
int
118
_PyLexer_type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset,
119
                         int end_col_offset, const char *start, const char *end)
120
0
{
121
0
    token->level = tok->level;
122
0
    token->lineno = token->end_lineno = tok->lineno;
123
0
    token->col_offset = col_offset;
124
0
    token->end_col_offset = end_col_offset;
125
0
    token->start = start;
126
0
    token->end = end;
127
0
    return type;
128
0
}
129
130
int
131
_PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end)
132
1.60M
{
133
1.60M
    assert((start == NULL && end == NULL) || (start != NULL && end != NULL));
134
1.60M
    token->level = tok->level;
135
1.60M
    if (ISSTRINGLIT(type)) {
136
81.8k
        token->lineno = tok->first_lineno;
137
81.8k
    }
138
1.52M
    else {
139
1.52M
        token->lineno = tok->lineno;
140
1.52M
    }
141
1.60M
    token->end_lineno = tok->lineno;
142
1.60M
    token->col_offset = token->end_col_offset = -1;
143
1.60M
    token->start = start;
144
1.60M
    token->end = end;
145
146
1.60M
    if (start != NULL && end != NULL) {
147
1.54M
        token->col_offset = tok->starting_col_offset;
148
1.54M
        token->end_col_offset = tok->col_offset;
149
1.54M
    }
150
1.60M
    return type;
151
1.60M
}