Skip to content

Commit ff1eac7

Browse files
authored
Merge branch 'main' into pass-item-info-to-item-name
2 parents b2000d7 + 2613129 commit ff1eac7

File tree

12 files changed

+130
-12
lines changed

12 files changed

+130
-12
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ on:
4747
jobs:
4848
# Run 'cargo dist plan' (or host) to determine what tasks we need to do
4949
plan:
50-
runs-on: "ubuntu-20.04"
50+
runs-on: "ubuntu-22.04"
5151
outputs:
5252
val: ${{ steps.plan.outputs.manifest }}
5353
tag: ${{ !github.event.pull_request && github.ref_name || '' }}
@@ -160,7 +160,7 @@ jobs:
160160
needs:
161161
- plan
162162
- build-local-artifacts
163-
runs-on: "ubuntu-20.04"
163+
runs-on: "ubuntu-22.04"
164164
env:
165165
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
166166
BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json
@@ -210,7 +210,7 @@ jobs:
210210
if: ${{ always() && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.build-local-artifacts.result == 'skipped' || needs.build-local-artifacts.result == 'success') }}
211211
env:
212212
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
213-
runs-on: "ubuntu-20.04"
213+
runs-on: "ubuntu-22.04"
214214
outputs:
215215
val: ${{ steps.host.outputs.manifest }}
216216
steps:
@@ -274,7 +274,7 @@ jobs:
274274
# still allowing individual publish jobs to skip themselves (for prereleases).
275275
# "host" however must run to completion, no skipping allowed!
276276
if: ${{ always() && needs.host.result == 'success' }}
277-
runs-on: "ubuntu-20.04"
277+
runs-on: "ubuntu-22.04"
278278
env:
279279
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
280280
steps:

bindgen-integration/build.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate bindgen;
22

33
use bindgen::callbacks::{
4-
DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks,
4+
DeriveInfo, IntKind, ItemInfo, MacroParsingBehavior, ParseCallbacks, Token, TokenKind,
55
};
66
use bindgen::{Builder, EnumVariation, Formatter};
77
use std::collections::HashSet;
@@ -147,6 +147,48 @@ impl ParseCallbacks for MacroCallback {
147147
vec![]
148148
}
149149
}
150+
151+
fn modify_macro(&self, _name: &str, tokens: &mut Vec<Token>) {
152+
// Handle macros dealing with bit positions of the format HI:LO
153+
if tokens.len() == 4 && tokens[2].kind == TokenKind::Punctuation {
154+
if let Ok(colon) = std::str::from_utf8(&tokens[2].raw) {
155+
if colon != ":" {
156+
return;
157+
}
158+
let high = match std::str::from_utf8(&tokens[1].raw) {
159+
Ok(s) => {
160+
if let Ok(val) = s.parse::<u16>() {
161+
val
162+
} else {
163+
return;
164+
}
165+
}
166+
Err(_) => {
167+
return;
168+
}
169+
};
170+
171+
let low = match std::str::from_utf8(&tokens[3].raw) {
172+
Ok(s) => {
173+
if let Ok(val) = s.parse::<u16>() {
174+
val
175+
} else {
176+
return;
177+
}
178+
}
179+
Err(_) => {
180+
return;
181+
}
182+
};
183+
let value: u32 = ((high as u32) << 16) | low as u32;
184+
tokens[1] = Token::from((
185+
TokenKind::Literal,
186+
value.to_string().as_bytes(),
187+
));
188+
tokens.truncate(2);
189+
}
190+
}
191+
}
150192
}
151193

152194
impl Drop for MacroCallback {

bindgen-integration/cpp/Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define TESTMACRO_STRING_EXPR ("string")
2323
#define TESTMACRO_STRING_FUNC_NON_UTF8(x) (x "ÿÿ") /* invalid UTF-8 on purpose */
2424

25+
#define TESTMACRO_COLON_VALUE 1:2
26+
2527
enum {
2628
MY_ANNOYING_MACRO =
2729
#define MY_ANNOYING_MACRO 1

bindgen-integration/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,9 @@ fn test_wrap_static_fns() {
345345
extern_bindings::wrap_as_variadic_fn2_wrapped(1, 2);
346346
}
347347
}
348+
349+
#[test]
350+
fn test_colon_define() {
351+
let gold: u32 = (1u32 << 16) | 2;
352+
assert_eq!(gold, bindings::TESTMACRO_COLON_VALUE);
353+
}

bindgen/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ default = ["logging", "prettyplease", "runtime"]
4747
logging = ["dep:log"]
4848
static = ["clang-sys/static"]
4949
runtime = ["clang-sys/runtime"]
50-
# This feature is no longer used for anything and should be removed in bindgen 0.70
51-
which-rustfmt = []
5250
experimental = ["dep:annotate-snippets"]
5351

5452
## The following features are for internal use and they shouldn't be used if

bindgen/callbacks.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ pub use crate::ir::analysis::DeriveTrait;
44
pub use crate::ir::derive::CanDerive as ImplementsTrait;
55
pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
66
pub use crate::ir::int::IntKind;
7+
pub use cexpr::token::Kind as TokenKind;
8+
pub use cexpr::token::Token;
79
use std::fmt;
810

911
/// An enum to allow ignoring parsing of macros.
@@ -49,6 +51,9 @@ pub trait ParseCallbacks: fmt::Debug {
4951
None
5052
}
5153

54+
/// Modify the contents of a macro
55+
fn modify_macro(&self, _name: &str, _tokens: &mut Vec<Token>) {}
56+
5257
/// The integer kind an integer macro should have, given a name and the
5358
/// value of that macro, or `None` if you want the default to be chosen.
5459
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {

bindgen/features.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,66 @@ impl RustTarget {
4848

4949
impl Default for RustTarget {
5050
fn default() -> Self {
51+
// Bindgen from build script: default to generating bindings compatible
52+
// with the Rust version currently performing this build.
53+
#[cfg(not(feature = "__cli"))]
54+
{
55+
use std::env;
56+
use std::iter;
57+
use std::process::Command;
58+
use std::sync::OnceLock;
59+
60+
static CURRENT_RUST: OnceLock<Option<RustTarget>> = OnceLock::new();
61+
62+
if let Some(current_rust) = *CURRENT_RUST.get_or_init(|| {
63+
let is_build_script =
64+
env::var_os("CARGO_CFG_TARGET_ARCH").is_some();
65+
if !is_build_script {
66+
return None;
67+
}
68+
69+
let rustc = env::var_os("RUSTC")?;
70+
let rustc_wrapper = env::var_os("RUSTC_WRAPPER")
71+
.filter(|wrapper| !wrapper.is_empty());
72+
let wrapped_rustc =
73+
rustc_wrapper.iter().chain(iter::once(&rustc));
74+
75+
let mut is_clippy_driver = false;
76+
loop {
77+
let mut wrapped_rustc = wrapped_rustc.clone();
78+
let mut command =
79+
Command::new(wrapped_rustc.next().unwrap());
80+
command.args(wrapped_rustc);
81+
if is_clippy_driver {
82+
command.arg("--rustc");
83+
}
84+
command.arg("--version");
85+
86+
let output = command.output().ok()?;
87+
let string = String::from_utf8(output.stdout).ok()?;
88+
89+
// Version string like "rustc 1.100.0-beta.5 (f0e1d2c3b 2026-10-17)"
90+
let last_line = string.lines().last().unwrap_or(&string);
91+
let (program, rest) = last_line.trim().split_once(' ')?;
92+
if program != "rustc" {
93+
if program.starts_with("clippy") && !is_clippy_driver {
94+
is_clippy_driver = true;
95+
continue;
96+
}
97+
return None;
98+
}
99+
100+
let number = rest.split([' ', '-', '+']).next()?;
101+
break RustTarget::from_str(number).ok();
102+
}
103+
}) {
104+
return current_rust;
105+
}
106+
}
107+
108+
// Bindgen from CLI, or cannot determine compiler version: default to
109+
// generating bindings compatible with the latest stable release of Rust
110+
// that Bindgen knows about.
51111
LATEST_STABLE_RUST
52112
}
53113
}

bindgen/ir/var.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,11 @@ fn parse_macro(
427427
) -> Option<(Vec<u8>, cexpr::expr::EvalResult)> {
428428
use cexpr::expr;
429429

430-
let cexpr_tokens = cursor.cexpr_tokens();
430+
let mut cexpr_tokens = cursor.cexpr_tokens();
431+
432+
for callbacks in &ctx.options().parse_callbacks {
433+
callbacks.modify_macro(&cursor.spelling(), &mut cexpr_tokens);
434+
}
431435

432436
let parser = expr::IdentifierParser::new(ctx.parsed_macros());
433437

bindgen/options/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ struct BindgenCommand {
480480
/// Derive custom traits on a `struct`. The CUSTOM value must be of the shape REGEX=DERIVE where DERIVE is a coma-separated list of derive macros.
481481
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_derive)]
482482
with_derive_custom_struct: Vec<(Vec<String>, String)>,
483-
/// Derive custom traits on an `enum. The CUSTOM value must be of the shape REGEX=DERIVE where DERIVE is a coma-separated list of derive macros.
483+
/// Derive custom traits on an `enum`. The CUSTOM value must be of the shape REGEX=DERIVE where DERIVE is a coma-separated list of derive macros.
484484
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_derive)]
485485
with_derive_custom_enum: Vec<(Vec<String>, String)>,
486486
/// Derive custom traits on a `union`. The CUSTOM value must be of the shape REGEX=DERIVE where DERIVE is a coma-separated list of derive macros.
@@ -492,7 +492,7 @@ struct BindgenCommand {
492492
/// Add custom attributes on a `struct`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
493493
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_attribute)]
494494
with_attribute_custom_struct: Vec<(Vec<String>, String)>,
495-
/// Add custom attributes on an `enum. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
495+
/// Add custom attributes on an `enum`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.
496496
#[arg(long, value_name = "CUSTOM", value_parser = parse_custom_attribute)]
497497
with_attribute_custom_enum: Vec<(Vec<String>, String)>,
498498
/// Add custom attributes on a `union`. The CUSTOM value must be of the shape REGEX=ATTRIBUTE where ATTRIBUTE is a coma-separated list of attributes.

book/src/cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ be nowhere near as nice as using them in C++. You will have to manually call
77
constructors, destructors, overloaded operators, etc yourself.
88

99
When passing in header files, the file will automatically be treated as C++ if
10-
it ends in `.hpp`. If it doesn't, adding `-x c++` clang args can be used to
10+
it ends in `.hpp`. If it doesn't, adding `clang_args(["-x", "c++"])` can be used to
1111
force C++ mode. You probably also want to use `-std=c++14` or similar clang args
1212
as well.
1313

0 commit comments

Comments
 (0)