Skip to content

Commit 41bd6e0

Browse files
Make bindgen generate CStrs and such (#1804)
While cutting a release, I noticed our bindgen options were wonky. I fixed a few blocklistings and adjusted the bindgen settings. Most of this should be invisible. There's only one case affected by nested struct naming, and it's not even present in pg16 and later. But the most notable change, and one that I expect might force people to have to update their code: now const strings from C are CStrs!
1 parent 8124b63 commit 41bd6e0

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

pgrx-bindgen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
license = "MIT"
77
homepage = "https://github.com/pgcentralfoundation/pgrx"
88
repository = "https://github.com/pgcentralfoundation/pgrx"
9-
docs = "https://docs.rs/pgrx-bindgen"
9+
documentation = "https://docs.rs/pgrx-bindgen"
1010

1111
[dependencies]
1212
pgrx-pg-config.workspace = true

pgrx-bindgen/src/build.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,10 @@ fn run_bindgen(
775775
.rustified_non_exhaustive_enum("NodeTag")
776776
.size_t_is_usize(true)
777777
.merge_extern_blocks(true)
778+
.wrap_unsafe_ops(true)
779+
.use_core()
780+
.generate_cstr(true)
781+
.disable_nested_struct_naming()
778782
.formatter(bindgen::Formatter::None)
779783
.layout_tests(false)
780784
.default_non_copy_union_style(NonCopyUnionStyle::ManuallyDrop)
@@ -819,9 +823,9 @@ fn add_blocklists(bind: bindgen::Builder) -> bindgen::Builder {
819823
.blocklist_function(".*(?:set|long)jmp")
820824
.blocklist_function("pg_re_throw")
821825
.blocklist_function("err(start|code|msg|detail|context_msg|hint|finish)")
822-
.blocklist_item("CONFIGURE_ARGS") // configuration during build is hopefully irrelevant
823-
.blocklist_item("_*(?:HAVE|have)_.*") // header tracking metadata
824-
.blocklist_item("_[A-Z_]+_H") // more header metadata
826+
.blocklist_var("CONFIGURE_ARGS") // configuration during build is hopefully irrelevant
827+
.blocklist_var("_*(?:HAVE|have)_.*") // header tracking metadata
828+
.blocklist_var("_[A-Z_]+_H") // more header metadata
825829
.blocklist_item("__[A-Z].*") // these are reserved and unused by Postgres
826830
.blocklist_item("__darwin.*") // this should always be Apple's names
827831
.blocklist_function("pq(?:Strerror|Get.*)") // wrappers around platform functions: user can call those themselves

pgrx-pg-sys/src/port.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ pub const VARHDRSZ_SHORT: usize = offset_of!(super::varattrib_1b, va_data);
137137

138138
#[inline]
139139
pub fn get_pg_major_version_string() -> &'static str {
140-
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_MAJORVERSION).unwrap();
141-
mver.to_str().unwrap()
140+
super::PG_MAJORVERSION.to_str().unwrap()
142141
}
143142

144143
#[inline]
@@ -148,14 +147,12 @@ pub fn get_pg_major_version_num() -> u16 {
148147

149148
#[inline]
150149
pub fn get_pg_version_string() -> &'static str {
151-
let ver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION_STR).unwrap();
152-
ver.to_str().unwrap()
150+
super::PG_VERSION_STR.to_str().unwrap()
153151
}
154152

155153
#[inline]
156154
pub fn get_pg_major_minor_version_string() -> &'static str {
157-
let mver = core::ffi::CStr::from_bytes_with_nul(super::PG_VERSION).unwrap();
158-
mver.to_str().unwrap()
155+
super::PG_VERSION.to_str().unwrap()
159156
}
160157

161158
#[inline]

pgrx/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,23 @@ mod seal {
145145
not(feature = "unsafe-postgres")
146146
))]
147147
const _: () = {
148+
use core::ffi::CStr;
148149
// to appease `const`
149-
const fn same_slice(a: &[u8], b: &[u8]) -> bool {
150-
if a.len() != b.len() {
150+
const fn same_cstr(a: &CStr, b: &CStr) -> bool {
151+
if a.to_bytes().len() != b.to_bytes().len() {
151152
return false;
152153
}
153154
let mut i = 0;
154-
while i < a.len() {
155-
if a[i] != b[i] {
155+
while i < a.to_bytes().len() {
156+
if a.to_bytes()[i] != b.to_bytes()[i] {
156157
return false;
157158
}
158159
i += 1;
159160
}
160161
true
161162
}
162163
assert!(
163-
same_slice(pg_sys::FMGR_ABI_EXTRA, b"PostgreSQL\0"),
164+
same_cstr(pg_sys::FMGR_ABI_EXTRA, c"PostgreSQL"),
164165
"Unsupported Postgres ABI. Perhaps you need `--features unsafe-postgres`?",
165166
);
166167
};
@@ -242,11 +243,11 @@ macro_rules! pg_magic_func {
242243
abi_extra: {
243244
// we'll use what the bindings tell us, but if it ain't "PostgreSQL" then we'll
244245
// raise a compilation error unless the `unsafe-postgres` feature is set
245-
let magic = ::pgrx::pg_sys::FMGR_ABI_EXTRA;
246+
let magic = ::pgrx::pg_sys::FMGR_ABI_EXTRA.to_bytes_with_nul();
246247
let mut abi = [0 as ::pgrx::ffi::c_char; 32];
247248
let mut i = 0;
248249
while i < magic.len() {
249-
abi[i] = magic[i] as _;
250+
abi[i] = magic[i] as ::pgrx::ffi::c_char;
250251
i += 1;
251252
}
252253
abi

0 commit comments

Comments
 (0)