Auto merge of #860 - emilio:issue-848, r=fitzgen

lib: Filter out include paths when looking for clang paths.

Fixes #848
diff --git a/Cargo.lock b/Cargo.lock
index 690c0a1..3f0f65d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,6 @@
 [root]
 name = "bindgen"
-version = "0.28.0"
+version = "0.29.0"
 dependencies = [
  "aster 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cexpr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 91ef727..d384b8b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,7 @@
 readme = "README.md"
 repository = "https://github.com/rust-lang-nursery/rust-bindgen"
 documentation = "https://docs.rs/bindgen"
-version = "0.28.0"
+version = "0.29.0"
 build = "build.rs"
 
 exclude = [
diff --git a/src/lib.rs b/src/lib.rs
index 4eeb95f..f2aa10a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1162,8 +1162,35 @@
 
         options.build();
 
+        // Filter out include paths and similar stuff, so we don't incorrectly
+        // promote them to `-isystem`.
+        let clang_args_for_clang_sys = {
+            let mut last_was_include_prefix = false;
+            options.clang_args.iter().filter(|arg| {
+                if last_was_include_prefix {
+                    last_was_include_prefix = false;
+                    return false;
+                }
+
+                let arg = &**arg;
+
+                // https://clang.llvm.org/docs/ClangCommandLineReference.html
+                // -isystem and -isystem-after are harmless.
+                if arg == "-I" || arg == "--include-directory" {
+                    last_was_include_prefix = true;
+                    return false;
+                }
+
+                if arg.starts_with("-I") || arg.starts_with("--include-directory=") {
+                    return false;
+                }
+
+                true
+            }).cloned().collect::<Vec<_>>()
+        };
+
         // TODO: Make this path fixup configurable?
-        if let Some(clang) = clang_sys::support::Clang::find(None, &options.clang_args) {
+        if let Some(clang) = clang_sys::support::Clang::find(None, &clang_args_for_clang_sys) {
             // 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
diff --git a/tests/expectations/tests/issue-848-replacement-system-include.rs b/tests/expectations/tests/issue-848-replacement-system-include.rs
new file mode 100644
index 0000000..1408370
--- /dev/null
+++ b/tests/expectations/tests/issue-848-replacement-system-include.rs
@@ -0,0 +1,24 @@
+/* automatically generated by rust-bindgen */
+
+
+#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
+
+
+/// This is intended to replace another type, but won't if we treat this include
+/// as a system include, because clang doesn't parse comments there.
+///
+/// See #848.
+///
+/// <div rustbindgen replaces="nsTArray"></div>
+#[repr(C)]
+#[derive(Debug, Copy, Clone)]
+pub struct nsTArray<T> {
+    pub m: *mut T,
+    pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
+}
+impl <T> Default for nsTArray<T> {
+    fn default() -> Self { unsafe { ::std::mem::zeroed() } }
+}
+extern "C" {
+    pub fn func() -> *mut nsTArray<::std::os::raw::c_int>;
+}
diff --git a/tests/headers/issue-848-replacement-system-include.hpp b/tests/headers/issue-848-replacement-system-include.hpp
new file mode 100644
index 0000000..e95c823
--- /dev/null
+++ b/tests/headers/issue-848-replacement-system-include.hpp
@@ -0,0 +1,7 @@
+// bindgen-flags: -- -Itests/headers/issue-848
+
+#include "an-include.h"
+
+extern "C" {
+  nsTArray<int>* func();
+}
diff --git a/tests/headers/issue-848/an-include.h b/tests/headers/issue-848/an-include.h
new file mode 100644
index 0000000..0421d19
--- /dev/null
+++ b/tests/headers/issue-848/an-include.h
@@ -0,0 +1,17 @@
+template<typename T>
+class nsTArray {
+  void* mBuffer;
+};
+
+/**
+ * This is intended to replace another type, but won't if we treat this include
+ * as a system include, because clang doesn't parse comments there.
+ *
+ * See #848.
+ *
+ * <div rustbindgen replaces="nsTArray"></div>
+ */
+template<typename T>
+class nsTArray_Simple {
+  T* m;
+};