Dear TCC developers,

While adding the C23 typeof_unqual operator, I found that TCC accepts
the following assignment without a diagnostic:

    void f(int n)
    {
        typedef int Row[n];
        const Row row;
        row[0] = 1;
    }

https://godbolt.org/z/TrraG5cMM

(Changing the bound from n to 4 is enough to get the expected warning.)

I've just pushed three small commits to mob that I hope will be
accepted and useful.

The first (8d174310b942) fixes a separate bug in the conditional
operator. When one pointer type supplies a known outer array bound and
the other supplies a known inner bound, reversing the operands could
lose the inner bound. The regression test uses sizeof to check both
bounds in both operand orders.

The second (619b6bd785e3) fixes the missing diagnostic in the above
example. TCC represents a variable-length array with VT_VLA in place
of VT_ARRAY. The code that qualifies the elements of an array typedef
checked only VT_ARRAY, so const never reached the element type. This
commit also fixes related cases involving address-taking and
array-to-pointer conversion, and a spurious pointer type mismatch
warning from the conditional operator. The regression tests cover
those cases as well as comparison of fixed and variable bounds.

The third commit (51effecceccd) adds typeof_unqual and the alternate
spelling __typeof_unqual__. It reuses the typeof parser and the array
qualifier handling from the second commit.

The full TCC test suite passes on Linux x86-64 and macOS AArch64 after
each commit.

One other known bug is not yet fixed: if p points to a VLA, C23
requires *p++ to be evaluated when it is the operand of typeof or
typeof_unqual. TCC obtains the correct array bound but fails to
increment p. For example, this program should return 1:

    int f(int n)  /* n > 0 */
    {
        int rows[2][n];
        int (*p)[n] = rows;
        typeof(*p++) row;

        return p == rows + 1;
    }

    int main(void)
    {
        return f(3);
    }

See https://godbolt.org/z/Ghv4b1W33 for a comparison of TCC's
behaviour with that of GCC and Clang.

Thanks,
Christopher

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to