[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.
+
+The attribute generally should only be used on functions with a non-``void`` return type which do not accept a pointer or reference argument.
+The ``const`` attribute imposes greater restrictions than the related ``pure`` attribute; applying both attributes to a declaration will be diagnosed and ``pure`` will be ignored.
+
+The following trivial example demonstrates how the attribute can be used:
+
+.. code-block:: c++
+
+  __attribute__((const)) int add_one(int x) { return x + 1; }
+
+  int main(void) {
+    int x = add_one(1);
+    int y = add_one(1); // Call can be elided
+  }
+  }];
+}
+
+def PureDocs :  Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+The ``pure`` 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 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 written to.
+
+Unlike the ``const`` attribute, the ``pure`` attribute allows memory to be read, even if it changes between subsequent calls.
----------------
AaronBallman wrote:

> OK, I think I just understood. Are you referring to const being allowed to read memory if it can't be changed between calls? I think this phrasing is more confusing than helpful if it's that.

`pure` is allowed to read memory if the memory isn't changed between calls.

I couldn't think of a better way to phrase this, so I may need some help. I checked the GCC docs and theirs say something similar to what I came up with:

> The pure attribute imposes similar but looser restrictions on a function’s definition than the const attribute: pure allows the function to read any non-volatile memory, even if it changes in between successive invocations of the function. 

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


More information about the cfe-commits mailing list