post-increment in formal argument list ignored
| Bugzilla Link | 9559 |
| Resolution | FIXED |
| Resolved on | Nov 14, 2012 16:10 |
| Version | trunk |
| OS | Linux |
| CC | @asl,@efriedma-quic,@tkremenek |
Extended Description
In the following example, main returns with exit code 1 because function sub returns i instead of i+1.
.c file
sub (int i, int array[i++]) { return i; }
main() { int array[10]; exit (sub (10, array) != 11); }
.ll file.
clang tests-c/970217-1.c -emit-llvm -O3 -S -o -
; ModuleID = 'tests-c/970217-1.c' target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" target triple = "i386-pc-linux-gnu"
define i32 @sub(i32 %i, i32* nocapture %array) nounwind readnone { entry: ret i32 %i }
define i32 @main() noreturn nounwind { entry: tail call void @exit(i32 1) noreturn nounwind unreachable }
declare void @exit(i32) noreturn
r167985.
"all expressions are evaluated as specified by the semantics" in that paragraph does not mean that every expression in a program will be evaluated. In fact, it's the opposite: expressions are only evaluated where explicitly required by the standard. The standard does not explicitly require evaluating the expression in question anywhere, so it isn't evaluated.
I don't claim to be a C language lawyer but this test is from the gcc validation suite. The gcc standard, to me, is defined by their reference manual and validation suite so the g89 or g99 switch on clang/llvm should follow those rules or else warn that the feature is not supported. These kinds of tests are in their validation suite for a reason and this one is clearly not some compiler regression test.
In this case, it seems to me that it's still an ANSI rule. I've sent some email to one committee member on this and will try and understand what process I can go through to get such resolutions.
" 10 On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment. (Array expressions and function designators as arguments were converted to pointers before the call.)"
int array[i++] is not an array expression or function designator so it seems that the size expression must be evaluated.
so only if you know that not evaluating the expression has no effect can you not evaluate it, and in this case it does have an effect.
"all expressions are evaluated as specified by the semantics" in that paragraph does not mean that every expression in a program will be evaluated. In fact, it's the opposite: expressions are only evaluated where explicitly required by the standard. The standard does not explicitly require evaluating the expression in question anywhere, so it isn't evaluated.
Additionally, 6.7.5.2p5 states:
| If the [array] size is an expression that is not an integer constant | expression: if it occurs in a declaration at function prototype scope, it is | treated as if it were replaced by *
So this does not seem to be a bug to me, neither. But perhaps clang should warn that the expression is not evaluated (as an enhancement, of course).
I'm fine with repurposing this PR for that purpose.
I think this is not consistent with the C standard.
Additionally, 6.7.5.2p5 states:
| If the [array] size is an expression that is not an integer constant | expression: if it occurs in a declaration at function prototype scope, it is | treated as if it were replaced by *
So this does not seem to be a bug to me, neither. But perhaps clang should warn that the expression is not evaluated (as an enhancement, of course).
I'm fine with repurposing this PR for that purpose.
I think this is not consistent with the C standard.
5.1.2.3 Program execution ... In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).
Notice... "all expressions"
The fact that the array is treated as if * replaced the expression does not free you from having to evaluate the expression.
Additionally, 6.7.5.2p5 states:
| If the [array] size is an expression that is not an integer constant | expression: if it occurs in a declaration at function prototype scope, it is | treated as if it were replaced by *
So this does not seem to be a bug to me, neither. But perhaps clang should warn that the expression is not evaluated (as an enhancement, of course).
I'm fine with repurposing this PR for that purpose.
Additionally, 6.7.5.2p5 states:
| If the [array] size is an expression that is not an integer constant | expression: if it occurs in a declaration at function prototype scope, it is | treated as if it were replaced by *
So this does not seem to be a bug to me, neither. But perhaps clang should warn that the expression is not evaluated (as an enhancement, of course).
This doesn't sound like a bug to me.
-Chris
Justification? C99 6.9.1p10 doesn't appear to require evaluating it.