From e23dcdd7981e9026eea2a5a19a1ebcabd8e7cfbf Mon Sep 17 00:00:00 2001 From: kevstone Date: Sun, 11 Feb 2024 07:52:04 +0000 Subject: [PATCH] Automatically detect if member function is non-const --- .../conversion_pack/conversion_data.py | 13 ++++++++++++- src/clang_convert/cpp_code/code_info.py | 9 +++++++++ .../default_conversion_validation.py | 7 +++++++ .../grib_accessor/grib_accessor_ccode_converter.py | 3 +++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/clang_convert/code_object_converter/conversion_pack/conversion_data.py b/src/clang_convert/code_object_converter/conversion_pack/conversion_data.py index 6e92717b8..cb10f4d69 100755 --- a/src/clang_convert/code_object_converter/conversion_pack/conversion_data.py +++ b/src/clang_convert/code_object_converter/conversion_pack/conversion_data.py @@ -392,6 +392,11 @@ class ConversionData: self._info.add_function_call_entry(cppfuncsig) debug.line("add_cppfunction_call", f"Added function call cppfunction_call=[{debug.as_debug_string(cppfunction_call)}] -> cppfuncsig=[{debug.as_debug_string(cppfuncsig)}]") + def set_current_cfuncname_non_const(self): + self._info.add_non_const_member_function_name(self._current_cfuncname) + debug.line("set_current_cfuncname_non_const", f"Set function name=[{self._current_cfuncname}] to non-const") + + # ------------------------------ QUERY ------------------------------ # Given the cppname, search all stores to see if an arg exists @@ -426,6 +431,13 @@ class ConversionData: return None + def is_cppdata_member(self, cppdata_member_name): + for mapping in self.all_mappings(): + for key, value in mapping.data_member_mappings.items(): + if value and value.name == cppdata_member_name: + return True + return False + def cppdata_member_for_cdata_member(self, cmember): for mapping in self.all_mappings(): for key, value in mapping.data_member_mappings.items(): @@ -480,7 +492,6 @@ class ConversionData: return False - def is_self_class_pointer_name(self, name): debug.line("is_self_class_pointer_name", f"Testing name=[{debug.as_debug_string(name)}]") for mapping in self.all_mappings(): diff --git a/src/clang_convert/cpp_code/code_info.py b/src/clang_convert/cpp_code/code_info.py index 7bdd743cc..a83842779 100755 --- a/src/clang_convert/cpp_code/code_info.py +++ b/src/clang_convert/cpp_code/code_info.py @@ -16,6 +16,7 @@ class CodeInfo: self._header_includes = [] self._source_includes = [] self._function_calls = [] # List all function calls made whilst converting (for post-processing) + self._non_const_member_function_names = [] # default to const unless told otherwise! @property def file_name(self): @@ -53,6 +54,10 @@ class CodeInfo: def function_calls(self): return self._function_calls + @property + def non_const_member_function_names(self): + return self._non_const_member_function_names + def add_namespace(self, entry): self._namespaces.append(entry) @@ -77,3 +82,7 @@ class CodeInfo: return # Duplicate self._function_calls.append(entry) + + def add_non_const_member_function_name(self, name): + if name not in self._non_const_member_function_names: + self._non_const_member_function_names.append(name) diff --git a/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py b/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py index f52191dde..d86a9ad3c 100755 --- a/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py +++ b/src/clang_convert/default/default_conversion_pack/default_conversion_validation.py @@ -157,7 +157,14 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation): cppleft.index = "[0]" debug.line("validate_binary_operation", f"Assigning number to container, so updating it to access first element: cppleft=[{debug.as_debug_string(cppleft)}] cppright_value=[{cppright_value}]") return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright) + + # Check if we're assigning to a data member and need to ensure the function is non-const + data_member_name = cppleft.name + debug.line("validate_binary_operation", f"CHECK is_cppdata_member({data_member_name})") + if self._conversion_data.is_cppdata_member(data_member_name): + self._conversion_data.set_current_cfuncname_non_const() + elif cppbinary_op.is_comparison(): cpparg = arg_utils.to_cpparg(cppleft, self._conversion_data) if cpparg and self._conversion_data.is_container_type(cpparg.decl_spec.type): diff --git a/src/clang_convert/grib_accessor/grib_accessor_ccode_converter.py b/src/clang_convert/grib_accessor/grib_accessor_ccode_converter.py index 3d6c7c17f..73637a74e 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_ccode_converter.py +++ b/src/clang_convert/grib_accessor/grib_accessor_ccode_converter.py @@ -171,6 +171,9 @@ class GribAccessorCCodeConverter(default_ccode_converter.DefaultCCodeConverter): if conv_pack.conversion_data.is_virtual_member_function(function_name): return function_name in virtual_member_functions.const_virtual_member_function_names + if function_name in self._code_info.non_const_member_function_names: + return False + if conv_pack.conversion_data.is_member_function(function_name): return True