[clang] Document the const and pure attributes (PR #205881)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 26 05:16:06 PDT 2026


================
@@ -10253,3 +10253,52 @@ The attribute is also supported with blocks and in Objective-C.
   }
   }];
 }
+
+def ConstDocs :  Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+The ``const`` attribute can be applied to the declaration of a function to signal that repeated calls to the function with the same argument values may be safe to elide because the subsequent calls will always return the same value as the initial call.
+
+The attribute informs the optimizer that the function cannot read or write to memory, does not support unwinding, will return (has no infinite loops), and that any pointer or reference arguments to the call will not be read from or written to.
----------------
AaronBallman wrote:

> willreturn also guarantees that it doesn't terminate I think.

Good call, based on the language ref, I think you're right. I updated.

> Could we clarify that the actual address of pointer/reference arguments can be used? It's a bit ambiguous currently I think.

Are you wondering whether this is valid:
```
__attribute__((const)) int func(int *ptr) {
  intptr_t val = (intptr_t)ptr;
  return 12;
}
```
or are you wondering whether this is valid:
```
__attribute__((const)) int func(int *ptr) {
  int **ptr_ptr = &ptr;
  return 12;
}
```
or something else?

I think in both cases, those are valid. What is prohibited is:
```
__attribute__((const)) int func(int *ptr) {
  return *ptr;
}
```

CC @nikic @efriedma-quic for extra eyeballs though because I'm not 100% sure of my interpretation.

https://github.com/llvm/llvm-project/pull/205881


More information about the cfe-commits mailing list