From bdef1cd0bb00251bf92e6a545bb6e0f2dc2814a5 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 2 Dec 2020 10:06:53 -0500 Subject: parse: Clean up debug flag parsing/printing --- ctx.c | 26 ++++++++++++++++++++++++++ ctx.h | 5 +++++ diag.c | 28 +--------------------------- parse.c | 59 ++++++++++++++++++++++------------------------------------- 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 #include #include #include #include +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 @@ -47,6 +47,11 @@ enum debug_flags { DEBUG_ALL = (1 << 7) - 1, }; +/** + * Convert a debug flag to a string. + */ +const char *debug_flag_name(enum debug_flags flag); + /** * The execution context for bfs. */ 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, ","); } -- cgit v1.2.3