11use std:: vec;
22
3+ use clang_sys:: clang_disposeIndex;
4+ use clang_sys:: clang_disposeTranslationUnit;
35use gitql_core:: object:: Row ;
46use gitql_core:: values:: base:: Value ;
57use gitql_core:: values:: boolean:: BoolValue ;
@@ -8,54 +10,71 @@ use gitql_core::values::null::NullValue;
810use gitql_core:: values:: text:: TextValue ;
911use gitql_engine:: data_provider:: DataProvider ;
1012
13+ use crate :: clang_parser:: CompilationUnit ;
1114use crate :: visitor:: class;
1215use crate :: visitor:: enumeration;
1316use crate :: visitor:: function;
1417use crate :: visitor:: global;
1518use crate :: visitor:: unions;
1619
17- pub struct ClangAstDataProvider {
18- pub paths : Vec < String > ,
20+ pub struct ClangDataProvider {
21+ pub compilation_units : Vec < CompilationUnit > ,
1922}
2023
21- impl ClangAstDataProvider {
22- pub fn new ( paths : Vec < String > ) -> Self {
23- Self { paths }
24+ impl ClangDataProvider {
25+ pub fn new ( compilation_units : Vec < CompilationUnit > ) -> Self {
26+ Self { compilation_units }
2427 }
2528}
2629
27- impl DataProvider for ClangAstDataProvider {
30+ impl DataProvider for ClangDataProvider {
2831 fn provide ( & self , table : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
2932 let mut rows: Vec < Row > = vec ! [ ] ;
30-
31- for path in & self . paths {
32- let mut selected_rows = select_clang_ast_objects ( path , table, selected_columns) ?;
33+ for compilation_unit in & self . compilation_units {
34+ let mut selected_rows =
35+ select_clang_ast_objects ( compilation_unit , table, selected_columns) ?;
3336 rows. append ( & mut selected_rows) ;
3437 }
35-
3638 Ok ( rows)
3739 }
3840}
3941
42+ impl Drop for ClangDataProvider {
43+ fn drop ( & mut self ) {
44+ for compilation_unit in self . compilation_units . iter ( ) {
45+ unsafe {
46+ // Dispose the translation unit
47+ clang_disposeTranslationUnit ( compilation_unit. translation_unit ) ;
48+
49+ // Dispose the index
50+ clang_disposeIndex ( compilation_unit. index ) ;
51+ }
52+ }
53+ }
54+ }
55+
4056fn select_clang_ast_objects (
41- path : & str ,
57+ compilation_unit : & CompilationUnit ,
4258 table : & str ,
4359 selected_columns : & [ String ] ,
4460) -> Result < Vec < Row > , String > {
4561 let rows = match table {
46- "classes" => select_classes ( path , selected_columns) ?,
47- "enums" => select_enums ( path , selected_columns) ?,
48- "unions" => select_unions ( path , selected_columns) ?,
49- "functions" => select_functions ( path , selected_columns) ?,
50- "globals" => select_variables ( path , selected_columns) ?,
62+ "classes" => select_classes ( compilation_unit , selected_columns) ?,
63+ "enums" => select_enums ( compilation_unit , selected_columns) ?,
64+ "unions" => select_unions ( compilation_unit , selected_columns) ?,
65+ "functions" => select_functions ( compilation_unit , selected_columns) ?,
66+ "globals" => select_variables ( compilation_unit , selected_columns) ?,
5167 _ => vec ! [ Row { values: vec![ ] } ] ,
5268 } ;
5369 Ok ( rows)
5470}
5571
56- fn select_classes ( path : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
72+ fn select_classes (
73+ compilation_unit : & CompilationUnit ,
74+ selected_columns : & [ String ] ,
75+ ) -> Result < Vec < Row > , String > {
5776 let mut rows: Vec < Row > = vec ! [ ] ;
58- let ast_classes = class:: select_clang_classes ( path ) ;
77+ let ast_classes = class:: select_clang_classes ( compilation_unit ) ;
5978 for class in ast_classes. iter ( ) {
6079 let mut values: Vec < Box < dyn Value > > = Vec :: with_capacity ( selected_columns. len ( ) ) ;
6180
@@ -129,9 +148,12 @@ fn select_classes(path: &str, selected_columns: &[String]) -> Result<Vec<Row>, S
129148 Ok ( rows)
130149}
131150
132- fn select_enums ( path : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
151+ fn select_enums (
152+ compilation_unit : & CompilationUnit ,
153+ selected_columns : & [ String ] ,
154+ ) -> Result < Vec < Row > , String > {
133155 let mut rows: Vec < Row > = vec ! [ ] ;
134- let ast_enums = enumeration:: select_clang_enums ( path ) ;
156+ let ast_enums = enumeration:: select_clang_enums ( compilation_unit ) ;
135157 for enumeration in ast_enums. iter ( ) {
136158 let mut values: Vec < Box < dyn Value > > = Vec :: with_capacity ( selected_columns. len ( ) ) ;
137159
@@ -184,9 +206,12 @@ fn select_enums(path: &str, selected_columns: &[String]) -> Result<Vec<Row>, Str
184206 Ok ( rows)
185207}
186208
187- fn select_unions ( path : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
209+ fn select_unions (
210+ compilation_unit : & CompilationUnit ,
211+ selected_columns : & [ String ] ,
212+ ) -> Result < Vec < Row > , String > {
188213 let mut rows: Vec < Row > = vec ! [ ] ;
189- let ast_unions = unions:: select_clang_unions ( path ) ;
214+ let ast_unions = unions:: select_clang_unions ( compilation_unit ) ;
190215 for union_node in ast_unions. iter ( ) {
191216 let mut values: Vec < Box < dyn Value > > = Vec :: with_capacity ( selected_columns. len ( ) ) ;
192217
@@ -238,9 +263,12 @@ fn select_unions(path: &str, selected_columns: &[String]) -> Result<Vec<Row>, St
238263 Ok ( rows)
239264}
240265
241- fn select_functions ( path : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
266+ fn select_functions (
267+ compilation_unit : & CompilationUnit ,
268+ selected_columns : & [ String ] ,
269+ ) -> Result < Vec < Row > , String > {
242270 let mut rows: Vec < Row > = vec ! [ ] ;
243- let ast_functions = function:: select_clang_functions ( path ) ;
271+ let ast_functions = function:: select_clang_functions ( compilation_unit ) ;
244272 for function in ast_functions. iter ( ) {
245273 let mut values: Vec < Box < dyn Value > > = Vec :: with_capacity ( selected_columns. len ( ) ) ;
246274
@@ -340,9 +368,12 @@ fn select_functions(path: &str, selected_columns: &[String]) -> Result<Vec<Row>,
340368 Ok ( rows)
341369}
342370
343- fn select_variables ( path : & str , selected_columns : & [ String ] ) -> Result < Vec < Row > , String > {
371+ fn select_variables (
372+ compilation_unit : & CompilationUnit ,
373+ selected_columns : & [ String ] ,
374+ ) -> Result < Vec < Row > , String > {
344375 let mut rows: Vec < Row > = vec ! [ ] ;
345- let ast_variables = global:: select_clang_variables ( path ) ;
376+ let ast_variables = global:: select_clang_variables ( compilation_unit ) ;
346377 for variable in ast_variables. iter ( ) {
347378 let mut values: Vec < Box < dyn Value > > = Vec :: with_capacity ( selected_columns. len ( ) ) ;
348379 for field_name in selected_columns {
0 commit comments