Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better working destructors on windows #2663

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -622,6 +622,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 @@ -323,11 +323,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 @@ -340,7 +341,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