[CLib] Fix size-related compile warnings in generated code

This commit is contained in:
Ray Speth
2025-10-28 17:17:06 -05:00
committed by Ingmar Schoegl
parent ef5284438b
commit da24ee1509
5 changed files with 27 additions and 10 deletions
@@ -383,12 +383,18 @@ class CLibSourceGenerator(SourceGenerator):
template = loader.from_string(self._templates["clib-destructor"])
elif recipe.what == "method":
template = loader.from_string(self._templates["clib-method"])
ret_type = c_func.wraps.ret_type
if "size_t" in ret_type:
template = loader.from_string(self._templates["clib-size-getter"])
else:
template = loader.from_string(self._templates["clib-method"])
elif recipe.what == "getter":
ret_type = c_func.wraps.ret_type
if "void" in ret_type or "vector" in ret_type:
template = loader.from_string(self._templates["clib-array-getter"])
elif "size_t" in ret_type:
template = loader.from_string(self._templates["clib-size-getter"])
else:
template = loader.from_string(self._templates["clib-method"])
@@ -23,7 +23,7 @@ clib-macros: |-
{% macro size_check(checks, c_func, c_args) %}
## size checking
{% if checks %}
if ({{ c_args[1] }} < obj->{{ checks[0] }}()) {
if (static_cast<size_t>({{ c_args[1] }}) < obj->{{ checks[0] }}()) {
throw ArraySizeError("{{ c_func }}", {{ c_args[1] }}, obj->{{ checks[0] }}());
}
{% else %}
@@ -149,7 +149,7 @@ clib-accessor: |-
{{ render_lines(before) | indent(4) }}
{% if checks %}
## accessor uses index checker (see: sol_adjacent)
if ({{ c_args[1] }} < 0 || {{ c_args[1] }} >= {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}()) {
if ({{ c_args[1] }} < 0 || static_cast<size_t>({{ c_args[1] }}) >= {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}()) {
throw IndexError("{{ c_func }}", "", {{ c_args[1] }}, {{ base }}Cabinet::at({{ handle }})->{{ checks[0] }}());
}
{% endif %}{# checks #}
@@ -191,9 +191,20 @@ clib-method: |-
return handleAllExceptions({{ error[0] }}, {{ error[1] }});
}
clib-size-getter: |-
## CLib template for methods returning sizes:
## handles methods that return size_t in C++ and int32_t in CLib
// size getter: {{ cxx_wraps }}
try {
{{ render_lines(before) | indent(4) }}
return static_cast<int32_t>({{ get_obj(base, cxx_base, handle )}}->{{ cxx_name }}({{ ', '.join(cxx_args) }}));
} catch (...) {
return handleAllExceptions({{ error[0] }}, {{ error[1] }});
}
clib-array-getter: |-
## CLib array getter template
// getter: {{ cxx_wraps }}
// array getter: {{ cxx_wraps }}
try {
auto{{ '&' if base == cxx_base else '' }} obj = {{ get_obj(base, cxx_base, handle )}};
{{ size_check(checks, c_func, c_args) | indent(4) }}
@@ -211,7 +222,7 @@ clib-array-getter: |-
clib-array-setter: |-
## CLib array setter template
// setter: {{ cxx_wraps }}
// array setter: {{ cxx_wraps }}
try {
auto{{ '&' if base == cxx_base else '' }} obj = {{ get_obj(base, cxx_base, handle )}};
{{ size_check(checks, c_func, c_args) | indent(4) }}
@@ -31,7 +31,7 @@ recipes:
ph: string name of the phase
uses: phaseIndex(const string&, bool)
code: |-
return KineticsCabinet::at(handle)->phaseIndex(ph, true);
return static_cast<int32_t>(KineticsCabinet::at(handle)->phaseIndex(ph, true));
- name: nTotalSpecies # Renamed in Cantera 3.2 (previously nSpecies)
- name: reactantStoichCoeff
- name: productStoichCoeff
@@ -59,7 +59,7 @@ recipes:
nm: name of the species
uses: kineticsSpeciesIndex(const string&, bool)
code: |-
return KineticsCabinet::at(handle)->kineticsSpeciesIndex(nm, true);
return static_cast<int32_t>(KineticsCabinet::at(handle)->kineticsSpeciesIndex(nm, true));
- name: advanceCoverages
wraps: advanceCoverages(double)
- name: getDeltaEnthalpy # Changed in Cantera 3.2 (previously part of getDelta)
@@ -26,7 +26,7 @@ recipes:
name: String name of the global element
uses: elementIndex(const string&, bool)
code: |-
return MultiPhaseCabinet::at(handle)->elementIndex(name, true);
return static_cast<int32_t>(MultiPhaseCabinet::at(handle)->elementIndex(name, true));
- name: nSpecies
- name: speciesIndex
wraps: speciesIndex(size_t, size_t)
@@ -57,7 +57,7 @@ recipes:
name: Name of the element
uses: elementIndex(const string&, bool)
code: |-
return ThermoPhaseCabinet::as<Phase>(handle)->elementIndex(name, true);
return static_cast<int32_t>(ThermoPhaseCabinet::as<Phase>(handle)->elementIndex(name, true));
- name: speciesIndex
# temporary custom code due to changing C++ API: replaceable by
# wraps: speciesIndex(const string&)
@@ -70,7 +70,7 @@ recipes:
name: String name of the species. It may also be in the form phaseName:speciesName
uses: speciesIndex(const string&, bool)
code: |-
return ThermoPhaseCabinet::as<Phase>(handle)->speciesIndex(name, true);
return static_cast<int32_t>(ThermoPhaseCabinet::as<Phase>(handle)->speciesIndex(name, true));
- name: nAtoms
- name: addElement
- name: refPressure