From b7e97cce37565bd5e2bea5a996f6ea57e260bf51 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 28 Oct 2020 14:32:24 +0100 Subject: [PATCH] intellisense: Convert to *mut ComPtr<> and simplify --- src/intellisense/ffi.rs | 90 ++++++++++++------------- src/intellisense/wrapper.rs | 128 +++++++++++------------------------- 2 files changed, 82 insertions(+), 136 deletions(-) diff --git a/src/intellisense/ffi.rs b/src/intellisense/ffi.rs index 8e29e6b..cf85e18 100644 --- a/src/intellisense/ffi.rs +++ b/src/intellisense/ffi.rs @@ -1,7 +1,7 @@ use crate::os::{BSTR, HRESULT, LPCSTR, LPSTR}; pub(crate) use crate::unknown::IDxcUnknownShim; use bitflags::bitflags; -use com_rs::{com_interface, iid, IUnknown}; +use com_rs::{com_interface, iid, ComPtr, IUnknown}; bitflags! { pub struct DxcGlobalOptions : u32 { @@ -364,13 +364,13 @@ com_interface! { vtable: IDxcDiagnosticVtbl, fn format_diagnostic(options: DxcDiagnosticDisplayOptions, result: *mut LPSTR) -> HRESULT; fn get_severity(result: *mut DxcDiagnosticSeverity) -> HRESULT; - fn get_location(result: *mut *mut IDxcSourceLocation) -> HRESULT; + fn get_location(result: *mut ComPtr) -> HRESULT; fn get_spelling(result: *mut LPSTR) -> HRESULT; fn get_category_text(result: *mut LPSTR) -> HRESULT; fn get_num_ranges(result: *mut u32) -> HRESULT; - fn get_range_at(index: u32, result: *mut *mut IDxcSourceRange) -> HRESULT; + fn get_range_at(index: u32, result: *mut ComPtr) -> HRESULT; fn get_num_fix_its(result: *mut u32) -> HRESULT; - fn get_fix_it_at(index: u32, replacement_range: *mut *mut IDxcSourceRange, text: *mut LPSTR) -> HRESULT; + fn get_fix_it_at(index: u32, replacement_range: *mut ComPtr, text: *mut LPSTR) -> HRESULT; } } @@ -379,9 +379,9 @@ com_interface! { interface IDxcInclusion: IDxcUnknownShim, IUnknown { iid: IID_IDxcInclusion, vtable: IDxcInclusionVtbl, - fn get_included_file(result: *mut *mut IDxcFile) -> HRESULT; + fn get_included_file(result: *mut ComPtr) -> HRESULT; fn get_stack_length(result: *mut u32) -> HRESULT; - fn get_stack_item(index: u32, result: *mut *mut IDxcSourceLocation) -> HRESULT; + fn get_stack_item(index: u32, result: *mut ComPtr) -> HRESULT; } } @@ -391,8 +391,8 @@ com_interface! { iid: IID_IDxcToken, vtable: IDxcTokenVtbl, fn get_kind(value: *mut DxcTokenKind) -> HRESULT; - fn get_location(value: *mut *mut IDxcSourceLocation) -> HRESULT; - fn get_extent(value: *mut *mut IDxcSourceRange) -> HRESULT; + fn get_location(value: *mut ComPtr) -> HRESULT; + fn get_extent(value: *mut ComPtr) -> HRESULT; fn get_spelling(value: *mut LPSTR) -> HRESULT; } } @@ -414,7 +414,7 @@ com_interface! { iid: IID_IDxcSourceLocation, vtable: IDxcSourceLocationVtbl, fn is_equal_to(other: *const IDxcSourceLocation, result: *mut bool) -> HRESULT; - fn get_spelling_location(file: *mut *mut IDxcFile, line: *mut u32, col: *mut u32, offset: *mut u32) -> HRESULT; + fn get_spelling_location(file: *mut ComPtr, line: *mut u32, col: *mut u32, offset: *mut u32) -> HRESULT; fn is_null(result: *mut bool) -> HRESULT; } } @@ -425,8 +425,8 @@ com_interface! { iid: IID_IDxcSourceRange, vtable: IDxcSourceRangeVtbl, fn is_null(value: *mut bool) -> HRESULT; - fn get_start(value: *mut *mut IDxcSourceLocation) -> HRESULT; - fn get_end(value: *mut *mut IDxcSourceLocation) -> HRESULT; + fn get_start(value: *mut ComPtr) -> HRESULT; + fn get_end(value: *mut ComPtr) -> HRESULT; fn get_offsets(start_offset: *mut u32, end_offset: *mut u32) -> HRESULT; } } @@ -436,27 +436,27 @@ com_interface! { interface IDxcCursor: IDxcUnknownShim, IUnknown { iid: IID_IDxcCursor, vtable: IDxcCursorVtbl, - fn get_extent(range: *mut *mut IDxcSourceRange) -> HRESULT; - fn get_location(result: *mut *mut IDxcSourceLocation) -> HRESULT; + fn get_extent(range: *mut ComPtr) -> HRESULT; + fn get_location(result: *mut ComPtr) -> HRESULT; fn get_kind(result: *mut DxcCursorKind) -> HRESULT; fn get_kind_flags(result: *mut DxcCursorKindFlags) -> HRESULT; - fn get_semantic_parent(result: *mut*mut IDxcCursor) -> HRESULT; - fn get_lexical_parent(result:*mut*mut IDxcCursor) -> HRESULT; - fn get_cursor_type(result:*mut*mut IDxcType) -> HRESULT; - fn get_num_arguments(result:*mut i32) -> HRESULT; - fn get_argument_at(index: i32, result: *mut *mut IDxcCursor) -> HRESULT; - fn get_referenced_cursor(result:*mut *mut IDxcCursor) -> HRESULT; - fn get_definition_cursor(result:*mut *mut IDxcCursor) -> HRESULT; - fn find_references_in_file(file: *const IDxcFile, skip: u32, top:u32, result_length: *mut u32, result: *mut *mut *mut IDxcCursor) -> HRESULT; + fn get_semantic_parent(result: *mut ComPtr) -> HRESULT; + fn get_lexical_parent(result: *mut ComPtr) -> HRESULT; + fn get_cursor_type(result: *mut ComPtr) -> HRESULT; + fn get_num_arguments(result: *mut i32) -> HRESULT; + fn get_argument_at(index: i32, result: *mut ComPtr) -> HRESULT; + fn get_referenced_cursor(result: *mut ComPtr) -> HRESULT; + fn get_definition_cursor(result: *mut ComPtr) -> HRESULT; + fn find_references_in_file(file: *const IDxcFile, skip: u32, top:u32, result_length: *mut u32, result: *mut *mut ComPtr) -> HRESULT; fn get_spelling(result: *mut LPSTR) -> HRESULT; - fn is_equal_to(other: *const IDxcCursor, result:*mut bool) -> HRESULT; - fn is_null(result:*mut bool) -> HRESULT; - fn is_definition(result:*mut bool) -> HRESULT; - fn get_display_name(result:*mut BSTR) -> HRESULT; - fn get_qualified_name(include_template_args:bool, result:*mut BSTR) -> HRESULT; - fn get_formatted_name(formatting: DxcCursorFormatting, result:*mut BSTR) -> HRESULT; - fn get_children(skip: u32, top: u32, result_length:*mut u32, result:*mut*mut*mut IDxcCursor) -> HRESULT; - fn get_snapped_child(location: *const IDxcSourceLocation, result:*mut*mut IDxcCursor) -> HRESULT; + fn is_equal_to(other: *const IDxcCursor, result: *mut bool) -> HRESULT; + fn is_null(result: *mut bool) -> HRESULT; + fn is_definition(result: *mut bool) -> HRESULT; + fn get_display_name(result: *mut BSTR) -> HRESULT; + fn get_qualified_name(include_template_args:bool, result: *mut BSTR) -> HRESULT; + fn get_formatted_name(formatting: DxcCursorFormatting, result: *mut BSTR) -> HRESULT; + fn get_children(skip: u32, top: u32, result_length: *mut u32, result: *mut *mut ComPtr) -> HRESULT; + fn get_snapped_child(location: *const IDxcSourceLocation, result: *mut ComPtr) -> HRESULT; } } @@ -486,21 +486,21 @@ com_interface! { interface IDxcTranslationUnit: IDxcUnknownShim, IUnknown { iid: IID_IDxcTranslationUnit, vtable: IDxcTranslationUnitVtbl, - fn get_cursor(cursor: *mut *mut IDxcCursor) -> HRESULT; - fn tokenize(range: *const IDxcSourceRange, tokens: *mut *mut *mut IDxcToken, token_count: *mut u32) -> HRESULT; - fn get_location( file: *mut IDxcFile, line: u32, column: u32, result: *mut *mut IDxcSourceLocation) -> HRESULT; + fn get_cursor(cursor: *mut ComPtr) -> HRESULT; + fn tokenize(range: *const IDxcSourceRange, tokens: *mut *mut ComPtr, token_count: *mut u32) -> HRESULT; + fn get_location( file: *mut IDxcFile, line: u32, column: u32, result: *mut ComPtr) -> HRESULT; fn get_num_diagnostics(value : *mut u32) -> HRESULT; - fn get_diagnostic(index: u32, value: *mut *mut IDxcDiagnostic) -> HRESULT; - fn get_file(name : *const u8, result : *mut *mut IDxcFile) -> HRESULT; + fn get_diagnostic(index: u32, value: *mut ComPtr) -> HRESULT; + fn get_file(name : *const u8, result : *mut ComPtr) -> HRESULT; fn get_file_name(result : *mut LPSTR) -> HRESULT; - fn reparse(unsaved_files : *mut *mut IDxcUnsavedFile, num_unsaved_files: u32) -> HRESULT; - fn get_cursor_for_location(location: *const IDxcSourceLocation, result : *mut *mut IDxcCursor) -> HRESULT; - fn get_location_for_offset(file : *const IDxcFile, offset: u32, result: *mut *mut IDxcSourceLocation) -> HRESULT; - fn get_skipped_ranges(file: *const IDxcFile, result_count: *mut u32, result: *mut *mut *mut IDxcSourceRange) -> HRESULT; + fn reparse(unsaved_files : *mut ComPtr, num_unsaved_files: u32) -> HRESULT; + fn get_cursor_for_location(location: *const IDxcSourceLocation, result : *mut ComPtr) -> HRESULT; + fn get_location_for_offset(file : *const IDxcFile, offset: u32, result: *mut ComPtr) -> HRESULT; + fn get_skipped_ranges(file: *const IDxcFile, result_count: *mut u32, result: *mut *mut ComPtr) -> HRESULT; fn get_diagnostic_details( index: u32, options: DxcDiagnosticDisplayOptions, error_code: *mut u32, error_line: *mut u32, error_column: *mut u32, error_file: *mut BSTR, error_offset: *mut u32, error_length: *mut u32, error_message: *mut BSTR) -> HRESULT; - fn get_inclusion_list(result_count: *mut u32, result: *mut *mut *mut IDxcInclusion) -> HRESULT; + fn get_inclusion_list(result_count: *mut u32, result: *mut *mut ComPtr) -> HRESULT; } } @@ -518,7 +518,7 @@ com_interface! { unsaved_files: *const *const IDxcUnsavedFile, num_unsaved_files: u32, options: DxcTranslationUnitFlags, - translation_unit: *mut *mut IDxcTranslationUnit) -> HRESULT; + translation_unit: *mut ComPtr) -> HRESULT; } } @@ -527,13 +527,13 @@ com_interface! { interface IDxcIntelliSense: IDxcUnknownShim, IUnknown { iid: IID_IDxcIntelliSense, vtable: IDxcIntelliSenseVtbl, - fn create_index(index: *mut *mut IDxcIndex) -> HRESULT; - fn get_null_location(location: *mut *mut IDxcSourceLocation) -> HRESULT; - fn get_null_range(location: *mut *mut IDxcSourceRange) -> HRESULT; - fn get_range( start: *const IDxcSourceLocation, end: *const IDxcSourceLocation, location: *mut *mut IDxcSourceRange) -> HRESULT; + fn create_index(index: *mut ComPtr) -> HRESULT; + fn get_null_location(location: *mut ComPtr) -> HRESULT; + fn get_null_range(location: *mut ComPtr) -> HRESULT; + fn get_range( start: *const IDxcSourceLocation, end: *const IDxcSourceLocation, location: *mut ComPtr) -> HRESULT; fn get_default_diagnostic_display_options(value: *mut DxcDiagnosticDisplayOptions) -> HRESULT; fn get_default_editing_tu_options(value: *mut DxcTranslationUnitFlags) -> HRESULT; - fn create_unsaved_file(file_name: LPCSTR, contents: LPCSTR, content_length: u32 , result: *mut *mut IDxcUnsavedFile) -> HRESULT; + fn create_unsaved_file(file_name: LPCSTR, contents: LPCSTR, content_length: u32 , result: *mut ComPtr) -> HRESULT; } } diff --git a/src/intellisense/wrapper.rs b/src/intellisense/wrapper.rs index 0a42080..6811e36 100644 --- a/src/intellisense/wrapper.rs +++ b/src/intellisense/wrapper.rs @@ -28,10 +28,7 @@ impl DxcIntellisense { pub fn create_index(&self) -> Result { let mut index: ComPtr = ComPtr::new(); unsafe { - return_hr!( - self.inner.create_index(index.as_mut_ptr()), - DxcIndex::new(index) - ); + return_hr!(self.inner.create_index(&mut index), DxcIndex::new(index)); } } @@ -50,7 +47,7 @@ impl DxcIntellisense { c_file_name.as_ptr(), c_contents.as_ptr(), contents.len() as u32, - file.as_mut_ptr() + &mut file ), DxcUnsavedFile::new(file) ); @@ -80,11 +77,10 @@ impl DxcIndex { let c_source_filename = CString::new(source_filename).expect("Failed to convert `source_filename`"); - let mut uf = vec![]; - - for unsaved_file in unsaved_files { - uf.push(unsaved_file.inner.as_ptr()); - } + let uf = unsaved_files + .iter() + .map(|unsaved_file| unsaved_file.inner.as_ptr()) + .collect::>(); unsafe { let mut c_args: Vec = vec![]; @@ -105,7 +101,7 @@ impl DxcIndex { uf.as_ptr(), uf.len() as u32, options, - tu.as_mut_ptr() + &mut tu ), DxcTranslationUnit::new(tu) ); @@ -145,7 +141,7 @@ impl DxcTranslationUnit { let mut file: ComPtr = ComPtr::new(); unsafe { return_hr!( - self.inner.get_file(name.as_ptr(), file.as_mut_ptr()), + self.inner.get_file(name.as_ptr(), &mut file), DxcFile::new(file) ); } @@ -154,10 +150,7 @@ impl DxcTranslationUnit { pub fn get_cursor(&self) -> Result { let mut cursor: ComPtr = ComPtr::new(); unsafe { - return_hr!( - self.inner.get_cursor(cursor.as_mut_ptr()), - DxcCursor::new(cursor) - ); + return_hr!(self.inner.get_cursor(&mut cursor), DxcCursor::new(cursor)); } } } @@ -174,67 +167,32 @@ impl DxcCursor { pub fn get_children(&self, skip: u32, max_count: u32) -> Result, HRESULT> { unsafe { - let mut result: *mut *mut IDxcCursor = std::ptr::null_mut(); + let mut result: *mut ComPtr = std::ptr::null_mut(); let mut result_length: u32 = 0; - let mut children = vec![]; return_hr!( self.inner .get_children(skip, max_count, &mut result_length, &mut result), - { - for i in 0..result_length { - let mut childcursor = ComPtr::::new(); - - let ptr: *mut *mut IDxcCursor = childcursor.as_mut_ptr(); - - *ptr = (*result).offset(i as isize); - - children.push(DxcCursor::new(childcursor)); - } - children - } + // Use a vector to be able to move the ComPtr out instead of having + // to clone it when using `*result.offset(i)`. + Vec::from_raw_parts(result, result_length as usize, result_length as usize) + .into_iter() + .map(DxcCursor::new) + .collect::>() ); } } pub fn get_all_children(&self) -> Result, HRESULT> { - let max_children_count = 10; - let mut current_children_count = 0; + let max_children_per_chunk = 10; let mut children = vec![]; - unsafe { - let mut result: *mut *mut IDxcCursor = std::ptr::null_mut(); - let mut result_length: u32 = 0; - - loop { - let hr = self.inner.get_children( - current_children_count, - max_children_count, - &mut result_length, - &mut result, - ); - - if hr != 0 { - return Err(hr); - } - - for i in 0..result_length { - let mut childcursor = ComPtr::::new(); - - let ptr: &mut *mut IDxcCursor = childcursor.as_mut_ptr(); - - *ptr = *(result.offset(i as isize)); - - let dxc_cursor = DxcCursor::new(childcursor); - - children.push(dxc_cursor); - } - - if result_length < max_children_count { - return Ok(children); - } - - current_children_count += result_length; + loop { + let res = self.get_children(children.len() as u32, max_children_per_chunk)?; + let res_len = res.len(); + children.extend(res); + if res_len < max_children_per_chunk as usize { + break Ok(children); } } } @@ -243,7 +201,7 @@ impl DxcCursor { unsafe { let mut range: ComPtr = ComPtr::new(); return_hr!( - self.inner.get_extent(range.as_mut_ptr()), + self.inner.get_extent(&mut range), DxcSourceRange::new(range) ); } @@ -253,7 +211,7 @@ impl DxcCursor { unsafe { let mut location: ComPtr = ComPtr::new(); return_hr!( - self.inner.get_location(location.as_mut_ptr()), + self.inner.get_location(&mut location), DxcSourceLocation::new(location) ); } @@ -311,7 +269,7 @@ impl DxcCursor { unsafe { let mut inner = ComPtr::::new(); return_hr!( - self.inner.get_semantic_parent(inner.as_mut_ptr()), + self.inner.get_semantic_parent(&mut inner), DxcCursor::new(inner) ); } @@ -321,7 +279,7 @@ impl DxcCursor { unsafe { let mut inner = ComPtr::::new(); return_hr!( - self.inner.get_lexical_parent(inner.as_mut_ptr()), + self.inner.get_lexical_parent(&mut inner), DxcCursor::new(inner) ); } @@ -330,10 +288,7 @@ impl DxcCursor { pub fn get_cursor_type(&self) -> Result { unsafe { let mut inner = ComPtr::::new(); - return_hr!( - self.inner.get_cursor_type(inner.as_mut_ptr()), - DxcType::new(inner) - ); + return_hr!(self.inner.get_cursor_type(&mut inner), DxcType::new(inner)); } } @@ -348,7 +303,7 @@ impl DxcCursor { unsafe { let mut inner = ComPtr::::new(); return_hr!( - self.inner.get_argument_at(index, inner.as_mut_ptr()), + self.inner.get_argument_at(index, &mut inner), DxcCursor::new(inner) ); } @@ -358,7 +313,7 @@ impl DxcCursor { unsafe { let mut inner = ComPtr::::new(); return_hr!( - self.inner.get_referenced_cursor(inner.as_mut_ptr()), + self.inner.get_referenced_cursor(&mut inner), DxcCursor::new(inner) ); } @@ -368,7 +323,7 @@ impl DxcCursor { unsafe { let mut inner = ComPtr::::new(); return_hr!( - self.inner.get_definition_cursor(inner.as_mut_ptr()), + self.inner.get_definition_cursor(&mut inner), DxcCursor::new(inner) ); } @@ -381,10 +336,9 @@ impl DxcCursor { top: u32, ) -> Result, HRESULT> { unsafe { - let mut result: *mut *mut IDxcCursor = std::ptr::null_mut(); + let mut result: *mut ComPtr = std::ptr::null_mut(); let mut result_length: u32 = 0; - let mut children = vec![]; return_hr!( self.inner.find_references_in_file( file.inner.as_ptr(), @@ -393,18 +347,10 @@ impl DxcCursor { &mut result_length, &mut result ), - { - for i in 0..result_length { - let mut childcursor = ComPtr::::new(); - - let ptr: *mut *mut IDxcCursor = childcursor.as_mut_ptr(); - - *ptr = (*result).offset(i as isize); - - children.push(DxcCursor::new(childcursor)); - } - children - } + Vec::from_raw_parts(result, result_length as usize, result_length as usize) + .into_iter() + .map(DxcCursor::new) + .collect::>() ); } } @@ -448,7 +394,7 @@ impl DxcCursor { let mut inner = ComPtr::::new(); return_hr!( self.inner - .get_snapped_child(location.inner.as_ptr(), inner.as_mut_ptr()), + .get_snapped_child(location.inner.as_ptr(), &mut inner), DxcCursor::new(inner) ); }