public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations
@ 2023-04-03 21:05 dev at joren dot ga
  2023-04-03 21:07 ` [Bug middle-end/109395] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: dev at joren dot ga @ 2023-04-03 21:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

            Bug ID: 109395
           Summary: -Wvla-larger-than has no effect when compiling without
                    optimizations
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dev at joren dot ga
  Target Milestone: ---

Let's take the following for an example:

    int bar(int n, int[n]);

    void foo(int n)
    {
        int arr[n];
        for (int i = 0; i < n; ++i) {
            arr[i] = i;
        }
        bar(n, arr);
    }

When compiled with 

    $ gcc -O -Wvla-larger-than=0 -c foo.c

a warning is shown as expected:

    foo.c: In function ‘foo’:
    foo.c:5:9: warning: argument to variable-length array may be too large
[-Wvla-larger-than=]
        5 |     int arr[n];
          |         ^~~

It works with -O, -O1, -O2, -O3, -Os, -Oz, -Ofast

On the other hand, when compiling with -O0, -Og or no -O flag, no warning is
triggered (https://godbolt.org/z/Penjbqojh).

I've tried to find if only a flag implied by optimization is required, but to
no avail.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
@ 2023-04-03 21:07 ` pinskia at gcc dot gnu.org
  2023-04-03 21:13 ` dev at joren dot ga
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-03 21:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end
           Keywords|                            |diagnostic
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is by designed and is even documented:
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla-larger-than_003d

-Wvla-larger-than=‘PTRDIFF_MAX’ is enabled by default but is typically only
effective when -ftree-vrp is active (default for -O2 and above).

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
  2023-04-03 21:07 ` [Bug middle-end/109395] " pinskia at gcc dot gnu.org
@ 2023-04-03 21:13 ` dev at joren dot ga
  2023-04-03 21:15 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dev at joren dot ga @ 2023-04-03 21:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #2 from Jakub Łukasiewicz <dev at joren dot ga> ---
(In reply to Andrew Pinski from comment #1)
> This is by designed and is even documented:
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-
> Wvla-larger-than_003d
> 
> -Wvla-larger-than=‘PTRDIFF_MAX’ is enabled by default but is typically only
> effective when -ftree-vrp is active (default for -O2 and above).

Passing `-ftree-vrp` doesn't activate it either

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
  2023-04-03 21:07 ` [Bug middle-end/109395] " pinskia at gcc dot gnu.org
  2023-04-03 21:13 ` dev at joren dot ga
@ 2023-04-03 21:15 ` pinskia at gcc dot gnu.org
  2023-04-03 21:17 ` dev at joren dot ga
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-03 21:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jakub Łukasiewicz from comment #2)
> Passing `-ftree-vrp` doesn't activate it either

Because of another part of the documentation:
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options

Most optimizations are completely disabled at -O0 or if an -O level is not set
on the command line, even if individual optimization flags are specified.
Similarly, -Og suppresses many optimization passes.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (2 preceding siblings ...)
  2023-04-03 21:15 ` pinskia at gcc dot gnu.org
@ 2023-04-03 21:17 ` dev at joren dot ga
  2023-04-03 21:22 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dev at joren dot ga @ 2023-04-03 21:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #4 from Jakub Łukasiewicz <dev at joren dot ga> ---
(In reply to Andrew Pinski from comment #3)
> (In reply to Jakub Łukasiewicz from comment #2)
> > Passing `-ftree-vrp` doesn't activate it either
> 
> Because of another part of the documentation:
> https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options
> 
> Most optimizations are completely disabled at -O0 or if an -O level is not
> set on the command line, even if individual optimization flags are
> specified. Similarly, -Og suppresses many optimization passes.

So there is not way of detecting automatic VLA in "debug release"?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (3 preceding siblings ...)
  2023-04-03 21:17 ` dev at joren dot ga
@ 2023-04-03 21:22 ` pinskia at gcc dot gnu.org
  2023-04-03 21:26 ` dev at joren dot ga
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-03 21:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jakub Łukasiewicz from comment #4)
> So there is not way of detecting automatic VLA in "debug release"?

-Wvla works.

https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (4 preceding siblings ...)
  2023-04-03 21:22 ` pinskia at gcc dot gnu.org
@ 2023-04-03 21:26 ` dev at joren dot ga
  2023-04-03 21:29 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: dev at joren dot ga @ 2023-04-03 21:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #6 from Jakub Łukasiewicz <dev at joren dot ga> ---
(In reply to Andrew Pinski from comment #5)
> (In reply to Jakub Łukasiewicz from comment #4)
> > So there is not way of detecting automatic VLA in "debug release"?
> 
> -Wvla works.
> 
> https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Warning-Options.html#index-Wvla

But also marks desired VLA (like in function parameters)

    $ gcc -std=c99 -Wvla -c foo.c
    foo.c:1:1: warning: ISO C90 forbids variable length array [-Wvla]
        1 | int bar(int n, int[n]);
          | ^~~
    foo.c: In function ‘foo’:
    foo.c:5:5: warning: ISO C90 forbids variable length array ‘arr’ [-Wvla]
        5 |     int arr[n];
          |     ^~~

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (5 preceding siblings ...)
  2023-04-03 21:26 ` dev at joren dot ga
@ 2023-04-03 21:29 ` pinskia at gcc dot gnu.org
  2023-04-03 21:31 ` pinskia at gcc dot gnu.org
  2023-04-03 21:41 ` dev at joren dot ga
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-03 21:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Yes and that is still a VLA in terms of C99 definition ...

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (6 preceding siblings ...)
  2023-04-03 21:29 ` pinskia at gcc dot gnu.org
@ 2023-04-03 21:31 ` pinskia at gcc dot gnu.org
  2023-04-03 21:41 ` dev at joren dot ga
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-03 21:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note GCC's C++ front-end even rejects VLA definitions like that for parameters
so I don't know how useful they are really.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug middle-end/109395] -Wvla-larger-than has no effect when compiling without optimizations
  2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
                   ` (7 preceding siblings ...)
  2023-04-03 21:31 ` pinskia at gcc dot gnu.org
@ 2023-04-03 21:41 ` dev at joren dot ga
  8 siblings, 0 replies; 10+ messages in thread
From: dev at joren dot ga @ 2023-04-03 21:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109395

--- Comment #9 from Jakub Łukasiewicz <dev at joren dot ga> ---
(In reply to Andrew Pinski from comment #7)
> Yes and that is still a VLA in terms of C99 definition ...

Indeed, but we only want to prevent automatic VLA, all other instances of VM
types are even encouraged.

(In reply to Andrew Pinski from comment #8)
> Note GCC's C++ front-end even rejects VLA definitions like that for
> parameters so I don't know how useful they are really.

The project is pure C and we really don't plan to convert it to C++ any time.
At the same time, (recently added) diagnostics for size checking array
parameter (https://godbolt.org/z/fdbheM8G9) could be really beneficial.
Also, allocating on heap multidimensional arrays of unknown size is really
convenient:

    int (*arr)[n][m] = malloc(sizeof *arr);

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-04-03 21:41 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-03 21:05 [Bug c/109395] New: -Wvla-larger-than has no effect when compiling without optimizations dev at joren dot ga
2023-04-03 21:07 ` [Bug middle-end/109395] " pinskia at gcc dot gnu.org
2023-04-03 21:13 ` dev at joren dot ga
2023-04-03 21:15 ` pinskia at gcc dot gnu.org
2023-04-03 21:17 ` dev at joren dot ga
2023-04-03 21:22 ` pinskia at gcc dot gnu.org
2023-04-03 21:26 ` dev at joren dot ga
2023-04-03 21:29 ` pinskia at gcc dot gnu.org
2023-04-03 21:31 ` pinskia at gcc dot gnu.org
2023-04-03 21:41 ` dev at joren dot ga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).