xref: /freebsd/contrib/llvm-project/compiler-rt/lib/orc/compiler.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 //===--------- compiler.h - Compiler abstraction support --------*- C++ -*-===//
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 // This file is a part of the ORC runtime support library.
10 //
11 // Most functionality in this file was swiped from llvm/Support/Compiler.h.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef ORC_RT_COMPILER_H
16 #define ORC_RT_COMPILER_H
17 
18 #define ORC_RT_INTERFACE extern "C" __attribute__((visibility("default")))
19 #define ORC_RT_HIDDEN __attribute__((visibility("hidden")))
20 
21 #ifndef __has_builtin
22 # define __has_builtin(x) 0
23 #endif
24 
25 // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
26 // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
27 #ifndef ORC_RT_HAS_CPP_ATTRIBUTE
28 #if defined(__cplusplus) && defined(__has_cpp_attribute)
29 #define ORC_RT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
30 #else
31 #define ORC_RT_HAS_CPP_ATTRIBUTE(x) 0
32 #endif
33 #endif
34 
35 // Use the 'nodiscard' attribute in C++17 or newer mode.
36 #if defined(__cplusplus) && __cplusplus > 201402L &&                           \
37     ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
38 #define ORC_RT_NODISCARD [[nodiscard]]
39 #elif ORC_RT_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
40 #define ORC_RT_NODISCARD [[clang::warn_unused_result]]
41 // Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
42 // warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
43 // Use the 'nodiscard' attribute in C++14 mode only with GCC.
44 // TODO: remove this workaround when PR33518 is resolved.
45 #elif defined(__GNUC__) && ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
46 #define ORC_RT_NODISCARD [[nodiscard]]
47 #else
48 #define ORC_RT_NODISCARD
49 #endif
50 
51 #if __has_builtin(__builtin_expect)
52 #define ORC_RT_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
53 #define ORC_RT_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
54 #else
55 #define ORC_RT_LIKELY(EXPR) (EXPR)
56 #define ORC_RT_UNLIKELY(EXPR) (EXPR)
57 #endif
58 
59 #ifdef __APPLE__
60 #define ORC_RT_WEAK_IMPORT __attribute__((weak_import))
61 #else
62 #define ORC_RT_WEAK_IMPORT __attribute__((weak))
63 #endif
64 
65 #endif // ORC_RT_COMPILER_H
66