Skip to content

Commit 15aef5e

Browse files
authored
Merge pull request #2285 from ferrous-systems/clone-options
Make `BindgenOptions` clonable
2 parents 8b69ba0 + 17252c7 commit 15aef5e

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

src/deps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Generating build depfiles from parsed bindings.
22
use std::{collections::BTreeSet, path::PathBuf};
33

4-
#[derive(Debug)]
4+
#[derive(Clone, Debug)]
55
pub(crate) struct DepfileSpec {
66
pub output_module: String,
77
pub depfile_path: PathBuf,

src/ir/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,10 @@ impl<'ctx> AllowlistedItemsTraversal<'ctx> {
505505

506506
impl BindgenContext {
507507
/// Construct the context for the given `options`.
508-
pub(crate) fn new(options: BindgenOptions) -> Self {
508+
pub(crate) fn new(
509+
options: BindgenOptions,
510+
input_unsaved_files: &[clang::UnsavedFile],
511+
) -> Self {
509512
// TODO(emilio): Use the CXTargetInfo here when available.
510513
//
511514
// see: https://reviews.llvm.org/D32389
@@ -522,7 +525,7 @@ impl BindgenContext {
522525
&index,
523526
"",
524527
&options.clang_args,
525-
&options.input_unsaved_files,
528+
input_unsaved_files,
526529
parse_options,
527530
).expect("libclang error; possible causes include:
528531
- Invalid flag syntax

src/lib.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ use std::fs::{File, OpenOptions};
8383
use std::io::{self, Write};
8484
use std::path::{Path, PathBuf};
8585
use std::process::{Command, Stdio};
86+
use std::rc::Rc;
8687
use std::{env, iter};
8788

8889
// Some convenient typedefs for a fast hash map and hash set.
@@ -1465,7 +1466,7 @@ impl Builder {
14651466
mut self,
14661467
cb: Box<dyn callbacks::ParseCallbacks>,
14671468
) -> Self {
1468-
self.options.parse_callbacks = Some(cb);
1469+
self.options.parse_callbacks = Some(Rc::from(cb));
14691470
self
14701471
}
14711472

@@ -1574,15 +1575,13 @@ impl Builder {
15741575
}),
15751576
);
15761577

1577-
self.options.input_unsaved_files.extend(
1578-
self.input_header_contents
1579-
.drain(..)
1580-
.map(|(name, contents)| {
1581-
clang::UnsavedFile::new(&name, &contents)
1582-
}),
1583-
);
1578+
let input_unsaved_files = self
1579+
.input_header_contents
1580+
.into_iter()
1581+
.map(|(name, contents)| clang::UnsavedFile::new(&name, &contents))
1582+
.collect::<Vec<_>>();
15841583

1585-
Bindings::generate(self.options)
1584+
Bindings::generate(self.options, input_unsaved_files)
15861585
}
15871586

15881587
/// Preprocess and dump the input header files to disk.
@@ -1774,7 +1773,7 @@ impl Builder {
17741773
}
17751774

17761775
/// Configuration options for generated bindings.
1777-
#[derive(Debug)]
1776+
#[derive(Clone, Debug)]
17781777
struct BindgenOptions {
17791778
/// The set of types that have been blocklisted and should not appear
17801779
/// anywhere in the generated code.
@@ -1977,12 +1976,9 @@ struct BindgenOptions {
19771976
/// Any additional input header files.
19781977
extra_input_headers: Vec<String>,
19791978

1980-
/// Unsaved files for input.
1981-
input_unsaved_files: Vec<clang::UnsavedFile>,
1982-
19831979
/// A user-provided visitor to allow customizing different kinds of
19841980
/// situations.
1985-
parse_callbacks: Option<Box<dyn callbacks::ParseCallbacks>>,
1981+
parse_callbacks: Option<Rc<dyn callbacks::ParseCallbacks>>,
19861982

19871983
/// Which kind of items should we generate? By default, we'll generate all
19881984
/// of them.
@@ -2230,7 +2226,6 @@ impl Default for BindgenOptions {
22302226
clang_args: vec![],
22312227
input_header: None,
22322228
extra_input_headers: vec![],
2233-
input_unsaved_files: vec![],
22342229
parse_callbacks: None,
22352230
codegen_config: CodegenConfig::all(),
22362231
conservative_inline_namespaces: false,
@@ -2388,6 +2383,7 @@ impl Bindings {
23882383
/// Generate bindings for the given options.
23892384
pub(crate) fn generate(
23902385
mut options: BindgenOptions,
2386+
input_unsaved_files: Vec<clang::UnsavedFile>,
23912387
) -> Result<Bindings, BindgenError> {
23922388
ensure_libclang_is_loaded();
23932389

@@ -2522,7 +2518,7 @@ impl Bindings {
25222518
}
25232519
}
25242520

2525-
for (idx, f) in options.input_unsaved_files.iter().enumerate() {
2521+
for (idx, f) in input_unsaved_files.iter().enumerate() {
25262522
if idx != 0 || options.input_header.is_some() {
25272523
options.clang_args.push("-include".to_owned());
25282524
}
@@ -2532,7 +2528,7 @@ impl Bindings {
25322528
debug!("Fixed-up options: {:?}", options);
25332529

25342530
let time_phases = options.time_phases;
2535-
let mut context = BindgenContext::new(options);
2531+
let mut context = BindgenContext::new(options, &input_unsaved_files);
25362532

25372533
if is_host_build {
25382534
debug_assert_eq!(

src/regex_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use regex::RegexSet as RxSet;
44
use std::cell::Cell;
55

66
/// A dynamic set of regular expressions.
7-
#[derive(Debug, Default)]
7+
#[derive(Clone, Debug, Default)]
88
pub struct RegexSet {
99
items: Vec<String>,
1010
/// Whether any of the items in the set was ever matched. The length of this

0 commit comments

Comments
 (0)