1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#[deny(missing_docs)]
pub mod error_handling;
use libc::c_char;
use llvm_sys::core::{LLVMCreateMessage, LLVMDisposeMessage};
use llvm_sys::support::LLVMLoadLibraryPermanently;
use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
use std::ffi::{CString, CStr};
use std::ops::Deref;
#[derive(Eq)]
pub struct LLVMString {
pub(crate) ptr: *const c_char,
}
impl LLVMString {
pub(crate) fn new(ptr: *const c_char) -> Self {
LLVMString {
ptr,
}
}
pub fn to_string(&self) -> String {
(*self).to_string_lossy().into_owned()
}
pub(crate) fn create(string: &str) -> LLVMString {
debug_assert_eq!(string.as_bytes()[string.as_bytes().len() - 1], 0);
let ptr = unsafe {
LLVMCreateMessage(string.as_ptr() as *const _)
};
LLVMString::new(ptr)
}
}
impl Deref for LLVMString {
type Target = CStr;
fn deref(&self) -> &Self::Target {
unsafe {
CStr::from_ptr(self.ptr)
}
}
}
impl Debug for LLVMString {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self.deref())
}
}
impl Display for LLVMString {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self.deref())
}
}
impl PartialEq for LLVMString {
fn eq(&self, other: &LLVMString) -> bool {
**self == **other
}
}
impl Error for LLVMString {
fn description(&self) -> &str {
self.to_str().expect("Could not convert LLVMString to str (likely invalid unicode)")
}
fn cause(&self) -> Option<&dyn Error> {
None
}
}
impl Drop for LLVMString {
fn drop(&mut self) {
unsafe {
LLVMDisposeMessage(self.ptr as *mut _);
}
}
}
#[derive(Eq)]
pub(crate) enum LLVMStringOrRaw {
Owned(LLVMString),
Borrowed(*const c_char),
}
impl LLVMStringOrRaw {
pub fn as_str(&self) -> &CStr {
match self {
LLVMStringOrRaw::Owned(llvm_string) => llvm_string.deref(),
LLVMStringOrRaw::Borrowed(ptr) => unsafe {
CStr::from_ptr(*ptr)
},
}
}
}
impl PartialEq for LLVMStringOrRaw {
fn eq(&self, other: &LLVMStringOrRaw) -> bool {
self.as_str() == other.as_str()
}
}
pub unsafe fn shutdown_llvm() {
use llvm_sys::core::LLVMShutdown;
LLVMShutdown()
}
pub fn load_library_permanently(filename: &str) -> bool {
let filename = CString::new(filename).expect("Conversion to CString failed unexpectedly");
unsafe {
LLVMLoadLibraryPermanently(filename.as_ptr()) == 1
}
}
pub fn is_multithreaded() -> bool {
use llvm_sys::core::LLVMIsMultithreaded;
unsafe {
LLVMIsMultithreaded() == 1
}
}
pub fn enable_llvm_pretty_stack_trace() {
#[llvm_versions(3.6..=3.7)]
use llvm_sys::core::LLVMEnablePrettyStackTrace;
#[llvm_versions(3.8..=latest)]
use llvm_sys::error_handling::LLVMEnablePrettyStackTrace;
unsafe {
LLVMEnablePrettyStackTrace()
}
}
macro_rules! enum_rename {
($(#[$enum_attrs:meta])* $enum_name:ident <=> $llvm_enum_name:ident {
$($(#[$variant_attrs:meta])* $args:ident <=> $llvm_args:ident,)+
}) => (
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
$(#[$enum_attrs])*
pub enum $enum_name {
$(
$(#[$variant_attrs])*
$args,
)*
}
impl $enum_name {
#[allow(dead_code)]
pub(crate) fn new(llvm_enum: $llvm_enum_name) -> Self {
match llvm_enum {
$(
$llvm_enum_name::$llvm_args => $enum_name::$args,
)*
}
}
pub(crate) fn as_llvm_enum(&self) -> $llvm_enum_name {
match *self {
$(
$enum_name::$args => $llvm_enum_name::$llvm_args,
)*
}
}
}
);
}