Skip to content

Add a Builder::header_contents method to allow passing source code as a string to a Builder #601

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 1 commit into from
Mar 27, 2017
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
13 changes: 12 additions & 1 deletion src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,9 @@ impl Drop for Diagnostic {
/// A file which has not been saved to disk.
pub struct UnsavedFile {
x: CXUnsavedFile,
name: CString,
/// The name of the unsaved file. Kept here to avoid leaving dangling pointers in
/// `CXUnsavedFile`.
pub name: CString,
contents: CString,
}

Expand All @@ -1431,6 +1433,15 @@ impl UnsavedFile {
}
}

impl fmt::Debug for UnsavedFile {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt,
"UnsavedFile(name: {:?}, contents: {:?})",
self.name,
self.contents)
}
}

/// Convert a cursor kind into a static string.
pub fn kind_to_str(x: CXCursorKind) -> String {
unsafe { cxstring_into_string(clang_getCursorKindSpelling(x)) }
Expand Down
2 changes: 1 addition & 1 deletion src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'ctx> BindgenContext<'ctx> {
clang::TranslationUnit::parse(&index,
"",
&options.clang_args,
&[],
&options.input_unsaved_files,
parse_options)
.expect("TranslationUnit::parse failed");

Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ impl Builder {
self
}

/// Add `contents` as an input C/C++ header named `name`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps note that the files are automatically added to the clang flags?

///
/// The file `name` will be added to the clang arguments.
pub fn header_contents(mut self, name: &str, contents: &str) -> Builder {
self.options.input_unsaved_files.push(clang::UnsavedFile::new(name, contents));
self
}

/// Set the output graphviz file.
pub fn emit_ir_graphviz<T: Into<String>>(mut self, path: T) -> Builder {
let path = path.into();
Expand Down Expand Up @@ -573,6 +581,9 @@ pub struct BindgenOptions {
/// The input header file.
pub input_header: Option<String>,

/// Unsaved files for input.
pub input_unsaved_files: Vec<clang::UnsavedFile>,

/// Generate a dummy C/C++ file that includes the header and has dummy uses
/// of all types defined therein. See the `uses` module for more.
pub dummy_uses: Option<String>,
Expand Down Expand Up @@ -662,6 +673,7 @@ impl Default for BindgenOptions {
raw_lines: vec![],
clang_args: vec![],
input_header: None,
input_unsaved_files: vec![],
dummy_uses: None,
parse_callbacks: None,
codegen_config: CodegenConfig::all(),
Expand Down Expand Up @@ -754,6 +766,10 @@ impl<'ctx> Bindings<'ctx> {
options.clang_args.push(h.clone())
}

for f in options.input_unsaved_files.iter() {
options.clang_args.push(f.name.to_str().unwrap().to_owned())
}

let mut context = BindgenContext::new(options);
try!(parse(&mut context));

Expand Down
18 changes: 17 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate diff;
extern crate bindgen;
extern crate shlex;

use bindgen::Builder;
use bindgen::{Builder, builder};
use std::fs;
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
use std::path::PathBuf;
Expand Down Expand Up @@ -144,3 +144,19 @@ macro_rules! test_header {

// This file is generated by build.rs
include!(concat!(env!("OUT_DIR"), "/tests.rs"));

#[test]
fn test_header_contents() {
let bindings = builder()
.header_contents("test.h", "int foo(const char* a);")
.no_unstable_rust()
.generate()
.unwrap()
.to_string();
assert_eq!(bindings, "/* automatically generated by rust-bindgen */

extern \"C\" {
pub fn foo(a: *const ::std::os::raw::c_schar) -> ::std::os::raw::c_int;
}
");
}