Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] NO.6 enable modernize-avoid-c-arrays step: 2 #55954

Merged
merged 8 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bugprone-unused-raii,
-misc-unused-alias-decls,
-misc-unused-using-decls,
modernize-avoid-bind,
-modernize-avoid-c-arrays,
modernize-avoid-c-arrays,
-modernize-deprecated-headers,
-modernize-deprecated-ios-base-aliases,
modernize-loop-convert,
Expand Down
2 changes: 2 additions & 0 deletions paddle/fluid/framework/data_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ proto::VarType::Type PromoteTypesIfComplexExists(
// Here is a complete rules table, but some rules are not used.
// It is still written this way because array accessing is still
// more efficient than if-else
// NOLINTBEGIN(*-avoid-c-arrays)
static constexpr proto::VarType::Type promote_types_table[4][4] = {
/* f4 f8 c4 c8*/
/* f4 */ {f4, f8, c4, c8},
/* f8 */ {f8, f8, c8, c8},
/* c4 */ {c4, c8, c4, c8},
/* c8 */ {c8, c8, c8, c8},
};
// NOLINTEND(*-avoid-c-arrays)

return promote_types_table[type_an][type_bn];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "paddle/phi/backends/device_memory_aligment.h"
#include "paddle/phi/core/flags.h"

DEFINE_bool(skip_fused_all_reduce_check, false, "");
DEFINE_bool(skip_fused_all_reduce_check, false, ""); // NOLINT
PHI_DECLARE_bool(allreduce_record_one_event);

namespace paddle {
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/framework/device_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License. */

#include "paddle/fluid/framework/device_worker.h"

#include <array>
#include <chrono>
#include "paddle/fluid/framework/convert_utils.h"
namespace phi {
Expand Down Expand Up @@ -90,7 +91,7 @@ void PrintLodTensorType<float>(phi::DenseTensor* tensor,
std::string& out_val, // NOLINT
char separator,
bool need_leading_separator) {
char buf[MAX_FLOAT_BUFF_SIZE];
std::array<char, MAX_FLOAT_BUFF_SIZE> buf;
auto count = tensor->numel();
if (start < 0 || end > count) {
VLOG(3) << "access violation";
Expand All @@ -104,8 +105,8 @@ void PrintLodTensorType<float>(phi::DenseTensor* tensor,
tensor->data<float>()[i] < FLOAT_EPS) {
out_val += "0";
} else {
sprintf(buf, "%.9f", tensor->data<float>()[i]); // NOLINT
out_val += buf;
sprintf(buf.data(), "%.9f", tensor->data<float>()[i]); // NOLINT
out_val += buf.data();
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions paddle/fluid/framework/hogwild_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <array>
#include <ctime>

#include "paddle/fluid/framework/barrier.h"
Expand Down Expand Up @@ -425,12 +426,14 @@ void HogwildWorker::PrintFetchVars() {
if (thread_id_ == 0 && batch_num_ % batch_per_print == 0) {
time_t curtime;
time(&curtime);
char mbstr[80];
std::strftime(
mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", std::localtime(&curtime));
std::array<char, 80> mbstr;
std::strftime(mbstr.data(),
sizeof(mbstr),
"%Y-%m-%d %H:%M:%S",
std::localtime(&curtime));

std::stringstream ss;
ss << "time: [" << mbstr << "], ";
ss << "time: [" << mbstr.data() << "], ";
ss << "batch: [" << batch_num_ << "], ";

for (int i = 0; i < fetch_var_num; ++i) {
Expand Down
11 changes: 6 additions & 5 deletions paddle/fluid/framework/io/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h
#include "paddle/fluid/framework/io/shell.h"

Expand Down Expand Up @@ -150,14 +151,14 @@ static int shell_popen_fork_internal(const char* real_cmd,
}

static int read_from_pipe(FILE* fp, std::string* output) {
char buf[4096];
std::array<char, 4096> buf;
while (1) {
int n = fread(buf, 1, 4096, fp);
int n = fread(buf.data(), 1, 4096, fp);
if (n <= 0) {
break;
}

output->append(buf, n);
output->append(buf.data(), n);
}

if (!feof(fp)) {
Expand Down Expand Up @@ -249,8 +250,8 @@ std::shared_ptr<FILE> shell_popen(const std::string& cmd,
}

static int shell_p2open_fork_internal(const char* real_cmd,
int pipein_fds[2],
int pipeout_fds[2]) {
int pipein_fds[2], // NOLINT
int pipeout_fds[2]) { // NOLINT
#if defined(_WIN32) || defined(__APPLE__) || defined(PADDLE_ARM)
return 0;
#else
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/framework/ir/lock_free_optimize_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace paddle {
namespace framework {
namespace ir {

const char kSumGradOpName[] = "sum";
const char kSumGradOpName[] = "sum"; // NOLINT
// TODO(minqiyang): only support sgd at current time, please add
// other optimizers later.
const char kOptimizerType[] = "sgd";
const char kOptimizerType[] = "sgd"; // NOLINT

void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const {
PADDLE_ENFORCE_NOT_NULL(
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/multi_batch_merge_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace paddle {
namespace framework {
namespace ir {

static const char kNumRepeats[] = "num_repeats";
static const char kNumRepeats[] = "num_repeats"; // NOLINT
typedef std::unordered_map<std::string, std::vector<ir::Node*>> SSAVarList;

ir::Node* SameNameVar(std::unordered_set<ir::Node*> all, ir::Node* target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace {
// all operators. NOTE that even we use a vector here, the operators is
// unordered.
typedef std::vector<details::OpHandleBase *> GraphOps;
const char kGraphOps[] = "ops";
const char kGraphOps[] = "ops"; // NOLINT

bool OpHaveRole(const ir::Node &node, const framework::OpRole &role) {
return PADDLE_GET_CONST(
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace ir {
#if !defined(_WIN32) && (__cplusplus < 201703L)
constexpr char Node::kControlDepVarName[];
#else
const char Node::kControlDepVarName[] = "__control_var";
const char Node::kControlDepVarName[] = "__control_var"; // NOLINT
#endif

std::unique_ptr<Node> CreateNodeForTest(const std::string &name,
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/pass.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace paddle {
namespace framework {
namespace ir {

static const char kParamScopeAttr[] = "__param_scope__";
static const char kParamScopeAttr[] = "__param_scope__"; // NOLINT

static const std::vector<std::string> support_subgraph_passes = {
"simplify_with_basic_ops_pass",
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/ir/runtime_context_cache_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace framework {
namespace ir {

void RuntimeContextCachePass::ApplyImpl(ir::Graph* graph) const {
static constexpr char kNotAllowInferShapeCahce[] =
static constexpr char kNotAllowInferShapeCahce[] = // NOLINT
"@NOT_ALLOW_INFERSHAPE_CACHE@";
VLOG(3) << "Applies Runtime Context Cache strategy.";
for (const Node* n : graph->Nodes()) {
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/framework/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ bool OpSupportGPU(const std::string& op_type) {
}

struct OperatorWithKernel::CacheImpl {
static const char kNotAllowInferShapeCahce[];
static const char kNotAllowInferShapeCahce[]; // NOLINT
explicit CacheImpl(phi::KernelContext* kernel_ctx,
RuntimeInferShapeContext* infer_shape_ctx,
const std::vector<phi::DenseTensor*>& tensors,
Expand Down Expand Up @@ -1273,8 +1273,9 @@ struct OperatorWithKernel::CacheImpl {
bool not_allow_infer_shape_cache_;
std::vector<phi::DDim> last_ddims_;
};
const char OperatorWithKernel::CacheImpl::kNotAllowInferShapeCahce[] =
"@NOT_ALLOW_INFERSHAPE_CACHE@";
const char // NOLINT
OperatorWithKernel::CacheImpl::kNotAllowInferShapeCahce[] =
"@NOT_ALLOW_INFERSHAPE_CACHE@";

static void CheckTensorNANOrInf(const std::string& op_type,
const std::string& name,
Expand Down
10 changes: 5 additions & 5 deletions paddle/fluid/framework/prune.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ limitations under the License. */
namespace paddle {
namespace framework {

const char kFeedOpType[] = "feed";
const char kFetchOpType[] = "fetch";
const char kFeedOpType[] = "feed"; // NOLINT
const char kFetchOpType[] = "fetch"; // NOLINT

const char kRecurrent[] = "recurrent";
const char kStates[] = "states";
const char kExStates[] = "ex_states";
const char kRecurrent[] = "recurrent"; // NOLINT
const char kStates[] = "states"; // NOLINT
const char kExStates[] = "ex_states"; // NOLINT

bool HasDependentInputVar(
const proto::OpDesc& op_desc,
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/framework/tensor_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ void TensorToStream(std::ostream& os,
#endif
} else if (platform::is_custom_place(tensor.place())) {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
constexpr size_t kBufSize = 1024 * 1024 * 64; // 64MB
std::unique_ptr<char[]> buf(new char[kBufSize]);
constexpr size_t kBufSize = 1024 * 1024 * 64; // 64MB
std::unique_ptr<char[]> buf(new char[kBufSize]); // NOLINT
auto& custom_device_context =
static_cast<const platform::CustomDeviceContext&>(dev_ctx);
platform::CPUPlace cpu;
Expand Down Expand Up @@ -598,7 +598,7 @@ void TensorFromStream(std::istream& is,
// proto buffer
int32_t size;
is.read(reinterpret_cast<char*>(&size), sizeof(size));
std::unique_ptr<char[]> buf(new char[size]);
std::unique_ptr<char[]> buf(new char[size]); // NOLINT
is.read(reinterpret_cast<char*>(buf.get()), size);
PADDLE_ENFORCE_EQ(
desc.ParseFromArray(buf.get(), size),
Expand Down Expand Up @@ -671,7 +671,7 @@ void TensorFromStream(std::istream& is,
0,
platform::errors::InvalidArgument(
"phi::DenseTensor desc size should >= 0"));
std::unique_ptr<char[]> buf(new char[size]);
std::unique_ptr<char[]> buf(new char[size]); // NOLINT
is.read(reinterpret_cast<char*>(buf.get()), size);
PADDLE_ENFORCE_EQ(
desc.ParseFromArray(buf.get(), size),
Expand Down
7 changes: 4 additions & 3 deletions paddle/fluid/framework/tensor_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <gtest/gtest.h>
#include <array>
#include <cmath>

#include "paddle/fluid/framework/tensor_util.h"
Expand All @@ -28,8 +29,8 @@ TEST(TensorCopy, Tensor) {
int* src_ptr = src_tensor.mutable_data<int>(phi::make_ddim({3, 3}),
platform::CPUPlace());

int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
memcpy(src_ptr, arr, 9 * sizeof(int));
std::array<int, 9> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
memcpy(src_ptr, arr.data(), 9 * sizeof(int));
src_tensor.set_layout(DataLayout::kAnyLayout);

auto cpu_place = new platform::CPUPlace();
Expand Down Expand Up @@ -467,7 +468,7 @@ TEST(TensorIsfinite, CPU) {

TEST(Tensor, FromAndToStream) {
phi::DenseTensor src_tensor;
int array[6] = {1, 2, 3, 4, 5, 6};
std::array<int, 6> array = {1, 2, 3, 4, 5, 6};
src_tensor.Resize({2, 3});
int* src_ptr = src_tensor.mutable_data<int>(platform::CPUPlace());
for (int i = 0; i < 6; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/core/dense_tensor.h"

DEFINE_bool(
DEFINE_bool( // NOLINT
custom_model_save_cpu,
false,
"Keep old mode for developers, the model is saved on cpu not device.");
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/inference/api/api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License. */
#include "paddle/fluid/platform/place.h"
#include "paddle/fluid/platform/profiler.h"

DEFINE_bool(profile, false, "Turn on profiler for fluid");
DEFINE_bool(profile, false, "Turn on profiler for fluid"); // NOLINT

namespace paddle {
namespace {
Expand Down
4 changes: 3 additions & 1 deletion paddle/fluid/inference/io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ limitations under the License. */
// phi
#include "paddle/phi/kernels/declarations.h"

DEFINE_string(devices, "", "The devices to be used which is joined by comma.");
DEFINE_string(devices, // NOLINT
"",
"The devices to be used which is joined by comma.");
DEFINE_int32(math_num_threads,
1,
"Number of threads used to run math functions.");
Expand Down
6 changes: 4 additions & 2 deletions paddle/fluid/ir/dialect/kernel_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
namespace paddle {
namespace dialect {

const char* PhiKernelOp::attributes_name[attributes_num] = {
"op_name", "kernel_name", "kernel_key"};
const char* PhiKernelOp::attributes_name[attributes_num] = { // NOLINT
"op_name",
"kernel_name",
"kernel_key"};

void PhiKernelOp::Verify() {
VLOG(4) << "Verifying inputs, outputs and attributes for: PhiKernelOp.";
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/ir_adaptor/translator/op_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ using InputHandlerFn = std::function<ir::OpResult(ir::IrContext*,
ir::Program*)>;
using AttributeHandlerFn = std::function<ir::Attribute(
ir::IrContext*, const OpDesc&, const OpAttributeInfo&)>;
constexpr char kTargetDialectPrefix[] = "pd.";
constexpr char kEmptyVarName[] = "@EMPTY@";
constexpr char kTargetDialectPrefix[] = "pd."; // NOLINT
constexpr char kEmptyVarName[] = "@EMPTY@"; // NOLINT

static const std::unordered_set<std::string> special_non_inplace_ops = {
"batch_norm",
Expand Down
Loading