summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-12-02 10:06:53 -0500
committerTavian Barnes <tavianator@tavianator.com>2020-12-02 10:06:53 -0500
commitbdef1cd0bb00251bf92e6a545bb6e0f2dc2814a5 (patch)
tree1d84e62abf5bfa01a04bca8b7305b78bb96880b2
parentbec07dfca5caefea28f20c7ce6c4a80c70cc6944 (diff)
downloadbfs-bdef1cd0bb00251bf92e6a545bb6e0f2dc2814a5.tar.xz
parse: Clean up debug flag parsing/printing
-rw-r--r--ctx.c26
-rw-r--r--ctx.h5
-rw-r--r--diag.c28
-rw-r--r--parse.c59
4 files changed, 54 insertions, 64 deletions
diff --git a/ctx.c b/ctx.c
index 26619d7..8f623e5 100644
--- a/ctx.c
+++ b/ctx.c
@@ -23,11 +23,37 @@
#include "pwcache.h"
#include "stat.h"
#include "trie.h"
+#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+const char *debug_flag_name(enum debug_flags flag) {
+ switch (flag) {
+ case DEBUG_COST:
+ return "cost";
+ case DEBUG_EXEC:
+ return "exec";
+ case DEBUG_OPT:
+ return "opt";
+ case DEBUG_RATES:
+ return "rates";
+ case DEBUG_SEARCH:
+ return "search";
+ case DEBUG_STAT:
+ return "stat";
+ case DEBUG_TREE:
+ return "tree";
+
+ case DEBUG_ALL:
+ break;
+ }
+
+ assert(false);
+ return "???";
+}
+
struct bfs_ctx *bfs_ctx_new(void) {
struct bfs_ctx *ctx = malloc(sizeof(*ctx));
if (!ctx) {
diff --git a/ctx.h b/ctx.h
index 39b3330..35eee76 100644
--- a/ctx.h
+++ b/ctx.h
@@ -48,6 +48,11 @@ enum debug_flags {
};
/**
+ * Convert a debug flag to a string.
+ */
+const char *debug_flag_name(enum debug_flags flag);
+
+/**
* The execution context for bfs.
*/
struct bfs_ctx {
diff --git a/diag.c b/diag.c
index bbf07e3..c2ed972 100644
--- a/diag.c
+++ b/diag.c
@@ -95,35 +95,9 @@ bool bfs_warning_prefix(const struct bfs_ctx *ctx) {
}
}
-static const char *debug_flag_str(enum debug_flags flag) {
- switch (flag) {
- case DEBUG_COST:
- return "cost";
- case DEBUG_EXEC:
- return "exec";
- case DEBUG_OPT:
- return "opt";
- case DEBUG_RATES:
- return "rates";
- case DEBUG_SEARCH:
- return "search";
- case DEBUG_STAT:
- return "stat";
- case DEBUG_TREE:
- return "tree";
-
- case DEBUG_ALL:
- assert(false);
- break;
- }
-
- assert(false);
- return "???";
-}
-
bool bfs_debug_prefix(const struct bfs_ctx *ctx, enum debug_flags flag) {
if (ctx->debug & flag) {
- cfprintf(ctx->cerr, "${bld}%s:${rs} ${cyn}-D %s${rs}: ", xbasename(ctx->argv[0]), debug_flag_str(flag));
+ cfprintf(ctx->cerr, "${bld}%s:${rs} ${cyn}-D %s${rs}: ", xbasename(ctx->argv[0]), debug_flag_name(flag));
return true;
} else {
return false;
diff --git a/parse.c b/parse.c
index aa4e175..571b652 100644
--- a/parse.c
+++ b/parse.c
@@ -736,25 +736,6 @@ static void debug_help(CFILE *cfile) {
cfprintf(cfile, " ${bld}all${rs}: All debug flags at once.\n");
}
-/** A named debug flag. */
-struct debug_flag {
- enum debug_flags flag;
- const char *name;
-};
-
-/** The table of debug flags. */
-struct debug_flag debug_flags[] = {
- {DEBUG_ALL, "all"},
- {DEBUG_COST, "cost"},
- {DEBUG_EXEC, "exec"},
- {DEBUG_OPT, "opt"},
- {DEBUG_RATES, "rates"},
- {DEBUG_SEARCH, "search"},
- {DEBUG_STAT, "stat"},
- {DEBUG_TREE, "tree"},
- {0},
-};
-
/** Check if a substring matches a debug flag. */
static bool parse_debug_flag(const char *flag, size_t len, const char *expected) {
if (len == strlen(expected)) {
@@ -792,22 +773,26 @@ static struct expr *parse_debug(struct parser_state *state, int arg1, int arg2)
debug_help(ctx->cout);
state->just_info = true;
return NULL;
+ } else if (parse_debug_flag(flag, len, "all")) {
+ ctx->debug = DEBUG_ALL;
+ continue;
}
- for (int i = 0; ; ++i) {
- const char *expected = debug_flags[i].name;
- if (!expected) {
- if (parse_warning(state, "Unrecognized debug flag ${bld}")) {
- fwrite(flag, 1, len, stderr);
- cfprintf(ctx->cerr, "${rs}.\n\n");
- unrecognized = true;
- }
+ enum debug_flags i;
+ for (i = 1; DEBUG_ALL & i; i <<= 1) {
+ const char *name = debug_flag_name(i);
+ if (parse_debug_flag(flag, len, name)) {
break;
}
+ }
- if (parse_debug_flag(flag, len, expected)) {
- ctx->debug |= debug_flags[i].flag;
- break;
+ if (DEBUG_ALL & i) {
+ ctx->debug |= i;
+ } else {
+ if (parse_warning(state, "Unrecognized debug flag ${bld}")) {
+ fwrite(flag, 1, len, stderr);
+ cfprintf(ctx->cerr, "${rs}.\n\n");
+ unrecognized = true;
}
}
}
@@ -3374,14 +3359,14 @@ void bfs_ctx_dump(const struct bfs_ctx *ctx, enum debug_flags flag) {
cfprintf(cerr, "${cyn}-S${rs} ${bld}%s${rs} ", strategy);
enum debug_flags debug = ctx->debug;
- if (debug) {
+ if (debug == DEBUG_ALL) {
+ cfprintf(cerr, "${cyn}-D${rs} ${bld}all${rs} ");
+ } else if (debug) {
cfprintf(cerr, "${cyn}-D${rs} ");
- for (int i = 0; debug; ++i) {
- enum debug_flags flag = debug_flags[i].flag;
- const char *name = debug_flags[i].name;
- if ((debug & flag) == flag) {
- cfprintf(cerr, "${bld}%s${rs}", name);
- debug ^= flag;
+ for (enum debug_flags i = 1; DEBUG_ALL & i; i <<= 1) {
+ if (debug & i) {
+ cfprintf(cerr, "${bld}%s${rs}", debug_flag_name(i));
+ debug ^= i;
if (debug) {
cfprintf(cerr, ",");
}