LLMs tends to re-declare the provided API in code. Those duplicate definitions cause link error, thus those duplicated definitions need to be removed.
(program: &Path)
| 183 | /// LLMs tends to re-declare the provided API in code. |
| 184 | /// Those duplicate definitions cause link error, thus those duplicated definitions need to be removed. |
| 185 | pub fn remove_duplicate_definition(program: &Path) -> Result<()> { |
| 186 | let ast = Executor::extract_program_ast(program)?; |
| 187 | let mut del_ranges = vec![]; |
| 188 | for child in &ast.inner { |
| 189 | if let Clang::FunctionDecl(fd) = &child.kind { |
| 190 | let name = fd.get_name(); |
| 191 | if get_func_gadget(&name).is_some() { |
| 192 | del_ranges.push(fd.range.clone()); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | let mut content = std::fs::read_to_string(program)?; |
| 197 | for sr in del_ranges.iter().rev() { |
| 198 | let (begin, end) = get_source_code_range(sr)?; |
| 199 | content.replace_range(begin..end + 1, ""); |
| 200 | } |
| 201 | std::fs::write(program, content)?; |
| 202 | Ok(()) |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn test_ast_dump_filter() -> Result<()> { |
no test coverage detected