xref: /netbsd-src/external/mpl/bind/dist/lib/isc/include/isc/attributes.h (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: attributes.h,v 1.4 2025/01/26 16:25:40 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #pragma once
17 
18 /***
19  *** Clang Compatibility Macros
20  ***/
21 
22 #if (!defined(__has_c_attribute) || defined(__lint__)) && !defined(__clang__)
23 #define __has_c_attribute(x) 0
24 #endif /* if !defined(__has_c_attribute) */
25 
26 #ifdef HAVE_STDNORETURN_H
27 #include <stdnoreturn.h>
28 #elif HAVE_FUNC_ATTRIBUTE_NORETURN
29 #define noreturn __attribute__((noreturn))
30 #else
31 #define noreturn
32 #endif
33 
34 #if HAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL
35 #define ISC_ATTR_RETURNS_NONNULL __attribute__((returns_nonnull))
36 #else
37 #define ISC_ATTR_RETURNS_NONNULL
38 #endif
39 
40 #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC
41 /*
42  * Indicates that a function is malloc-like, i.e., that the
43  * pointer P returned by the function cannot alias any other
44  * pointer valid when the function returns.
45  */
46 #define ISC_ATTR_MALLOC __attribute__((malloc))
47 #if HAVE_MALLOC_EXT_ATTR
48 /*
49  * Associates deallocator as a suitable deallocation function
50  * for pointers returned from the function marked with the attribute.
51  */
52 #define ISC_ATTR_DEALLOCATOR(deallocator) __attribute__((malloc(deallocator)))
53 /*
54  * Similar to ISC_ATTR_DEALLOCATOR, but allows to speficy an index "idx",
55  * which denotes the positional argument to which when the pointer is passed
56  * in calls to deallocator has the effect of deallocating it.
57  */
58 #define ISC_ATTR_DEALLOCATOR_IDX(deallocator, idx) \
59 	__attribute__((malloc(deallocator, idx)))
60 /*
61  * Combines both ISC_ATTR_MALLOC and ISC_ATTR_DEALLOCATOR attributes.
62  */
63 #define ISC_ATTR_MALLOC_DEALLOCATOR(deallocator) \
64 	__attribute__((malloc, malloc(deallocator)))
65 /*
66  * Similar to ISC_ATTR_MALLOC_DEALLOCATOR, but allows to speficy an index "idx",
67  * which denotes the positional argument to which when the pointer is passed
68  * in calls to deallocator has the effect of deallocating it.
69  */
70 #define ISC_ATTR_MALLOC_DEALLOCATOR_IDX(deallocator, idx) \
71 	__attribute__((malloc, malloc(deallocator, idx)))
72 #else /* #ifdef HAVE_MALLOC_EXT_ATTR */
73 /*
74  * There is support for malloc attribute but not for
75  * extended attributes, so macros that combine attribute malloc
76  * with a deallocator will only expand to malloc attribute.
77  */
78 #define ISC_ATTR_DEALLOCATOR(deallocator)
79 #define ISC_ATTR_DEALLOCATOR_IDX(deallocator, idx)
80 #define ISC_ATTR_MALLOC_DEALLOCATOR(deallocator)	  ISC_ATTR_MALLOC
81 #define ISC_ATTR_MALLOC_DEALLOCATOR_IDX(deallocator, idx) ISC_ATTR_MALLOC
82 #endif
83 #else /* #ifdef HAVE_FUNC_ATTRIBUTE_MALLOC */
84 /*
85  * There is no support for malloc attribute.
86  */
87 #define ISC_ATTR_MALLOC
88 #define ISC_ATTR_DEALLOCATOR(deallocator)
89 #define ISC_ATTR_DEALLOCATOR_IDX(deallocator, idx)
90 #define ISC_ATTR_MALLOC_DEALLOCATOR(deallocator)
91 #define ISC_ATTR_MALLOC_DEALLOCATOR_IDX(deallocator, idx)
92 #endif /* HAVE_FUNC_ATTRIBUTE_MALLOC */
93 
94 #if __has_c_attribute(fallthrough)
95 #define FALLTHROUGH [[fallthrough]]
96 #elif __GNUC__ >= 7 && !defined(__clang__)
97 #define FALLTHROUGH __attribute__((fallthrough))
98 #else
99 /* clang-format off */
100 #define FALLTHROUGH do {} while (0) /* FALLTHROUGH */
101 /* clang-format on */
102 #endif
103 
104 #if __has_c_attribute(maybe_unused)
105 #define ISC_ATTR_UNUSED [[maybe_unused]]
106 #else
107 #define ISC_ATTR_UNUSED __attribute__((__unused__))
108 #endif
109 
110 #if __STDC_VERSION__ >= 202311L
111 #define ISC_CONSTEXPR constexpr
112 #else
113 #define ISC_CONSTEXPR static const
114 #endif
115