Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 1 | /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 1993, 1994, 1995, 1996, 1997 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. All advertising materials mentioning features or use of this software |
| 15 | * must display the following acknowledgement: |
| 16 | * This product includes software developed by the Computer Systems |
| 17 | * Engineering Group at Lawrence Berkeley Laboratory. |
| 18 | * 4. Neither the name of the University nor of the Laboratory may be used |
| 19 | * to endorse or promote products derived from this software without |
| 20 | * specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 32 | * SUCH DAMAGE. |
| 33 | */ |
| 34 | |
| 35 | #ifndef lib_funcattrs_h |
| 36 | #define lib_funcattrs_h |
| 37 | |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 38 | #include "compiler-tests.h" |
| 39 | |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 40 | /* |
| 41 | * Attributes to apply to functions and their arguments, using various |
| 42 | * compiler-specific extensions. |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * This was introduced by Clang: |
| 47 | * |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 48 | * https://clang.llvm.org/docs/LanguageExtensions.html#has-attribute |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 49 | * |
| 50 | * in some version (which version?); it has been picked up by GCC 5.0. |
| 51 | */ |
| 52 | #ifndef __has_attribute |
| 53 | /* |
| 54 | * It's a macro, so you can check whether it's defined to check |
| 55 | * whether it's supported. |
| 56 | * |
| 57 | * If it's not, define it to always return 0, so that we move on to |
| 58 | * the fallback checks. |
| 59 | */ |
| 60 | #define __has_attribute(x) 0 |
| 61 | #endif |
| 62 | |
| 63 | /* |
| 64 | * NORETURN, before a function declaration, means "this function |
| 65 | * never returns". (It must go before the function declaration, e.g. |
| 66 | * "extern NORETURN func(...)" rather than after the function |
| 67 | * declaration, as the MSVC version has to go before the declaration.) |
| 68 | */ |
| 69 | #if __has_attribute(noreturn) \ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 70 | || ND_IS_AT_LEAST_GNUC_VERSION(2,5) \ |
| 71 | || ND_IS_AT_LEAST_SUNC_VERSION(5,9) \ |
| 72 | || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \ |
| 73 | || ND_IS_AT_LEAST_HP_C_VERSION(6,10) |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 74 | /* |
| 75 | * Compiler with support for __attribute((noreturn)), or GCC 2.5 and |
| 76 | * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1 |
| 77 | * and later (do any earlier versions of XL C support this?), or |
| 78 | * HP aCC A.06.10 and later. |
| 79 | */ |
| 80 | #define NORETURN __attribute((noreturn)) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * However, GCC didn't support that for function *pointers* until GCC |
| 84 | * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481. |
| 85 | * |
| 86 | * Sun C/Oracle Studio C doesn't seem to support it, either. |
| 87 | */ |
| 88 | #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401)) \ |
| 89 | || (defined(__SUNPRO_C)) |
| 90 | #define NORETURN_FUNCPTR |
| 91 | #else |
| 92 | #define NORETURN_FUNCPTR __attribute((noreturn)) |
| 93 | #endif |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 94 | #elif defined(_MSC_VER) |
| 95 | /* |
| 96 | * MSVC. |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 97 | * It doesn't allow __declspec(noreturn) to be applied to function |
| 98 | * pointers. |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 99 | */ |
| 100 | #define NORETURN __declspec(noreturn) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 101 | #define NORETURN_FUNCPTR |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 102 | #else |
| 103 | #define NORETURN |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 104 | #define NORETURN_FUNCPTR |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 105 | #endif |
| 106 | |
| 107 | /* |
| 108 | * PRINTFLIKE(x,y), after a function declaration, means "this function |
| 109 | * does printf-style formatting, with the xth argument being the format |
| 110 | * string and the yth argument being the first argument for the format |
| 111 | * string". |
| 112 | */ |
| 113 | #if __has_attribute(__format__) \ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 114 | || ND_IS_AT_LEAST_GNUC_VERSION(2,3) \ |
| 115 | || ND_IS_AT_LEAST_XL_C_VERSION(10,1) \ |
| 116 | || ND_IS_AT_LEAST_HP_C_VERSION(6,10) |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 117 | /* |
| 118 | * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1 |
| 119 | * and later (do any earlier versions of XL C support this?), |
| 120 | * or HP aCC A.06.10 and later. |
| 121 | */ |
| 122 | #define PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y))) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * However, GCC didn't support that for function *pointers* until GCC |
| 126 | * 4.1.0; see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3481. |
| 127 | */ |
| 128 | #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) < 401)) |
| 129 | #define PRINTFLIKE_FUNCPTR(x,y) |
| 130 | #else |
| 131 | #define PRINTFLIKE_FUNCPTR(x,y) __attribute__((__format__(__printf__,x,y))) |
| 132 | #endif |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 133 | #else |
| 134 | #define PRINTFLIKE(x,y) |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 135 | #define PRINTFLIKE_FUNCPTR(x,y) |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 136 | #endif |
| 137 | |
| 138 | /* |
| 139 | * For flagging arguments as format strings in MSVC. |
| 140 | */ |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 141 | #ifdef _MSC_VER |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 142 | #include <sal.h> |
Elliott Hughes | 820eced | 2021-08-20 18:00:50 -0700 | [diff] [blame] | 143 | #define FORMAT_STRING(p) _Printf_format_string_ p |
Elliott Hughes | cec480a | 2017-12-19 16:54:57 -0800 | [diff] [blame] | 144 | #else |
| 145 | #define FORMAT_STRING(p) p |
| 146 | #endif |
| 147 | |
| 148 | #endif /* lib_funcattrs_h */ |