please diagnose invalid 'pure' attribute on functions
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
As clang makes more and more optimizations, `pure` attributes on functions that are in fact not `pure` (as defined in https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html) can lead to undefined behaviour. See e.g. https://lists.gnu.org/archive/html/bug-gettext/2026-08/msg00000.html
It would therefore be useful to diagnose common cases of such invalid `pure` attributes. (I'm aware that it cannot diagnose all such cases, because that would be equivalent to solving the halting problem.)
How to reproduce:
1. Save this file as foo.c.
```c
int lookup (int key, int *value) __attribute__ ((__pure__));
int lookup (int key, int *value)
{
if (key == 42)
{
*value = 47;
return 0;
}
return -1;
}
#include
int main ()
{
int misses = 0;
for (int repeat = 10; repeat > 0; repeat--)
{
int value = 0;
misses += (lookup (42, &value) < 0);
printf ("%d\n", value);
}
printf ("misses = %d\n", misses);
}
```
2.
```console
$ clang -Wall foo.c
$ ./a.out
47
47
47
47
47
47
47
47
47
47
misses = 0
```
```console
$ clang -Wall -O2 foo.c
$ ./a.out
0
0
0
0
0
0
0
0
0
0
misses = 0
```
Contributor guide
Assessment
This issue has not been assessed yet.