Skip to content

Commit 6187c35

Browse files
committed
Preserve comments when dumping preprocessed input headers
The -C flag tells Clang's preprocessor not to strip comments, but unfortunately Clang will only accept it when dumping the preprocessed file to stdout, which is the -E flag. This means that we need to capture the child process's stdout and then copy it to a file ourselves now.
1 parent 767ac02 commit 6187c35

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/lib.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ use std::fs::{File, OpenOptions};
9191
use std::iter;
9292
use std::io::{self, Write};
9393
use std::path::{Path, PathBuf};
94-
use std::process::Command;
94+
use std::process::{Command, Stdio};
9595
use std::sync::Arc;
9696

9797
use syntax::ast;
@@ -870,19 +870,30 @@ impl Builder {
870870

871871
let mut cmd = Command::new(&clang.path);
872872
cmd.arg("-save-temps")
873+
.arg("-E")
874+
.arg("-C")
873875
.arg("-c")
874-
.arg(&wrapper_path);
876+
.arg(&wrapper_path)
877+
.stdout(Stdio::piped());
875878

876879
for a in &self.options.clang_args {
877880
cmd.arg(a);
878881
}
879882

880-
if cmd.spawn()?.wait()?.success() {
881-
Ok(())
882-
} else {
883-
Err(io::Error::new(io::ErrorKind::Other,
884-
"clang exited with non-zero status"))
883+
let mut child = cmd.spawn()?;
884+
if !child.wait()?.success() {
885+
return Err(io::Error::new(io::ErrorKind::Other,
886+
"clang exited with non-zero status"));
885887
}
888+
889+
let mut preprocessed = child.stdout.take().unwrap();
890+
let mut file = File::create(if is_cpp {
891+
"__bindgen.ii"
892+
} else {
893+
"__bindgen.i"
894+
})?;
895+
io::copy(&mut preprocessed, &mut file)?;
896+
Ok(())
886897
}
887898
}
888899

tests/headers/arg_keyword.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
// This comment exists to ensure that `--dump-preprocessed-input` doesn't strip
2+
// comments.
13
void foo(const char* type);

0 commit comments

Comments
 (0)