Re: [PATCH v3] kernel.h: Skip single-eval logic on literals in min()/max()
From: Ingo Molnar
Date: Sun Mar 11 2018 - 07:05:55 EST
* Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> So an error message like
>
> warning: ISO C90 requires array sizes to be constant-expressions
>
> would be technically correct and useful from a portability angle. It
> tells you when you're doing something non-portable, and should be
> automatically enabled with "-ansi -pedantic", for example.
>
> So what's misleading is actually the name of the warning and the
> message, not that it happens. The warning isn't about "variable
> length", it's literally about the rules for what a
> "constant-expression" is.
>
> And in C, the expression (2,3) has a constant _value_ (namely 3), but
> it isn't a constant-expression as specified by the language.
>
> Now, the thing is that once you actually do variable length arrays,
> those old front-end rules make no sense any more (outside of the "give
> portability warnings" thing).
>
> Because once you do variable length arrays, you obviously _parse_
> everything just fine, and you're doing to evaluate much more complex
> expressions than some limited constant-expression rule.
BTW., while I fully agree with everything you said, it's not entirely correct to
claim that if a C compiler can generate VLA code it is necessarily able to parse
and evaluate constant array sizes "just fine".
Constant expressions are typically parsed very early on, at the preprocessing
stage. They can be used with some preprocessor directives as well, such as '#if'
(with some further limitations on their syntax).
If VLA support is implemented in a later stage, and results in heavy-handed code
generation that will technically work for constant value expressions as well but
results in suboptimal code, then a warning should probably be emitted - and it
wouldn't be pedantic.
The existing warning is still very misleading:
warning: ISO C90 forbids variable length array âarrayâ [-Wvla]
... and if my above theory is correct then I think a better warning would be
something like:
warning: Array declaration is not a C90 constant expression, resulting in VLA code generation
... and note that in this specific case it's not misleading to talk about VLAs in
the warning text, because the array size, even if it's constant value, results in
VLA code generation.
I don't know whether GCC has such a limitation, but a quick experiment with GCC
7.2 suggests that a (2,3) array size expression results in a lot more code being
generated than with a constant expression.
Thanks,
Ingo