]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Output more details in the re tracing (GH-111357)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 26 Oct 2023 13:42:42 +0000 (16:42 +0300)
committerGitHub <noreply@github.com>
Thu, 26 Oct 2023 13:42:42 +0000 (16:42 +0300)
Modules/_sre/sre.c
Modules/_sre/sre_lib.h

index 0f134b194de6f67bb832a4c1ba7f7eb5ced6edd4..d451974b9cf81e6de5e97ea3e7eae45f20cd222f 100644 (file)
@@ -107,9 +107,11 @@ 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;       \
@@ -117,6 +119,7 @@ static unsigned int sre_toupper(unsigned int ch) {
     } 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
index 92dd725c70fd3891d08f2db3ff8902255200cfbd..f5497d9ff2b93fdbefd3956cb9af889677a7be45 100644 (file)
@@ -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)
@@ -449,8 +462,27 @@ 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)
@@ -458,16 +490,19 @@ do { \
     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
@@ -1150,11 +1185,11 @@ dispatch:
                 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);
@@ -1235,11 +1270,11 @@ dispatch:
 
             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;