| 1 | /*===---- stdckdint.h - Standard header for checking integer----------------=== |
| 2 | * |
| 3 | * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | * See https://llvm.org/LICENSE.txt for license information. |
| 5 | * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | * |
| 7 | *===-----------------------------------------------------------------------=== |
| 8 | */ |
| 9 | |
| 10 | #ifndef __STDCKDINT_H |
| 11 | #define __STDCKDINT_H |
| 12 | |
| 13 | /* If we're hosted, fall back to the system's stdckdint.h. FreeBSD, for |
| 14 | * example, already has a Clang-compatible stdckdint.h header. |
| 15 | * |
| 16 | * The `stdckdint.h` header requires C 23 or newer. |
| 17 | */ |
| 18 | #if __STDC_HOSTED__ && __has_include_next(<stdckdint.h>) |
| 19 | #include_next <stdckdint.h> |
| 20 | #else |
| 21 | |
| 22 | /* C23 7.20.1 Defines several macros for performing checked integer arithmetic*/ |
| 23 | |
| 24 | #define __STDC_VERSION_STDCKDINT_H__ 202311L |
| 25 | |
| 26 | // Both A and B shall be any integer type other than "plain" char, bool, a bit- |
| 27 | // precise integer type, or an enumerated type, and they need not be the same. |
| 28 | |
| 29 | // R shall be a modifiable lvalue of any integer type other than "plain" char, |
| 30 | // bool, a bit-precise integer type, or an enumerated type. It shouldn't be |
| 31 | // short type, either. Otherwise, it may be unable to hold two the result of |
| 32 | // operating two 'int's. |
| 33 | |
| 34 | // A diagnostic message will be produced if A or B are not suitable integer |
| 35 | // types, or if R is not a modifiable lvalue of a suitable integer type or R |
| 36 | // is short type. |
| 37 | #define ckd_add(R, A, B) __builtin_add_overflow((A), (B), (R)) |
| 38 | #define ckd_sub(R, A, B) __builtin_sub_overflow((A), (B), (R)) |
| 39 | #define ckd_mul(R, A, B) __builtin_mul_overflow((A), (B), (R)) |
| 40 | |
| 41 | #endif /* __STDC_HOSTED__ */ |
| 42 | #endif /* __STDCKDINT_H */ |