<commit_before>use llvm_sys::core::{LLVMCreateMemoryBufferWithContentsOfFile, LLVMCreateMemoryBufferWithSTDIN, LLVMCreateMemoryBufferWithMemoryRange, LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMGetBufferStart, LLVMGetBufferSize, LLVMDisposeMemoryBuffer};
use llvm_sys::prelude::LLVMMemoryBufferRef;
use llvm_sys::object::LLVMCreateObjectFile;

use crate::object_file::ObjectFile;
use crate::support::LLVMString;

use std::ffi::CString;
use std::mem::{forget, zeroed};
use std::path::Path;
use std::ptr;
use std::slice;

#[derive(Debug)]
pub struct MemoryBuffer {
    pub(crate) memory_buffer: LLVMMemoryBufferRef
}

impl MemoryBuffer {
    pub(crate) fn new(memory_buffer: LLVMMemoryBufferRef) -> Self {
        assert!(!memory_buffer.is_null());

        MemoryBuffer {
            memory_buffer
        }
    }

    pub fn create_from_file(path: &Path) -> Result<Self, LLVMString> {
        let path = path.to_str().expect("Did not find a valid Unicode path string");
        let mut memory_buffer = ptr::null_mut();
        let mut err_string = unsafe { zeroed() };

        let return_code = unsafe {
            // REVIEW: Unclear why this expects *const i8 instead of *const u8
            LLVMCreateMemoryBufferWithContentsOfFile(path.as_ptr() as *const i8, &mut memory_buffer, &mut err_string)
        };
<commit_msg>bugfix: use CString when passing path across FFI<commit_after>use llvm_sys::core::{LLVMCreateMemoryBufferWithContentsOfFile, LLVMCreateMemoryBufferWithSTDIN, LLVMCreateMemoryBufferWithMemoryRange, LLVMCreateMemoryBufferWithMemoryRangeCopy, LLVMGetBufferStart, LLVMGetBufferSize, LLVMDisposeMemoryBuffer};
use llvm_sys::prelude::LLVMMemoryBufferRef;
use llvm_sys::object::LLVMCreateObjectFile;

use crate::object_file::ObjectFile;
use crate::support::LLVMString;

use std::ffi::CString;
use std::mem::{forget, zeroed};
use std::path::Path;
use std::ptr;
use std::slice;

#[derive(Debug)]
pub struct MemoryBuffer {
    pub(crate) memory_buffer: LLVMMemoryBufferRef
}

impl MemoryBuffer {
    pub(crate) fn new(memory_buffer: LLVMMemoryBufferRef) -> Self {
        assert!(!memory_buffer.is_null());

        MemoryBuffer {
            memory_buffer
        }
    }

    pub fn create_from_file(path: &Path) -> Result<Self, LLVMString> {
        let path = CString::new(path.to_str().expect("Did not find a valid Unicode path string")).expect("Failed to convert to CString");
        let mut memory_buffer = ptr::null_mut();
        let mut err_string = unsafe { zeroed() };

        let return_code = unsafe {
            // REVIEW: Unclear why this expects *const i8 instead of *const u8
            LLVMCreateMemoryBufferWithContentsOfFile(path.as_ptr() as *const i8, &mut memory_buffer, &mut err_string)
        };

{'fraction_non_alphanumeric': 0.10641399416909621, 'fraction_numerical': 0.002186588921282799, 'mean_word_length': 4.545454545454546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2057219', 'n_tokens_mistral': 847, 'n_tokens_neox': 866, 'n_words': 193}