[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] C99 static array indices in function args

From: Michael Matz
Subject: Re: [Tinycc-devel] C99 static array indices in function args
Date: Sat, 23 Dec 2017 14:43:59 +0100 (CET)
User-agent: Alpine 2.20 (LSU 67 2015-01-07)


On Wed, 20 Dec 2017, Michael B. Smith wrote:

It's in in C99 for 'static'.

It's in 6.7.3/5 in C99 for 'const'.

Using 'static' seems to have two implied contracts:

[1] don't allow NULL parameters
[2] verify, when possible, that the passed array has AT LEAST the number of elements defined

Using 'const' has an implied contract:

[3] treat the array as if were a const array (e.g., "char a[const] --> char * const a")

Recent versions of gcc and clang do 1 and 2 (as warnings). I can't find anything definitive about 3. #7 is pretty clear, the type qualifiers applied are those from within the [ ]. TCC doesn't support this syntax properly right now.

'restrict' is allowed by tcc, but ignored. I think that that is a bug.

Why would you think so? restrict is a type qualifier that is allowed to be applied only to pointer types, parmdecls of array type transform into pointer types hence "int a[restrict 3]" in a parmdecl is well defined and meaningful.

'restrict' has several defined (not just implied) contracts. This should require at least a warning.

No. Warnings are never required diagnostics (and even if, what specifically would you want to warn about in this case?). The only require diagnostics are from constraint violations. Most other undefined behaviours don't need to be diagnosed (of course, if easily doable it's nice to diagnose them).

Supporting 'const' properly is actually pretty easy.

Supporting 'static' - well, [1] requires that tcc generate code (to handle both runtime and compile-time cases), and [2] may (or may not) be easy to do depending on the parameter. If we assume locally defined arrays, like VLAs, then it is easy.

There are no changes to emitted code necessary. The [static 3] syntax is an assertion of the programmer, the compiler doesn't have to check it. On the contrary, the compiler is allowed (but not required) to make use of this assertion, and assume there are at least 3 elements. If the programmer then gives fewer its his problem, it's undefined behaviour, no diagnostic required. (Of course TCC, not being an optimizing compiler wouldn't have much opportunity to make use of these kinds of assertion, the same that it doesn't make use of restrict qualitications).


reply via email to

[Prev in Thread] Current Thread [Next in Thread]