diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2020-12-02 10:06:53 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2020-12-02 10:06:53 -0500 |
commit | bdef1cd0bb00251bf92e6a545bb6e0f2dc2814a5 (patch) | |
tree | 1d84e62abf5bfa01a04bca8b7305b78bb96880b2 | |
parent | bec07dfca5caefea28f20c7ce6c4a80c70cc6944 (diff) | |
download | bfs-bdef1cd0bb00251bf92e6a545bb6e0f2dc2814a5.tar.xz |
parse: Clean up debug flag parsing/printing
-rw-r--r-- | ctx.c | 26 | ||||
-rw-r--r-- | ctx.h | 5 | ||||
-rw-r--r-- | diag.c | 28 | ||||
-rw-r--r-- | parse.c | 59 |
4 files changed, 54 insertions, 64 deletions
@@ -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) { @@ -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 { @@ -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; @@ -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, ","); } |