diff options
author | Christian Poveda <christian.poveda@ferrous-systems.com> | 2022-08-10 12:51:38 -0500 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2022-08-18 18:08:37 +0200 |
commit | 15a720bafa435d95cfb4896beeb1140bc0536595 (patch) | |
tree | b5aab0ce7c2ee14bddea268fd5b3172d61d04eaf | |
parent | 50878f37e639b8c868adec8a8c745e788c2d870a (diff) |
store warnings and emit them later
-rw-r--r-- | src/codegen/mod.rs | 2 | ||||
-rw-r--r-- | src/ir/context.rs | 27 | ||||
-rw-r--r-- | src/lib.rs | 23 |
3 files changed, 45 insertions, 7 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 6bbb7ab5..77e9ba95 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -4329,7 +4329,7 @@ impl CodeGenerator for ObjCInterface { pub(crate) fn codegen( context: BindgenContext, -) -> (Vec<proc_macro2::TokenStream>, BindgenOptions) { +) -> (Vec<proc_macro2::TokenStream>, BindgenOptions, Vec<String>) { context.gen(|context| { let _t = context.timer("codegen"); let counter = Cell::new(0); diff --git a/src/ir/context.rs b/src/ir/context.rs index 36d7eedb..61fa8439 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -459,6 +459,9 @@ pub struct BindgenContext { /// Populated when we enter codegen by `compute_has_float`; always `None` /// before that and `Some` after. has_float: Option<HashSet<ItemId>>, + + /// The set of warnings raised during binding generation. + warnings: Vec<String>, } /// A traversal of allowlisted items. @@ -579,6 +582,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" have_destructor: None, has_type_param_in_array: None, has_float: None, + warnings: Vec::new(), } } @@ -1134,7 +1138,10 @@ If you encounter an error missing from this list, please file an issue or a PR!" /// Enter the code generation phase, invoke the given callback `cb`, and /// leave the code generation phase. - pub(crate) fn gen<F, Out>(mut self, cb: F) -> (Out, BindgenOptions) + pub(crate) fn gen<F, Out>( + mut self, + cb: F, + ) -> (Out, BindgenOptions, Vec<String>) where F: FnOnce(&Self) -> Out, { @@ -1171,7 +1178,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" self.compute_cannot_derive_partialord_partialeq_or_eq(); let ret = cb(&self); - (ret, self.options) + (ret, self.options, self.warnings) } /// When the `testing_only_extra_assertions` feature is enabled, this @@ -2430,17 +2437,27 @@ If you encounter an error missing from this list, please file an issue or a PR!" self.allowlisted = Some(allowlisted); self.codegen_items = Some(codegen_items); + let mut warnings = Vec::new(); + for item in self.options().allowlisted_functions.unmatched_items() { - warn!("unused option: --allowlist-function {}", item); + let warn = format!("unused option: --allowlist-function {}", item); + warn!("{}", warn); + warnings.push(warn); } for item in self.options().allowlisted_vars.unmatched_items() { - warn!("unused option: --allowlist-var {}", item); + let warn = format!("unused option: --allowlist-var {}", item); + warn!("{}", warn); + warnings.push(warn); } for item in self.options().allowlisted_types.unmatched_items() { - warn!("unused option: --allowlist-type {}", item); + let warn = format!("unused option: --allowlist-type {}", item); + warn!("{}", warn); + warnings.push(warn); } + + self.warnings.extend(warnings); } /// Convenient method for getting the prefix to use for most traits in @@ -33,6 +33,19 @@ mod log_stubs; #[macro_use] mod extra_assertions; +/// Print all the warning messages raised while generating the bindings in a build script. +/// +/// If you are using `bindgen` outside of a build script you should use [`Bindings::take_warnings`] +/// directly instead. +#[macro_export] +macro_rules! print_warnings { + ($bindings:expr) => { + for message in $bindings.take_warnings() { + println!("cargo:warning={}", message); + } + }; +} + // A macro to declare an internal module for which we *must* provide // documentation for. If we are building with the "testing_only_docs" feature, // then the module is declared public, and our `#![deny(missing_docs)]` pragma @@ -2222,6 +2235,7 @@ impl std::error::Error for BindgenError {} #[derive(Debug)] pub struct Bindings { options: BindgenOptions, + warnings: Vec<String>, module: proc_macro2::TokenStream, } @@ -2435,10 +2449,11 @@ impl Bindings { parse(&mut context)?; } - let (items, options) = codegen::codegen(context); + let (items, options, warnings) = codegen::codegen(context); Ok(Bindings { options, + warnings, module: quote! { #( #items )* }, @@ -2583,6 +2598,12 @@ impl Bindings { _ => Ok(Cow::Owned(source)), } } + + /// Take all the warning messages. + #[inline] + pub fn take_warnings(&mut self) -> impl Iterator<Item = String> + '_ { + self.warnings.drain(..) + } } impl std::fmt::Display for Bindings { |