xref: /openbsd-src/gnu/llvm/compiler-rt/lib/orc/compiler.h (revision 810390e339a5425391477d5d41c78d7cab2424ac)
1d89ec533Spatrick //===--------- compiler.h - Compiler abstraction support --------*- C++ -*-===//
2d89ec533Spatrick //
3d89ec533Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d89ec533Spatrick // See https://llvm.org/LICENSE.txt for license information.
5d89ec533Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d89ec533Spatrick //
7d89ec533Spatrick //===----------------------------------------------------------------------===//
8d89ec533Spatrick //
9d89ec533Spatrick // This file is a part of the ORC runtime support library.
10d89ec533Spatrick //
11d89ec533Spatrick // Most functionality in this file was swiped from llvm/Support/Compiler.h.
12d89ec533Spatrick //
13d89ec533Spatrick //===----------------------------------------------------------------------===//
14d89ec533Spatrick 
15d89ec533Spatrick #ifndef ORC_RT_COMPILER_H
16d89ec533Spatrick #define ORC_RT_COMPILER_H
17d89ec533Spatrick 
18*810390e3Srobert #if defined(_WIN32)
19*810390e3Srobert #define ORC_RT_INTERFACE extern "C"
20*810390e3Srobert #define ORC_RT_HIDDEN
21*810390e3Srobert #define ORC_RT_IMPORT extern "C" __declspec(dllimport)
22*810390e3Srobert #else
23d89ec533Spatrick #define ORC_RT_INTERFACE extern "C" __attribute__((visibility("default")))
24d89ec533Spatrick #define ORC_RT_HIDDEN __attribute__((visibility("hidden")))
25*810390e3Srobert #define ORC_RT_IMPORT extern "C"
26*810390e3Srobert #endif
27d89ec533Spatrick 
28d89ec533Spatrick #ifndef __has_builtin
29d89ec533Spatrick # define __has_builtin(x) 0
30d89ec533Spatrick #endif
31d89ec533Spatrick 
32d89ec533Spatrick // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
33d89ec533Spatrick // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
34d89ec533Spatrick #ifndef ORC_RT_HAS_CPP_ATTRIBUTE
35d89ec533Spatrick #if defined(__cplusplus) && defined(__has_cpp_attribute)
36d89ec533Spatrick #define ORC_RT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
37d89ec533Spatrick #else
38d89ec533Spatrick #define ORC_RT_HAS_CPP_ATTRIBUTE(x) 0
39d89ec533Spatrick #endif
40d89ec533Spatrick #endif
41d89ec533Spatrick 
42d89ec533Spatrick // Use the 'nodiscard' attribute in C++17 or newer mode.
43d89ec533Spatrick #if defined(__cplusplus) && __cplusplus > 201402L &&                           \
44d89ec533Spatrick     ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
45d89ec533Spatrick #define ORC_RT_NODISCARD [[nodiscard]]
46d89ec533Spatrick #elif ORC_RT_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
47d89ec533Spatrick #define ORC_RT_NODISCARD [[clang::warn_unused_result]]
48d89ec533Spatrick // Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
49d89ec533Spatrick // warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
50d89ec533Spatrick // Use the 'nodiscard' attribute in C++14 mode only with GCC.
51d89ec533Spatrick // TODO: remove this workaround when PR33518 is resolved.
52d89ec533Spatrick #elif defined(__GNUC__) && ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
53d89ec533Spatrick #define ORC_RT_NODISCARD [[nodiscard]]
54d89ec533Spatrick #else
55d89ec533Spatrick #define ORC_RT_NODISCARD
56d89ec533Spatrick #endif
57d89ec533Spatrick 
58d89ec533Spatrick #if __has_builtin(__builtin_expect)
59d89ec533Spatrick #define ORC_RT_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
60d89ec533Spatrick #define ORC_RT_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
61d89ec533Spatrick #else
62d89ec533Spatrick #define ORC_RT_LIKELY(EXPR) (EXPR)
63d89ec533Spatrick #define ORC_RT_UNLIKELY(EXPR) (EXPR)
64d89ec533Spatrick #endif
65d89ec533Spatrick 
66*810390e3Srobert #if defined(__APPLE__)
67d89ec533Spatrick #define ORC_RT_WEAK_IMPORT __attribute__((weak_import))
68*810390e3Srobert #elif defined(_WIN32)
69*810390e3Srobert #define ORC_RT_WEAK_IMPORT
70d89ec533Spatrick #else
71d89ec533Spatrick #define ORC_RT_WEAK_IMPORT __attribute__((weak))
72d89ec533Spatrick #endif
73d89ec533Spatrick 
74d89ec533Spatrick #endif // ORC_RT_COMPILER_H
75