<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Difference between GCC and Clang on a defined program: GCC's VLA is Clang fixed-size automatic array"
href="https://bugs.llvm.org/show_bug.cgi?id=48735">48735</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Difference between GCC and Clang on a defined program: GCC's VLA is Clang fixed-size automatic array
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>11.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>cuoq@trust-in-soft.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Consider the compilation unit:
int t[12];
#define ADDR_FIRST (&(t[0]))
#define ADDR_LAST (&(t[11]))
int Y, Z;
void f(void) {
// Define u the same size as t:
long u[ADDR_LAST - ADDR_FIRST + 1];
Y = sizeof *(Z=1, &u);
}
Compiler Explorer link: <a href="https://gcc.godbolt.org/z/fa8frb">https://gcc.godbolt.org/z/fa8frb</a>
When u is an automatic variable, GCC 10.2 treats u, declared as above, as a
VLA, and therefore generates code for the function f that assigns Z:
f:
movl $1, Z(%rip)
movl $96, Y(%rip)
ret
Meanwhile Clang 11.0.0 treats the expression “ADDR_LAST - ADDR_FIRST + 1” as a
constant expression, which I think it is entitled to per C17 6.6:10
(<a href="https://cigix.me/c17#6.6.p10">https://cigix.me/c17#6.6.p10</a> ), meaning that u is a fixed-size array and that
f should not assign Z:
f: # @f
movl $96, Y(%rip)
retq
Note that the two compilers's behaviors agree when u is a static-lifetime
variable. In that case GCC emits a somewhat misleading warning about u having
file scope (whereas it should be the lifetime of u that's the problem) and
variable size, and subsequently treats u as a fixed-size array. Changing the
recognition of constant expressions to be stricter for all array sizes would
make Clang reject a static-lifetime definition for u, which GCC accepts.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>