Minor fixes to improve regex debugging code.
authorTom Lane <[email protected]>
Mon, 15 Feb 2021 00:53:28 +0000 (19:53 -0500)
committerTom Lane <[email protected]>
Mon, 15 Feb 2021 00:53:42 +0000 (19:53 -0500)
When REG_DEBUG is defined, ensure that an un-filled "struct cnfa"
is all-zeroes, not just that it has nstates == 0.  This is mainly
so that looking at "struct subre" structs in gdb doesn't distract
one with a lot of garbage fields during regex compilation.

Adjust some places that print debug output to have suitable fflush
calls afterwards.

In passing, correct an erroneous ancient comment: the concatenation
subre-s created by parsebranch() have op == '.' not ','.

Noted while fooling around with some regex performance improvements.

src/backend/regex/regc_nfa.c
src/backend/regex/regcomp.c
src/include/regex/regguts.h

index 92c9c4d795d1a4d32a4677c4e00e25123506b072..cde82625c85871bb90a0bba74522368fe326ad9d 100644 (file)
@@ -2951,11 +2951,11 @@ carc_cmp(const void *a, const void *b)
 static void
 freecnfa(struct cnfa *cnfa)
 {
-   assert(cnfa->nstates != 0); /* not empty already */
-   cnfa->nstates = 0;
+   assert(!NULLCNFA(*cnfa));   /* not empty already */
    FREE(cnfa->stflags);
    FREE(cnfa->states);
    FREE(cnfa->arcs);
+   ZAPCNFA(*cnfa);
 }
 
 /*
@@ -3012,13 +3012,13 @@ dumpstate(struct state *s,
        fprintf(f, "\tno out arcs\n");
    else
        dumparcs(s, f);
-   fflush(f);
    for (a = s->ins; a != NULL; a = a->inchain)
    {
        if (a->to != s)
            fprintf(f, "\tlink from %d to %d on %d's in-chain\n",
                    a->from->no, a->to->no, s->no);
    }
+   fflush(f);
 }
 
 /*
index 91078dcd806475670aa69de3014fb3c02a142437..f0896d2db14943c2300d0a7c906f8279af0aa059 100644 (file)
@@ -479,7 +479,10 @@ pg_regcomp(regex_t *re,
 
 #ifdef REG_DEBUG
    if (flags & REG_DUMP)
+   {
        dump(re, stdout);
+       fflush(stdout);
+   }
 #endif
 
    assert(v->err == 0);
@@ -721,7 +724,7 @@ parse(struct vars *v,
  *
  * This mostly manages concatenation, working closely with parseqatom().
  * Concatenated things are bundled up as much as possible, with separate
- * ',' nodes introduced only when necessary due to substructure.
+ * '.' nodes introduced only when necessary due to substructure.
  */
 static struct subre *
 parsebranch(struct vars *v,
index 5d0e7a961c920bee2e025d8b6bc8cb9922c16463..0a616562d03b6249c8da32590a39bc357e46110a 100644 (file)
@@ -368,7 +368,15 @@ struct cnfa
    struct carc *arcs;          /* the area for the lists */
 };
 
+/*
+ * When debugging, it's helpful if an un-filled CNFA is all-zeroes.
+ * In production, though, we only require nstates to be zero.
+ */
+#ifdef REG_DEBUG
+#define ZAPCNFA(cnfa)  memset(&(cnfa), 0, sizeof(cnfa))
+#else
 #define ZAPCNFA(cnfa)  ((cnfa).nstates = 0)
+#endif
 #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
 
 /*