|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use csv; |
| 4 | + |
| 5 | +// Essentially the CSV file in a more accessible manner. |
| 6 | +pub struct AnalysisData { |
| 7 | + pub var_map: HashMap<String, HashMap<String, String>>, |
| 8 | + pub var_ref_map: HashMap<String, Vec<HashMap<String, String>>>, |
| 9 | + pub type_map: HashMap<String, HashMap<String, String>>, |
| 10 | + pub type_ref_map: HashMap<String, Vec<HashMap<String, String>>>, |
| 11 | + pub func_map: HashMap<String, HashMap<String, String>>, |
| 12 | + pub func_ref_map: HashMap<String, Vec<HashMap<String, String>>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl AnalysisData { |
| 16 | + pub fn new(analysis: &str) -> AnalysisData { |
| 17 | + let mut var_map = HashMap::new(); |
| 18 | + let mut var_ref_map = HashMap::new(); |
| 19 | + let mut type_map = HashMap::new(); |
| 20 | + let mut type_ref_map = HashMap::new(); |
| 21 | + let mut ctor_map = HashMap::new(); |
| 22 | + let mut qual_type_map = HashMap::new(); |
| 23 | + let mut func_map = HashMap::new(); |
| 24 | + let mut func_ref_map = HashMap::new(); |
| 25 | + |
| 26 | + for line in analysis.lines() { |
| 27 | + //println!("{}", line); |
| 28 | + let mut rdr = csv::Reader::from_string(line).has_headers(false); |
| 29 | + for row in rdr.records() { |
| 30 | + let row = row.unwrap(); |
| 31 | + let mut map_record = HashMap::new(); |
| 32 | + //println!("{:?}", row); |
| 33 | + |
| 34 | + let mut it = row.iter(); |
| 35 | + it.next(); // discard first value |
| 36 | + while let Some(key) = it.next() { |
| 37 | + if let Some(val) = it.next() { |
| 38 | + // has pair of values as expected |
| 39 | + if key.to_string() == "qualname" { |
| 40 | + let new_val = val.trim_left_matches(':'); |
| 41 | + map_record.insert(key.clone(), new_val.to_string()); |
| 42 | + if !map_record.contains_key("name") { |
| 43 | + let name: Vec<&str> = new_val.split("::").collect(); |
| 44 | + map_record.insert("name".to_string(), name[name.len()-1].to_string()); |
| 45 | + } |
| 46 | + } else { |
| 47 | + map_record.insert(key.clone(), val.clone()); |
| 48 | + } |
| 49 | + } else { |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + match &row[0][..] { |
| 55 | + "crate" => {}, |
| 56 | + "external_crate" => {}, |
| 57 | + "end_external_crates" => {}, |
| 58 | + "function" | "function_impl" | "method_decl" => { |
| 59 | + let rec = map_record.clone(); |
| 60 | + let copy = map_record.clone(); |
| 61 | + let key = rec.get("id").unwrap(); |
| 62 | + func_map.insert(key.clone(), map_record); |
| 63 | + |
| 64 | + // Treat method impl as a function ref |
| 65 | + let declid = rec.get("declid"); |
| 66 | + match declid { |
| 67 | + Some(x) if *x != "" => { |
| 68 | + if !func_ref_map.contains_key(x) { |
| 69 | + let v = vec![copy]; |
| 70 | + func_ref_map.insert(x.clone(), v); |
| 71 | + } else { |
| 72 | + let vec = func_ref_map.get_mut(x); |
| 73 | + vec.unwrap().push(copy); |
| 74 | + } |
| 75 | + }, |
| 76 | + _ => {} |
| 77 | + } |
| 78 | + }, |
| 79 | + "fn_ref" | "fn_call" | "method_call" => { |
| 80 | + let rec = map_record.clone(); |
| 81 | + let refid = rec.get("refid"); |
| 82 | + let declid = rec.get("declid"); |
| 83 | + let mut key = "".to_string(); |
| 84 | + |
| 85 | + match refid { |
| 86 | + Some(x) if *x != "" && *x != "0" => { |
| 87 | + key = x.clone(); |
| 88 | + }, |
| 89 | + _ => { |
| 90 | + match declid { |
| 91 | + Some(x) if *x != "" => { |
| 92 | + key = x.clone(); |
| 93 | + }, |
| 94 | + None | _ => {} |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if !func_ref_map.contains_key(&key) { |
| 100 | + let v = vec![map_record]; |
| 101 | + func_ref_map.insert(key, v); |
| 102 | + } else { |
| 103 | + let vec = func_ref_map.get_mut(&key); |
| 104 | + vec.unwrap().push(map_record); |
| 105 | + |
| 106 | + } |
| 107 | + }, |
| 108 | + "variable" => { |
| 109 | + let key = map_record.get("id").unwrap().clone(); |
| 110 | + var_map.insert(key, map_record); |
| 111 | + }, |
| 112 | + "var_ref" => { |
| 113 | + let key = map_record.get("refid").unwrap().clone(); |
| 114 | + |
| 115 | + if !var_ref_map.contains_key(&key) { |
| 116 | + let v = vec![map_record]; |
| 117 | + var_ref_map.insert(key, v); |
| 118 | + } else { |
| 119 | + let vec = var_ref_map.get_mut(&key); |
| 120 | + vec.unwrap().push(map_record); |
| 121 | + |
| 122 | + } |
| 123 | + }, |
| 124 | + "enum" => { |
| 125 | + let rec = map_record.clone(); |
| 126 | + let key = rec.get("id").unwrap(); |
| 127 | + let q_key = rec.get("qualname").unwrap(); |
| 128 | + type_map.insert(key.clone(), map_record); |
| 129 | + qual_type_map.insert(q_key.clone(), key.clone()); |
| 130 | + }, |
| 131 | + "struct" => { |
| 132 | + let rec = map_record.clone(); |
| 133 | + let key = rec.get("id").unwrap(); |
| 134 | + let c_key = rec.get("ctor_id").unwrap(); |
| 135 | + let q_key = rec.get("qualname").unwrap(); |
| 136 | + type_map.insert(key.clone(), map_record); |
| 137 | + ctor_map.insert(c_key.clone(), key.clone()); |
| 138 | + qual_type_map.insert(q_key.clone(), key.clone()); |
| 139 | + }, |
| 140 | + "type_ref" | "struct_ref" | "mod_ref" => { |
| 141 | + let key = map_record.get("refid").unwrap().clone(); |
| 142 | + |
| 143 | + if !type_ref_map.contains_key(&key) { |
| 144 | + let v = vec![map_record]; |
| 145 | + type_ref_map.insert(key, v); |
| 146 | + } else { |
| 147 | + let vec = type_ref_map.get_mut(&key); |
| 148 | + vec.unwrap().push(map_record); |
| 149 | + |
| 150 | + } |
| 151 | + }, |
| 152 | + "module" => {}, |
| 153 | + "module_alias" => {}, |
| 154 | + "unknown_ref" => {}, |
| 155 | + _ => {} |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + // Fixup type_refs with refid = 0 and ctor_id references |
| 162 | + let mut to_add = Vec::new(); |
| 163 | + for (key, value) in type_ref_map.iter() { |
| 164 | + if *key == "0" { |
| 165 | + for i in value.iter() { |
| 166 | + // must check qualname |
| 167 | + let name = i.get("qualname").unwrap(); |
| 168 | + if qual_type_map.contains_key(name) { |
| 169 | + let mut modified = i.clone(); |
| 170 | + modified.insert("refid".to_string(), qual_type_map.get(name).unwrap().clone()); |
| 171 | + to_add.push(modified); |
| 172 | + } |
| 173 | + } |
| 174 | + } else if let Some(ctor) = ctor_map.get(key) { |
| 175 | + for i in value.iter() { |
| 176 | + let mut modified = i.clone(); |
| 177 | + modified.insert("refid".to_string(), ctor.clone()); |
| 178 | + to_add.push(modified); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + for add in to_add.iter() { |
| 184 | + let key = add.get("refid").unwrap().clone(); |
| 185 | + if !type_ref_map.contains_key(&key) { |
| 186 | + let v = vec![add.clone()]; |
| 187 | + type_ref_map.insert(key, v); |
| 188 | + } else { |
| 189 | + let vec = type_ref_map.get_mut(&key); |
| 190 | + vec.unwrap().push(add.clone()); |
| 191 | + |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + AnalysisData{ var_map: var_map, var_ref_map: var_ref_map, type_map: type_map, |
| 196 | + type_ref_map: type_ref_map, func_map: func_map, func_ref_map: func_ref_map } |
| 197 | + } |
| 198 | +} |
0 commit comments