llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

please diagnose invalid 'pure' attribute on functions

Open bhaible opened this issue 3 months ago • 5 comments

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.
int lookup (int key, int *value) __attribute__ ((__pure__));

int lookup (int key, int *value)
{
  if (key == 42)
    {
      *value = 47;
      return 0;
    }
  return -1;
}

#include <stdio.h>
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);
}
$ clang -Wall foo.c
<no warning>
$ ./a.out 
47
47
47
47
47
47
47
47
47
47
misses = 0
$ clang -Wall -O2 foo.c
<no warning>
$ ./a.out 
0
0
0
0
0
0
0
0
0
0
misses = 0

bhaible avatar Aug 09 '26 15:08 bhaible

I'd like to work on this issue. I'll start researching the implementation details and will post an update or questions here if needed.

AhmedKamel10 avatar Aug 18 '26 11:08 AhmedKamel10

I'm looking into implementing a diagnostic for this. I realize that checking full purity across complex call graphs is out of scope, as proving that would be too complex (as you mentioned). I was wondering what specific cases you think the warning should fire on when encountered—for example, direct writes to dereferenced pointers/references (*ptr = ...) or assignments to global/static variables?

@EugeneZelenko @bhaible

AhmedKamel10 avatar Aug 18 '26 16:08 AhmedKamel10

direct writes to dereferenced pointers/references (*ptr = ...)

Yes, especially direct writes to pointer arguments.

assignments to global/static variables?

Yes, writes into global/static variables too.

These would cover the most frequent programmer mistakes in this area.

bhaible avatar Aug 18 '26 16:08 bhaible

I've just opened a PR for this. I ended up writing a lightweight DynamicRecursiveASTVisitor that performs a syntactic check over the function's body. Right now, it successfully flags the most common direct assignments we discussed: writing through pointer parameters (*ptr = ...), modifying non-const reference arguments, and assigning directly to global or static-local variables.

I wanted to get your thoughts on the scope of this initial patch. Are there any other specific cases you think I should implement for this, or does this seem like a solid starting point? @bhaible

AhmedKamel10 avatar Aug 21 '26 05:08 AhmedKamel10

Are there any other specific cases you think I should implement for this, or does this seem like a solid starting point? @bhaible

I think the scope is perfect.

bhaible avatar Aug 21 '26 10:08 bhaible