[llvm-bugs] [Bug 48735] New: Difference between GCC and Clang on a defined program: GCC's VLA is Clang fixed-size automatic array

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 13 02:26:04 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=48735

            Bug ID: 48735
           Summary: Difference between GCC and Clang on a defined program:
                    GCC's VLA is Clang fixed-size automatic array
           Product: clang
           Version: 11.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C
          Assignee: unassignedclangbugs at nondot.org
          Reporter: cuoq at trust-in-soft.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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: https://gcc.godbolt.org/z/fa8frb

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
(https://cigix.me/c17#6.6.p10 ), 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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210113/75b128e9/attachment.html>


More information about the llvm-bugs mailing list