From: Arnaud Giersch Date: Fri, 10 Jul 2020 09:27:15 +0000 (+0200) Subject: [sonar] Fix more implicit casts that should not lower precision (S5276). X-Git-Tag: v3.26~476 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/13e64de335db45c87e2d7c8668ce8b16b7cb2139?ds=sidebyside [sonar] Fix more implicit casts that should not lower precision (S5276). --- diff --git a/examples/smpi/replay/replay.cpp b/examples/smpi/replay/replay.cpp index 50b515e31f..9cb0c63fe0 100644 --- a/examples/smpi/replay/replay.cpp +++ b/examples/smpi/replay/replay.cpp @@ -29,7 +29,8 @@ static void overriding_send(simgrid::xbt::ReplayAction& args) int main(int argc, char* argv[]) { const char* instance_id = simgrid::s4u::Actor::self()->get_property("instance_id"); - const int rank = xbt_str_parse_int(simgrid::s4u::Actor::self()->get_property("rank"), "Cannot parse rank"); + const int rank = + static_cast(xbt_str_parse_int(simgrid::s4u::Actor::self()->get_property("rank"), "Cannot parse rank")); const char* trace_filename = argv[1]; double start_delay_flops = 0; diff --git a/src/bindings/lua/lua_host.cpp b/src/bindings/lua/lua_host.cpp index 918973120e..59361fe515 100644 --- a/src/bindings/lua/lua_host.cpp +++ b/src/bindings/lua/lua_host.cpp @@ -101,7 +101,7 @@ static int l_host_number(lua_State * L) */ static int l_host_at(lua_State * L) { - int index = luaL_checkinteger(L, 1); + lua_Integer index = luaL_checkinteger(L, 1); std::vector hosts = simgrid::s4u::Engine::get_instance()->get_all_hosts(); sg_host_t host = hosts[index - 1]; // lua indexing start by 1 (lua[1] <=> C[0]) lua_newtable(L); /* create a table, put the userdata on top of it */ diff --git a/src/bindings/lua/lua_platf.cpp b/src/bindings/lua/lua_platf.cpp index 9b47ad0593..b29dcde512 100644 --- a/src/bindings/lua/lua_platf.cpp +++ b/src/bindings/lua/lua_platf.cpp @@ -179,7 +179,7 @@ int console_add_host(lua_State *L) { lua_ensure(type == LUA_TSTRING || type == LUA_TNUMBER, "Attribute 'speed' must be specified for host and must either be a string (in the correct format; check documentation) or a number."); if (type == LUA_TNUMBER) - host.speed_per_pstate.push_back(lua_tointeger(L, -1)); + host.speed_per_pstate.push_back(lua_tonumber(L, -1)); else // LUA_TSTRING host.speed_per_pstate.push_back( xbt_parse_get_speed(ar.short_src, ar.currentline, lua_tostring(L, -1), "speed of host", host.id)); @@ -191,7 +191,7 @@ int console_add_host(lua_State *L) { if (not lua_isnumber(L, -1)) host.core_amount = 1; // Default value else - host.core_amount = lua_tonumber(L, -1); + host.core_amount = static_cast(lua_tointeger(L, -1)); if (host.core_amount == 0) host.core_amount = 1; lua_pop(L, 1); diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index 16729041a1..f900c97a91 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -152,7 +152,7 @@ PYBIND11_MODULE(simgrid, m) py::class_(m, "Engine", "Simulation Engine") .def(py::init([](std::vector args) { static char noarg[] = {'\0'}; - int argc = args.size(); + int argc = static_cast(args.size()); std::unique_ptr argv(new char*[argc + 1]); for (int i = 0; i != argc; ++i) argv[i] = args[i].empty() ? noarg : &args[i].front(); diff --git a/src/instr/instr_config.cpp b/src/instr/instr_config.cpp index a0f9afb9ba..f1c9d17394 100644 --- a/src/instr/instr_config.cpp +++ b/src/instr/instr_config.cpp @@ -172,7 +172,7 @@ static void print_line(const char* option, const char* desc, const char* longdes { std::string str = std::string("--cfg=") + option + " "; - int len = str.size(); + int len = static_cast(str.size()); XBT_HELP("%s%*.*s %s", str.c_str(), 30 - len, 30 - len, "", desc); if (longdesc != nullptr) { XBT_HELP("%s\n", longdesc); diff --git a/src/instr/instr_platform.cpp b/src/instr/instr_platform.cpp index b2d214c92a..f0f6442bf6 100644 --- a/src/instr/instr_platform.cpp +++ b/src/instr/instr_platform.cpp @@ -53,8 +53,8 @@ static container_t lowestCommonAncestor(const simgrid::instr::Container* a1, con // find the lowest ancestor p = nullptr; - int i = ancestors_a1.size() - 1; - int j = ancestors_a2.size() - 1; + int i = static_cast(ancestors_a1.size()) - 1; + int j = static_cast(ancestors_a2.size()) - 1; while (i >= 0 && j >= 0) { container_t a1p = ancestors_a1.at(i); const simgrid::instr::Container* a2p = ancestors_a2.at(j); @@ -286,7 +286,8 @@ static void on_netzone_creation(s4u::NetZone const& netzone) } if (TRACE_needs_platform()) { - NetZoneContainer* container = new NetZoneContainer(id, currentContainer.size(), currentContainer.back()); + unsigned level = static_cast(currentContainer.size()); + NetZoneContainer* container = new NetZoneContainer(id, level, currentContainer.back()); currentContainer.push_back(container); } } @@ -342,9 +343,9 @@ static void on_host_creation(s4u::Host const& host) static void on_action_state_change(kernel::resource::Action const& action, kernel::resource::Action::State /* previous */) { - int n = action.get_variable()->get_number_of_constraint(); + unsigned n = static_cast(action.get_variable()->get_number_of_constraint()); - for (int i = 0; i < n; i++) { + for (unsigned i = 0; i < n; i++) { double value = action.get_variable()->get_value() * action.get_variable()->get_constraint_weight(i); /* Beware of composite actions: ptasks put links and cpus together. Extra pb: we cannot dynamic_cast from void* */ kernel::resource::Resource* resource = action.get_variable()->get_constraint(i)->get_id(); diff --git a/src/instr/instr_resource_utilization.cpp b/src/instr/instr_resource_utilization.cpp index 8ab07eb7c4..f67b618f89 100644 --- a/src/instr/instr_resource_utilization.cpp +++ b/src/instr/instr_resource_utilization.cpp @@ -16,7 +16,7 @@ void resource_set_utilization(const char* type, const char* name, const char* re { // only trace resource utilization if resource is known by tracing mechanism container_t container = Container::by_name_or_null(resource); - if (not container || not value) + if (container == nullptr || value == 0.0) return; // trace uncategorized resource utilization diff --git a/src/instr/jedule/jedule_platform.cpp b/src/instr/jedule/jedule_platform.cpp index f9dcd9c7f4..523d5a80dd 100644 --- a/src/instr/jedule/jedule_platform.cpp +++ b/src/instr/jedule/jedule_platform.cpp @@ -69,7 +69,7 @@ int Container::get_child_position(const Container* child) const { auto it = std::find_if(begin(children_), end(children_), [&child](const std::unique_ptr& c) { return c.get() == child; }); - return it == end(children_) ? -1 : std::distance(begin(children_), it); + return it == end(children_) ? -1 : static_cast(std::distance(begin(children_), it)); } std::vector Container::get_hierarchy() @@ -96,7 +96,7 @@ std::string Container::get_hierarchy_as_string() std::vector heir_list = this->get_hierarchy(); - unsigned int length = heir_list.size(); + unsigned int length = static_cast(heir_list.size()); unsigned int i = 0; for (auto const& id : heir_list) { output += std::to_string(id); @@ -113,7 +113,7 @@ void Container::print_resources(FILE* jed_file) unsigned int i=0; xbt_assert(not this->resource_list.empty()); - unsigned int res_nb = this->resource_list.size(); + unsigned int res_nb = static_cast(this->resource_list.size()); std::string resid = this->get_hierarchy_as_string(); fprintf(jed_file, " & subset_list, st unsigned int id = parent_cont->get_id_by_name(host_name); id_list.push_back(id); } - unsigned int nb_ids = id_list.size(); + unsigned int nb_ids = static_cast(id_list.size()); std::sort(id_list.begin(), id_list.end()); if( nb_ids > 0 ) { diff --git a/src/kernel/lmm/maxmin.cpp b/src/kernel/lmm/maxmin.cpp index 7e25ce0a58..3dd1bb67f2 100644 --- a/src/kernel/lmm/maxmin.cpp +++ b/src/kernel/lmm/maxmin.cpp @@ -253,7 +253,7 @@ void System::expand(Constraint* cnst, Variable* var, double consumption_weight) elem.constraint = cnst; elem.variable = var; - if (var->sharing_penalty_) { + if (var->sharing_penalty_ != 0.0) { elem.constraint->enabled_element_set_.push_front(elem); elem.increase_concurrency(); } else @@ -283,7 +283,7 @@ void System::expand_add(Constraint* cnst, Variable* var, double value) std::find_if(begin(var->cnsts_), end(var->cnsts_), [&cnst](Element const& x) { return x.constraint == cnst; }); if (elem_it != end(var->cnsts_)) { Element& elem = *elem_it; - if (var->sharing_penalty_) + if (var->sharing_penalty_ != 0.0) elem.decrease_concurrency(); if (cnst->sharing_policy_ != s4u::Link::SharingPolicy::FATPIPE) @@ -292,7 +292,7 @@ void System::expand_add(Constraint* cnst, Variable* var, double value) elem.consumption_weight = std::max(elem.consumption_weight, value); // We need to check that increasing value of the element does not cross the concurrency limit - if (var->sharing_penalty_) { + if (var->sharing_penalty_ != 0.0) { if (cnst->get_concurrency_slack() < elem.get_concurrency()) { double penalty = var->sharing_penalty_; disable_var(var); @@ -939,8 +939,8 @@ double Constraint::get_usage() const int Constraint::get_variable_amount() const { - return std::count_if(std::begin(enabled_element_set_), std::end(enabled_element_set_), - [](const Element& elem) { return elem.consumption_weight > 0; }); + return static_cast(std::count_if(std::begin(enabled_element_set_), std::end(enabled_element_set_), + [](const Element& elem) { return elem.consumption_weight > 0; })); } } // namespace lmm diff --git a/src/kernel/routing/FatTreeZone.cpp b/src/kernel/routing/FatTreeZone.cpp index c7ac3d8d8b..ab626648ea 100644 --- a/src/kernel/routing/FatTreeZone.cpp +++ b/src/kernel/routing/FatTreeZone.cpp @@ -442,7 +442,7 @@ FatTreeNode::FatTreeNode(const ClusterCreationArgs* cluster, int id, int level, : id(id), level(level), position(position) { LinkCreationArgs linkTemplate; - if (cluster->limiter_link) { + if (cluster->limiter_link != 0.0) { linkTemplate.bandwidths.push_back(cluster->limiter_link); linkTemplate.latency = 0; linkTemplate.policy = s4u::Link::SharingPolicy::SHARED; @@ -450,7 +450,7 @@ FatTreeNode::FatTreeNode(const ClusterCreationArgs* cluster, int id, int level, sg_platf_new_link(&linkTemplate); this->limiter_link_ = s4u::Link::by_name(linkTemplate.id)->get_impl(); } - if (cluster->loopback_bw || cluster->loopback_lat) { + if (cluster->loopback_bw != 0.0 || cluster->loopback_lat != 0.0) { linkTemplate.bandwidths.push_back(cluster->loopback_bw); linkTemplate.latency = cluster->loopback_lat; linkTemplate.policy = s4u::Link::SharingPolicy::FATPIPE; diff --git a/src/plugins/vm/VmLiveMigration.cpp b/src/plugins/vm/VmLiveMigration.cpp index c047a12298..0712e95848 100644 --- a/src/plugins/vm/VmLiveMigration.cpp +++ b/src/plugins/vm/VmLiveMigration.cpp @@ -115,7 +115,7 @@ sg_size_t MigrationTx::sendMigrationData(sg_size_t size, int stage, int stage2_r double clock_end = s4u::Engine::get_clock(); double duration = clock_end - clock_sta; - double actual_speed = size / duration; + double actual_speed = static_cast(size) / duration; if (stage == 2) XBT_DEBUG("mig-stage%d.%d: sent %llu duration %f actual_speed %f (target %f)", stage, stage2_round, size, duration, @@ -134,7 +134,7 @@ void MigrationTx::operator()() double host_speed = vm_->get_pm()->get_speed(); const sg_size_t ramsize = vm_->get_ramsize(); const double dp_rate = - host_speed ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1; + host_speed != 0.0 ? (sg_vm_get_migration_speed(vm_) * sg_vm_get_dirty_page_intensity(vm_)) / host_speed : 1; const sg_size_t dp_cap = sg_vm_get_working_set_memory(vm_); const double mig_speed = sg_vm_get_migration_speed(vm_); double max_downtime = sg_vm_get_max_downtime(vm_); diff --git a/src/simdag/sd_dotloader.cpp b/src/simdag/sd_dotloader.cpp index 39792bb996..d9aaeafdd6 100644 --- a/src/simdag/sd_dotloader.cpp +++ b/src/simdag/sd_dotloader.cpp @@ -213,7 +213,7 @@ xbt_dynar_t SD_dotload_generic(const char* filename, bool sequential, bool sched if (previous_task && not SD_task_dependency_exists(previous_task, cur_task)) SD_task_dependency_add(previous_task, cur_task); - SD_task_schedulel(cur_task, 1, hosts[std::stod(elm.first)]); + SD_task_schedulel(cur_task, 1, hosts[std::stoi(elm.first)]); previous_task = cur_task; } } diff --git a/src/simdag/sd_task.cpp b/src/simdag/sd_task.cpp index ec33841917..05a3afc6ec 100644 --- a/src/simdag/sd_task.cpp +++ b/src/simdag/sd_task.cpp @@ -371,7 +371,7 @@ xbt_dynar_t SD_task_get_children(const_SD_task_t task) */ int SD_task_get_workstation_count(const_SD_task_t task) { - return task->allocation->size(); + return static_cast(task->allocation->size()); } /** @@ -591,10 +591,10 @@ int SD_task_dependency_exists(const_SD_task_t src, SD_task_t dst) if (dst) { return (src->successors->find(dst) != src->successors->end() || src->outputs->find(dst) != src->outputs->end()); } else { - return src->successors->size() + src->outputs->size(); + return static_cast(src->successors->size() + src->outputs->size()); } } else { - return dst->predecessors->size() + dst->inputs->size(); + return static_cast(dst->predecessors->size() + dst->inputs->size()); } } @@ -916,7 +916,7 @@ void SD_task_schedulev(SD_task_t task, int count, const sg_host_t * list) /* Iterate over all inputs and outputs to say where I am located (and start them if runnable) */ for (auto const& input : *task->inputs) { - int src_nb = input->allocation->size(); + int src_nb = static_cast(input->allocation->size()); int dst_nb = count; if (input->allocation->empty()) XBT_VERB("Sender side of '%s' not scheduled. Set receiver side to '%s''s allocation", input->name, task->name); @@ -936,7 +936,7 @@ void SD_task_schedulev(SD_task_t task, int count, const sg_host_t * list) for (auto const& output : *task->outputs) { int src_nb = count; - int dst_nb = output->allocation->size(); + int dst_nb = static_cast(output->allocation->size()); if (output->allocation->empty()) XBT_VERB("Receiver side of '%s' not scheduled. Set sender side to '%s''s allocation", output->name, task->name); diff --git a/src/surf/disk_s19.cpp b/src/surf/disk_s19.cpp index 66525d7eee..0f3f4b7423 100644 --- a/src/surf/disk_s19.cpp +++ b/src/surf/disk_s19.cpp @@ -48,7 +48,7 @@ void DiskS19Model::update_actions_state(double /*now*/, double delta) for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) { auto& action = *it; ++it; // increment iterator here since the following calls to action.finish() may invalidate it - action.update_remains(lrint(action.get_variable()->get_value() * delta)); + action.update_remains(rint(action.get_variable()->get_value() * delta)); action.update_max_duration(delta); if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) || @@ -70,17 +70,17 @@ DiskS19::DiskS19(DiskModel* model, const std::string& name, lmm::System* maxminS DiskAction* DiskS19::io_start(sg_size_t size, s4u::Io::OpType type) { - return new DiskS19Action(get_model(), size, not is_on(), this, type); + return new DiskS19Action(get_model(), static_cast(size), not is_on(), this, type); } DiskAction* DiskS19::read(sg_size_t size) { - return new DiskS19Action(get_model(), size, not is_on(), this, s4u::Io::OpType::READ); + return new DiskS19Action(get_model(), static_cast(size), not is_on(), this, s4u::Io::OpType::READ); } DiskAction* DiskS19::write(sg_size_t size) { - return new DiskS19Action(get_model(), size, not is_on(), this, s4u::Io::OpType::WRITE); + return new DiskS19Action(get_model(), static_cast(size), not is_on(), this, s4u::Io::OpType::WRITE); } /********** diff --git a/src/surf/network_wifi.cpp b/src/surf/network_wifi.cpp index 6d90be5044..9c60524148 100644 --- a/src/surf/network_wifi.cpp +++ b/src/surf/network_wifi.cpp @@ -64,8 +64,8 @@ s4u::Link::SharingPolicy NetworkWifiLink::get_sharing_policy() void NetworkWifiLink::refresh_decay_bandwidths(){ // Compute number of STAtion on the Access Point - int nSTA=host_rates_.size(); - + int nSTA = static_cast(host_rates_.size()); + std::vector new_bandwidths; for (auto bandwidth : bandwidths_){ // Instantiate decay model relatively to the actual bandwidth diff --git a/src/surf/storage_n11.cpp b/src/surf/storage_n11.cpp index cc116c5e66..dba05a810f 100644 --- a/src/surf/storage_n11.cpp +++ b/src/surf/storage_n11.cpp @@ -78,7 +78,7 @@ void StorageN11Model::update_actions_state(double /*now*/, double delta) for (auto it = std::begin(*get_started_action_set()); it != std::end(*get_started_action_set());) { auto& action = *it; ++it; // increment iterator here since the following calls to action.finish() may invalidate it - action.update_remains(lrint(action.get_variable()->get_value() * delta)); + action.update_remains(rint(action.get_variable()->get_value() * delta)); action.update_max_duration(delta); if (((action.get_remains_no_update() <= 0) && (action.get_variable()->get_penalty() > 0)) || @@ -103,17 +103,17 @@ StorageN11::StorageN11(StorageModel* model, const std::string& name, lmm::System StorageAction* StorageN11::io_start(sg_size_t size, s4u::Io::OpType type) { - return new StorageN11Action(get_model(), size, not is_on(), this, type); + return new StorageN11Action(get_model(), static_cast(size), not is_on(), this, type); } StorageAction* StorageN11::read(sg_size_t size) { - return new StorageN11Action(get_model(), size, not is_on(), this, s4u::Io::OpType::READ); + return new StorageN11Action(get_model(), static_cast(size), not is_on(), this, s4u::Io::OpType::READ); } StorageAction* StorageN11::write(sg_size_t size) { - return new StorageN11Action(get_model(), size, not is_on(), this, s4u::Io::OpType::WRITE); + return new StorageN11Action(get_model(), static_cast(size), not is_on(), this, s4u::Io::OpType::WRITE); } /**********