From 8e0c1babc95ad3981259af20d3b7269062ed167f Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 14 Jan 2024 05:43:51 +0900 Subject: [PATCH 1/4] Define MARK_VAR_AS_USED in generate code Signed-off-by: Masatake YAMATO --- src/packcc.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/packcc.c b/src/packcc.c index 3ce3b28..7bb1aa7 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3365,6 +3365,11 @@ static bool_t generate(context_t *ctx) { "#else\n" "#define MARK_FUNC_AS_USED __attribute__((__unused__))\n" "#endif\n" + "#ifdef _MSC_VER\n" + "#define MARK_VAR_AS_USED\n" + "#else\n" + "#define MARK_VAR_AS_USED __attribute__((__unused__))\n" + "#endif\n" "\n" "#ifndef PCC_BUFFER_MIN_SIZE\n" "#define PCC_BUFFER_MIN_SIZE 256\n" From 01090d103491492941e6d1aefb216f90b6d54912 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 14 Jan 2024 05:45:00 +0900 Subject: [PATCH 2/4] Put MARK_VAR_AS_USED to some parameters in generated code The parameters are ununused at least calc.c. Signed-off-by: Masatake YAMATO --- src/packcc.c | 78 ++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 7bb1aa7..29d41c8 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -3671,7 +3671,7 @@ static bool_t generate(context_t *ctx) { "#define PCC_DEBUG(auxil, event, rule, level, pos, buffer, length) ((void)0)\n" "#endif /* !PCC_DEBUG */\n" "\n" - "static char *pcc_strndup_e(pcc_auxil_t auxil, const char *str, size_t len) {\n" + "static char *pcc_strndup_e(pcc_auxil_t auxil MARK_VAR_AS_USED, const char *str, size_t len) {\n" " const size_t m = strnlen(str, len);\n" " char *const s = (char *)PCC_MALLOC(auxil, m + 1);\n" " memcpy(s, str, m);\n" @@ -3682,13 +3682,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_char_array__init(pcc_auxil_t auxil, pcc_char_array_t *array) {\n" + "static void pcc_char_array__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_char_array_t *array) {\n" " array->len = 0;\n" " array->max = 0;\n" " array->buf = NULL;\n" "}\n" "\n" - "static void pcc_char_array__add(pcc_auxil_t auxil, pcc_char_array_t *array, char ch) {\n" + "static void pcc_char_array__add(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_char_array_t *array, char ch) {\n" " if (array->max <= array->len) {\n" " const size_t n = array->len + 1;\n" " size_t m = array->max;\n" @@ -3701,21 +3701,21 @@ static bool_t generate(context_t *ctx) { " array->buf[array->len++] = ch;\n" "}\n" "\n" - "static void pcc_char_array__term(pcc_auxil_t auxil, pcc_char_array_t *array) {\n" + "static void pcc_char_array__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_char_array_t *array) {\n" " PCC_FREE(auxil, array->buf);\n" "}\n" "\n" ); stream__puts( &sstream, - "static void pcc_value_table__init(pcc_auxil_t auxil, pcc_value_table_t *table) {\n" + "static void pcc_value_table__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_table_t *table) {\n" " table->len = 0;\n" " table->max = 0;\n" " table->buf = NULL;\n" "}\n" "\n" "MARK_FUNC_AS_USED\n" - "static void pcc_value_table__resize(pcc_auxil_t auxil, pcc_value_table_t *table, size_t len) {\n" + "static void pcc_value_table__resize(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_table_t *table, size_t len) {\n" " if (table->max < len) {\n" " size_t m = table->max;\n" " if (m == 0) m = PCC_ARRAY_MIN_SIZE;\n" @@ -3728,24 +3728,24 @@ static bool_t generate(context_t *ctx) { "}\n" "\n" "MARK_FUNC_AS_USED\n" - "static void pcc_value_table__clear(pcc_auxil_t auxil, pcc_value_table_t *table) {\n" + "static void pcc_value_table__clear(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_table_t *table) {\n" " memset(table->buf, 0, sizeof(pcc_value_t) * table->len);\n" "}\n" "\n" - "static void pcc_value_table__term(pcc_auxil_t auxil, pcc_value_table_t *table) {\n" + "static void pcc_value_table__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_table_t *table) {\n" " PCC_FREE(auxil, table->buf);\n" "}\n" "\n" ); stream__puts( &sstream, - "static void pcc_value_refer_table__init(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {\n" + "static void pcc_value_refer_table__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_refer_table_t *table) {\n" " table->len = 0;\n" " table->max = 0;\n" " table->buf = NULL;\n" "}\n" "\n" - "static void pcc_value_refer_table__resize(pcc_auxil_t auxil, pcc_value_refer_table_t *table, size_t len) {\n" + "static void pcc_value_refer_table__resize(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_refer_table_t *table, size_t len) {\n" " size_t i;\n" " if (table->max < len) {\n" " size_t m = table->max;\n" @@ -3759,21 +3759,21 @@ static bool_t generate(context_t *ctx) { " table->len = len;\n" "}\n" "\n" - "static void pcc_value_refer_table__term(pcc_auxil_t auxil, pcc_value_refer_table_t *table) {\n" + "static void pcc_value_refer_table__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_value_refer_table_t *table) {\n" " PCC_FREE(auxil, table->buf);\n" "}\n" "\n" ); stream__puts( &sstream, - "static void pcc_capture_table__init(pcc_auxil_t auxil, pcc_capture_table_t *table) {\n" + "static void pcc_capture_table__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_table_t *table) {\n" " table->len = 0;\n" " table->max = 0;\n" " table->buf = NULL;\n" "}\n" "\n" "MARK_FUNC_AS_USED\n" - "static void pcc_capture_table__resize(pcc_auxil_t auxil, pcc_capture_table_t *table, size_t len) {\n" + "static void pcc_capture_table__resize(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_table_t *table, size_t len) {\n" " size_t i;\n" " for (i = len; i < table->len; i++) PCC_FREE(auxil, table->buf[i].string);\n" " if (table->max < len) {\n" @@ -3792,7 +3792,7 @@ static bool_t generate(context_t *ctx) { " table->len = len;\n" "}\n" "\n" - "static void pcc_capture_table__term(pcc_auxil_t auxil, pcc_capture_table_t *table) {\n" + "static void pcc_capture_table__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_table_t *table) {\n" " while (table->len > 0) {\n" " table->len--;\n" " PCC_FREE(auxil, table->buf[table->len].string);\n" @@ -3803,13 +3803,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_capture_const_table__init(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {\n" + "static void pcc_capture_const_table__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_const_table_t *table) {\n" " table->len = 0;\n" " table->max = 0;\n" " table->buf = NULL;\n" "}\n" "\n" - "static void pcc_capture_const_table__resize(pcc_auxil_t auxil, pcc_capture_const_table_t *table, size_t len) {\n" + "static void pcc_capture_const_table__resize(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_const_table_t *table, size_t len) {\n" " size_t i;\n" " if (table->max < len) {\n" " size_t m = table->max;\n" @@ -3823,7 +3823,7 @@ static bool_t generate(context_t *ctx) { " table->len = len;\n" "}\n" "\n" - "static void pcc_capture_const_table__term(pcc_auxil_t auxil, pcc_capture_const_table_t *table) {\n" + "static void pcc_capture_const_table__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_capture_const_table_t *table) {\n" " PCC_FREE(auxil, (void *)table->buf);\n" "}\n" "\n" @@ -3845,7 +3845,7 @@ static bool_t generate(context_t *ctx) { " return thunk;\n" "}\n" "\n" - "static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n" + "static pcc_thunk_t *pcc_thunk__create_node(pcc_auxil_t auxil MARK_VAR_AS_USED, const pcc_thunk_array_t *thunks, pcc_value_t *value) {\n" " pcc_thunk_t *const thunk = (pcc_thunk_t *)PCC_MALLOC(auxil, sizeof(pcc_thunk_t));\n" " thunk->type = PCC_THUNK_NODE;\n" " thunk->data.node.thunks = thunks;\n" @@ -3872,13 +3872,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_thunk_array__init(pcc_auxil_t auxil, pcc_thunk_array_t *array) {\n" + "static void pcc_thunk_array__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_thunk_array_t *array) {\n" " array->len = 0;\n" " array->max = 0;\n" " array->buf = NULL;\n" "}\n" "\n" - "static void pcc_thunk_array__add(pcc_auxil_t auxil, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {\n" + "static void pcc_thunk_array__add(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_thunk_array_t *array, pcc_thunk_t *thunk) {\n" " if (array->max <= array->len) {\n" " const size_t n = array->len + 1;\n" " size_t m = array->max;\n" @@ -3909,13 +3909,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_memory_recycler__init(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, size_t element_size) {\n" + "static void pcc_memory_recycler__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_memory_recycler_t *recycler, size_t element_size) {\n" " recycler->pool_list = NULL;\n" " recycler->entry_list = NULL;\n" " recycler->element_size = element_size;\n" "}\n" "\n" - "static void *pcc_memory_recycler__supply(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" + "static void *pcc_memory_recycler__supply(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_memory_recycler_t *recycler) {\n" " if (recycler->entry_list) {\n" " pcc_memory_entry_t *const tmp = recycler->entry_list;\n" " recycler->entry_list = tmp->next;\n" @@ -3941,13 +3941,13 @@ static bool_t generate(context_t *ctx) { " return (char *)recycler->pool_list + sizeof(pcc_memory_pool_t) + recycler->element_size * recycler->pool_list->unused;\n" "}\n" "\n" - "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler, void *ptr) {\n" + "static void pcc_memory_recycler__recycle(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_memory_recycler_t *recycler, void *ptr) {\n" " pcc_memory_entry_t *const tmp = (pcc_memory_entry_t *)ptr;\n" " tmp->next = recycler->entry_list;\n" " recycler->entry_list = tmp;\n" "}\n" "\n" - "static void pcc_memory_recycler__term(pcc_auxil_t auxil, pcc_memory_recycler_t *recycler) {\n" + "static void pcc_memory_recycler__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_memory_recycler_t *recycler) {\n" " while (recycler->pool_list) {\n" " pcc_memory_pool_t *const tmp = recycler->pool_list;\n" " recycler->pool_list = tmp->next;\n" @@ -3979,13 +3979,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_rule_set__init(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n" + "static void pcc_rule_set__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_rule_set_t *set) {\n" " set->len = 0;\n" " set->max = 0;\n" " set->buf = NULL;\n" "}\n" "\n" - "static size_t pcc_rule_set__index(pcc_auxil_t auxil, const pcc_rule_set_t *set, pcc_rule_t rule) {\n" + "static size_t pcc_rule_set__index(pcc_auxil_t auxil MARK_VAR_AS_USED, const pcc_rule_set_t *set, pcc_rule_t rule) {\n" " size_t i;\n" " for (i = 0; i < set->len; i++) {\n" " if (set->buf[i] == rule) return i;\n" @@ -4016,7 +4016,7 @@ static bool_t generate(context_t *ctx) { " return PCC_TRUE;\n" "}\n" "\n" - "static void pcc_rule_set__clear(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n" + "static void pcc_rule_set__clear(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_rule_set_t *set) {\n" " set->len = 0;\n" "}\n" "\n" @@ -4028,7 +4028,7 @@ static bool_t generate(context_t *ctx) { " }\n" "}\n" "\n" - "static void pcc_rule_set__term(pcc_auxil_t auxil, pcc_rule_set_t *set) {\n" + "static void pcc_rule_set__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_rule_set_t *set) {\n" " PCC_FREE(auxil, set->buf);\n" "}\n" "\n" @@ -4115,13 +4115,13 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_lr_memo_map__init(pcc_auxil_t auxil, pcc_lr_memo_map_t *map) {\n" + "static void pcc_lr_memo_map__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_memo_map_t *map) {\n" " map->len = 0;\n" " map->max = 0;\n" " map->buf = NULL;\n" "}\n" "\n" - "static size_t pcc_lr_memo_map__index(pcc_context_t *ctx, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" + "static size_t pcc_lr_memo_map__index(pcc_context_t *ctx MARK_VAR_AS_USED, pcc_lr_memo_map_t *map, pcc_rule_t rule) {\n" " size_t i;\n" " for (i = 0; i < map->len; i++) {\n" " if (map->buf[i].rule == rule) return i;\n" @@ -4187,7 +4187,7 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static void pcc_lr_table__init(pcc_auxil_t auxil, pcc_lr_table_t *table) {\n" + "static void pcc_lr_table__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_table_t *table) {\n" " table->ofs = 0;\n" " table->len = 0;\n" " table->max = 0;\n" @@ -4239,7 +4239,7 @@ static bool_t generate(context_t *ctx) { " table->buf[index]->hold_a = answer;\n" "}\n" "\n" - "static pcc_lr_head_t *pcc_lr_table__get_head(pcc_context_t *ctx, pcc_lr_table_t *table, size_t index) {\n" + "static pcc_lr_head_t *pcc_lr_table__get_head(pcc_context_t *ctx MARK_VAR_AS_USED, pcc_lr_table_t *table, size_t index) {\n" " index += table->ofs;\n" " if (index >= table->len || table->buf[index] == NULL) return NULL;\n" " return table->buf[index]->head;\n" @@ -4273,7 +4273,7 @@ static bool_t generate(context_t *ctx) { ); stream__puts( &sstream, - "static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil, pcc_rule_t rule) {\n" + "static pcc_lr_entry_t *pcc_lr_entry__create(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_rule_t rule) {\n" " pcc_lr_entry_t *const lr = (pcc_lr_entry_t *)PCC_MALLOC(auxil, sizeof(pcc_lr_entry_t));\n" " lr->rule = rule;\n" " lr->seed = NULL;\n" @@ -4281,20 +4281,20 @@ static bool_t generate(context_t *ctx) { " return lr;\n" "}\n" "\n" - "static void pcc_lr_entry__destroy(pcc_auxil_t auxil, pcc_lr_entry_t *lr) {\n" + "static void pcc_lr_entry__destroy(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_entry_t *lr) {\n" " PCC_FREE(auxil, lr);\n" "}\n" "\n" ); stream__puts( &sstream, - "static void pcc_lr_stack__init(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {\n" + "static void pcc_lr_stack__init(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_stack_t *stack) {\n" " stack->len = 0;\n" " stack->max = 0;\n" " stack->buf = NULL;\n" "}\n" "\n" - "static void pcc_lr_stack__push(pcc_auxil_t auxil, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {\n" + "static void pcc_lr_stack__push(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_stack_t *stack, pcc_lr_entry_t *lr) {\n" " if (stack->max <= stack->len) {\n" " const size_t n = stack->len + 1;\n" " size_t m = stack->max;\n" @@ -4307,11 +4307,11 @@ static bool_t generate(context_t *ctx) { " stack->buf[stack->len++] = lr;\n" "}\n" "\n" - "static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {\n" + "static pcc_lr_entry_t *pcc_lr_stack__pop(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_stack_t *stack) {\n" " return stack->buf[--stack->len];\n" "}\n" "\n" - "static void pcc_lr_stack__term(pcc_auxil_t auxil, pcc_lr_stack_t *stack) {\n" + "static void pcc_lr_stack__term(pcc_auxil_t auxil MARK_VAR_AS_USED, pcc_lr_stack_t *stack) {\n" " PCC_FREE(auxil, stack->buf);\n" "}\n" "\n" @@ -4594,7 +4594,7 @@ static bool_t generate(context_t *ctx) { } stream__printf( &sstream, - "static void pcc_action_%s_" FMT_LU "(%s_context_t *__pcc_ctx, pcc_thunk_t *__pcc_in, pcc_value_t *__pcc_out) {\n", + "static void pcc_action_%s_" FMT_LU "(%s_context_t *__pcc_ctx MARK_VAR_AS_USED, pcc_thunk_t *__pcc_in MARK_VAR_AS_USED, pcc_value_t *__pcc_out MARK_VAR_AS_USED) {\n", r->name, (ulong_t)d, get_prefix(ctx) ); stream__puts( From 005141610afccab493eef8121b68f2c27388f237 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 14 Jan 2024 05:58:50 +0900 Subject: [PATCH 3/4] Don't use -Wno-unused-parameter when compiling generated code Signed-off-by: Masatake YAMATO --- build/gcc/Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/build/gcc/Makefile b/build/gcc/Makefile index e3a39c0..36cdabe 100644 --- a/build/gcc/Makefile +++ b/build/gcc/Makefile @@ -1,6 +1,9 @@ CC=gcc -CFLAGS_D=-std=gnu89 -fsigned-char -Wall -Wextra -Wno-unused-parameter -Wno-overlength-strings -pedantic -O0 -g2 -CFLAGS_R=-std=gnu89 -fsigned-char -Wall -Wextra -Wno-unused-parameter -Wno-overlength-strings -pedantic -O2 -DNDEBUG +CFLAGS_0=-std=gnu89 -fsigned-char -Wall -Wextra -Wno-overlength-strings -pedantic +CFLAGS_D= $(CFLAGS_0) -Wno-unused-parameter -O0 -g2 +CFLAGS_EXAMPLE_D=$(CFLAGS_0) -O0 -g2 +CFLAGS_R= $(CFLAGS_0) -Wno-unused-parameter -O2 -DNDEBUG +CFLAGS_EXAMPLE_R=$(CFLAGS_0) -O2 -DNDEBUG LDFLAGS_D= LDFLAGS_R= From 7315406453286e724ff0a1768b640ab8c5b03ccf Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sun, 14 Jan 2024 06:32:46 +0900 Subject: [PATCH 4/4] Mark some local variables in generated code as "unused" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When building Universal Ctags, some local variables in code generated by packcc cause warnings: peg/kotlin.c: In function ‘pcc_evaluate_rule_classParameter’: peg/kotlin.c:3474:22: warning: unused variable ‘n’ [-Wunused-variable] 3474 | const size_t n = chunk->thunks.len; | ^ peg/kotlin.c:3473:22: warning: unused variable ‘p’ [-Wunused-variable] 3473 | const size_t p = ctx->cur; | ^ Signed-off-by: Masatake YAMATO --- src/packcc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/packcc.c b/src/packcc.c index 29d41c8..d73fa6c 100644 --- a/src/packcc.c +++ b/src/packcc.c @@ -2898,9 +2898,9 @@ static code_reach_t generate_quantifying_code(generate_t *gen, const node_t *exp indent += 4; } stream__write_characters(gen->stream, ' ', indent); - stream__puts(gen->stream, "const size_t p = ctx->cur;\n"); + stream__puts(gen->stream, "const size_t p MARK_VAR_AS_USED = ctx->cur;\n"); stream__write_characters(gen->stream, ' ', indent); - stream__puts(gen->stream, "const size_t n = chunk->thunks.len;\n"); + stream__puts(gen->stream, "const size_t n MARK_VAR_AS_USED = chunk->thunks.len;\n"); { const int l = ++gen->label; if (generate_code(gen, expr, l, indent, FALSE) != CODE_REACH__ALWAYS_SUCCEED) { @@ -3024,9 +3024,9 @@ static code_reach_t generate_alternative_code(generate_t *gen, const node_array_ indent += 4; } stream__write_characters(gen->stream, ' ', indent); - stream__puts(gen->stream, "const size_t p = ctx->cur;\n"); + stream__puts(gen->stream, "const size_t p MARK_VAR_AS_USED = ctx->cur;\n"); stream__write_characters(gen->stream, ' ', indent); - stream__puts(gen->stream, "const size_t n = chunk->thunks.len;\n"); + stream__puts(gen->stream, "const size_t n MARK_VAR_AS_USED = chunk->thunks.len;\n"); for (i = 0; i < nodes->len; i++) { const bool_t c = (i + 1 < nodes->len) ? TRUE : FALSE; const int l = ++gen->label;