diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clang.rs | 13 | ||||
-rw-r--r-- | src/ir/context.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 16 |
3 files changed, 29 insertions, 2 deletions
diff --git a/src/clang.rs b/src/clang.rs index fc7950dc..ee422745 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -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, } @@ -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)) } diff --git a/src/ir/context.rs b/src/ir/context.rs index 4a6785b1..c95d5179 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -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"); @@ -175,6 +175,14 @@ impl Builder { self } + /// Add `contents` as an input C/C++ header named `name`. + /// + /// 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(); @@ -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>, @@ -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(), @@ -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)); |