<commit_before>use il;
use clang as cx;

static PREFIX: &'static str = "POSGEN_VAR_";

struct Context<'a> {
    file: &'a [u8],
    defs: &'a ::Defs,
    globals: Vec<il::Global>,
}

impl<'a> Context<'a> {
    fn conv_ptr_ty(&mut self, ty: &cx::Type) -> String {
        let mutable = if ty.is_const() {
            "const"
        } else {
            "mut"
        };
        match ty.kind() {
            cx::ll::CXType_Unexposed | cx::ll::CXType_FunctionProto
                    | cx::ll::CXType_FunctionNoProto => {
                let ret_ty = ty.ret_type();
                let decl = ty.declaration();
                if ret_ty.kind() != cx::ll::CXType_Invalid {
                    let mut ret = String::new();
                    ret.push_str("fn(");
                    for arg in ty.arg_types().iter() {
                        ret.push_str(self.type_to_str(arg).as_slice());
                        ret.push_str(", ");
                    }
                    ret.push_str(")");
                    match ret_ty.kind() {
                        cx::ll::CXType_Void => { },
                        _ => ret.push_str(format!(" -> {}", self.type_to_str(&ret_ty)).as_slice()),
                    }
                    ret
                } else if decl.kind() != cx::ll::CXCursor_NoDeclFound {
                    format!("*{} {}", mutable, self.type_to_str(&decl.cur_type()))
                } else {
                    format!("*{} ::void_t", mutable)
                }
            }
            _ => {
                format!("*{} {}", mutable, self.type_to_str(ty))
            }
        }
    }

    fn type_to_str(&mut self, ty: &cx::Type) -> String {
        self.type_to_str_int(ty, true)
    }

    fn find_type(&self, spelling: &str, skip: bool) -> Option<String> {
        let trimmed = spelling.as_slice().trim_left_chars('_');
        match self.defs.types.iter().find(|t| t.as_slice() == trimmed) {
            Some(s) => if skip { return Some(s.to_string()) },
            _ => match ::types::find(trimmed) {
                Some(t) => return Some(t.to_string()),
                _ => { },
            },
        }
        None
    }

    fn type_to_str_int(&mut self, ty: &cx::Type, skip: bool) -> String {
        match ty.kind() {
            cx::ll::CXType_SChar | cx::ll::CXType_Char_S => "::schar_t".to_string(),
            cx::ll::CXType_UChar | cx::ll::CXType_Char_U => "::uchar_t".to_string(),
            cx::ll::CXType_Void       => "::void_t".to_string(),
            cx::ll::CXType_Bool       => "::bool_t".to_string(),
            cx::ll::CXType_UShort     => "::ushort_t".to_string(),
            cx::ll::CXType_UInt       => "::uint_t".to_string(),
            cx::ll::CXType_ULong      => "::ulong_t".to_string(),
            cx::ll::CXType_ULongLong  => "::ulonglong_t".to_string(),
            cx::ll::CXType_Short      => "::short_t".to_string(),
            cx::ll::CXType_Int        => "::int_t".to_string(),
            cx::ll::CXType_Long       => "::long_t".to_string(),
            cx::ll::CXType_LongLong   => "::longlong_t".to_string(),
            cx::ll::CXType_Float      => "::float_t".to_string(),
            cx::ll::CXType_Double     => "::double_t".to_string(),
            cx::ll::CXType_LongDouble => "::longdouble_t".to_string(),
            cx::ll::CXType_Pointer => self.conv_ptr_ty(&ty.pointee_type()),
            cx::ll::CXType_Record | cx::ll::CXType_Unexposed | cx::ll::CXType_Typedef => {
                let spelling = ty.spelling();
                match self.find_type(spelling.as_slice(), skip) {
                    Some(s) => return s,
                    _ => { }
                }
                let cursor = ty.declaration();
                match cursor.kind() {
                    cx::ll::CXCursor_StructDecl | cx::ll::CXCursor_UnionDecl => {
                        let spelling = cursor.spelling();
                        match self.find_type(spelling.as_slice(), skip) {
                            Some(s) => s,
                            _ => unknown_array(ty),
                        }
                    },
                    cx::ll::CXCursor_EnumDecl => {
                        match ty.size() {
                            1 => "i8".to_string(),
                            2 => "i16".to_string(),
                            4 => "i32".to_string(),
                            8 => "i64".to_string(),
                            _ => fail!("Enum type too large"),
                        }
                    },
                    cx::ll::CXCursor_TypedefDecl => {
                        let ty = cursor.typedef_type();
                        self.type_to_str_int(&ty, skip)
                    },
                    _ => format!("UNKNOWN: {} {}", cursor.kind(), ty.kind()),
                }
            },
            cx::ll::CXType_Enum => {
                match ty.size() {
                    1 => "i8".to_string(),
                    2 => "i16".to_string(),
                    4 => "i32".to_string(),
                    8 => "i64".to_string(),
                    _ => fail!("Enum type too large"),<commit_msg>Fix: Rename fail! to panic!<commit_after>use il;
use clang as cx;

static PREFIX: &'static str = "POSGEN_VAR_";

struct Context<'a> {
    file: &'a [u8],
    defs: &'a ::Defs,
    globals: Vec<il::Global>,
}

impl<'a> Context<'a> {
    fn conv_ptr_ty(&mut self, ty: &cx::Type) -> String {
        let mutable = if ty.is_const() {
            "const"
        } else {
            "mut"
        };
        match ty.kind() {
            cx::ll::CXType_Unexposed | cx::ll::CXType_FunctionProto
                    | cx::ll::CXType_FunctionNoProto => {
                let ret_ty = ty.ret_type();
                let decl = ty.declaration();
                if ret_ty.kind() != cx::ll::CXType_Invalid {
                    let mut ret = String::new();
                    ret.push_str("fn(");
                    for arg in ty.arg_types().iter() {
                        ret.push_str(self.type_to_str(arg).as_slice());
                        ret.push_str(", ");
                    }
                    ret.push_str(")");
                    match ret_ty.kind() {
                        cx::ll::CXType_Void => { },
                        _ => ret.push_str(format!(" -> {}", self.type_to_str(&ret_ty)).as_slice()),
                    }
                    ret
                } else if decl.kind() != cx::ll::CXCursor_NoDeclFound {
                    format!("*{} {}", mutable, self.type_to_str(&decl.cur_type()))
                } else {
                    format!("*{} ::void_t", mutable)
                }
            }
            _ => {
                format!("*{} {}", mutable, self.type_to_str(ty))
            }
        }
    }

    fn type_to_str(&mut self, ty: &cx::Type) -> String {
        self.type_to_str_int(ty, true)
    }

    fn find_type(&self, spelling: &str, skip: bool) -> Option<String> {
        let trimmed = spelling.as_slice().trim_left_chars('_');
        match self.defs.types.iter().find(|t| t.as_slice() == trimmed) {
            Some(s) => if skip { return Some(s.to_string()) },
            _ => match ::types::find(trimmed) {
                Some(t) => return Some(t.to_string()),
                _ => { },
            },
        }
        None
    }

    fn type_to_str_int(&mut self, ty: &cx::Type, skip: bool) -> String {
        match ty.kind() {
            cx::ll::CXType_SChar | cx::ll::CXType_Char_S => "::schar_t".to_string(),
            cx::ll::CXType_UChar | cx::ll::CXType_Char_U => "::uchar_t".to_string(),
            cx::ll::CXType_Void       => "::void_t".to_string(),
            cx::ll::CXType_Bool       => "::bool_t".to_string(),
            cx::ll::CXType_UShort     => "::ushort_t".to_string(),
            cx::ll::CXType_UInt       => "::uint_t".to_string(),
            cx::ll::CXType_ULong      => "::ulong_t".to_string(),
            cx::ll::CXType_ULongLong  => "::ulonglong_t".to_string(),
            cx::ll::CXType_Short      => "::short_t".to_string(),
            cx::ll::CXType_Int        => "::int_t".to_string(),
            cx::ll::CXType_Long       => "::long_t".to_string(),
            cx::ll::CXType_LongLong   => "::longlong_t".to_string(),
            cx::ll::CXType_Float      => "::float_t".to_string(),
            cx::ll::CXType_Double     => "::double_t".to_string(),
            cx::ll::CXType_LongDouble => "::longdouble_t".to_string(),
            cx::ll::CXType_Pointer => self.conv_ptr_ty(&ty.pointee_type()),
            cx::ll::CXType_Record | cx::ll::CXType_Unexposed | cx::ll::CXType_Typedef => {
                let spelling = ty.spelling();
                match self.find_type(spelling.as_slice(), skip) {
                    Some(s) => return s,
                    _ => { }
                }
                let cursor = ty.declaration();
                match cursor.kind() {
                    cx::ll::CXCursor_StructDecl | cx::ll::CXCursor_UnionDecl => {
                        let spelling = cursor.spelling();
                        match self.find_type(spelling.as_slice(), skip) {
                            Some(s) => s,
                            _ => unknown_array(ty),
                        }
                    },
                    cx::ll::CXCursor_EnumDecl => {
                        match ty.size() {
                            1 => "i8".to_string(),
                            2 => "i16".to_string(),
                            4 => "i32".to_string(),
                            8 => "i64".to_string(),
                            _ => panic!("Enum type too large"),
                        }
                    },
                    cx::ll::CXCursor_TypedefDecl => {
                        let ty = cursor.typedef_type();
                        self.type_to_str_int(&ty, skip)
                    },
                    _ => format!("UNKNOWN: {} {}", cursor.kind(), ty.kind()),
                }
            },
            cx::ll::CXType_Enum => {
                match ty.size() {
                    1 => "i8".to_string(),
                    2 => "i16".to_string(),
                    4 => "i32".to_string(),
                    8 => "i64".to_string(),
                    _ => panic!("Enum type too large"),
{'fraction_non_alphanumeric': 0.16296944930920412, 'fraction_numerical': 0.004475578906402024, 'mean_word_length': 1.3451973534109058, 'pattern_counts': {'":': 30, '<': 13, '<?xml version=': 0, '>': 111, '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': '4399522', 'n_tokens_mistral': 3201, 'n_tokens_neox': 3034, 'n_words': 455}