1 //! The CXX code generator for constructing and compiling C++ code.
2 //!
3 //! This is intended as a mechanism for embedding the `cxx` crate into
4 //! higher-level code generators. See [dtolnay/cxx#235] and
5 //! [https://github.com/google/autocxx].
6 //!
7 //! [dtolnay/cxx#235]: https://github.com/dtolnay/cxx/issues/235
8 //! [https://github.com/google/autocxx]: https://github.com/google/autocxx
9
10 #![deny(missing_docs)]
11 #![allow(dead_code)]
12 #![allow(
13 clippy::cast_sign_loss,
14 clippy::default_trait_access,
15 clippy::derive_partial_eq_without_eq,
16 clippy::enum_glob_use,
17 clippy::if_same_then_else,
18 clippy::inherent_to_string,
19 clippy::items_after_statements,
20 clippy::match_bool,
21 clippy::match_on_vec_items,
22 clippy::match_same_arms,
23 clippy::missing_errors_doc,
24 clippy::module_name_repetitions,
25 clippy::needless_pass_by_value,
26 clippy::new_without_default,
27 clippy::nonminimal_bool,
28 clippy::option_if_let_else,
29 clippy::or_fun_call,
30 clippy::redundant_else,
31 clippy::shadow_unrelated,
32 clippy::similar_names,
33 clippy::single_match_else,
34 clippy::struct_excessive_bools,
35 clippy::too_many_arguments,
36 clippy::too_many_lines,
37 clippy::toplevel_ref_arg,
38 // clippy bug: https://github.com/rust-lang/rust-clippy/issues/6983
39 clippy::wrong_self_convention
40 )]
41
42 mod error;
43 mod gen;
44 mod syntax;
45
46 pub use crate::error::Error;
47 pub use crate::gen::include::{Include, HEADER};
48 pub use crate::gen::{GeneratedCode, Opt};
49 pub use crate::syntax::IncludeKind;
50 use proc_macro2::TokenStream;
51
52 /// Generate C++ bindings code from a Rust token stream. This should be a Rust
53 /// token stream which somewhere contains a `#[cxx::bridge] mod {}`.
generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error>54 pub fn generate_header_and_cc(rust_source: TokenStream, opt: &Opt) -> Result<GeneratedCode, Error> {
55 let syntax = syn::parse2(rust_source)
56 .map_err(crate::gen::Error::from)
57 .map_err(Error::from)?;
58 gen::generate(syntax, opt).map_err(Error::from)
59 }
60