Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * attributes.h -- internal compiler attribute abstractions
      3  *
      4  * Copyright (c) 2023-2024, NLnet Labs. All rights reserved.
      5  *
      6  * SPDX-License-Identifier: BSD-3-Clause
      7  *
      8  */
      9 #include "zone/attributes.h"
     10 
     11 #ifndef ATTRIBUTES_H
     12 #define ATTRIBUTES_H
     13 
     14 #define nonnull(params) zone_nonnull(params)
     15 #define nonnull_all zone_nonnull_all
     16 
     17 #if _MSC_VER
     18 # define really_inline __forceinline
     19 # define never_inline __declspec(noinline)
     20 # define warn_unused_result
     21 # define no_sanitize_undefined
     22 
     23 # define likely(params) (params)
     24 # define unlikely(params) (params)
     25 
     26 #else // _MSC_VER
     27 #if defined __has_builtin
     28 # define has_builtin(params) __has_builtin(params)
     29 #else
     30 # define has_builtin(params) (0)
     31 #endif
     32 
     33 # if (zone_has_attribute(always_inline) || zone_has_gnuc(3, 1)) && ! defined __NO_INLINE__
     34     // Compilation using GCC 4.2.1 without optimizations fails.
     35     //   sorry, unimplemented: inlining failed in call to ...
     36     // GCC 4.1.2 and GCC 4.30 compile forward declared functions annotated
     37     // with __attribute__((always_inline)) without problems. Test if
     38     // __NO_INLINE__ is defined and define macro accordingly.
     39 #   define really_inline inline __attribute__((always_inline))
     40 # else
     41 #   define really_inline inline
     42 # endif
     43 
     44 # if zone_has_attribute(noinline) || zone_has_gnuc(2, 96)
     45 #   define never_inline __attribute__((noinline))
     46 # else
     47 #   define never_inline
     48 # endif
     49 
     50 # if zone_has_attribute(warn_unused_result)
     51 #   define warn_unused_result __attribute__((warn_unused_result))
     52 # else
     53 #   define warn_unused_result
     54 # endif
     55 
     56 # if zone_has_attribute(no_sanitize)
     57     // GCC 8.1 added the no_sanitize function attribute.
     58 #   define no_sanitize_undefined __attribute__((no_sanitize("undefined")))
     59 # elif zone_has_attribute(no_sanitize_undefined)
     60     // GCC 4.9.0 added the UndefinedBehaviorSanitizer (ubsan) and the
     61     // no_sanitize_undefined function attribute.
     62 #   define no_sanitize_undefined
     63 # else
     64 #   define no_sanitize_undefined
     65 # endif
     66 
     67 # if has_builtin(__builtin_expect)
     68 #   define likely(params) __builtin_expect(!!(params), 1)
     69 #   define unlikely(params) __builtin_expect(!!(params), 0)
     70 # else
     71 #   define likely(params) (params)
     72 #   define unlikely(params) (params)
     73 # endif
     74 #endif
     75 
     76 #endif // ATTRIBUTES_H
     77