Skip to content

Commit 3ce25ab

Browse files
authored
[orc-rt] Add error.h: structured error support. (#154869)
Adds support for the Error class, Expected class template, and related APIs that will be used for error propagation and handling in the new ORC runtime. The implementations of these types are cut-down versions of similar APIs in llvm/Support/Error.h. Most advice on llvm::Error and llvm::Expected (e.g. from the LLVM Programmer's manual) applies equally to orc_rt::Error and orc_rt::Expected. Ported from the old ORC runtime at compiler-rt/lib/orc.
1 parent 783859b commit 3ce25ab

File tree

5 files changed

+1024
-0
lines changed

5 files changed

+1024
-0
lines changed

orc-rt/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
set(ORC_RT_HEADERS
22
orc-rt-c/orc-rt.h
33
orc-rt/bitmask-enum.h
4+
orc-rt/error.h
5+
orc-rt/compiler.h
46
orc-rt/math.h
57
orc-rt/rtti.h
68
orc-rt/span.h

orc-rt/include/orc-rt/compiler.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
#if defined(_WIN32)
19+
#define ORC_RT_INTERFACE extern "C"
20+
#define ORC_RT_HIDDEN
21+
#define ORC_RT_IMPORT extern "C" __declspec(dllimport)
22+
#else
23+
#define ORC_RT_INTERFACE extern "C" __attribute__((visibility("default")))
24+
#define ORC_RT_HIDDEN __attribute__((visibility("hidden")))
25+
#define ORC_RT_IMPORT extern "C"
26+
#endif
27+
28+
#ifndef __has_builtin
29+
#define __has_builtin(x) 0
30+
#endif
31+
32+
// Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
33+
// C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
34+
#ifndef ORC_RT_HAS_CPP_ATTRIBUTE
35+
#if defined(__cplusplus) && defined(__has_cpp_attribute)
36+
#define ORC_RT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
37+
#else
38+
#define ORC_RT_HAS_CPP_ATTRIBUTE(x) 0
39+
#endif
40+
#endif
41+
42+
// Use the 'nodiscard' attribute in C++17 or newer mode.
43+
#if defined(__cplusplus) && __cplusplus > 201402L && \
44+
ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
45+
#define ORC_RT_NODISCARD [[nodiscard]]
46+
#elif ORC_RT_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
47+
#define ORC_RT_NODISCARD [[clang::warn_unused_result]]
48+
// Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
49+
// warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
50+
// Use the 'nodiscard' attribute in C++14 mode only with GCC.
51+
// TODO: remove this workaround when PR33518 is resolved.
52+
#elif defined(__GNUC__) && ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
53+
#define ORC_RT_NODISCARD [[nodiscard]]
54+
#else
55+
#define ORC_RT_NODISCARD
56+
#endif
57+
58+
#if __has_builtin(__builtin_expect)
59+
#define ORC_RT_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
60+
#define ORC_RT_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
61+
#else
62+
#define ORC_RT_LIKELY(EXPR) (EXPR)
63+
#define ORC_RT_UNLIKELY(EXPR) (EXPR)
64+
#endif
65+
66+
#if defined(__APPLE__)
67+
#define ORC_RT_WEAK_IMPORT __attribute__((weak_import))
68+
#elif defined(_WIN32)
69+
#define ORC_RT_WEAK_IMPORT
70+
#else
71+
#define ORC_RT_WEAK_IMPORT __attribute__((weak))
72+
#endif
73+
74+
#endif // ORC_RT_COMPILER_H

0 commit comments

Comments
 (0)