Issue |
151526
|
Summary |
New dialect flag: -farray-parameters-are-const
|
Labels |
new issue
|
Assignees |
|
Reporter |
alejandro-colomar
|
This would change the dialect to make array parameters implicitly const.
The following code:
```c
int
foo(int size, char buf[100], pid_t pid)
{
if (stprintf(buf, _Countof(buf), "/proc/%d/", pid) == -1)
return -1;
...
return 0;
}
```
would be equivalent to
```c
int
foo(int size, char buf[const 100], pid_t pid)
{
if (stprintf(buf, _Countof(buf), "/proc/%d/", pid) == -1)
return -1;
...
return 0;
}
```
This would allow one to safely use `_Countof()` with array parameters, without the pointer being advanced accidentally, which would turn the size information outdated.
We can't make this the default behavior, as it would break existing code, but programmers might want to change the behavior in their own programs (I do). That would make it so that we don't need to write `const` in every array parameter to be able to use `_Countof()` on them. (I've written a proposal for extending `_Countof()` to work on array parameters, and a constraint would be that they need to be `const`-qualified.)
It is also morally appropriate, as these pointers represent arrays (even if they are not), so it wouldn't make sense to try to modify the address.
This closes part of the gap between array parameters and arrays.
See GCC proposal: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121271>
Cc: @AaronBallman
Related: <https://github.com/llvm/llvm-project/issues/150953>
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs