Variable length arrays are not permitted in C++

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Variable length arrays are not permitted in C++

Rene Rebe
Hi all,

starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:

lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
  uint8_t gray_lookup[vmax];

the code is straight forward variable length array, like:

..
  const int vmax = 1 << bps;
  uint8_t gray_lookup[vmax];

The error already indicates it is intentionally disabled in clang++ for C++

Even old (3.x?) versions of G++ happily accept this.

Is there an std=c99 like option to allow this in Clang++? Maybe it should accept it by default and warn that it is not portable, GNU extension (but I think present in C++0x)?

René

--
  René Rebe, ExactCODE GmbH, Jaegerstr. 67, DE-10117 Berlin
  http://exactcode.com | http://t2-project.org | http://rene.rebe.name


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Rene Rebe
Hello again,

On 16.02.2010, at 12:21, Rene Rebe wrote:

> Hi all,
>
> starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:
>
> lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
>  uint8_t gray_lookup[vmax];
>
> the code is straight forward variable length array, like:
>
> ..
>  const int vmax = 1 << bps;
>  uint8_t gray_lookup[vmax];
>
> The error already indicates it is intentionally disabled in clang++ for C++
>
> Even old (3.x?) versions of G++ happily accept this.
>
> Is there an std=c99 like option to allow this in Clang++? Maybe it should accept it by default and warn that it is not portable, GNU extension (but I think present in C++0x)?
>
> René
>


This trivial changes makes some quick test work:

--- tools/clang/lib/Sema/SemaType.cpp (revision 96337)
+++ tools/clang/lib/Sema/SemaType.cpp (working copy)
@@ -686,7 +686,7 @@
     T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
   }
   // If this is not C99, extwarn about VLA's and C99 array size modifiers.
-  if (!getLangOptions().C99) {
+  if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
     if (ArraySize && !ArraySize->isTypeDependent() &&
         !ArraySize->isValueDependent() &&
         !ArraySize->isIntegerConstantExpr(Context))

I'm aware that this is not apply-worthy code, ...

--
  René Rebe, ExactCODE GmbH, Jaegerstr. 67, DE-10117 Berlin
  http://exactcode.com | http://t2-project.org | http://rene.rebe.name


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Eli Friedman
In reply to this post by Rene Rebe
On Tue, Feb 16, 2010 at 3:21 AM, Rene Rebe <[hidden email]> wrote:
> Hi all,
>
> starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:
>
> lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
>  uint8_t gray_lookup[vmax];

VLAs are disabled until someone goes through and makes sure they work
properly and that we give appropriate errors for C++-only constructs.

-Eli

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Rene Rebe
Hi,

On 16.02.2010, at 16:39, Eli Friedman wrote:

> On Tue, Feb 16, 2010 at 3:21 AM, Rene Rebe <[hidden email]> wrote:
>> Hi all,
>>
>> starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:
>>
>> lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
>>  uint8_t gray_lookup[vmax];
>
> VLAs are disabled until someone goes through and makes sure they work
> properly and that we give appropriate errors for C++-only constructs.


Well, what's needed? I'm certainly interested in LLVM and getting further into it. Though I'm not yet too familiar with the codebase, yet, nor am an language lawyer.

Basic, POD, use appear to just work after I enabled it with the patch posted before. Any pointers welcome.

  René
--
  René Rebe, ExactCODE GmbH, Jaegerstr. 67, DE-10117 Berlin
  http://exactcode.com | http://t2-project.org | http://rene.rebe.name


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Eli Friedman
On Tue, Feb 16, 2010 at 7:52 AM, Rene Rebe <[hidden email]> wrote:

> Hi,
>
> On 16.02.2010, at 16:39, Eli Friedman wrote:
>
>> On Tue, Feb 16, 2010 at 3:21 AM, Rene Rebe <[hidden email]> wrote:
>>> Hi all,
>>>
>>> starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:
>>>
>>> lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
>>>  uint8_t gray_lookup[vmax];
>>
>> VLAs are disabled until someone goes through and makes sure they work
>> properly and that we give appropriate errors for C++-only constructs.
>
>
> Well, what's needed? I'm certainly interested in LLVM and getting further into it. Though I'm not yet too familiar with the codebase, yet, nor am an language lawyer.
>
> Basic, POD, use appear to just work after I enabled it with the patch posted before. Any pointers welcome.

Anything that works in C probably works in C++ as well; it gets
complicated when we try to deal with constructs like VLAs with
non-trivial destructors, using a VLA with new, attempting to use a
variably modified type as a template argument, attempting to use a
variably modified type inside a local class, etc.  The idea is that if
we're going to support VLAs inside of C++, we should make sure they
work correctly in every situation, not just in trivial cases.

-Eli

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

David Chisnall
On 16 Feb 2010, at 17:08, Eli Friedman wrote:

> Anything that works in C probably works in C++ as well; it gets
> complicated when we try to deal with constructs like VLAs with
> non-trivial destructors, using a VLA with new, attempting to use a
> variably modified type as a template argument, attempting to use a
> variably modified type inside a local class, etc.  The idea is that if
> we're going to support VLAs inside of C++, we should make sure they
> work correctly in every situation, not just in trivial cases.


There is already a bug tracking this:

http://llvm.org/bugs/show_bug.cgi?id=5678

There's a patch attached to it for handling VLAs with just POD types.  Handling them with other types sounds like it would be quite complex.  I'm not sure if this even works reliably with GCC either - I've heard some complaints that it doesn't, but they might have been from older versions of GCC.

As this is a GCC extension, presumably our priority is compatibility with existing code, rather than implementing every possible case, so only supporting POD types might be Good Enough™, as long as we emit a helpful error if someone tries to use VLAs with non-POD types.  

David

-- Sent from my Cray X1


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Eli Friedman
On Tue, Feb 16, 2010 at 9:21 AM, David Chisnall <[hidden email]> wrote:

> On 16 Feb 2010, at 17:08, Eli Friedman wrote:
>
>> Anything that works in C probably works in C++ as well; it gets
>> complicated when we try to deal with constructs like VLAs with
>> non-trivial destructors, using a VLA with new, attempting to use a
>> variably modified type as a template argument, attempting to use a
>> variably modified type inside a local class, etc.  The idea is that if
>> we're going to support VLAs inside of C++, we should make sure they
>> work correctly in every situation, not just in trivial cases.
>
>
> There is already a bug tracking this:
>
> http://llvm.org/bugs/show_bug.cgi?id=5678
>
> There's a patch attached to it for handling VLAs with just POD types.  Handling them with other types sounds like it would be quite complex.  I'm not sure if this even works reliably with GCC either - I've heard some complaints that it doesn't, but they might have been from older versions of GCC.
>
> As this is a GCC extension, presumably our priority is compatibility with existing code, rather than implementing every possible case, so only supporting POD types might be Good Enough™, as long as we emit a helpful error if someone tries to use VLAs with non-POD types.

Hmm, that's a reasonable restriction, but some of the stuff I
mentioned needs to be dealt with anyway.

-Eli

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Douglas Gregor
In reply to this post by Eli Friedman

On Feb 16, 2010, at 9:08 AM, Eli Friedman wrote:

> On Tue, Feb 16, 2010 at 7:52 AM, Rene Rebe <[hidden email]> wrote:
>> Hi,
>>
>> On 16.02.2010, at 16:39, Eli Friedman wrote:
>>
>>> On Tue, Feb 16, 2010 at 3:21 AM, Rene Rebe <[hidden email]> wrote:
>>>> Hi all,
>>>>
>>>> starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:
>>>>
>>>> lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
>>>>  uint8_t gray_lookup[vmax];
>>>
>>> VLAs are disabled until someone goes through and makes sure they work
>>> properly and that we give appropriate errors for C++-only constructs.
>>
>>
>> Well, what's needed? I'm certainly interested in LLVM and getting further into it. Though I'm not yet too familiar with the codebase, yet, nor am an language lawyer.
>>
>> Basic, POD, use appear to just work after I enabled it with the patch posted before. Any pointers welcome.
>
> Anything that works in C probably works in C++ as well; it gets
> complicated when we try to deal with constructs like VLAs with
> non-trivial destructors, using a VLA with new, attempting to use a
> variably modified type as a template argument, attempting to use a
> variably modified type inside a local class, etc.  The idea is that if
> we're going to support VLAs inside of C++, we should make sure they
> work correctly in every situation, not just in trivial cases.


I would be perfectly happy to have Clang produce errors for all of these cases, so that VLAs in C++ are restricted to the places where they work in C. A patch that correctly restricts VLAs to that subset, and downgrades the "variable length arrays are not permitted in C++" error to an ExtWarn, will be accepted.

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Renato Golin
On 16 February 2010 18:46, Douglas Gregor <[hidden email]> wrote:
> I would be perfectly happy to have Clang produce errors for all of these cases, so that VLAs in C++ are restricted to the places where they work in C. A patch that correctly restricts VLAs to that subset, and downgrades the "variable length arrays are not permitted in C++" error to an ExtWarn, will be accepted.

Still, shouldn't the message be "not supported yet" rather than "not permitted"?


cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Ahmed Charles


On Wed, Feb 17, 2010 at 2:52 AM, Renato Golin <[hidden email]> wrote:
On 16 February 2010 18:46, Douglas Gregor <[hidden email]> wrote:
> I would be perfectly happy to have Clang produce errors for all of these cases, so that VLAs in C++ are restricted to the places where they work in C. A patch that correctly restricts VLAs to that subset, and downgrades the "variable length arrays are not permitted in C++" error to an ExtWarn, will be accepted.

Still, shouldn't the message be "not supported yet" rather than "not permitted"?


It's an g++ extension rather than a C++ standard feature, hence why "not permitted" is applicable.

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Variable length arrays are not permitted in C++

Renato Golin
On 17 February 2010 19:27, Ahmed Charles <[hidden email]> wrote:
> It's an g++ extension rather than a C++ standard feature, hence why "not
> permitted" is applicable.

Indeed, not even in c++0x, my bad. Then maybe a hint to use dynamic
arrays or vectors? ;)


--
cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev