mirror of https://github.com/ecmwf/eccodes.git
Added compile fixes for proj_string
This commit is contained in:
parent
eb31cd5cd5
commit
b656c31f8e
|
@ -329,12 +329,13 @@ class AstParser:
|
||||||
|
|
||||||
if_expression = self.parse_ast_node(children[0])
|
if_expression = self.parse_ast_node(children[0])
|
||||||
if_action = self.parse_ast_node(children[1])
|
if_action = self.parse_ast_node(children[1])
|
||||||
if_stmt = if_statement.IfStatement(if_expression, if_action)
|
|
||||||
|
|
||||||
if child_count == 3:
|
if child_count == 3:
|
||||||
else_statement = self.parse_ast_node(children[2])
|
else_statement = self.parse_ast_node(children[2])
|
||||||
if_stmt.add_else(else_statement)
|
else:
|
||||||
|
else_statement = None
|
||||||
|
|
||||||
|
if_stmt = if_statement.IfStatement(if_expression, if_action, else_statement)
|
||||||
return if_stmt
|
return if_stmt
|
||||||
|
|
||||||
def parse_SWITCH_STMT(self, node):
|
def parse_SWITCH_STMT(self, node):
|
||||||
|
|
|
@ -10,14 +10,15 @@ from utils.string_funcs import strip_semicolon
|
||||||
#
|
#
|
||||||
# expression & action can be a string or a code_interface subclass (or None)
|
# expression & action can be a string or a code_interface subclass (or None)
|
||||||
class IfStatement(code_interface.CodeInterface):
|
class IfStatement(code_interface.CodeInterface):
|
||||||
def __init__(self, expression, action) -> None:
|
def __init__(self, expression, action, else_statement) -> None:
|
||||||
self._expression = expression
|
self._expression = expression
|
||||||
assert isinstance(self._expression, code_interface.CodeInterface), f"Expression must be a CodeInterface class"
|
assert isinstance(self._expression, code_interface.CodeInterface), f"Expression must be a CodeInterface class"
|
||||||
|
|
||||||
self._action = action
|
self._action = action
|
||||||
assert isinstance(self._action, code_interface.CodeInterface), f"Action must be a CodeInterface class"
|
assert isinstance(self._action, code_interface.CodeInterface), f"Action must be a CodeInterface class"
|
||||||
|
|
||||||
self._else = None
|
self._else_statement = else_statement
|
||||||
|
assert self._else_statement is None or isinstance(self._else_statement, code_interface.CodeInterface), f"Else statement must be a CodeInterface class"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def expression(self):
|
def expression(self):
|
||||||
|
@ -29,11 +30,7 @@ class IfStatement(code_interface.CodeInterface):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def else_statement(self):
|
def else_statement(self):
|
||||||
return self._else
|
return self._else_statement
|
||||||
|
|
||||||
def add_else(self, else_statement):
|
|
||||||
self._else = else_statement
|
|
||||||
assert isinstance(self._else, code_interface.CodeInterface), f"Else statement must be a CodeInterface class"
|
|
||||||
|
|
||||||
def as_lines(self):
|
def as_lines(self):
|
||||||
if_lines = []
|
if_lines = []
|
||||||
|
@ -48,8 +45,8 @@ class IfStatement(code_interface.CodeInterface):
|
||||||
action_lines.extend(self._action.as_lines())
|
action_lines.extend(self._action.as_lines())
|
||||||
|
|
||||||
else_lines = []
|
else_lines = []
|
||||||
if self._else:
|
if self._else_statement:
|
||||||
else_lines.extend(self._else.as_lines())
|
else_lines.extend(self._else_statement.as_lines())
|
||||||
else_lines[0] = "else " + else_lines[0]
|
else_lines[0] = "else " + else_lines[0]
|
||||||
|
|
||||||
return if_lines + action_lines + else_lines
|
return if_lines + action_lines + else_lines
|
|
@ -19,10 +19,8 @@ class IfStatementConverter(code_interface_converter.CodeInterfaceConverter):
|
||||||
debug.line("create_cpp_code_object", f"C expression=[{debug.as_debug_string(self._ccode_object.expression)}] converted to NONE_VALUE: setting to false")
|
debug.line("create_cpp_code_object", f"C expression=[{debug.as_debug_string(self._ccode_object.expression)}] converted to NONE_VALUE: setting to false")
|
||||||
cpp_expression = literal.Literal("false")
|
cpp_expression = literal.Literal("false")
|
||||||
|
|
||||||
cpp_if = if_statement.IfStatement(cpp_expression, cpp_action)
|
cpp_else = conversion_funcs.convert_ccode_object(self._ccode_object.else_statement, conversion_pack)
|
||||||
|
|
||||||
if self._ccode_object.else_statement:
|
cpp_if = if_statement.IfStatement(cpp_expression, cpp_action, cpp_else)
|
||||||
cpp_else = conversion_funcs.convert_ccode_object(self._ccode_object.else_statement, conversion_pack)
|
|
||||||
cpp_if.add_else(cpp_else)
|
|
||||||
|
|
||||||
return conversion_pack.conversion_validation.validate_if_statement(self._ccode_object, cpp_if)
|
return conversion_pack.conversion_validation.validate_if_statement(self._ccode_object, cpp_if)
|
||||||
|
|
|
@ -13,6 +13,7 @@ import code_object.variable_declaration as variable_declaration
|
||||||
import code_object.paren_expression as paren_expression
|
import code_object.paren_expression as paren_expression
|
||||||
import code_object.array_access as array_access
|
import code_object.array_access as array_access
|
||||||
import code_object.if_statement as if_statement
|
import code_object.if_statement as if_statement
|
||||||
|
import code_object.struct_member_access as struct_member_access
|
||||||
from code_object_converter.conversion_utils import as_commented_out_code
|
from code_object_converter.conversion_utils import as_commented_out_code
|
||||||
from utils.string_funcs import is_number
|
from utils.string_funcs import is_number
|
||||||
import code_object.cast_expression as cast_expression
|
import code_object.cast_expression as cast_expression
|
||||||
|
@ -205,10 +206,15 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
||||||
# We are assigning a container value to a pointer arg, so need to call .data()
|
# We are assigning a container value to a pointer arg, so need to call .data()
|
||||||
cppright = literal.Literal(f"{right_cpparg.name}.data()")
|
cppright = literal.Literal(f"{right_cpparg.name}.data()")
|
||||||
return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
||||||
|
|
||||||
if not left_cpparg.decl_spec.pointer:
|
if not left_cpparg.decl_spec.pointer:
|
||||||
# We are assigning a container value to a basic variable, so use index 0
|
if isinstance(cppright, array_access.ArrayAccess) or \
|
||||||
cppright = array_access.ArrayAccess(literal.Literal(right_cpparg.name), literal.Literal("0"))
|
isinstance(cppright, struct_member_access.StructMemberAccess):
|
||||||
return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
debug.line("validate_binary_operation", f"Container assignment check OK, using cppbinary_operation=[{debug.as_debug_string(cppbinary_operation)}]")
|
||||||
|
else:
|
||||||
|
# We are assigning a container value to a basic variable, so use index 0
|
||||||
|
cppright = array_access.ArrayAccess(literal.Literal(right_cpparg.name), literal.Literal("0"))
|
||||||
|
return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
||||||
|
|
||||||
elif cppbinary_op.is_comparison():
|
elif cppbinary_op.is_comparison():
|
||||||
cpparg = arg_utils.to_cpparg(cppleft, self._conversion_data)
|
cpparg = arg_utils.to_cpparg(cppleft, self._conversion_data)
|
||||||
|
@ -271,7 +277,7 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
||||||
value_cpparg = arg_utils.to_cpparg(cppvariable_declaration.value, self._conversion_data)
|
value_cpparg = arg_utils.to_cpparg(cppvariable_declaration.value, self._conversion_data)
|
||||||
|
|
||||||
if variable_cpparg and value_cpparg:
|
if variable_cpparg and value_cpparg:
|
||||||
debug.line("validate_variable_declaration", f"Container assignment check: variable_cpparg=[{debug.as_debug_string(variable_cpparg)}] value_cpparg=[{debug.as_debug_string(value_cpparg)}] ")
|
debug.line("validate_variable_declaration", f"Container assignment check [1]: variable_cpparg=[{debug.as_debug_string(variable_cpparg)}] value_cpparg=[{debug.as_debug_string(value_cpparg)}]")
|
||||||
if self._conversion_data.is_container_type(value_cpparg.decl_spec.type) and \
|
if self._conversion_data.is_container_type(value_cpparg.decl_spec.type) and \
|
||||||
not self._conversion_data.is_container_type(variable_cpparg.decl_spec.type):
|
not self._conversion_data.is_container_type(variable_cpparg.decl_spec.type):
|
||||||
if variable_cpparg.decl_spec.is_raw_pointer():
|
if variable_cpparg.decl_spec.is_raw_pointer():
|
||||||
|
@ -279,9 +285,14 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
||||||
updated_value = literal.Literal(f"{value_cpparg.name}.data()")
|
updated_value = literal.Literal(f"{value_cpparg.name}.data()")
|
||||||
return variable_declaration.VariableDeclaration(cppvariable, updated_value)
|
return variable_declaration.VariableDeclaration(cppvariable, updated_value)
|
||||||
if not variable_cpparg.decl_spec.pointer:
|
if not variable_cpparg.decl_spec.pointer:
|
||||||
# We are assigning a container value to a basic variable, so use index 0
|
if isinstance(cppvariable_declaration.value, array_access.ArrayAccess) or \
|
||||||
updated_value = array_access.ArrayAccess(literal.Literal(value_cpparg.name), literal.Literal("0"))
|
isinstance(cppvariable_declaration.value, struct_member_access.StructMemberAccess):
|
||||||
return variable_declaration.VariableDeclaration(cppvariable, updated_value)
|
pass
|
||||||
|
else:
|
||||||
|
# We are assigning a container value to a basic variable, so use index 0
|
||||||
|
updated_value = array_access.ArrayAccess(literal.Literal(value_cpparg.name), literal.Literal("0"))
|
||||||
|
debug.line("validate_variable_declaration", f"Container assignment check [2]: updated_value=[{debug.as_debug_string(updated_value)}] because cppvariable_declaration.value type=[{cppvariable_declaration.value}]")
|
||||||
|
return variable_declaration.VariableDeclaration(cppvariable, updated_value)
|
||||||
|
|
||||||
return cppvariable_declaration
|
return cppvariable_declaration
|
||||||
|
|
||||||
|
@ -289,7 +300,7 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
||||||
|
|
||||||
cppexpression = self.try_to_make_boolean(cppif_statement.expression)
|
cppexpression = self.try_to_make_boolean(cppif_statement.expression)
|
||||||
|
|
||||||
return if_statement.IfStatement(cppexpression, cppif_statement.action)
|
return if_statement.IfStatement(cppexpression, cppif_statement.action, cppif_statement.else_statement)
|
||||||
|
|
||||||
|
|
||||||
# Helper to determine how a container should receive the value returned by the function
|
# Helper to determine how a container should receive the value returned by the function
|
||||||
|
|
|
@ -168,7 +168,15 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
||||||
updated_cppfunction_call = function_call.FunctionCall("copyBuffer", updated_args)
|
updated_cppfunction_call = function_call.FunctionCall("copyBuffer", updated_args)
|
||||||
debug.line("apply_special_function_call_conversions", f"Updated memcpy: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(updated_cppfunction_call)}]")
|
debug.line("apply_special_function_call_conversions", f"Updated memcpy: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(updated_cppfunction_call)}]")
|
||||||
return updated_cppfunction_call
|
return updated_cppfunction_call
|
||||||
|
|
||||||
|
# Convert from: snprintf(result, 1024, "+proj=longlat +datum=WGS84 +no_defs +type=crs", ...);
|
||||||
|
# to : result = fmtString("+proj=longlat +datum=WGS84 +no_defs +type=crs", ...);
|
||||||
|
if cppfunction_call.name == "snprintf":
|
||||||
|
fmtString_call = function_call.FunctionCall("fmtString", cppfunction_call.args[2:])
|
||||||
|
snprintf_call = binary_operation.BinaryOperation(cppfunction_call.args[0], "=", fmtString_call)
|
||||||
|
|
||||||
|
debug.line("apply_special_function_call_conversions", f"Updated snprintf: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(snprintf_call)}]")
|
||||||
|
return snprintf_call
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -215,8 +223,8 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
||||||
expression_value = cppif_statement.expression.as_string()
|
expression_value = cppif_statement.expression.as_string()
|
||||||
cpparg = self._conversion_data.funcbody_cpparg_for_carg_name(expression_value)
|
cpparg = self._conversion_data.funcbody_cpparg_for_carg_name(expression_value)
|
||||||
if cpparg and cpparg != NONE_VALUE and cpparg.decl_spec.type == "GribStatus":
|
if cpparg and cpparg != NONE_VALUE and cpparg.decl_spec.type == "GribStatus":
|
||||||
updated_expression = literal.Literal(f"GribStatusSuccess({cpparg.name})")
|
updated_expression = literal.Literal(f"GribStatusError({cpparg.name})")
|
||||||
updated_cppif_statement = if_statement.IfStatement(updated_expression, cppif_statement.action)
|
updated_cppif_statement = if_statement.IfStatement(updated_expression, cppif_statement.action, cppif_statement.else_statement)
|
||||||
debug.line("validate_if_statement", f"updated_cppif_statement=[{debug.as_debug_string(updated_cppif_statement)}]")
|
debug.line("validate_if_statement", f"updated_cppif_statement=[{debug.as_debug_string(updated_cppif_statement)}]")
|
||||||
return updated_cppif_statement
|
return updated_cppif_statement
|
||||||
|
|
||||||
|
@ -306,7 +314,7 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
||||||
if cppcode_object.unary_op.value == "!":
|
if cppcode_object.unary_op.value == "!":
|
||||||
updated_expression = literal.Literal(f"GribStatusSuccess({cpparg.name})")
|
updated_expression = literal.Literal(f"GribStatusSuccess({cpparg.name})")
|
||||||
else:
|
else:
|
||||||
updated_expression = literal.Literal(f"!GribStatusSuccess({cpparg.name})")
|
updated_expression = literal.Literal(f"GribStatusError({cpparg.name})")
|
||||||
|
|
||||||
debug.line("apply_special_unary_operation_conversion", f"Updating GribStatus error: [{debug.as_debug_string(cppcode_object)}]->[{debug.as_debug_string(updated_expression)}]")
|
debug.line("apply_special_unary_operation_conversion", f"Updating GribStatus error: [{debug.as_debug_string(cppcode_object)}]->[{debug.as_debug_string(updated_expression)}]")
|
||||||
return updated_expression
|
return updated_expression
|
||||||
|
@ -315,7 +323,7 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
||||||
cppfuncsig = self._conversion_data.cppfuncsig_for_cppfuncname(cppcode_object.name)
|
cppfuncsig = self._conversion_data.cppfuncsig_for_cppfuncname(cppcode_object.name)
|
||||||
if cppfuncsig:
|
if cppfuncsig:
|
||||||
if cppfuncsig.return_type.type == "GribStatus":
|
if cppfuncsig.return_type.type == "GribStatus":
|
||||||
updated_cppcode_object = literal.Literal(f"GribStatusSuccess({cppcode_object.as_string()})")
|
updated_cppcode_object = literal.Literal(f"GribStatusError({cppcode_object.as_string()})")
|
||||||
return updated_cppcode_object
|
return updated_cppcode_object
|
||||||
|
|
||||||
return super().try_to_make_boolean(cppcode_object)
|
return super().try_to_make_boolean(cppcode_object)
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
special_function_name_mapping = {
|
special_function_name_mapping = {
|
||||||
"snprintf" : "fmtString",
|
|
||||||
"strtol" : "strToLong",
|
"strtol" : "strToLong",
|
||||||
"strtod" : "strToDouble",
|
"strtod" : "strToDouble",
|
||||||
"atol" : "strAtoL",
|
"atol" : "strAtoL",
|
||||||
|
|
|
@ -22,8 +22,8 @@ std::string fmtString(std::string format, Args... args) {
|
||||||
// Determine buffer size
|
// Determine buffer size
|
||||||
size_t formatSize = snprintf(nullptr, 0, format.c_str(), args...);
|
size_t formatSize = snprintf(nullptr, 0, format.c_str(), args...);
|
||||||
|
|
||||||
char buf[formatSize];
|
char buf[formatSize+1];
|
||||||
snprintf(buf, formatSize, format.c_str(), args...);
|
snprintf(buf, sizeof(buf), format.c_str(), args...);
|
||||||
|
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,4 +8,9 @@ bool GribStatusSuccess(GribStatus stat)
|
||||||
return stat == GribStatus::SUCCESS;
|
return stat == GribStatus::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GribStatusError(GribStatus stat)
|
||||||
|
{
|
||||||
|
return stat != GribStatus::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,5 +87,6 @@ enum class GribStatus{
|
||||||
|
|
||||||
// Helper for the conversion process
|
// Helper for the conversion process
|
||||||
bool GribStatusSuccess(GribStatus stat);
|
bool GribStatusSuccess(GribStatus stat);
|
||||||
|
bool GribStatusError(GribStatus stat);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,8 +315,9 @@ int grib_unpack_string(grib_accessor* a, char* v, size_t* len)
|
||||||
int ret = c->unpack_string(a, v, len);
|
int ret = c->unpack_string(a, v, len);
|
||||||
if(auto accessorPtr = eccodes::accessor::get(eccodes::accessor::AccessorName(a->name)); accessorPtr)
|
if(auto accessorPtr = eccodes::accessor::get(eccodes::accessor::AccessorName(a->name)); accessorPtr)
|
||||||
{
|
{
|
||||||
std::string value = accessorPtr->unpack<std::string>();
|
std::string cvalue = std::string(v, *len);
|
||||||
if(value != std::string(v, *len)) { Assert(false); }
|
std::string cppvalue = accessorPtr->unpack<std::string>();
|
||||||
|
if(cvalue != cppvalue) { Assert(false); }
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Reference in New Issue