Skip to content

Commit

Permalink
Sanitize macros and debug functions in pegen.c (GH-25291)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal committed Apr 9, 2021
1 parent 5436695 commit 58bafe4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Parser/pegen.c
Expand Up @@ -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.
Expand Down Expand Up @@ -761,6 +763,7 @@ _PyPegen_get_memo_statistics()
}
return ret;
}
#endif

int // bool
_PyPegen_is_memoized(Parser *p, int type, void *pres)
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;\
Expand Down
4 changes: 3 additions & 1 deletion Parser/pegen.h
Expand Up @@ -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);
Expand Down Expand Up @@ -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__)
Expand Down
8 changes: 8 additions & 0 deletions Tools/peg_generator/peg_extension/peg_extension.c
Expand Up @@ -109,20 +109,27 @@ parse_string(PyObject *self, PyObject *args, PyObject *kwds)
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;
Expand All @@ -139,6 +146,7 @@ dump_memo_stats(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
}
}
Py_DECREF(list);
#endif
Py_RETURN_NONE;
}

Expand Down

0 comments on commit 58bafe4

Please sign in to comment.