Skip to content

Commit

Permalink
better working destructors on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xTachyon authored and emilio committed Dec 18, 2023
1 parent 4f1125c commit 138cdae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
19 changes: 19 additions & 0 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,15 @@ impl Drop for EvalResult {
unsafe { clang_EvalResult_dispose(self.x) };
}
}
/// ABI kinds as defined in
/// <https://github.com/llvm/llvm-project/blob/ddf1de20a3f7db3bca1ef6ba7e6cbb90aac5fd2d/clang/include/clang/Basic/TargetCXXABI.def>
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub(crate) enum ABIKind {
/// All the regular targets like Linux, Mac, WASM, etc. implement the Itanium ABI
GenericItanium,
/// The ABI used when compiling for the MSVC target
Microsoft,
}

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

impl TargetInfo {
Expand All @@ -2320,9 +2331,17 @@ impl TargetInfo {
}
assert!(pointer_width > 0);
assert_eq!(pointer_width % 8, 0);

let abi = if triple.contains("msvc") {
ABIKind::Microsoft
} else {
ABIKind::GenericItanium
};

TargetInfo {
triple,
pointer_width: pointer_width as usize,
abi,
}
}
}
7 changes: 6 additions & 1 deletion bindgen/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::module::{Module, ModuleKind};
use super::template::{TemplateInstantiation, TemplateParameters};
use super::traversal::{self, Edge, ItemTraversal};
use super::ty::{FloatKind, Type, TypeKind};
use crate::clang::{self, Cursor};
use crate::clang::{self, ABIKind, Cursor};
use crate::codegen::CodegenError;
use crate::BindgenOptions;
use crate::{Entry, HashMap, HashSet};
Expand Down Expand Up @@ -626,6 +626,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
self.target_info.pointer_width / 8
}

/// Returns the ABI, which is mostly useful for determining the mangling kind.
pub(crate) fn abi_kind(&self) -> ABIKind {
self.target_info.abi
}

/// Get the stack of partially parsed types that we are in the middle of
/// parsing.
pub(crate) fn currently_parsed_types(&self) -> &[PartialType] {
Expand Down
7 changes: 4 additions & 3 deletions bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::item::Item;
use super::traversal::{EdgeKind, Trace, Tracer};
use super::ty::TypeKind;
use crate::callbacks::{ItemInfo, ItemKind};
use crate::clang::{self, Attribute};
use crate::clang::{self, ABIKind, Attribute};
use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
use clang_sys::{self, CXCallingConv};

Expand Down Expand Up @@ -324,11 +324,12 @@ pub(crate) fn cursor_mangling(
return None;
}

let is_itanium_abi = ctx.abi_kind() == ABIKind::GenericItanium;
let is_destructor = cursor.kind() == clang_sys::CXCursor_Destructor;
if let Ok(mut manglings) = cursor.cxx_manglings() {
while let Some(m) = manglings.pop() {
// Only generate the destructor group 1, see below.
if is_destructor && !m.ends_with("D1Ev") {
if is_itanium_abi && is_destructor && !m.ends_with("D1Ev") {
continue;
}

Expand All @@ -341,7 +342,7 @@ pub(crate) fn cursor_mangling(
return None;
}

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

0 comments on commit 138cdae

Please sign in to comment.