llvm / llvm/llvm-project

please diagnose invalid 'pure' attribute on functions

Open
#215,106 5 comments 0 reactions 1 assignee Claimed by @AhmedKamel10 View on GitHub
clang:diagnostics
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.