-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve using libclang and fix memory issue
- Loading branch information
1 parent
ac10013
commit 0689fa8
Showing
8 changed files
with
133 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::ffi::CString; | ||
use std::ptr; | ||
|
||
use clang_sys::clang_createIndex; | ||
use clang_sys::clang_parseTranslationUnit; | ||
use clang_sys::CXIndex; | ||
use clang_sys::CXTranslationUnit; | ||
|
||
pub struct CompilationUnit { | ||
#[allow(dead_code)] | ||
pub path: String, | ||
|
||
pub index: CXIndex, | ||
pub translation_unit: CXTranslationUnit, | ||
} | ||
|
||
pub fn parse_files(files: &[String]) -> Vec<CompilationUnit> { | ||
let mut translation_units: Vec<CompilationUnit> = vec![]; | ||
|
||
for file in files { | ||
unsafe { | ||
let fname: CString = CString::new(file.as_str()).unwrap(); | ||
let index: CXIndex = clang_createIndex(0, 0); | ||
let translation_unit: CXTranslationUnit = clang_parseTranslationUnit( | ||
index, | ||
fname.as_ptr(), | ||
ptr::null_mut(), | ||
0, | ||
ptr::null_mut(), | ||
0, | ||
0, | ||
); | ||
|
||
if translation_unit.is_null() { | ||
continue; | ||
} | ||
|
||
let compilation_unit = CompilationUnit { | ||
path: file.to_string(), | ||
index, | ||
translation_unit, | ||
}; | ||
|
||
translation_units.push(compilation_unit); | ||
} | ||
} | ||
|
||
translation_units | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.