|
| 1 | +use std::fs::File; |
| 2 | +use std::io::Write; |
| 3 | +use std::path::Path; |
| 4 | + |
| 5 | +use clang::diagnostic::Severity; |
| 6 | +use clang::{Clang, Entity, Index}; |
| 7 | +use opencv_binding_generator::writer::rust_native::element::RustNativeGeneratedElement; |
| 8 | +use opencv_binding_generator::{EntityWalkerExt, Func, GeneratorEnv, GeneratorVisitor, OpenCvWalker}; |
| 9 | +use tempfile::TempDir; |
| 10 | + |
| 11 | +fn clang_parse(code: &str, op: impl FnOnce(Entity)) { |
| 12 | + const CODE_TPL: &str = include_str!("code_template.cpp"); |
| 13 | + const CODE_PAT: &str = "{{code}}"; |
| 14 | + |
| 15 | + let temp_dir = TempDir::new().expect("Can't create temp dir"); |
| 16 | + let temp_file_path = temp_dir.path().join("temp.cpp"); |
| 17 | + if let Some(start) = CODE_TPL.find(CODE_PAT) { |
| 18 | + let mut temp_cpp = File::create(&temp_file_path).expect("Can't create temp file"); |
| 19 | + temp_cpp |
| 20 | + .write_all(CODE_TPL[..start].as_bytes()) |
| 21 | + .expect("Can't write to temp file"); |
| 22 | + temp_cpp.write_all(code.as_bytes()).expect("Can't write to temp file"); |
| 23 | + temp_cpp |
| 24 | + .write_all(CODE_TPL[start + CODE_PAT.len()..].as_bytes()) |
| 25 | + .expect("Can't write to temp file"); |
| 26 | + } |
| 27 | + let clang = Clang::new().expect("Can't init clang"); |
| 28 | + let index = Index::new(&clang, false, false); |
| 29 | + let root_tu = index |
| 30 | + .parser(&temp_file_path) |
| 31 | + .skip_function_bodies(true) |
| 32 | + .detailed_preprocessing_record(true) |
| 33 | + .parse() |
| 34 | + .expect("Can't parse"); |
| 35 | + let diags = root_tu.get_diagnostics(); |
| 36 | + if !diags.is_empty() { |
| 37 | + let mut has_error = false; |
| 38 | + eprintln!("WARNING: {} diagnostic messages", diags.len()); |
| 39 | + for diag in diags { |
| 40 | + if !has_error && matches!(diag.get_severity(), Severity::Error | Severity::Fatal) { |
| 41 | + has_error = true; |
| 42 | + } |
| 43 | + eprintln!(" {diag}"); |
| 44 | + } |
| 45 | + if has_error { |
| 46 | + panic!("Errors during header parsing"); |
| 47 | + } |
| 48 | + } |
| 49 | + op(root_tu.get_entity()); |
| 50 | +} |
| 51 | + |
| 52 | +fn extract_functions(code: &str, cb: impl FnMut(Func)) { |
| 53 | + struct FunctionExtractor<F> { |
| 54 | + cb: F, |
| 55 | + } |
| 56 | + |
| 57 | + impl<F: FnMut(Func)> GeneratorVisitor<'_> for FunctionExtractor<F> { |
| 58 | + fn visit_func(&mut self, func: Func) { |
| 59 | + (self.cb)(func); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + clang_parse(code, |root_tu| { |
| 64 | + let gen_env = GeneratorEnv::empty(); |
| 65 | + let visitor = FunctionExtractor { cb }; |
| 66 | + let opencv_walker = OpenCvWalker::new("core", Path::new(""), visitor, gen_env); |
| 67 | + |
| 68 | + root_tu.walk_opencv_entities(opencv_walker); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn char_ptr_slice() { |
| 74 | + extract_functions("CV_EXPORTS int startLoop(int argc, char* argv[]);", |f| { |
| 75 | + assert_eq!(f.gen_rust("0.0.0").trim(), "#[inline]\npub fn start_loop(argv: &mut [&str]) -> Result<i32> {\n\tstring_array_arg_mut!(argv);\n\treturn_send!(via ocvrs_return);\n\tunsafe { sys::cv_startLoop_int_charXX(argv.len().try_into()?, argv.as_mut_ptr(), ocvrs_return.as_mut_ptr()) };\n\treturn_receive!(unsafe ocvrs_return => ret);\n\tlet ret = ret.into_result()?;\n\tOk(ret)\n}"); |
| 76 | + assert_eq!(f.gen_cpp().trim(), "void cv_startLoop_int_charXX(int argc, char** argv, Result<int>* ocvrs_return) {\n\ttry {\n\t\tint ret = cv::startLoop(argc, argv);\n\t\tOk(ret, ocvrs_return);\n\t} OCVRS_CATCH(ocvrs_return);\n}"); |
| 77 | + assert_eq!( |
| 78 | + f.gen_rust_externs().trim(), |
| 79 | + r#"pub fn cv_startLoop_int_charXX(argc: i32, argv: *mut *mut c_char, ocvrs_return: *mut Result<i32>);"#, |
| 80 | + ); |
| 81 | + }); |
| 82 | +} |
0 commit comments