Skip to content

Commit 138cdae

Browse files
xTachyonemilio
authored andcommitted
better working destructors on windows
1 parent 4f1125c commit 138cdae

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

bindgen/clang.rs

+19
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,15 @@ impl Drop for EvalResult {
22972297
unsafe { clang_EvalResult_dispose(self.x) };
22982298
}
22992299
}
2300+
/// ABI kinds as defined in
2301+
/// <https://github.com/llvm/llvm-project/blob/ddf1de20a3f7db3bca1ef6ba7e6cbb90aac5fd2d/clang/include/clang/Basic/TargetCXXABI.def>
2302+
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
2303+
pub(crate) enum ABIKind {
2304+
/// All the regular targets like Linux, Mac, WASM, etc. implement the Itanium ABI
2305+
GenericItanium,
2306+
/// The ABI used when compiling for the MSVC target
2307+
Microsoft,
2308+
}
23002309

23012310
/// Target information obtained from libclang.
23022311
#[derive(Debug)]
@@ -2305,6 +2314,8 @@ pub(crate) struct TargetInfo {
23052314
pub(crate) triple: String,
23062315
/// The width of the pointer _in bits_.
23072316
pub(crate) pointer_width: usize,
2317+
/// The ABI of the target
2318+
pub(crate) abi: ABIKind,
23082319
}
23092320

23102321
impl TargetInfo {
@@ -2320,9 +2331,17 @@ impl TargetInfo {
23202331
}
23212332
assert!(pointer_width > 0);
23222333
assert_eq!(pointer_width % 8, 0);
2334+
2335+
let abi = if triple.contains("msvc") {
2336+
ABIKind::Microsoft
2337+
} else {
2338+
ABIKind::GenericItanium
2339+
};
2340+
23232341
TargetInfo {
23242342
triple,
23252343
pointer_width: pointer_width as usize,
2344+
abi,
23262345
}
23272346
}
23282347
}

bindgen/ir/context.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use super::module::{Module, ModuleKind};
1919
use super::template::{TemplateInstantiation, TemplateParameters};
2020
use super::traversal::{self, Edge, ItemTraversal};
2121
use super::ty::{FloatKind, Type, TypeKind};
22-
use crate::clang::{self, Cursor};
22+
use crate::clang::{self, ABIKind, Cursor};
2323
use crate::codegen::CodegenError;
2424
use crate::BindgenOptions;
2525
use crate::{Entry, HashMap, HashSet};
@@ -626,6 +626,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
626626
self.target_info.pointer_width / 8
627627
}
628628

629+
/// Returns the ABI, which is mostly useful for determining the mangling kind.
630+
pub(crate) fn abi_kind(&self) -> ABIKind {
631+
self.target_info.abi
632+
}
633+
629634
/// Get the stack of partially parsed types that we are in the middle of
630635
/// parsing.
631636
pub(crate) fn currently_parsed_types(&self) -> &[PartialType] {

bindgen/ir/function.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::item::Item;
77
use super::traversal::{EdgeKind, Trace, Tracer};
88
use super::ty::TypeKind;
99
use crate::callbacks::{ItemInfo, ItemKind};
10-
use crate::clang::{self, Attribute};
10+
use crate::clang::{self, ABIKind, Attribute};
1111
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
1212
use clang_sys::{self, CXCallingConv};
1313

@@ -324,11 +324,12 @@ pub(crate) fn cursor_mangling(
324324
return None;
325325
}
326326

327+
let is_itanium_abi = ctx.abi_kind() == ABIKind::GenericItanium;
327328
let is_destructor = cursor.kind() == clang_sys::CXCursor_Destructor;
328329
if let Ok(mut manglings) = cursor.cxx_manglings() {
329330
while let Some(m) = manglings.pop() {
330331
// Only generate the destructor group 1, see below.
331-
if is_destructor && !m.ends_with("D1Ev") {
332+
if is_itanium_abi && is_destructor && !m.ends_with("D1Ev") {
332333
continue;
333334
}
334335

@@ -341,7 +342,7 @@ pub(crate) fn cursor_mangling(
341342
return None;
342343
}
343344

344-
if is_destructor {
345+
if is_itanium_abi && is_destructor {
345346
// With old (3.8-) libclang versions, and the Itanium ABI, clang returns
346347
// the "destructor group 0" symbol, which means that it'll try to free
347348
// memory, which definitely isn't what we want.

0 commit comments

Comments
 (0)