From: Stephen Dolan Date: Sun, 5 May 2013 22:12:10 +0000 (+0100) Subject: Remove JQ_DEBUG #define and jq_test binary, simplifying build. X-Git-Tag: jq-1.3~35 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1741d8c1618c1826628c9deb70f3876ed22501f3;p=jq Remove JQ_DEBUG #define and jq_test binary, simplifying build. The debugging features previously available via JQ_DEBUG are now command-line options. --- diff --git a/Makefile b/Makefile index 45f8ea7..9b3b663 100644 --- a/Makefile +++ b/Makefile @@ -26,19 +26,15 @@ main.c: version.gen.h JQ_SRC=parser.gen.c lexer.gen.c opcode.c bytecode.c compile.c execute.c builtin.c jv.c jv_parse.c jv_print.c jv_dtoa.c jv_unicode.c jv_aux.c jv_alloc.c -jq_test: CFLAGS += -DJQ_DEBUG=1 -jq_test: $(JQ_SRC) main.c jq_test.c - $(CC) $(CFLAGS) $(CFLAGS_DEBUG) -o $@ $^ -jq: CFLAGS += -O -DJQ_DEBUG=0 jq: $(JQ_SRC) main.c jq_test.c $(CC) $(CFLAGS) $(CFLAGS_OPT) -o $@ $^ -test: jq_test - valgrind --error-exitcode=1 -q --leak-check=full ./jq_test --run-tests >/dev/null +test: jq + valgrind --error-exitcode=1 -q --leak-check=full ./jq --run-tests >/dev/null LIBRARIES=libjq -BINARIES=jq jq_test +BINARIES=jq PLATFORMS=linux32 linux64 osx32 osx64 win32 win64 build/linux32%: CC='x86_64-linux-gnu-gcc -m32' diff --git a/execute.c b/execute.c index 7f1dea9..22ffadb 100644 --- a/execute.c +++ b/execute.c @@ -29,6 +29,7 @@ struct jq_state { struct forkable_stack fork_stk; jv* pathbuf; int pathsize; // number of allocated elements + int debug_trace_enabled; }; int path_push(jq_state *jq, stackval sv, jv val) { @@ -162,29 +163,29 @@ jv jq_next(jq_state *jq) { while (1) { uint16_t opcode = *pc; -#if JQ_DEBUG - dump_operation(frame_current_bytecode(&jq->frame_stk), pc); - printf("\t"); - const struct opcode_description* opdesc = opcode_describe(opcode); - data_stk_elem* param; - int stack_in = opdesc->stack_in; - if (stack_in == -1) stack_in = pc[1]; - for (int i=0; idata_stk); - } else { - printf(" | "); - param = forkable_stack_peek_next(&jq->data_stk, param); + if (jq->debug_trace_enabled) { + dump_operation(frame_current_bytecode(&jq->frame_stk), pc); + printf("\t"); + const struct opcode_description* opdesc = opcode_describe(opcode); + data_stk_elem* param; + int stack_in = opdesc->stack_in; + if (stack_in == -1) stack_in = pc[1]; + for (int i=0; idata_stk); + } else { + printf(" | "); + param = forkable_stack_peek_next(&jq->data_stk, param); + } + if (!param) break; + jv_dump(jv_copy(param->sv.value), 0); + printf("<%d>", jv_get_refcnt(param->sv.value)); } - if (!param) break; - jv_dump(jv_copy(param->sv.value), 0); - printf("<%d>", jv_get_refcnt(param->sv.value)); - } - if (backtracking) printf("\t"); + if (backtracking) printf("\t"); - printf("\n"); -#endif + printf("\n"); + } if (backtracking) { opcode = ON_BACKTRACK(opcode); backtracking = 0; @@ -265,11 +266,11 @@ jv jq_next(jq_state *jq) { uint16_t v = *pc++; frame_ptr fp = frame_get_level(&jq->frame_stk, frame_current(&jq->frame_stk), level); jv* var = frame_local_var(fp, v); - #if JQ_DEBUG - printf("V%d = ", v); - jv_dump(jv_copy(*var), 0); - printf("\n"); - #endif + if (jq->debug_trace_enabled) { + printf("V%d = ", v); + jv_dump(jv_copy(*var), 0); + printf("\n"); + } stack_push(jq, stackval_replace(stack_pop(jq), jv_copy(*var))); break; } @@ -280,11 +281,11 @@ jv jq_next(jq_state *jq) { frame_ptr fp = frame_get_level(&jq->frame_stk, frame_current(&jq->frame_stk), level); jv* var = frame_local_var(fp, v); stackval val = stack_pop(jq); - #if JQ_DEBUG - printf("V%d = ", v); - jv_dump(jv_copy(val.value), 0); - printf("\n"); - #endif + if (jq->debug_trace_enabled) { + printf("V%d = ", v); + jv_dump(jv_copy(val.value), 0); + printf("\n"); + } jv_free(*var); *var = val.value; break; @@ -463,7 +464,7 @@ jv jq_next(jq_state *jq) { } -void jq_init(struct bytecode* bc, jv input, jq_state **jq) { +void jq_init(struct bytecode* bc, jv input, jq_state **jq, int flags) { jq_state *new_jq; new_jq = jv_mem_alloc(sizeof(*new_jq)); memset(new_jq, 0, sizeof(*new_jq)); @@ -475,6 +476,11 @@ void jq_init(struct bytecode* bc, jv input, jq_state **jq) { struct closure top = {bc, -1}; frame_push(&new_jq->frame_stk, top, 0); frame_push_backtrack(&new_jq->frame_stk, bc->code); + if (flags & JQ_DEBUG_TRACE) { + new_jq->debug_trace_enabled = 1; + } else { + new_jq->debug_trace_enabled = 0; + } *jq = new_jq; } diff --git a/execute.h b/execute.h index 77509f2..1cf14df 100644 --- a/execute.h +++ b/execute.h @@ -6,8 +6,9 @@ struct bytecode* jq_compile(const char* str); typedef struct jq_state jq_state; +enum {JQ_DEBUG_TRACE = 1}; -void jq_init(struct bytecode* bc, jv value, jq_state **); +void jq_init(struct bytecode* bc, jv value, jq_state **, int flags); jv jq_next(jq_state *); void jq_teardown(jq_state **); diff --git a/jq_test.c b/jq_test.c index 5abd027..64b88fb 100644 --- a/jq_test.c +++ b/jq_test.c @@ -59,7 +59,7 @@ static void run_jq_tests() { fgets(buf, sizeof(buf), testdata); jv input = jv_parse(buf); if (!jv_is_valid(input)){ invalid++; continue; } - jq_init(bc, input, &jq); + jq_init(bc, input, &jq, JQ_DEBUG_TRACE); while (fgets(buf, sizeof(buf), testdata)) { if (skipline(buf)) break; diff --git a/jv_alloc.c b/jv_alloc.c index e127029..4fd36a6 100644 --- a/jv_alloc.c +++ b/jv_alloc.c @@ -27,7 +27,7 @@ void* jv_mem_realloc(void* p, size_t sz) { return p; } -#if JQ_DEBUG +#ifndef NDEBUG volatile char jv_mem_uninitialised; __attribute__((constructor)) void jv_mem_uninit_setup(){ char* p = malloc(1); diff --git a/jv_alloc.h b/jv_alloc.h index b4e72c0..9cf1d34 100644 --- a/jv_alloc.h +++ b/jv_alloc.h @@ -3,12 +3,12 @@ #include -#if JQ_DEBUG +#ifndef NDEBUG extern volatile char jv_mem_uninitialised; #endif static void jv_mem_invalidate(void* mem, size_t n) { -#if JQ_DEBUG +#ifndef NDEBUG char* m = mem; while (n--) *m++ ^= jv_mem_uninitialised ^ jv_mem_uninitialised; #endif diff --git a/main.c b/main.c index 7115016..d2c553c 100644 --- a/main.c +++ b/main.c @@ -64,9 +64,9 @@ enum { static int options = 0; static struct bytecode* bc; -static void process(jv value) { +static void process(jv value, int flags) { jq_state *jq = NULL; - jq_init(bc, value, &jq); + jq_init(bc, value, &jq, flags); jv result; while (jv_is_valid(result = jq_next(jq))) { if ((options & RAW_OUTPUT) && jv_get_kind(result) == JV_KIND_STRING) { @@ -153,6 +153,7 @@ int main(int argc, char* argv[]) { input_filenames = jv_mem_alloc(sizeof(const char*) * argc); ninput_files = 0; int further_args_are_files = 0; + int jq_flags = 0; for (int i=1; i