Sanitize macros and debug functions in pegen.c (GH-25291)

This commit is contained in:
Pablo Galindo 2021-04-09 01:17:31 +01:00 committed by GitHub
parent 5436695363
commit 58bafe42ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View File

@ -725,6 +725,8 @@ _PyPegen_fill_token(Parser *p)
return 0;
}
#if defined(Py_DEBUG)
// Instrumentation to count the effectiveness of memoization.
// The array counts the number of tokens skipped by memoization,
// indexed by type.
@ -761,6 +763,7 @@ _PyPegen_get_memo_statistics()
}
return ret;
}
#endif
int // bool
_PyPegen_is_memoized(Parser *p, int type, void *pres)
@ -776,6 +779,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
for (Memo *m = t->memo; m != NULL; m = m->next) {
if (m->type == type) {
#if defined(PY_DEBUG)
if (0 <= type && type < NSTATISTICS) {
long count = m->mark - p->mark;
// A memoized negative result counts for one.
@ -784,6 +788,7 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres)
}
memo_statistics[type] += count;
}
#endif
p->mark = m->mark;
*(void **)(pres) = m->node;
return 1;
@ -2286,9 +2291,9 @@ _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type)
}
#define VISIT_CONTAINER(CONTAINER, TYPE) do { \
Py_ssize_t len = asdl_seq_LEN(CONTAINER->v.TYPE.elts);\
Py_ssize_t len = asdl_seq_LEN((CONTAINER)->v.TYPE.elts);\
for (Py_ssize_t i = 0; i < len; i++) {\
expr_ty other = asdl_seq_GET(CONTAINER->v.TYPE.elts, i);\
expr_ty other = asdl_seq_GET((CONTAINER)->v.TYPE.elts, i);\
expr_ty child = _PyPegen_get_invalid_target(other, targets_type);\
if (child != NULL) {\
return child;\

View File

@ -107,8 +107,10 @@ typedef struct {
int is_keyword;
} KeywordOrStarred;
#if defined(Py_DEBUG)
void _PyPegen_clear_memo_statistics(void);
PyObject *_PyPegen_get_memo_statistics(void);
#endif
int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
@ -150,7 +152,7 @@ RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
#define UNUSED(expr) do { (void)(expr); } while (0)
#define EXTRA_EXPR(head, tail) head->lineno, head->col_offset, tail->end_lineno, tail->end_col_offset, p->arena
#define EXTRA_EXPR(head, tail) head->lineno, (head)->col_offset, (tail)->end_lineno, (tail)->end_col_offset, p->arena
#define EXTRA _start_lineno, _start_col_offset, _end_lineno, _end_col_offset, p->arena
#define RAISE_SYNTAX_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, msg, ##__VA_ARGS__)
#define RAISE_INDENTATION_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_IndentationError, msg, ##__VA_ARGS__)

View File

@ -109,20 +109,27 @@ error:
static PyObject *
clear_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
#if defined(PY_DEBUG)
_PyPegen_clear_memo_statistics();
#endif
Py_RETURN_NONE;
}
static PyObject *
get_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
#if defined(PY_DEBUG)
return _PyPegen_get_memo_statistics();
#else
Py_RETURN_NONE;
#endif
}
// TODO: Write to Python's sys.stdout instead of C's stdout.
static PyObject *
dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
{
#if defined(PY_DEBUG)
PyObject *list = _PyPegen_get_memo_statistics();
if (list == NULL) {
return NULL;
@ -139,6 +146,7 @@ dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
}
}
Py_DECREF(list);
#endif
Py_RETURN_NONE;
}