Skip to content

Make BindgenOptions clonable #2285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/deps.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Generating build depfiles from parsed bindings.
use std::{collections::BTreeSet, path::PathBuf};

#[derive(Debug)]
#[derive(Clone, Debug)]
pub(crate) struct DepfileSpec {
pub output_module: String,
pub depfile_path: PathBuf,
Expand Down
7 changes: 5 additions & 2 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,10 @@ impl<'ctx> AllowlistedItemsTraversal<'ctx> {

impl BindgenContext {
/// Construct the context for the given `options`.
pub(crate) fn new(options: BindgenOptions) -> Self {
pub(crate) fn new(
options: BindgenOptions,
input_unsaved_files: &[clang::UnsavedFile],
) -> Self {
// TODO(emilio): Use the CXTargetInfo here when available.
//
// see: https://reviews.llvm.org/D32389
Expand All @@ -522,7 +525,7 @@ impl BindgenContext {
&index,
"",
&options.clang_args,
&options.input_unsaved_files,
input_unsaved_files,
parse_options,
).expect("libclang error; possible causes include:
- Invalid flag syntax
Expand Down
30 changes: 13 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::rc::Rc;
use std::{env, iter};

// Some convenient typedefs for a fast hash map and hash set.
Expand Down Expand Up @@ -1466,7 +1467,7 @@ impl Builder {
mut self,
cb: Box<dyn callbacks::ParseCallbacks>,
) -> Self {
self.options.parse_callbacks = Some(cb);
self.options.parse_callbacks = Some(Rc::from(cb));
self
}

Expand Down Expand Up @@ -1575,15 +1576,13 @@ impl Builder {
}),
);

self.options.input_unsaved_files.extend(
self.input_header_contents
.drain(..)
.map(|(name, contents)| {
clang::UnsavedFile::new(&name, &contents)
}),
);
let input_unsaved_files = self
.input_header_contents
.into_iter()
.map(|(name, contents)| clang::UnsavedFile::new(&name, &contents))
.collect::<Vec<_>>();

Bindings::generate(self.options)
Bindings::generate(self.options, input_unsaved_files)
}

/// Preprocess and dump the input header files to disk.
Expand Down Expand Up @@ -1775,7 +1774,7 @@ impl Builder {
}

/// Configuration options for generated bindings.
#[derive(Debug)]
#[derive(Clone, Debug)]
struct BindgenOptions {
/// The set of types that have been blocklisted and should not appear
/// anywhere in the generated code.
Expand Down Expand Up @@ -1978,12 +1977,9 @@ struct BindgenOptions {
/// Any additional input header files.
extra_input_headers: Vec<String>,

/// Unsaved files for input.
input_unsaved_files: Vec<clang::UnsavedFile>,

/// A user-provided visitor to allow customizing different kinds of
/// situations.
parse_callbacks: Option<Box<dyn callbacks::ParseCallbacks>>,
parse_callbacks: Option<Rc<dyn callbacks::ParseCallbacks>>,

/// Which kind of items should we generate? By default, we'll generate all
/// of them.
Expand Down Expand Up @@ -2236,7 +2232,6 @@ impl Default for BindgenOptions {
clang_args: vec![],
input_header: None,
extra_input_headers: vec![],
input_unsaved_files: vec![],
parse_callbacks: None,
codegen_config: CodegenConfig::all(),
conservative_inline_namespaces: false,
Expand Down Expand Up @@ -2394,6 +2389,7 @@ impl Bindings {
/// Generate bindings for the given options.
pub(crate) fn generate(
mut options: BindgenOptions,
input_unsaved_files: Vec<clang::UnsavedFile>,
) -> Result<Bindings, BindgenError> {
ensure_libclang_is_loaded();

Expand Down Expand Up @@ -2528,7 +2524,7 @@ impl Bindings {
}
}

for (idx, f) in options.input_unsaved_files.iter().enumerate() {
for (idx, f) in input_unsaved_files.iter().enumerate() {
if idx != 0 || options.input_header.is_some() {
options.clang_args.push("-include".to_owned());
}
Expand All @@ -2538,7 +2534,7 @@ impl Bindings {
debug!("Fixed-up options: {:?}", options);

let time_phases = options.time_phases;
let mut context = BindgenContext::new(options);
let mut context = BindgenContext::new(options, &input_unsaved_files);

if is_host_build {
debug_assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion src/regex_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use regex::RegexSet as RxSet;
use std::cell::Cell;

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