<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62208>62208</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `-Warray-bounds` Regression with `static const` varaibles 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Dylan-Brotherston
      </td>
    </tr>
</table>

<pre>
    The following code
```c
static const int LENGTH = 10;

int main(int argc, char **argv) {
    char array1[10];
    char array2[LENGTH];
    char array3[LENGTH + 1];

    array1[5] = 'A';
    array2[5] = 'B';
    array3[5] = 'C';

    array1[20] = 'A';
    array2[20] = 'B';
    array3[20] = 'C';
}
```
produces `-Warray-bounds` warnings for all three arrays with `clang-11`
```
$ clang-11 test.c

test.c:12:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array1[20] = 'A';
    ^      ~~
test.c:4:5: note: array 'array1' declared here
    char array1[10];
 ^
test.c:13:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array2[20] = 'B';
    ^ ~~
test.c:5:5: note: array 'array2' declared here
    char array2[LENGTH];
    ^
test.c:14:5: warning: array index 20 is past the end of the array (which contains 11 elements) [-Warray-bounds]
 array3[20] = 'C';
    ^      ~~
test.c:6:5: note: array 'array3' declared here
    char array3[LENGTH + 1];
    ^
3 warnings generated.
``` 
But only processes a warning for `array1` with `clang-12` -> `clang-16`
```
$ clang-12 test.c

test.c:12:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array1[20] = 'A';
    ^ ~~
test.c:4:5: note: array 'array1' declared here
    char array1[10];
    ^
1 warning generated.
```
But if the arrays are global variables the bounds checking returns
```c
static const int LENGTH = 10;

char array1[10];
char array2[LENGTH];
char array3[LENGTH + 1];

int main(int argc, char **argv) {

 array1[5] = 'A';
    array2[5] = 'B';
    array3[5] = 'C';

 array1[20] = 'A';
    array2[20] = 'B';
    array3[20] = 'C';
}
```
`clang-11`
```
$ clang-11 test.c

test.c:13:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array1[20] = 'A';
    ^      ~~
test.c:3:1: note: array 'array1' declared here
char array1[10];
^
test.c:14:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
 array2[20] = 'B';
    ^      ~~
test.c:4:1: note: array 'array2' declared here
char array2[LENGTH];
^
test.c:15:5: warning: array index 20 is past the end of the array (which contains 11 elements) [-Warray-bounds]
    array3[20] = 'C';
    ^      ~~
test.c:5:1: note: array 'array3' declared here
char array3[LENGTH + 1];
^
3 warnings generated.
```
`clang-12` -> `clang-16`
```
$ clang-12 test.c

test.c:4:6: warning: variable length array folded to constant array as an extension [-Wgnu-folding-constant]
char array2[LENGTH];
 ^
test.c:5:6: warning: variable length array folded to constant array as an extension [-Wgnu-folding-constant]
char array3[LENGTH + 1];
 ^
test.c:13:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array1[20] = 'A';
    ^ ~~
test.c:3:1: note: array 'array1' declared here
char array1[10];
^
test.c:14:5: warning: array index 20 is past the end of the array (which contains 10 elements) [-Warray-bounds]
    array2[20] = 'B';
    ^      ~~
test.c:4:1: note: array 'array2' declared here
char array2[LENGTH];
^
test.c:15:5: warning: array index 20 is past the end of the array (which contains 11 elements) [-Warray-bounds]
 array3[20] = 'C';
    ^      ~~
test.c:5:1: note: array 'array3' declared here
char array3[LENGTH + 1];
^
5 warnings generated.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWMFy4jgTfprm0gUlS5aBgw8wJP9_2NrD1lTtWbYbW7uKlJJEMrnMs2_ZBgJMIGY2m8nOUhTYVqtb-rr765ZVCLq2RDnIJcjVSG1i43y-ejLKjpfexYZ8iM6OClc95Z8bwrUzxj1qW2PpKgK2AraAjPXfsr8PUUVdYulsiKhtxF9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