summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2020-11-26 12:09:15 +0100
committerEmilio Cobos Álvarez <emilio@crisal.io>2020-11-26 13:26:08 +0100
commit337703b38f35ca9a87390720b339132a01e4c011 (patch)
tree9be43498616c70cdeaeea053544b139f1e1ebcf5
parent11ae35089e4748b7e2a6f50f6bef7709af72e724 (diff)
lib: Improve c++ auto-detection.
Fixes #1919, as otherwise we peek the wrong include paths unless you pass "-x c++" or such.
-rw-r--r--src/lib.rs40
1 files changed, 27 insertions, 13 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 09410b5e..a29c2cbb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -90,10 +90,26 @@ pub(crate) use std::collections::hash_map::Entry;
/// Default prefix for the anon fields.
pub const DEFAULT_ANON_FIELDS_PREFIX: &'static str = "__bindgen_anon_";
+fn file_is_cpp(name_file: &str) -> bool {
+ name_file.ends_with(".hpp") ||
+ name_file.ends_with(".hxx") ||
+ name_file.ends_with(".hh") ||
+ name_file.ends_with(".h++")
+}
+
fn args_are_cpp(clang_args: &[String]) -> bool {
- return clang_args
- .windows(2)
- .any(|w| w[0] == "-xc++" || w[1] == "-xc++" || w == &["-x", "c++"]);
+ for w in clang_args.windows(2) {
+ if w[0] == "-xc++" || w[1] == "-xc++" {
+ return true;
+ }
+ if w[0] == "-x" && w[1] == "c++" {
+ return true;
+ }
+ if w[0] == "-include" && file_is_cpp(&w[1]) {
+ return true;
+ }
+ }
+ false
}
bitflags! {
@@ -1341,13 +1357,6 @@ impl Builder {
/// issues. The resulting file will be named something like `__bindgen.i` or
/// `__bindgen.ii`
pub fn dump_preprocessed_input(&self) -> io::Result<()> {
- fn check_is_cpp(name_file: &str) -> bool {
- name_file.ends_with(".hpp") ||
- name_file.ends_with(".hxx") ||
- name_file.ends_with(".hh") ||
- name_file.ends_with(".h++")
- }
-
let clang =
clang_sys::support::Clang::find(None, &[]).ok_or_else(|| {
io::Error::new(
@@ -1365,7 +1374,7 @@ impl Builder {
// For each input header, add `#include "$header"`.
for header in &self.input_headers {
- is_cpp |= check_is_cpp(header);
+ is_cpp |= file_is_cpp(header);
wrapper_contents.push_str("#include \"");
wrapper_contents.push_str(header);
@@ -1375,7 +1384,7 @@ impl Builder {
// For each input header content, add a prefix line of `#line 0 "$name"`
// followed by the contents.
for &(ref name, ref contents) in &self.input_header_contents {
- is_cpp |= check_is_cpp(name);
+ is_cpp |= file_is_cpp(name);
wrapper_contents.push_str("#line 0 \"");
wrapper_contents.push_str(name);
@@ -2069,7 +2078,12 @@ impl Bindings {
debug!("Found clang: {:?}", clang);
// Whether we are working with C or C++ inputs.
- let is_cpp = args_are_cpp(&options.clang_args);
+ let is_cpp = args_are_cpp(&options.clang_args) ||
+ options
+ .input_header
+ .as_ref()
+ .map_or(false, |i| file_is_cpp(&i));
+
let search_paths = if is_cpp {
clang.cpp_search_paths
} else {