[clang] [docs] Add "constexpr" or "consteval" to pseudo-declarations of builtins (PR #247014)
Jan Schultke via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 13:26:00 PST 2026
https://github.com/eisenwave created https://github.com/llvm/llvm-project/pull/247014
This is a follow-up to #246637. On that PR, @tbaederr suggested to put `constexpr` before pseudo-declarations to indicate whether the intrinsic can be used in constant expressions. This suggestion is implemented here.
In addition to this change, intrinsics that always produce a constant (e.g. `__builtin_offsetof`) are prefixed with `consteval`.
>From 4a714a984284366111831285842d7fc648c21d08 Mon Sep 17 00:00:00 2001
From: Eisenwave <me at eisenwave.net>
Date: Sun, 29 Nov 2026 22:21:54 +0100
Subject: [PATCH] [docs] Add "constexpr" or "consteval" to pseudo-declarations
of builtins
---
clang/docs/LanguageExtensions.md | 233 +++++++++++++++----------------
1 file changed, 116 insertions(+), 117 deletions(-)
diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index f53d4f636b4640..c305901246c294 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -3255,7 +3255,7 @@ invariant that is defined to be true.
**Syntax**:
```c++
-void __builtin_assume(bool);
+constexpr void __builtin_assume(bool);
```
**Example of Use**:
@@ -3366,7 +3366,7 @@ calculates the offset (in bytes) to a given member of the given type.
**Syntax**:
```c++
-size_t __builtin_offsetof(type-name, member-designator);
+consteval size_t __builtin_offsetof(type-name, member-designator);
```
**Example of Use**:
@@ -3648,7 +3648,7 @@ for the implementation of various target-specific header files like
**Syntax**:
```c++
-T __builtin_shufflevector(T vec1, T vec2, integer index1, integer index2, ...);
+constexpr T __builtin_shufflevector(T vec1, T vec2, integer index1, integer index2, ...);
```
**Examples**:
@@ -3704,7 +3704,7 @@ constant expressions.
**Syntax**:
```c++
-R __builtin_convertvector(T src_vec, type-id R);
+constexpr R __builtin_convertvector(T src_vec, type-id R);
```
**Examples**:
@@ -3743,11 +3743,11 @@ Query for this feature with `__has_builtin(__builtin_convertvector)`.
**Syntax**:
```c++
-uint8_t __builtin_bitreverse8(uint8_t x);
-uint16_t __builtin_bitreverse16(uint16_t x);
-uint32_t __builtin_bitreverse32(uint32_t x);
-uint64_t __builtin_bitreverse64(uint64_t x);
-T __builtin_bitreverseg(T x);
+constexpr uint8_t __builtin_bitreverse8(uint8_t x);
+constexpr uint16_t __builtin_bitreverse16(uint16_t x);
+constexpr uint32_t __builtin_bitreverse32(uint32_t x);
+constexpr uint64_t __builtin_bitreverse64(uint64_t x);
+constexpr T __builtin_bitreverseg(T x);
```
**Examples**:
@@ -3771,10 +3771,10 @@ the bitpattern of an integer value; for example, `0b10110110` becomes
**Syntax**:
```c++
-uint8_t __builtin_rotateleft8(uint8_t x, uint8_t y);
-uint16_t __builtin_rotateleft16(uint16_t x, uint16_t y);
-uint32_t __builtin_rotateleft32(uint32_t x, uint32_t y);
-uint64_t __builtin_rotateleft64(uint64_t x, uint64_t y);
+constexpr uint8_t __builtin_rotateleft8(uint8_t x, uint8_t y);
+constexpr uint16_t __builtin_rotateleft16(uint16_t x, uint16_t y);
+constexpr uint32_t __builtin_rotateleft32(uint32_t x, uint32_t y);
+constexpr uint64_t __builtin_rotateleft64(uint64_t x, uint64_t y);
```
**Examples**:
@@ -3801,10 +3801,10 @@ expressions.
**Syntax**:
```c++
-uint8_t __builtin_rotateright8(uint8_t x, uint8_t y);
-uint16_t __builtin_rotateright16(uint16_t x, uint16_t y);
-uint32_t __builtin_rotateright32(uint32_t x, uint32_t y);
-uint64_t __builtin_rotateright64(uint64_t x, uint64_t y);
+constexpr uint8_t __builtin_rotateright8(uint8_t x, uint8_t y);
+constexpr uint16_t __builtin_rotateright16(uint16_t x, uint16_t y);
+constexpr uint32_t __builtin_rotateright32(uint32_t x, uint32_t y);
+constexpr uint64_t __builtin_rotateright64(uint64_t x, uint64_t y);
```
**Examples**:
@@ -3830,9 +3830,9 @@ expressions.
**Syntax**:
-```c
-T __builtin_stdc_rotate_left(T value, integer count);
-T __builtin_stdc_rotate_right(T value, integer count);
+```c++
+constexpr T __builtin_stdc_rotate_left(T value, integer count);
+constexpr T __builtin_stdc_rotate_right(T value, integer count);
```
where `T` is any unsigned integer type and `count` is any integer type.
@@ -3865,21 +3865,21 @@ unsigned _BitInt(20) rotated = __builtin_stdc_rotate_left(value, 5);
**Syntax**:
-```c
-unsigned int __builtin_stdc_leading_zeros(T value);
-unsigned int __builtin_stdc_leading_ones(T value);
-unsigned int __builtin_stdc_trailing_zeros(T value);
-unsigned int __builtin_stdc_trailing_ones(T value);
-unsigned int __builtin_stdc_first_leading_zero(T value);
-unsigned int __builtin_stdc_first_leading_one(T value);
-unsigned int __builtin_stdc_first_trailing_zero(T value);
-unsigned int __builtin_stdc_first_trailing_one(T value);
-unsigned int __builtin_stdc_count_zeros(T value);
-unsigned int __builtin_stdc_count_ones(T value);
-bool __builtin_stdc_has_single_bit(T value);
-unsigned int __builtin_stdc_bit_width(T value);
-T __builtin_stdc_bit_floor(T value);
-T __builtin_stdc_bit_ceil(T value);
+```c++
+constexpr unsigned int __builtin_stdc_leading_zeros(T value);
+constexpr unsigned int __builtin_stdc_leading_ones(T value);
+constexpr unsigned int __builtin_stdc_trailing_zeros(T value);
+constexpr unsigned int __builtin_stdc_trailing_ones(T value);
+constexpr unsigned int __builtin_stdc_first_leading_zero(T value);
+constexpr unsigned int __builtin_stdc_first_leading_one(T value);
+constexpr unsigned int __builtin_stdc_first_trailing_zero(T value);
+constexpr unsigned int __builtin_stdc_first_trailing_one(T value);
+constexpr unsigned int __builtin_stdc_count_zeros(T value);
+constexpr unsigned int __builtin_stdc_count_ones(T value);
+constexpr bool __builtin_stdc_has_single_bit(T value);
+constexpr unsigned int __builtin_stdc_bit_width(T value);
+constexpr T __builtin_stdc_bit_floor(T value);
+constexpr T __builtin_stdc_bit_ceil(T value);
```
where `T` is any unsigned integer type except `bool` and enumeration types,
@@ -3985,7 +3985,7 @@ anticipated to be the same as a statically known result.
**Syntax**:
```c++
-long __builtin_expect(long expr, long val);
+constexpr long __builtin_expect(long expr, long val);
```
**Example of use**:
@@ -4013,7 +4013,7 @@ takes a probability as third argument.
**Syntax**:
```c++
-long __builtin_expect_with_probability(long expr, long val, double p);
+constexpr long __builtin_expect_with_probability(long expr, long val, double p);
```
**Example of use**:
@@ -4114,7 +4114,7 @@ template<typename T> constexpr T *addressof(T &value) {
**Syntax**:
```c++
-void *__builtin_function_start(function);
+constexpr void *__builtin_function_start(function);
```
**Example of use**:
@@ -4191,7 +4191,7 @@ This builtin is used to implement `std::trivially_relocate`.
```c++
template <class Callee, class... Args>
-decltype(auto) __builtin_invoke(Callee&& callee, Args&&... args);
+constexpr decltype(auto) __builtin_invoke(Callee&& callee, Args&&... args);
```
`__builtin_invoke` is equivalent to `std::invoke`.
@@ -4433,7 +4433,7 @@ argument can be of any unsigned integer type or fixed boolean vector.
**Syntax**:
```c++
-int __builtin_popcountg(T x);
+constexpr int __builtin_popcountg(T x);
```
**Examples**:
@@ -4474,10 +4474,10 @@ only one argument is provided, then the behavior is undefined.
**Syntax**:
```c++
-int __builtin_clzg(T x);
-int __builtin_clzg(T x, int fallback);
-int __builtin_ctzg(T x);
-int __builtin_ctzg(T x, int fallback);
+constexpr int __builtin_clzg(T x);
+constexpr int __builtin_clzg(T x, int fallback);
+constexpr int __builtin_ctzg(T x);
+constexpr int __builtin_ctzg(T x, int fallback);
```
**Examples**:
@@ -4627,17 +4627,17 @@ z[3] = __builtin_addc(x[3], y[3], carryin, &carryout);
The complete list of builtins are:
-```c
-unsigned char __builtin_addcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
-unsigned short __builtin_addcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
-unsigned __builtin_addc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
-unsigned long __builtin_addcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
-unsigned long long __builtin_addcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
-unsigned char __builtin_subcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
-unsigned short __builtin_subcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
-unsigned __builtin_subc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
-unsigned long __builtin_subcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
-unsigned long long __builtin_subcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
+```c++
+constexpr unsigned char __builtin_addcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
+constexpr unsigned short __builtin_addcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
+constexpr unsigned __builtin_addc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
+constexpr unsigned long __builtin_addcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
+constexpr unsigned long long __builtin_addcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
+constexpr unsigned char __builtin_subcb (unsigned char x, unsigned char y, unsigned char carryin, unsigned char *carryout);
+constexpr unsigned short __builtin_subcs (unsigned short x, unsigned short y, unsigned short carryin, unsigned short *carryout);
+constexpr unsigned __builtin_subc (unsigned x, unsigned y, unsigned carryin, unsigned *carryout);
+constexpr unsigned long __builtin_subcl (unsigned long x, unsigned long y, unsigned long carryin, unsigned long *carryout);
+constexpr unsigned long long __builtin_subcll(unsigned long long x, unsigned long long y, unsigned long long carryin, unsigned long long *carryout);
```
### Checked Arithmetic Builtins
@@ -4660,28 +4660,28 @@ errorcode_t security_critical_application(...) {
Clang provides the following checked arithmetic builtins:
-```c
-bool __builtin_add_overflow (T x, U y, R *sum);
-bool __builtin_sub_overflow (T x, U y, R *diff);
-bool __builtin_mul_overflow (T x, U y, R *prod);
-bool __builtin_uadd_overflow (unsigned x, unsigned y, unsigned *sum);
-bool __builtin_uaddl_overflow (unsigned long x, unsigned long y, unsigned long *sum);
-bool __builtin_uaddll_overflow(unsigned long long x, unsigned long long y, unsigned long long *sum);
-bool __builtin_usub_overflow (unsigned x, unsigned y, unsigned *diff);
-bool __builtin_usubl_overflow (unsigned long x, unsigned long y, unsigned long *diff);
-bool __builtin_usubll_overflow(unsigned long long x, unsigned long long y, unsigned long long *diff);
-bool __builtin_umul_overflow (unsigned x, unsigned y, unsigned *prod);
-bool __builtin_umull_overflow (unsigned long x, unsigned long y, unsigned long *prod);
-bool __builtin_umulll_overflow(unsigned long long x, unsigned long long y, unsigned long long *prod);
-bool __builtin_sadd_overflow (int x, int y, int *sum);
-bool __builtin_saddl_overflow (long x, long y, long *sum);
-bool __builtin_saddll_overflow(long long x, long long y, long long *sum);
-bool __builtin_ssub_overflow (int x, int y, int *diff);
-bool __builtin_ssubl_overflow (long x, long y, long *diff);
-bool __builtin_ssubll_overflow(long long x, long long y, long long *diff);
-bool __builtin_smul_overflow (int x, int y, int *prod);
-bool __builtin_smull_overflow (long x, long y, long *prod);
-bool __builtin_smulll_overflow(long long x, long long y, long long *prod);
+```c++
+constexpr bool __builtin_add_overflow (T x, U y, R *sum);
+constexpr bool __builtin_sub_overflow (T x, U y, R *diff);
+constexpr bool __builtin_mul_overflow (T x, U y, R *prod);
+constexpr bool __builtin_uadd_overflow (unsigned x, unsigned y, unsigned *sum);
+constexpr bool __builtin_uaddl_overflow (unsigned long x, unsigned long y, unsigned long *sum);
+constexpr bool __builtin_uaddll_overflow(unsigned long long x, unsigned long long y, unsigned long long *sum);
+constexpr bool __builtin_usub_overflow (unsigned x, unsigned y, unsigned *diff);
+constexpr bool __builtin_usubl_overflow (unsigned long x, unsigned long y, unsigned long *diff);
+constexpr bool __builtin_usubll_overflow(unsigned long long x, unsigned long long y, unsigned long long *diff);
+constexpr bool __builtin_umul_overflow (unsigned x, unsigned y, unsigned *prod);
+constexpr bool __builtin_umull_overflow (unsigned long x, unsigned long y, unsigned long *prod);
+constexpr bool __builtin_umulll_overflow(unsigned long long x, unsigned long long y, unsigned long long *prod);
+constexpr bool __builtin_sadd_overflow (int x, int y, int *sum);
+constexpr bool __builtin_saddl_overflow (long x, long y, long *sum);
+constexpr bool __builtin_saddll_overflow(long long x, long long y, long long *sum);
+constexpr bool __builtin_ssub_overflow (int x, int y, int *diff);
+constexpr bool __builtin_ssubl_overflow (long x, long y, long *diff);
+constexpr bool __builtin_ssubll_overflow(long long x, long long y, long long *diff);
+constexpr bool __builtin_smul_overflow (int x, int y, int *prod);
+constexpr bool __builtin_smull_overflow (long x, long y, long *prod);
+constexpr bool __builtin_smulll_overflow(long long x, long long y, long long *prod);
```
Each builtin performs the specified mathematical operation on the
@@ -4710,8 +4710,8 @@ fall into one of the specified floating-point classes.
**Syntax**:
```c++
-int __builtin_isfpclass(fp_type expr, int mask);
-int_vector __builtin_isfpclass(fp_vector expr, int mask);
+constexpr int __builtin_isfpclass(fp_type expr, int mask);
+constexpr int_vector __builtin_isfpclass(fp_vector expr, int mask);
```
**Example of use**:
@@ -4806,22 +4806,22 @@ have unexpected behavior. Please see the section on [Accessing the floating poin
### String builtins
-```c
-void *__builtin_memchr(const void *, int, size_t);
-int __builtin_memcmp(const void *, const void *, size_t);
-int __builtin_bcmp(const void *, const void *, size_t); // deprecated BSD / POSIX alias
-char *__builtin_strchr(const char *, int);
-int __builtin_strcmp(const char *, const char *);
-size_t __builtin_strlen(const char *);
-int __builtin_strncmp(const char *, const char *, size_t);
-
-wchar_t *__builtin_wmemchr(const wchar_t *, wchar_t, size_t);
-int __builtin_wmemcmp(const wchar_t *, const wchar_t *, size_t);
-wchar_t *__builtin_wcschr(const wchar_t *, wchar_t);
-int __builtin_wcscmp(const wchar_t *, const wchar_t *);
-size_t __builtin_wcslen(const wchar_t *);
-int __builtin_wcsncmp(const wchar_t *, const wchar_t *, size_t);
-wchar_t *__builtin_wmemchr(const wchar_t *, wchar_t, size_t);
+```c++
+constexpr void *__builtin_memchr(const void *, int, size_t);
+constexpr int __builtin_memcmp(const void *, const void *, size_t);
+constexpr int __builtin_bcmp(const void *, const void *, size_t); // deprecated BSD / POSIX alias
+constexpr char *__builtin_strchr(const char *, int);
+constexpr int __builtin_strcmp(const char *, const char *);
+constexpr size_t __builtin_strlen(const char *);
+constexpr int __builtin_strncmp(const char *, const char *, size_t);
+
+constexpr wchar_t *__builtin_wmemchr(const wchar_t *, wchar_t, size_t);
+constexpr int __builtin_wmemcmp(const wchar_t *, const wchar_t *, size_t);
+constexpr wchar_t *__builtin_wcschr(const wchar_t *, wchar_t);
+constexpr int __builtin_wcscmp(const wchar_t *, const wchar_t *);
+constexpr size_t __builtin_wcslen(const wchar_t *);
+constexpr int __builtin_wcsncmp(const wchar_t *, const wchar_t *, size_t);
+constexpr wchar_t *__builtin_wmemchr(const wchar_t *, wchar_t, size_t);
```
Clang provides constant expression evaluation support for builtins forms of
@@ -4830,8 +4830,8 @@ the following functions from the C standard library headers
In addition to the above, one further builtin is provided:
-```c
-char *__builtin_char_memchr(const char *haystack, int needle, size_t size);
+```c++
+constexpr char *__builtin_char_memchr(const char *haystack, int needle, size_t size);
```
`__builtin_char_memchr(a, b, c)` is identical to
@@ -4911,13 +4911,12 @@ Clang provides constant expression evaluation support for builtin forms of the
following functions from the C standard library headers
`<string.h>` and `<wchar.h>`:
-- `memcpy`
-- `memmove`
-- `wmemcpy`
-- `wmemmove`
-
-In each case, the builtin form has the name of the C library function prefixed
-by `__builtin_`.
+```c++
+constexpr void *__builtin_memcpy(void *, const void *, size_t);
+constexpr void *__builtin_memmove(void *, const void *, size_t);
+constexpr wchar_t *__builtin_wmemcpy(wchar_t *, const wchar_t *, size_t);
+constexpr wchar_t *__builtin_wmemmove(wchar_t *, const wchar_t *, size_t);
+```
Constant evaluation support is only provided when the source and destination
are pointers to arrays with the same trivially copyable element type, and the
@@ -4967,7 +4966,7 @@ A type trait is used to check whether a type can be safely copied by `memcpy`.
**Syntax**:
```c++
-bool __is_bitwise_cloneable(type-id);
+consteval bool __is_bitwise_cloneable(type-id);
```
**Description**:
@@ -5222,14 +5221,14 @@ these builtins are also implemented by GCC.
**Syntax**:
-```c
-const char *__builtin_FILE();
-const char *__builtin_FILE_NAME(); // Clang only
-const char *__builtin_FUNCTION();
-const char *__builtin_FUNCSIG(); // Microsoft
-unsigned __builtin_LINE();
-unsigned __builtin_COLUMN(); // Clang only
-const std::source_location::__impl *__builtin_source_location();
+```c++
+consteval const char *__builtin_FILE();
+consteval const char *__builtin_FILE_NAME(); // Clang only
+consteval const char *__builtin_FUNCTION();
+consteval const char *__builtin_FUNCSIG(); // Microsoft
+consteval unsigned __builtin_LINE();
+consteval unsigned __builtin_COLUMN(); // Clang only
+consteval const std::source_location::__impl *__builtin_source_location();
```
**Example of use**:
@@ -5295,10 +5294,10 @@ arithmetic, they can perform semantic checking on the alignment value.
**Syntax**:
-```c
-T __builtin_align_up(T value, size_t alignment);
-T __builtin_align_down(T value, size_t alignment);
-bool __builtin_is_aligned(T value, size_t alignment);
+```c++
+constexpr T __builtin_align_up(T value, size_t alignment);
+constexpr T __builtin_align_down(T value, size_t alignment);
+constexpr bool __builtin_is_aligned(T value, size_t alignment);
```
**Example of use**:
More information about the cfe-commits
mailing list