Skip to content

Commit

Permalink
Fix sonarsource smells in CodegenCppVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan0x committed Sep 29, 2023
1 parent 5efdd29 commit 04a5cba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
72 changes: 40 additions & 32 deletions src/codegen/codegen_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,17 @@ std::vector<std::string> CodegenCppVisitor::ion_read_statements(BlockType type)
if (iter != ion.implicit_reads.end()) {
continue;
}
const auto& variable_names = read_ion_variable_name(var);
auto first = get_variable_name(variable_names.first);
auto second = get_variable_name(variable_names.second);
statements.push_back(fmt::format("{} = {};", first, second));
const auto& [dest, src] = read_ion_variable_name(var);
auto dest_var = get_variable_name(dest);
auto src_var = get_variable_name(src);
statements.push_back(fmt::format("{} = {};", dest_var, src_var));
}
for (const auto& var: ion.writes) {
if (ion.is_ionic_conc(var)) {
const auto& variables = read_ion_variable_name(var);
auto first = get_variable_name(variables.first);
auto second = get_variable_name(variables.second);
statements.push_back(fmt::format("{} = {};", first, second));
const auto& [dest, src] = read_ion_variable_name(var);
auto dest_var = get_variable_name(dest);
auto src_var = get_variable_name(src);
statements.push_back(fmt::format("{} = {};", dest_var, src_var));
}
}
}
Expand All @@ -575,27 +575,50 @@ std::vector<std::string> CodegenCppVisitor::ion_read_statements_optimized(BlockT
for (const auto& ion: info.ions) {
for (const auto& var: ion.writes) {
if (ion.is_ionic_conc(var)) {
auto variables = read_ion_variable_name(var);
auto first = "ionvar." + variables.first;
const auto& second = get_variable_name(variables.second);
statements.push_back(fmt::format("{} = {};", first, second));
const auto& [dest, src] = read_ion_variable_name(var);
auto dest_var = "ionvar." + dest;
const auto& src_var = get_variable_name(src);
statements.push_back(fmt::format("{} = {};", dest_var, src_var));
}
}
}
return statements;
}


void CodegenCppVisitor::handle_initial_ion_write_statement(const Ion& ion,
const std::string& concentration,
std::vector<ShadowUseStatement>& statements) const {
int index = 0;
if (ion.is_intra_cell_conc(concentration)) {
index = 1;
} else if (ion.is_extra_cell_conc(concentration)) {
index = 2;
} else {
/// \todo Unhandled case in neuron implementation
throw std::logic_error(fmt::format("codegen error for {} ion", ion.name));
}
auto ion_type_name = fmt::format("{}_type", ion.name);
auto lhs = fmt::format("int {}", ion_type_name);
auto op = "=";
auto rhs = get_variable_name(ion_type_name);
statements.push_back(ShadowUseStatement{lhs, op, rhs});
auto statement = conc_write_statement(ion.name, concentration, index);
statements.push_back(ShadowUseStatement{statement, "", ""});

}

// NOLINTNEXTLINE(readability-function-cognitive-complexity)
std::vector<ShadowUseStatement> CodegenCppVisitor::ion_write_statements(BlockType type) const {
std::vector<ShadowUseStatement> statements;
for (const auto& ion: info.ions) {
std::string concentration;
for (const auto& var: ion.writes) {
const auto& variable_names = write_ion_variable_name(var);
const auto& [dest, src] = write_ion_variable_name(var);
if (ion.is_ionic_current(var)) {
if (type == BlockType::Equation) {
auto current = breakpoint_current(var);
auto lhs = variable_names.first;
auto lhs = dest;
auto op = "+=";
auto rhs = get_variable_name(current);
if (info.point_process) {
Expand All @@ -608,30 +631,15 @@ std::vector<ShadowUseStatement> CodegenCppVisitor::ion_write_statements(BlockTyp
if (!ion.is_rev_potential(var)) {
concentration = var;
}
const auto& lhs = variable_names.first;
const auto& lhs = dest;
auto op = "=";
const auto& rhs = get_variable_name(variable_names.second);
const auto& rhs = get_variable_name(src);
statements.push_back(ShadowUseStatement{lhs, op, rhs});
}
}

if (type == BlockType::Initial && !concentration.empty()) {
int index = 0;
if (ion.is_intra_cell_conc(concentration)) {
index = 1;
} else if (ion.is_extra_cell_conc(concentration)) {
index = 2;
} else {
/// \todo Unhandled case in neuron implementation
throw std::logic_error(fmt::format("codegen error for {} ion", ion.name));
}
auto ion_type_name = fmt::format("{}_type", ion.name);
auto lhs = fmt::format("int {}", ion_type_name);
auto op = "=";
auto rhs = get_variable_name(ion_type_name);
statements.push_back(ShadowUseStatement{lhs, op, rhs});
auto statement = conc_write_statement(ion.name, concentration, index);
statements.push_back(ShadowUseStatement{statement, "", ""});
handle_initial_ion_write_statement(ion, concentration, statements);
}
}
return statements;
Expand Down
3 changes: 3 additions & 0 deletions src/codegen/codegen_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor {
std::vector<ShadowUseStatement> ion_write_statements(BlockType type) const;


void handle_initial_ion_write_statement(const Ion& ion,
const std::string& concentration,
std::vector<ShadowUseStatement>& statements) const;
/**
* Return ion variable name and corresponding ion read variable name
* \param name The ion variable name
Expand Down

0 comments on commit 04a5cba

Please sign in to comment.