diff --git a/bindgen/src/main.rs b/bindgen/src/main.rs index 8cbff63bb8..c1dc632f00 100644 --- a/bindgen/src/main.rs +++ b/bindgen/src/main.rs @@ -42,31 +42,6 @@ pub fn main() { _ => {} } - if let Some(clang) = clang_sys::support::Clang::find(None) { - let has_clang_args = - bind_args.iter().rposition(|arg| *arg == "--").is_some(); - if !has_clang_args { - bind_args.push("--".to_owned()); - } - - // If --target is specified, assume caller knows what they're doing and - // don't mess with - // include paths for them - let has_target_arg = bind_args.iter() - .rposition(|arg| arg.starts_with("--target")) - .is_some(); - if !has_target_arg { - // TODO: distinguish C and C++ paths? C++'s should be enough, I - // guess. - for path in clang.cpp_search_paths.into_iter() { - if let Ok(path) = path.into_os_string().into_string() { - bind_args.push("-isystem".to_owned()); - bind_args.push(path); - } - } - } - } - match builder_from_flags(bind_args.into_iter()) { Ok((builder, output)) => { let mut bindings = builder.generate() diff --git a/libbindgen/Cargo.toml b/libbindgen/Cargo.toml index 8d050a965f..47e8b2827e 100644 --- a/libbindgen/Cargo.toml +++ b/libbindgen/Cargo.toml @@ -12,7 +12,7 @@ license = "BSD-3-Clause" name = "libbindgen" readme = "README.md" repository = "https://github.com/servo/rust-bindgen" -version = "0.1.1" +version = "0.1.2" workspace = ".." [dev-dependencies] diff --git a/libbindgen/src/lib.rs b/libbindgen/src/lib.rs index 7db26eabeb..29580f3682 100644 --- a/libbindgen/src/lib.rs +++ b/libbindgen/src/lib.rs @@ -168,8 +168,8 @@ impl Builder { /// Set the input C/C++ header. pub fn header>(mut self, header: T) -> Builder { let header = header.into(); - self.options.input_header = Some(header.clone()); - self.clang_arg(header) + self.options.input_header = Some(header); + self } /// Generate a C/C++ file that includes the header and has dummy uses of @@ -504,13 +504,36 @@ impl<'ctx> Bindings<'ctx> { /// /// Deprecated - use a `Builder` instead #[deprecated] - pub fn generate(options: BindgenOptions, + pub fn generate(mut options: BindgenOptions, span: Option) -> Result, ()> { let span = span.unwrap_or(DUMMY_SP); + // TODO: Make this path fixup configurable? + if let Some(clang) = clang_sys::support::Clang::find(None) { + // If --target is specified, assume caller knows what they're doing + // and don't mess with include paths for them + let has_target_arg = options.clang_args.iter() + .rposition(|arg| arg.starts_with("--target")) + .is_some(); + if !has_target_arg { + // TODO: distinguish C and C++ paths? C++'s should be enough, I + // guess. + for path in clang.cpp_search_paths.into_iter() { + if let Ok(path) = path.into_os_string().into_string() { + options.clang_args.push("-isystem".to_owned()); + options.clang_args.push(path); + } + } + } + } + + if let Some(h) = options.input_header.as_ref() { + options.clang_args.push(h.clone()) + } + let mut context = BindgenContext::new(options); - parse(&mut context); + try!(parse(&mut context)); let module = ast::Mod { inner: span, @@ -624,14 +647,20 @@ pub fn parse_one(ctx: &mut BindgenContext, } /// Parse the Clang AST into our `Item` internal representation. -fn parse(context: &mut BindgenContext) { +fn parse(context: &mut BindgenContext) -> Result<(), ()> { use clang::Diagnostic; use clangll::*; + let mut any_error = false; for d in context.translation_unit().diags().iter() { let msg = d.format(Diagnostic::default_opts()); let is_err = d.severity() >= CXDiagnostic_Error; println!("{}, err: {}", msg, is_err); + any_error |= is_err; + } + + if any_error { + return Err(()); } let cursor = context.translation_unit().cursor(); @@ -646,6 +675,7 @@ fn parse(context: &mut BindgenContext) { assert!(context.current_module() == context.root_module(), "How did this happen?"); + Ok(()) } /// Extracted Clang version data diff --git a/libbindgen/tests/expectations/tests/dash_language.rs b/libbindgen/tests/expectations/tests/dash_language.rs new file mode 100644 index 0000000000..148f9c32d7 --- /dev/null +++ b/libbindgen/tests/expectations/tests/dash_language.rs @@ -0,0 +1,12 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Foo { + pub bar: ::std::os::raw::c_int, + pub _phantom_0: ::std::marker::PhantomData, +} diff --git a/libbindgen/tests/headers/bitfield-enum-basic.h b/libbindgen/tests/headers/bitfield-enum-basic.hpp similarity index 100% rename from libbindgen/tests/headers/bitfield-enum-basic.h rename to libbindgen/tests/headers/bitfield-enum-basic.hpp diff --git a/libbindgen/tests/headers/dash_language.h b/libbindgen/tests/headers/dash_language.h new file mode 100644 index 0000000000..4c8bb58dd7 --- /dev/null +++ b/libbindgen/tests/headers/dash_language.h @@ -0,0 +1,6 @@ +// bindgen-flags: -- -x c++ --std=c++11 + +template +struct Foo { + int bar; +}; diff --git a/libbindgen/tests/headers/empty_template_param_name.hpp b/libbindgen/tests/headers/empty_template_param_name.hpp index b3360bc95c..0e9f3c341f 100644 --- a/libbindgen/tests/headers/empty_template_param_name.hpp +++ b/libbindgen/tests/headers/empty_template_param_name.hpp @@ -1,3 +1,5 @@ +// bindgen-flags: -- -std=c++11 + template using __void_t = void; template>