Skip to content

Generated code contains unused variables #99

@dolik-rce

Description

@dolik-rce

PackCC in some cases generates parser code with unused variables. This might trigger warnings/errors (depending on compiler options used) in code that uses the parser.

Simple grammar to reproduce the issue:

X <- ("A" / .*)+

When processed and compiled, it returns warning:

$ packcc -o parser test.peg
$ gcc -Wall -Werror  parser.c -o parser
parser.c: In function ‘pcc_evaluate_rule_X’:
parser.c:1224:26: error: unused variable ‘n’ [-Werror=unused-variable]
 1224 |             const size_t n = chunk->thunks.n;
      |                          ^
cc1: all warnings being treated as errors

The generated code looks like this:

static pcc_thunk_chunk_t *pcc_evaluate_rule_X(pcc_context_t *ctx) {
    pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx);
    chunk->pos = ctx->cur;
    PCC_DEBUG(ctx->auxil, PCC_DBG_EVALUATE, "X", ctx->level, chunk->pos, (ctx->buffer.p + chunk->pos), (ctx->buffer.n - chunk->pos));
    ctx->level++;
    {
        const size_t p0 = ctx->cur;
        const size_t n0 = chunk->thunks.n;
        int i;
        for (i = 0;; i++) {
            const size_t p = ctx->cur;
            const size_t n = chunk->thunks.n;
            {
                MARK_VAR_AS_USED
                const size_t p = ctx->cur;
                MARK_VAR_AS_USED
                const size_t n = chunk->thunks.n;
...

It seems that there are some situations where the MARK_VAR_AS_USED is not generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions