From 9be529895c01e6a7a1dbeb6133aa56861654780f Mon Sep 17 00:00:00 2001 From: yujianfeng Date: Tue, 22 Feb 2022 16:25:20 +0800 Subject: [PATCH] Clean code for core directory --- mindspore/core/load_mindir/infer_mindir.cc | 26 ++++++++++----------- mindspore/core/load_mindir/load_model.cc | 8 +++---- mindspore/core/mindapi/ir/tensor.h | 4 ++-- mindspore/core/mindapi/src/func_graph.cc | 6 ++--- mindspore/core/mindapi/src/primitive.cc | 4 ++-- mindspore/core/mindapi/src/tensor.cc | 4 ++-- mindspore/core/utils/log_adapter.cc | 4 +--- mindspore/core/utils/system/sha256.cc | 27 +++++++++++----------- mindspore/core/utils/trace_base.cc | 10 ++++---- 9 files changed, 46 insertions(+), 47 deletions(-) diff --git a/mindspore/core/load_mindir/infer_mindir.cc b/mindspore/core/load_mindir/infer_mindir.cc index 770ab2aa088..821347c4817 100644 --- a/mindspore/core/load_mindir/infer_mindir.cc +++ b/mindspore/core/load_mindir/infer_mindir.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -116,8 +116,8 @@ void MindIREngine::Init(const AbstractBasePtrList &args) { if (node->isa()) { auto cnode = node->cast(); MS_EXCEPTION_IF_NULL(cnode); - todo_.insert(node); - node_input_depends_[node] = cnode->inputs().size(); + (void)todo_.insert(node); + node_input_depends_[node] = SizeToInt(cnode->inputs().size()); } else if (node->isa()) { auto param = node->cast(); MS_EXCEPTION_IF_NULL(param); @@ -127,7 +127,7 @@ void MindIREngine::Init(const AbstractBasePtrList &args) { ready_.push_back(node); } else { node_input_depends_[node] = 1; - todo_.insert(node); + (void)todo_.insert(node); } } else { // Value Node @@ -192,7 +192,7 @@ void MindIREngine::EvalCommonPrimitive(const PrimitivePtr &prim, const CNodePtr AbstractBasePtrList args_spec_list; // Args has been resolved by partial if (args != nullptr) { - args_spec_list.insert(args_spec_list.end(), args->begin(), args->end()); + (void)args_spec_list.insert(args_spec_list.end(), args->begin(), args->end()); } else { (void)std::transform(node->inputs().begin() + 1, node->inputs().end(), std::back_inserter(args_spec_list), [this](const AnfNodePtr &arg) { return infer_resut_[arg]; }); @@ -244,7 +244,7 @@ void MindIREngine::EvalPartialPrimitive(const CNodePtr &node, const AbstractBase MS_LOG(EXCEPTION) << (*args)[0]->ToString() << " is not a function abstract."; } AbstractBasePtrList partial_args_list; - partial_args_list.insert(partial_args_list.end(), args->begin() + 1, args->end()); + (void)partial_args_list.insert(partial_args_list.end(), args->begin() + 1, args->end()); auto partial_func = std::make_shared(real_func, partial_args_list, node); SaveNodeInferResult(node, partial_func); return; @@ -270,14 +270,14 @@ void MindIREngine::EvalPartialAbastract(const abstract::PartialAbstractClosurePt AbstractBasePtrListPtr partial_args_list = std::make_shared(); // Join arguments in partial and the rest arguments from args_conf_list. auto func_args = func->args(); - partial_args_list->insert(partial_args_list->end(), func_args.begin(), func_args.end()); + (void)partial_args_list->insert(partial_args_list->end(), func_args.begin(), func_args.end()); if (args == nullptr) { // Not Recursive (void)std::transform(node->inputs().begin() + 1, node->inputs().end(), std::back_inserter(*partial_args_list), [this](const AnfNodePtr &arg) { return infer_resut_[arg]; }); } else { // Recursive - partial_args_list->insert(partial_args_list->end(), args->begin(), args->end()); + (void)partial_args_list->insert(partial_args_list->end(), args->begin(), args->end()); } // Get real function @@ -337,7 +337,7 @@ bool MindIREngine::CheckCNodeNotReady(const CNodePtr &node) { depend += infer_resut_.find(input) != infer_resut_.end() ? 0 : 1; } this->node_input_depends_[node] = depend; - return depend; + return depend != 0; } void MindIREngine::EvalFuncGraphAbastract(const abstract::FuncGraphAbstractClosurePtr &func, const CNodePtr &node, @@ -364,7 +364,7 @@ void MindIREngine::EvalFuncGraphAbastract(const abstract::FuncGraphAbstractClosu for (size_t i = 0; i < func_inputs.size(); ++i) { infer_resut_[func_inputs[i]] = (*args)[i]; // Not use SaveNodeInferResult because this function has been evaluated. - todo_.erase(func_inputs[i]); + (void)todo_.erase(func_inputs[i]); } return; } @@ -376,7 +376,7 @@ void MindIREngine::EvalFuncGraphAbastract(const abstract::FuncGraphAbstractClosu } for (size_t i = 0; i < func_inputs.size(); ++i) { infer_resut_[func_inputs[i]] = infer_resut_[cnode_inputs[i + 1]]; - todo_.erase(func_inputs[i]); + (void)todo_.erase(func_inputs[i]); } return; } @@ -384,7 +384,7 @@ void MindIREngine::EvalFuncGraphAbastract(const abstract::FuncGraphAbstractClosu // Be handling auto visitIt = func_graph_visited_.find(funcName); if (visitIt != func_graph_visited_.end()) { - visitIt->second.insert(node); + (void)visitIt->second.insert(node); return; } func_graph_visited_[funcName] = std::set({node}); @@ -477,7 +477,7 @@ void MindIREngine::EvalAbstractFunction(const abstract::AbstractFuncAtomPtr &fun } void MindIREngine::UpdateReady(const AnfNodePtr &node) { - todo_.erase(node); + (void)todo_.erase(node); auto it = nodeuser_map_.find(node); if (it == nodeuser_map_.end()) { return; diff --git a/mindspore/core/load_mindir/load_model.cc b/mindspore/core/load_mindir/load_model.cc index b5a8891971f..4a8a72fbc96 100644 --- a/mindspore/core/load_mindir/load_model.cc +++ b/mindspore/core/load_mindir/load_model.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2020-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,7 +62,7 @@ bool get_all_files(const std::string &dir_in, std::vector *files) { return false; } if (S_ISDIR(st.st_mode)) { - ret = get_all_files(name, files); + ret = static_cast(get_all_files(name, files)); if (!ret) { MS_LOG(ERROR) << "Get files failed, ret is : " << ret; closedir(open_dir); @@ -196,7 +196,7 @@ FuncGraphPtr MindIRLoader::LoadMindIR(const void *buffer, const size_t &size) { /* mindir -> func_graph * only support lite */ mind_ir::ModelProto model; - auto ret = model.ParseFromArray(buffer, size); + auto ret = model.ParseFromArray(buffer, SizeToInt(size)); if (!ret) { MS_LOG(ERROR) << "ParseFromArray failed."; return nullptr; @@ -255,7 +255,7 @@ FuncGraphPtr MindIRLoader::LoadMindIR(const std::string &file_name) { std::ifstream ifs(var_path); if (ifs.good()) { MS_LOG(DEBUG) << "MindIR file has variables path, load parameter into graph."; - get_all_files(var_path, &files); + (void)get_all_files(var_path, &files); } else { MS_LOG(ERROR) << "Load graph's variable folder failed, please check the correctness of variable folder."; return nullptr; diff --git a/mindspore/core/mindapi/ir/tensor.h b/mindspore/core/mindapi/ir/tensor.h index dd43755594f..2811098a37b 100644 --- a/mindspore/core/mindapi/ir/tensor.h +++ b/mindspore/core/mindapi/ir/tensor.h @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,7 +80,7 @@ class MIND_API Tensor : public Value { /// \brief Get tensor data size. /// /// \return The total number of elements in the tensor. - int DataSize() const; + size_t DataSize() const; /// \brief Get tensor data size in bytes. /// diff --git a/mindspore/core/mindapi/src/func_graph.cc b/mindspore/core/mindapi/src/func_graph.cc index 4edc2175f1c..6577444c4f5 100644 --- a/mindspore/core/mindapi/src/func_graph.cc +++ b/mindspore/core/mindapi/src/func_graph.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -154,8 +154,8 @@ std::vector> FuncGraphManager::GetUsers(const AnfNode auto &users_impl = iter->second; std::vector> users; users.reserve(users_impl.size()); - std::transform(users_impl.begin(), users_impl.end(), std::back_inserter(users), - [](const auto &user) { return std::make_pair(ToWrapper(user.first), user.second); }); + (void)std::transform(users_impl.begin(), users_impl.end(), std::back_inserter(users), + [](const auto &user) { return std::make_pair(ToWrapper(user.first), user.second); }); return users; } diff --git a/mindspore/core/mindapi/src/primitive.cc b/mindspore/core/mindapi/src/primitive.cc index e5a554ab536..703a0aba32c 100644 --- a/mindspore/core/mindapi/src/primitive.cc +++ b/mindspore/core/mindapi/src/primitive.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,7 @@ std::unordered_map Primitive::attrs() const { attr_map.reserve(impl_attrs.size()); for (auto &attr : impl_attrs) { auto value = ToWrapper(attr.second); - attr_map.emplace(attr.first, value); + (void)attr_map.emplace(attr.first, value); } return attr_map; } diff --git a/mindspore/core/mindapi/src/tensor.cc b/mindspore/core/mindapi/src/tensor.cc index d37da12c0d2..a1f1886c1d6 100644 --- a/mindspore/core/mindapi/src/tensor.cc +++ b/mindspore/core/mindapi/src/tensor.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ const void *Tensor::data() const { return ToRef(impl_).data_c(); } void *Tensor::data() { return ToRef(impl_).data_c(); } -int Tensor::DataSize() const { return ToRef(impl_).DataSize(); } +size_t Tensor::DataSize() const { return ToRef(impl_).DataSize(); } size_t Tensor::Size() const { return ToRef(impl_).Size(); } } // namespace mindspore::api diff --git a/mindspore/core/utils/log_adapter.cc b/mindspore/core/utils/log_adapter.cc index 25366c8446a..0a4adf5a53d 100644 --- a/mindspore/core/utils/log_adapter.cc +++ b/mindspore/core/utils/log_adapter.cc @@ -1,5 +1,5 @@ /** - * Copyright 2019 Huawei Technologies Co., Ltd + * Copyright 2019-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,11 +58,9 @@ static std::string GetProcName() { } static std::string GetLogLevel(MsLogLevel level) { -#define _TO_STRING(x) #x static const char *const level_names[] = { "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", }; -#undef _TO_STRING if (level > this_thread_max_log_level) { level = this_thread_max_log_level; } diff --git a/mindspore/core/utils/system/sha256.cc b/mindspore/core/utils/system/sha256.cc index dc169550a98..f6e35bba746 100644 --- a/mindspore/core/utils/system/sha256.cc +++ b/mindspore/core/utils/system/sha256.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ #include #include "securec/include/securec.h" #include "utils/log_adapter.h" +#include "utils/convert_utils_base.h" namespace mindspore { namespace system { @@ -76,7 +77,7 @@ bool Padding(std::string *message) { message->push_back(0x00); } for (int i = size_append - 1; i >= 0; --i) { - message->push_back(static_cast((bits_message >> static_cast(i * kBitNumber)) & 0xff)); + message->push_back(static_cast((bits_message >> static_cast(i * kBitNumber)) & 0xff)); } return true; } @@ -87,17 +88,17 @@ bool ProcessInner(const std::string &message, const int &bias, uint32_t *digest, } uint32_t w[kIterationNumber] = {0}; for (int i = 0; i < 16; ++i) { - w[i] = (static_cast(static_cast(message[bias + i * 4]) & 0xff) << 24) | - (static_cast(static_cast(message[bias + i * 4 + 1]) & 0xff) << 16) | - (static_cast(static_cast(message[bias + i * 4 + 2]) & 0xff) << 8) | - (static_cast(static_cast(message[bias + i * 4 + 3]) & 0xff)); + w[i] = (static_cast(static_cast(message[IntToSize(bias + i * 4)]) & 0xff) << 24) | + (static_cast(static_cast(message[IntToSize(bias + i * 4 + 1)]) & 0xff) << 16) | + (static_cast(static_cast(message[IntToSize(bias + i * 4 + 2)]) & 0xff) << 8) | + (static_cast(static_cast(message[IntToSize(bias + i * 4 + 3)]) & 0xff)); } for (int i = 16; i < kIterationNumber; ++i) { w[i] = sigma3(w[i - 2]) + w[i - 7] + sigma2(w[i - 15]) + w[i - 16]; } std::vector hash(digest_size); - size_t mem_size = digest_size * sizeof(uint32_t); + size_t mem_size = IntToSize(digest_size) * sizeof(uint32_t); auto ret = memcpy_s(hash.data(), mem_size, digest, mem_size); if (ret != EOK) { return false; @@ -107,15 +108,15 @@ bool ProcessInner(const std::string &message, const int &bias, uint32_t *digest, uint32_t t2 = sigma0(hash[0]) + ma(hash[0], hash[1], hash[2]); for (int j = digest_size - 1; j >= 0; --j) { if (j == 4) { - hash[j] = hash[j - 1] + t1; + hash[IntToSize(j)] = hash[IntToSize(j - 1)] + t1; } else if (j == 0) { - hash[j] = t1 + t2; + hash[IntToSize(j)] = t1 + t2; } else { - hash[j] = hash[j - 1]; + hash[IntToSize(j)] = hash[IntToSize(j - 1)]; } } } - for (int i = 0; i < digest_size; ++i) { + for (size_t i = 0; i < IntToSize(digest_size); ++i) { digest[i] += hash[i]; } return true; @@ -188,9 +189,9 @@ std::string GetHashFromDir(const std::string &dir) { if (d_name == "." || d_name == ".." || filename->d_type != DT_REG) { continue; } - file_hashes.emplace_back(GetHashFromFile(std::string(dir) + "/" + filename->d_name)); + (void)file_hashes.emplace_back(GetHashFromFile(std::string(dir) + "/" + filename->d_name)); } - closedir(open_dir); + (void)closedir(open_dir); std::sort(file_hashes.begin(), file_hashes.end()); auto dir_hash = std::accumulate(file_hashes.begin(), file_hashes.end(), std::string{}); return dir_hash; diff --git a/mindspore/core/utils/trace_base.cc b/mindspore/core/utils/trace_base.cc index aa93fbb0743..f14c32a54c4 100644 --- a/mindspore/core/utils/trace_base.cc +++ b/mindspore/core/utils/trace_base.cc @@ -1,5 +1,5 @@ /** - * Copyright 2020-2021 Huawei Technologies Co., Ltd + * Copyright 2020-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -188,14 +188,14 @@ void GetSourceLineFromDebugInfo(const DebugInfoPtr &debug_info, std::vector *result) { MS_EXCEPTION_IF_NULL(result); - result->push_back("Corresponding code candidate:\n"); + (void)result->emplace_back("Corresponding code candidate:\n"); // Flag to mark whether fused_debug_infos has valid print. bool is_empty = true; for (const auto &debug_info : fused_debug_infos) { std::vector debug_info_vec_str; GetSourceLineFromDebugInfo(debug_info, &debug_info_vec_str, kSectionPrefix); if (!debug_info_vec_str.empty()) { - result->insert(result->end(), debug_info_vec_str.begin(), debug_info_vec_str.end()); + (void)result->insert(result->end(), debug_info_vec_str.begin(), debug_info_vec_str.end()); is_empty = false; } } @@ -210,12 +210,12 @@ void GetPrimalDebugInfos(const CNodePtr &cnode, std::vector *result MS_EXCEPTION_IF_NULL(result); auto primal_debug_infos = cnode->primal_debug_infos(); if (!primal_debug_infos.empty()) { - result->emplace_back("Corresponding forward node candidate:\n"); + (void)result->emplace_back("Corresponding forward node candidate:\n"); for (const auto &primal_debug_info : primal_debug_infos) { std::vector debug_info_vec_str; GetSourceLineFromDebugInfo(primal_debug_info, &debug_info_vec_str, kSectionPrefix); if (!debug_info_vec_str.empty()) { - result->insert(result->end(), debug_info_vec_str.begin(), debug_info_vec_str.end()); + (void)result->insert(result->end(), debug_info_vec_str.begin(), debug_info_vec_str.end()); } } }