Skip to content

Preserve comments when dumping preprocessed input headers #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
// files, and therefore any `#include` harms reproducibility. Additionally,
// the test case isn't minimal since the included file almost assuredly
// contains things that aren't necessary to reproduce the bug, and makes
// tracking it down much more difficult. If necessary, see
// tracking it down much more difficult.
//
// Use the `--dump-preprocessed-input` flag or the
// `bindgen::Builder::dump_preprocessed_input` method to make your test case
// standalone and without `#include`s, and then use C-Reduce to minimize it:
// https://github.com/servo/rust-bindgen/blob/master/CONTRIBUTING.md#using-creduce-to-minimize-test-cases
```

Expand Down Expand Up @@ -59,8 +63,8 @@ bindings is missing a field that exists in the C/C++ struct, note that here.
<details>

```
Insert debug logging when running bindgen with the `RUST_LOG=bindgen` environment
variable set.
Insert debug logging when running bindgen (not when compiling bindgen's output)
with the `RUST_LOG=bindgen` environment variable set.
```

</details>
5 changes: 0 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,6 @@ Afterwards, there should be a `__bindgen.i` or `__bindgen.ii` file containing
the combined and preprocessed input headers, which is usable as an isolated,
standalone test case.

Note that the preprocessor likely removed all comments, so if the bug you're
trying to pin down involves source annotations (for example, `/** <div
rustbindgen opaque> */`), then you will have to manually reapply them to the
preprocessed file.

### Writing a Predicate Script

Writing a `predicate.sh` script for a `bindgen` test case is fairly
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use std::fs::{File, OpenOptions};
use std::iter;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::process::{Command, Stdio};
use std::sync::Arc;

use syntax::ast;
Expand Down Expand Up @@ -870,14 +870,27 @@ impl Builder {

let mut cmd = Command::new(&clang.path);
cmd.arg("-save-temps")
.arg("-E")
.arg("-C")
.arg("-c")
.arg(&wrapper_path);
.arg(&wrapper_path)
.stdout(Stdio::piped());

for a in &self.options.clang_args {
cmd.arg(a);
}

if cmd.spawn()?.wait()?.success() {
let mut child = cmd.spawn()?;

let mut preprocessed = child.stdout.take().unwrap();
let mut file = File::create(if is_cpp {
"__bindgen.ii"
} else {
"__bindgen.i"
})?;
io::copy(&mut preprocessed, &mut file)?;

if child.wait()?.success() {
Ok(())
} else {
Err(io::Error::new(io::ErrorKind::Other,
Expand Down
2 changes: 2 additions & 0 deletions tests/headers/arg_keyword.hpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// This comment exists to ensure that `--dump-preprocessed-input` doesn't strip
// comments.
void foo(const char* type);