Skip to content

Commit 6d748fb

Browse files
author
Jethro Beekman
committed
Merge branch 'jb/anonymous-enum-variant-rename' into jb/issue-1454-alt
2 parents 22a5ed9 + 7f45e2d commit 6d748fb

File tree

6 files changed

+101
-15
lines changed

6 files changed

+101
-15
lines changed

src/ir/context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
22992299
let mut prefix_path =
23002300
parent.path_for_allowlisting(self).clone();
23012301
enum_.variants().iter().any(|variant| {
2302-
prefix_path.push(variant.name().into());
2302+
prefix_path.push(
2303+
variant.name_for_allowlisting().into(),
2304+
);
23032305
let name = prefix_path[1..].join("::");
23042306
prefix_path.pop().unwrap();
23052307
self.options().allowlisted_vars.matches(&name)

src/ir/enum_ty.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Enum {
118118
}
119119
});
120120

121-
let name = ctx
121+
let new_name = ctx
122122
.parse_callbacks()
123123
.and_then(|callbacks| {
124124
callbacks.enum_variant_name(type_name, &name, val)
@@ -130,10 +130,11 @@ impl Enum {
130130
.last()
131131
.cloned()
132132
})
133-
.unwrap_or(name);
133+
.unwrap_or_else(|| name.clone());
134134

135135
let comment = cursor.raw_comment();
136136
variants.push(EnumVariant::new(
137+
new_name,
137138
name,
138139
comment,
139140
val,
@@ -224,6 +225,9 @@ pub struct EnumVariant {
224225
/// The name of the variant.
225226
name: String,
226227

228+
/// The original name of the variant (without user mangling)
229+
name_for_allowlisting: String,
230+
227231
/// An optional doc comment.
228232
comment: Option<String>,
229233

@@ -251,12 +255,14 @@ impl EnumVariant {
251255
/// Construct a new enumeration variant from the given parts.
252256
pub fn new(
253257
name: String,
258+
name_for_allowlisting: String,
254259
comment: Option<String>,
255260
val: EnumVariantValue,
256261
custom_behavior: Option<EnumVariantCustomBehavior>,
257262
) -> Self {
258263
EnumVariant {
259264
name,
265+
name_for_allowlisting,
260266
comment,
261267
val,
262268
custom_behavior,
@@ -268,6 +274,11 @@ impl EnumVariant {
268274
&self.name
269275
}
270276

277+
/// Get this variant's name.
278+
pub fn name_for_allowlisting(&self) -> &str {
279+
&self.name_for_allowlisting
280+
}
281+
271282
/// Get this variant's value.
272283
pub fn val(&self) -> EnumVariantValue {
273284
self.val
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(
2+
dead_code,
3+
non_snake_case,
4+
non_camel_case_types,
5+
non_upper_case_globals
6+
)]
7+
8+
pub const RENAMED_MyVal: ::std::os::raw::c_uint = 0;
9+
pub type _bindgen_ty_1 = ::std::os::raw::c_uint;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: --allowlist-var ^MyVal$
2+
// bindgen-parse-callbacks: enum-variant-rename
3+
4+
enum {
5+
MyVal = 0,
6+
};

tests/parse_callbacks/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use bindgen::callbacks::ParseCallbacks;
2+
3+
#[derive(Debug)]
4+
struct EnumVariantRename;
5+
6+
impl ParseCallbacks for EnumVariantRename {
7+
fn enum_variant_name(
8+
&self,
9+
_enum_name: Option<&str>,
10+
original_variant_name: &str,
11+
_variant_value: bindgen::callbacks::EnumVariantValue,
12+
) -> Option<String> {
13+
Some(format!("RENAMED_{}", original_variant_name))
14+
}
15+
}
16+
17+
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
18+
match cb {
19+
"enum-variant-rename" => Box::new(EnumVariantRename),
20+
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
21+
}
22+
}

tests/tests.rs

+48-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::sync::Once;
1717
mod options;
1818
use crate::options::builder_from_flags;
1919

20+
mod parse_callbacks;
21+
2022
// Run `rustfmt` on the given source string and return a tuple of the formatted
2123
// bindings, and rustfmt's stderr.
2224
fn rustfmt(source: String) -> (String, String) {
@@ -115,7 +117,7 @@ The latest `rustfmt` is required to run the `bindgen` test suite. Install
115117

116118
fn compare_generated_header(
117119
header: &PathBuf,
118-
builder: Builder,
120+
builder: BuilderState,
119121
check_roundtrip: bool,
120122
) -> Result<(), Error> {
121123
let file_name = header.file_name().ok_or(Error::new(
@@ -190,13 +192,7 @@ fn compare_generated_header(
190192
),
191193
};
192194

193-
let flags = if check_roundtrip {
194-
let mut flags = builder.command_line_flags();
195-
flags.insert(0, "bindgen".into());
196-
flags
197-
} else {
198-
vec![]
199-
};
195+
let (builder, roundtrip_builder) = builder.into_builder(check_roundtrip)?;
200196

201197
// We skip the generate() error here so we get a full diff below
202198
let (actual, rustfmt_stderr) = match builder.generate() {
@@ -262,8 +258,7 @@ fn compare_generated_header(
262258
return Err(Error::new(ErrorKind::Other, "Header and binding differ! Run with BINDGEN_OVERWRITE_EXPECTED=1 in the environment to automatically overwrite the expectation or with BINDGEN_TESTS_DIFFTOOL=meld to do this manually."));
263259
}
264260

265-
if check_roundtrip {
266-
let roundtrip_builder = builder_from_flags(flags.into_iter())?.0;
261+
if let Some(roundtrip_builder) = roundtrip_builder {
267262
if let Err(e) =
268263
compare_generated_header(&header, roundtrip_builder, false)
269264
{
@@ -281,7 +276,36 @@ fn builder() -> Builder {
281276
bindgen::builder().disable_header_comment()
282277
}
283278

284-
fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
279+
struct BuilderState {
280+
builder: Builder,
281+
parse_callbacks: Vec<String>,
282+
}
283+
284+
impl BuilderState {
285+
fn into_builder(
286+
self,
287+
with_roundtrip_builder: bool,
288+
) -> Result<(Builder, Option<BuilderState>), Error> {
289+
let roundtrip_builder = if with_roundtrip_builder {
290+
let mut flags = self.builder.command_line_flags();
291+
flags.insert(0, "bindgen".into());
292+
let mut builder = builder_from_flags(flags.into_iter())?.0;
293+
for parse_cb in &self.parse_callbacks {
294+
builder =
295+
builder.parse_callbacks(parse_callbacks::lookup(parse_cb));
296+
}
297+
Some(BuilderState {
298+
builder,
299+
parse_callbacks: self.parse_callbacks,
300+
})
301+
} else {
302+
None
303+
};
304+
Ok((self.builder, roundtrip_builder))
305+
}
306+
}
307+
308+
fn create_bindgen_builder(header: &PathBuf) -> Result<BuilderState, Error> {
285309
#[cfg(feature = "logging")]
286310
let _ = env_logger::try_init();
287311

@@ -290,6 +314,7 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
290314

291315
// Scoop up bindgen-flags from test header
292316
let mut flags = Vec::with_capacity(2);
317+
let mut parse_callbacks = vec![];
293318

294319
for line in reader.lines() {
295320
let line = line?;
@@ -311,6 +336,10 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
311336
.map(ToString::to_string)
312337
.chain(flags)
313338
.collect();
339+
} else if line.contains("bindgen-parse-callbacks: ") {
340+
let parse_cb =
341+
line.split("bindgen-parse-callbacks: ").last().unwrap();
342+
parse_callbacks.push(parse_cb.to_owned());
314343
}
315344
}
316345

@@ -353,7 +382,14 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
353382
.map(ToString::to_string)
354383
.chain(flags.into_iter());
355384

356-
builder_from_flags(args).map(|(builder, _, _)| builder)
385+
let mut builder = builder_from_flags(args)?.0;
386+
for parse_cb in &parse_callbacks {
387+
builder = builder.parse_callbacks(parse_callbacks::lookup(parse_cb));
388+
}
389+
Ok(BuilderState {
390+
builder,
391+
parse_callbacks,
392+
})
357393
}
358394

359395
macro_rules! test_header {

0 commit comments

Comments
 (0)