xref: /llvm-project/libc/include/__llvm-libc-common.h (revision a4e87da963a67aed33b672582406d576553b2399)
1 //===-- Common definitions for LLVM-libc public header files --------------===//
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 #ifndef LLVM_LIBC_COMMON_H
10 #define LLVM_LIBC_COMMON_H
11 
12 #ifdef __cplusplus
13 
14 #undef __BEGIN_C_DECLS
15 #define __BEGIN_C_DECLS extern "C" {
16 
17 #undef __END_C_DECLS
18 #define __END_C_DECLS }
19 
20 // Standard C++ doesn't have C99 restrict but GNU C++ has it with __ spelling.
21 #undef __restrict
22 #ifndef __GNUC__
23 #define __restrict
24 #endif
25 
26 #undef _Noreturn
27 #define _Noreturn [[noreturn]]
28 
29 #undef _Alignas
30 #define _Alignas alignas
31 
32 #undef _Static_assert
33 #define _Static_assert static_assert
34 
35 #undef _Alignof
36 #define _Alignof alignof
37 
38 #undef _Thread_local
39 #define _Thread_local thread_local
40 
41 #undef __NOEXCEPT
42 #if __cplusplus >= 201103L
43 #define __NOEXCEPT noexcept
44 #else
45 #define __NOEXCEPT throw()
46 #endif
47 
48 #else // not __cplusplus
49 
50 #undef __BEGIN_C_DECLS
51 #define __BEGIN_C_DECLS
52 
53 #undef __END_C_DECLS
54 #define __END_C_DECLS
55 
56 #undef __restrict
57 #if __STDC_VERSION__ >= 199901L
58 // C99 and above support the restrict keyword.
59 #define __restrict restrict
60 #elif !defined(__GNUC__)
61 // GNU-compatible compilers accept the __ spelling in all modes.
62 // Otherwise, omit the qualifier for pure C89 compatibility.
63 #define __restrict
64 #endif
65 
66 #undef _Noreturn
67 #if __STDC_VERSION__ >= 201112L
68 // In C11 and later, _Noreturn is a keyword.
69 #elif defined(__GNUC__)
70 // GNU-compatible compilers have an equivalent attribute.
71 #define _Noreturn __attribute__((__noreturn__))
72 #else
73 #define _Noreturn
74 #endif
75 
76 #undef __NOEXCEPT
77 #ifdef __GNUC__
78 #define __NOEXCEPT __attribute__((__nothrow__))
79 #else
80 #define __NOEXCEPT
81 #endif
82 
83 #endif // __cplusplus
84 
85 #endif // LLVM_LIBC_COMMON_H
86