Skip to content

Output more details in the re tracing #111357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,19 @@ static unsigned int sre_toupper(unsigned int ch) {

#if VERBOSE == 0
# define INIT_TRACE(state)
# define DO_TRACE 0
# define TRACE(v)
#elif VERBOSE == 1
# define INIT_TRACE(state) int _debug = (state)->debug
# define DO_TRACE (_debug)
# define TRACE(v) do { \
if (_debug) { \
printf v; \
} \
} while (0)
#elif VERBOSE == 2
# define INIT_TRACE(state)
# define DO_TRACE 1
# define TRACE(v) printf v
#else
# error VERBOSE must be 0, 1 or 2
Expand Down
43 changes: 39 additions & 4 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount)
state->lastindex = ctx->lastindex; \
} while (0)

#define LAST_PTR_PUSH() \
do { \
TRACE(("push last_ptr: %zd", \
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
DATA_PUSH(&ctx->u.rep->last_ptr); \
} while (0)
#define LAST_PTR_POP() \
do { \
DATA_POP(&ctx->u.rep->last_ptr); \
TRACE(("pop last_ptr: %zd", \
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \
} while (0)

#define RETURN_ERROR(i) do { return i; } while(0)
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
Expand Down Expand Up @@ -449,25 +462,47 @@ do { \
#define DATA_LOOKUP_AT(t,p,pos) \
DATA_STACK_LOOKUP_AT(state,t,p,pos)

#define PTR_TO_INDEX(ptr) \
((ptr) ? ((char*)(ptr) - (char*)state->beginning) / state->charsize : -1)

#if VERBOSE
# define MARK_TRACE(label, lastmark) \
do if (DO_TRACE) { \
TRACE(("%s %d marks:", (label), (lastmark)+1)); \
for (int j = 0; j <= (lastmark); j++) { \
if (j && (j & 1) == 0) { \
TRACE((" ")); \
} \
TRACE((" %zd", PTR_TO_INDEX(state->mark[j]))); \
} \
TRACE(("\n")); \
} while (0)
#else
# define MARK_TRACE(label, lastmark)
#endif
#define MARK_PUSH(lastmark) \
do if (lastmark >= 0) { \
MARK_TRACE("push", (lastmark)); \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_PUSH(state, state->mark, _marks_size); \
} while (0)
#define MARK_POP(lastmark) \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP(state, state->mark, _marks_size, 1); \
MARK_TRACE("pop", (lastmark)); \
} while (0)
#define MARK_POP_KEEP(lastmark) \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP(state, state->mark, _marks_size, 0); \
MARK_TRACE("pop keep", (lastmark)); \
} while (0)
#define MARK_POP_DISCARD(lastmark) \
do if (lastmark >= 0) { \
size_t _marks_size = (lastmark+1) * sizeof(void*); \
DATA_STACK_POP_DISCARD(state, _marks_size); \
MARK_TRACE("pop discard", (lastmark)); \
} while (0)

#define JUMP_NONE 0
Expand Down Expand Up @@ -1150,11 +1185,11 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
LASTMARK_SAVE();
MARK_PUSH(ctx->lastmark);
/* zero-width match protection */
DATA_PUSH(&ctx->u.rep->last_ptr);
LAST_PTR_PUSH();
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
ctx->u.rep->pattern+3);
DATA_POP(&ctx->u.rep->last_ptr);
LAST_PTR_POP();
if (ret) {
MARK_POP_DISCARD(ctx->lastmark);
RETURN_ON_ERROR(ret);
Expand Down Expand Up @@ -1235,11 +1270,11 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

ctx->u.rep->count = ctx->count;
/* zero-width match protection */
DATA_PUSH(&ctx->u.rep->last_ptr);
LAST_PTR_PUSH();
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
ctx->u.rep->pattern+3);
DATA_POP(&ctx->u.rep->last_ptr);
LAST_PTR_POP();
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;
Expand Down