Qpid Proton C  0.40.0
annotations.h
1 #ifndef PROTON_ANNOTATIONS_H
2 #define PROTON_ANNOTATIONS_H 1
3 
4 /*
5  * Copyright (c) Meta Platforms, Inc. and affiliates.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
43 // compiler specific attribute translation
44 // msvc should come first, so if clang is in msvc mode it gets the right defines
45 
46 // warn format placeholders
47 
48 // NOTE: this will only do checking in msvc with versions that support /analyze
49 #ifdef _MSC_VER
50 
51  #include <stddef.h>
52 
53  #ifdef _USE_ATTRIBUTES_FOR_SAL
54  #undef _USE_ATTRIBUTES_FOR_SAL
55  #endif
56 
57  #define _USE_ATTRIBUTES_FOR_SAL 1
58  #include <sal.h>
59 
60  #define PN_PRINTF_FORMAT _Printf_format_string_
61  #define PN_PRINTF_FORMAT_ATTR(format_param, dots_param)
62 
63 #elif defined(__GNUC__)
64 
65  #define PN_PRINTF_FORMAT
66  #define PN_PRINTF_FORMAT_ATTR(format_param, dots_param) \
67  __attribute__((__format__(__printf__, format_param, dots_param)))
68 
69 #else
70 
71  #define PN_PRINTF_FORMAT
72  #define PN_PRINTF_FORMAT_ATTR(format_param, dots_param)
73 
74 #endif
75 
76 
77 // warn unused result
78 #if defined(__has_cpp_attribute)
79  #if __has_cpp_attribute(nodiscard)
80  #define PN_NODISCARD [[nodiscard]]
81  #endif
82 #endif
83 #if !defined PN_NODISCARD
84  #if defined(_MSC_VER) && (_MSC_VER >= 1700)
85  #define PN_NODISCARD _Check_return_
86  #elif defined(__GNUC__)
87  #define PN_NODISCARD __attribute__((__warn_unused_result__))
88  #else
89  #define PN_NODISCARD
90  #endif
91 #endif
92 
93 // fallthrough
94 #if defined __cplusplus && defined __has_cpp_attribute
95  #if __has_cpp_attribute(fallthrough) && __cplusplus >= __has_cpp_attribute(fallthrough)
96  #define PN_FALLTHROUGH [[fallthrough]]
97  #endif
98 #elif defined __STDC_VERSION__ && defined __has_c_attribute
99  #if __has_c_attribute(fallthrough) && __STDC_VERSION__ >= __has_c_attribute(fallthrough)
100  #define PN_FALLTHROUGH [[fallthrough]]
101  #endif
102 #endif
103 #if !defined PN_FALLTHROUGH && defined __has_attribute
104  #if __has_attribute(__fallthrough__)
105  #define PN_FALLTHROUGH __attribute__((__fallthrough__))
106  #endif
107 #endif
108 #if !defined PN_FALLTHROUGH
109  #define PN_FALLTHROUGH (void)0
110 #endif
111 
112 // Generalize warning push/pop.
113 #if defined(__GNUC__) || defined(__clang__)
114  // Clang & GCC
115  #define PN_PUSH_WARNING _Pragma("GCC diagnostic push")
116  #define PN_POP_WARNING _Pragma("GCC diagnostic pop")
117  #define PN_GNU_DISABLE_WARNING_INTERNAL2(warningName) #warningName
118  #define PN_GNU_DISABLE_WARNING(warningName) _Pragma(PN_GNU_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
119  #ifdef __clang__
120  #define PN_CLANG_DISABLE_WARNING(warningName) PN_GNU_DISABLE_WARNING(warningName)
121  #define PN_GCC_DISABLE_WARNING(warningName)
122  #else
123  #define PN_CLANG_DISABLE_WARNING(warningName)
124  #define PN_GCC_DISABLE_WARNING(warningName) PN_GNU_DISABLE_WARNING(warningName)
125  #endif
126  #define PN_MSVC_DISABLE_WARNING(warningNumber)
127 #elif defined(_MSC_VER)
128  #define PN_PUSH_WARNING __pragma(warning(push))
129  #define PN_POP_WARNING __pragma(warning(pop))
130  // Disable the GCC warnings.
131  #define PN_GNU_DISABLE_WARNING(warningName)
132  #define PN_GCC_DISABLE_WARNING(warningName)
133  #define PN_CLANG_DISABLE_WARNING(warningName)
134  #define PN_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable : warningNumber))
135 #else
136  #define PN_PUSH_WARNING
137  #define PN_POP_WARNING
138  #define PN_GNU_DISABLE_WARNING(warningName)
139  #define PN_GCC_DISABLE_WARNING(warningName)
140  #define PN_CLANG_DISABLE_WARNING(warningName)
141  #define PN_MSVC_DISABLE_WARNING(warningNumber)
142 #endif
143 
144 #endif /* annotations.h */