From ee441391a7781e6522f429b3e4da6399c606753b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 2 Sep 2024 15:40:37 +0200 Subject: [PATCH] ir: Dig into atomic types. Fixes #2920 --- .../tests/expectations/tests/atomic-constant.rs | 7 +++++++ .../expectations/tests/libclang-9/atomic-constant.rs | 4 ++++ bindgen-tests/tests/headers/atomic-constant.h | 2 ++ bindgen/Cargo.toml | 2 +- bindgen/clang.rs | 9 +++++++++ bindgen/ir/ty.rs | 12 ++++++++++++ bindgen/ir/var.rs | 3 ++- 7 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/atomic-constant.rs create mode 100644 bindgen-tests/tests/expectations/tests/libclang-9/atomic-constant.rs create mode 100644 bindgen-tests/tests/headers/atomic-constant.h diff --git a/bindgen-tests/tests/expectations/tests/atomic-constant.rs b/bindgen-tests/tests/expectations/tests/atomic-constant.rs new file mode 100644 index 0000000000..bd3c18697b --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/atomic-constant.rs @@ -0,0 +1,7 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +extern "C" { + pub static mut a: ::std::os::raw::c_int; +} +extern "C" { + pub static mut b: ::std::os::raw::c_int; +} diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/atomic-constant.rs b/bindgen-tests/tests/expectations/tests/libclang-9/atomic-constant.rs new file mode 100644 index 0000000000..ce12eaad3a --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/libclang-9/atomic-constant.rs @@ -0,0 +1,4 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +extern "C" { + pub static mut b: ::std::os::raw::c_int; +} diff --git a/bindgen-tests/tests/headers/atomic-constant.h b/bindgen-tests/tests/headers/atomic-constant.h new file mode 100644 index 0000000000..b28f76f7e4 --- /dev/null +++ b/bindgen-tests/tests/headers/atomic-constant.h @@ -0,0 +1,2 @@ +_Atomic(int) a; +int b; diff --git a/bindgen/Cargo.toml b/bindgen/Cargo.toml index 9eddd5157d..1c19cc3539 100644 --- a/bindgen/Cargo.toml +++ b/bindgen/Cargo.toml @@ -28,7 +28,7 @@ path = "lib.rs" annotate-snippets = { version = "0.9.1", features = ["color"], optional = true } bitflags = "2.2.1" cexpr = "0.6" -clang-sys = { version = "1", features = ["clang_6_0"] } +clang-sys = { version = "1", features = ["clang_11_0"] } itertools = { version = ">=0.10,<0.14", default-features = false } log = { version = "0.4", optional = true } prettyplease = { version = "0.2.7", optional = true, features = ["verbatim"] } diff --git a/bindgen/clang.rs b/bindgen/clang.rs index 47c7b1704a..e585fb31bd 100644 --- a/bindgen/clang.rs +++ b/bindgen/clang.rs @@ -1493,6 +1493,15 @@ impl Type { } } + /// For atomic types, get the underlying type. + pub(crate) fn atomic_value_type(&self) -> Type { + unsafe { + Type { + x: clang_Type_getValueType(self.x), + } + } + } + /// Is this a valid type? pub(crate) fn is_valid(&self) -> bool { self.kind() != CXType_Invalid diff --git a/bindgen/ir/ty.rs b/bindgen/ir/ty.rs index 2a24dd0291..6d4c5666dc 100644 --- a/bindgen/ir/ty.rs +++ b/bindgen/ir/ty.rs @@ -1167,6 +1167,18 @@ impl Type { .expect("Not able to resolve array element?"); TypeKind::Array(inner, ty.num_elements().unwrap()) } + CXType_Atomic => { + // TODO(emilio): Maybe we can preserve the "is atomic" bit somehow and generate + // something more useful... But for now this is better than panicking or + // generating nothing. + return Self::from_clang_ty( + potential_id, + &ty.atomic_value_type(), + location, + parent_id, + ctx, + ); + } CXType_Elaborated => { return Self::from_clang_ty( potential_id, diff --git a/bindgen/ir/var.rs b/bindgen/ir/var.rs index 40a061e16c..b8fffc7d66 100644 --- a/bindgen/ir/var.rs +++ b/bindgen/ir/var.rs @@ -320,7 +320,8 @@ impl ClangSubItemParser for Var { matches!(ty.kind(), CXType_Auto | CXType_Unexposed), "Couldn't resolve constant type, and it \ wasn't an nondeductible auto type or unexposed \ - type!" + type: {:?}", + ty ); return Err(e); }