Skip to content

Commit

Permalink
[clang-tidy] NO.6 enable modernize-avoid-c-arrays check (#55774)
Browse files Browse the repository at this point in the history
* [clang-tidy] modernize-avoid-c-arrays

* rollback

* [clang-tidy] fix

* close modernize-avoid-c-arrays

* fix PHI_DEFINE_string; add PHI_DEFINE_bool NOLINT

* fix PHI_DEFINE_string

* fix next_h_state and parity err

* fix win32

* fix cuda_graph

* fix accuracy_kernel

* fix math_function

* fix fused_softmax_mask_kernel.cu load_data and warp_reduce; rollback concat_and_split_functor ins_addr

* fix fused_dropout_add_grad_kernel

* fix

* rollback cu

* rollback concat_and_split_functor.cu

* rollback
  • Loading branch information
gouzil authored Aug 2, 2023
1 parent 22c7a6e commit c000091
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 102 deletions.
10 changes: 5 additions & 5 deletions paddle/ir/core/builtin_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace ir {

const char *ModuleOp::attributes_name[attributes_num] = {"program"};
const char *ModuleOp::attributes_name[attributes_num] = {"program"}; // NOLINT

Program *ModuleOp::program() {
const AttributeMap &attr = this->attributes();
Expand Down Expand Up @@ -67,7 +67,7 @@ void ModuleOp::Verify() const {
IR_ENFORCE(num_results() == 0u, "The size of inputs must be equal to 0.");
}

const char *GetParameterOp::attributes_name[attributes_num] = {
const char *GetParameterOp::attributes_name[attributes_num] = { // NOLINT
"parameter_name"};

void GetParameterOp::Build(Builder &builder,
Expand All @@ -94,7 +94,7 @@ void GetParameterOp::Verify() const {
IR_ENFORCE(num_results() == 1u, "The size of outputs must be equal to 1.");
}

const char *SetParameterOp::attributes_name[attributes_num] = {
const char *SetParameterOp::attributes_name[attributes_num] = { // NOLINT
"parameter_name"};

void SetParameterOp::Build(Builder &builder, // NOLINT
Expand Down Expand Up @@ -161,7 +161,7 @@ void CombineOp::Verify() const {
}
}

const char *SliceOp::attributes_name[attributes_num] = {"index"};
const char *SliceOp::attributes_name[attributes_num] = {"index"}; // NOLINT
void SliceOp::Verify() const {
// inputs.size() == 1
auto input_size = num_operands();
Expand Down Expand Up @@ -207,7 +207,7 @@ void SliceOp::Verify() const {
output_type);
}

const char *ConstantOp::attributes_name[attributes_num] = {"value"};
const char *ConstantOp::attributes_name[attributes_num] = {"value"}; // NOLINT

void ConstantOp::Build(Builder &builder,
OperationArgument &argument,
Expand Down
2 changes: 1 addition & 1 deletion paddle/ir/core/ir_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
namespace ir {

namespace {
constexpr char newline[] = "\n";
constexpr char newline[] = "\n"; // NOLINT
} // namespace

void BasicIrPrinter::PrintType(Type type) {
Expand Down
2 changes: 1 addition & 1 deletion paddle/ir/core/op_info_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ OpInfo OpInfoImpl::Create(Dialect *dialect,
std::vector<InterfaceValue> &&interface_map,
const std::vector<TypeId> &trait_set,
size_t attributes_num,
const char *attributes_name[],
const char *attributes_name[], // NOLINT
VerifyPtr verify) {
// (1) Malloc memory for interfaces, traits, opinfo_impl.
size_t interfaces_num = interface_map.size();
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/backends/cpu/cpu_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/phi/backends/cpu/cpu_info.h"
#include <array>

#ifdef __APPLE__
#include <sys/sysctl.h>
Expand Down Expand Up @@ -43,7 +44,7 @@ DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
// between host and device. Allocates too much would reduce the amount
// of memory available to the system for paging. So, by default, we
// should set false to use_pinned_memory.
PHI_DEFINE_EXPORTED_bool(use_pinned_memory,
PHI_DEFINE_EXPORTED_bool(use_pinned_memory, // NOLINT
true,
"If set, allocate cpu pinned memory.");

Expand All @@ -53,12 +54,12 @@ namespace cpu {

size_t CpuTotalPhysicalMemory() {
#ifdef __APPLE__
int mib[2];
std::array<int, 2> mib;
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
int64_t size = 0;
size_t len = sizeof(size);
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) {
if (sysctl(mib.data(), 2, &size, &len, NULL, 0) == 0) {
return static_cast<size_t>(size);
}
return 0L;
Expand Down
30 changes: 18 additions & 12 deletions paddle/phi/backends/dynload/dynamic_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,54 @@ limitations under the License. */
#include "glog/logging.h"
#include "paddle/phi/core/flags.h"

PHI_DEFINE_string(cudnn_dir,
PHI_DEFINE_string(cudnn_dir, // NOLINT
"",
"Specify path for loading libcudnn.so. For instance, "
"/usr/local/cudnn/lib. If empty [default], dlopen "
"will search cudnn from LD_LIBRARY_PATH");

PHI_DEFINE_string(
PHI_DEFINE_string( // NOLINT
cuda_dir,
"",
"Specify path for loading cuda library, such as libcublas, libcublasLt "
"libcurand, libcusolver. For instance, /usr/local/cuda/lib64. "
"If default, dlopen will search cuda from LD_LIBRARY_PATH");

PHI_DEFINE_string(nccl_dir,
PHI_DEFINE_string(nccl_dir, // NOLINT
"",
"Specify path for loading nccl library, such as libnccl.so. "
"For instance, /usr/local/cuda/lib64. If default, "
"dlopen will search cuda from LD_LIBRARY_PATH");

PHI_DEFINE_string(cupti_dir, "", "Specify path for loading cupti.so.");
PHI_DEFINE_string(cupti_dir,
"",
"Specify path for loading cupti.so."); // NOLINT

PHI_DEFINE_string(
PHI_DEFINE_string( // NOLINT
tensorrt_dir,
"",
"Specify path for loading tensorrt library, such as libnvinfer.so.");

PHI_DEFINE_string(mklml_dir, "", "Specify path for loading libmklml_intel.so.");
PHI_DEFINE_string(mklml_dir,
"",
"Specify path for loading libmklml_intel.so."); // NOLINT

PHI_DEFINE_string(lapack_dir, "", "Specify path for loading liblapack.so.");
PHI_DEFINE_string(lapack_dir,
"",
"Specify path for loading liblapack.so."); // NOLINT

PHI_DEFINE_string(mkl_dir,
PHI_DEFINE_string(mkl_dir, // NOLINT
"",
"Specify path for loading libmkl_rt.so. "
"For insrance, /opt/intel/oneapi/mkl/latest/lib/intel64/."
"If default, "
"dlopen will search mkl from LD_LIBRARY_PATH");

PHI_DEFINE_string(op_dir,
PHI_DEFINE_string(op_dir, // NOLINT
"",
"Specify path for loading user-defined op library.");

PHI_DEFINE_string(cusparselt_dir,
PHI_DEFINE_string(cusparselt_dir, // NOLINT
"",
"Specify path for loading libcusparseLt.so.");

Expand Down Expand Up @@ -107,13 +113,13 @@ struct PathNode {
std::string path = "";
};

static constexpr char cupti_lib_path[] = CUPTI_LIB_PATH;
static constexpr char cupti_lib_path[] = CUPTI_LIB_PATH; // NOLINT

// NOTE: In order to adapt to the default installation path of cuda
#if defined(_WIN32) && defined(PADDLE_WITH_CUDA)
static constexpr char cuda_lib_path[] = CUDA_TOOLKIT_ROOT_DIR "/bin";
#else
static constexpr char cuda_lib_path[] = "/usr/local/cuda/lib64";
static constexpr char cuda_lib_path[] = "/usr/local/cuda/lib64"; // NOLINT
#endif

static PathNode s_py_site_pkg_path;
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/backends/dynload/port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <paddle/phi/backends/dynload/port.h>

#include <array>
#include <memory>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -74,7 +75,7 @@ int gettimeofday(struct timeval *tp, void *tzp) {
#endif // !_WIN32

void ExecShellCommand(const std::string &cmd, std::string *message) {
char buffer[128];
std::array<char, 128> buffer;
#if !defined(_WIN32)
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
#else
Expand All @@ -85,8 +86,8 @@ void ExecShellCommand(const std::string &cmd, std::string *message) {
return;
}
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != nullptr) {
*message += buffer;
if (fgets(buffer.data(), 128, pipe.get()) != nullptr) {
*message += buffer.data();
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/backends/gpu/cuda/cuda_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "paddle/phi/backends/gpu/cuda/cuda_graph.h"

#include <array>
#include <queue>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -277,14 +278,14 @@ bool CUDAGraph::IsValidCapturing() {
static std::string ConcatPath(const std::string &dirname,
const std::string &filename) {
#ifdef _WIN32
const char kFileSep[] = "\\";
const std::array<char, 3> kFileSep = {"\\"};
#else
const char kFileSep[] = "/";
const std::array<char, 2> kFileSep = {"/"};
#endif
if (!dirname.empty() && dirname.back() == kFileSep[0]) {
return dirname + filename;
} else {
return dirname + kFileSep + filename;
return dirname + kFileSep.data() + filename;
}
}

Expand Down
15 changes: 8 additions & 7 deletions paddle/phi/core/distributed/store/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "paddle/phi/core/distributed/store/socket.h"
#include <array>

#ifndef _WIN32
#include <arpa/inet.h>
Expand Down Expand Up @@ -42,21 +43,21 @@ static int _get_sockname(int sock, char *out, int out_len) {
return -1;
}

char ip[128];
std::array<char, 128> ip;
int port = 0;

// deal with both IPv4 and IPv6:
if (addr.sin_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
port = ntohs(s->sin_port);
::inet_ntop(AF_INET, &s->sin_addr, ip, sizeof(ip));
::inet_ntop(AF_INET, &s->sin_addr, ip.data(), sizeof(ip));
} else { // AF_INET6
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
port = ntohs(s->sin6_port);
::inet_ntop(AF_INET6, &s->sin6_addr, ip, sizeof(ip));
::inet_ntop(AF_INET6, &s->sin6_addr, ip.data(), sizeof(ip));
}

::snprintf(out, out_len, "%s:%d", ip, port);
::snprintf(out, out_len, "%s:%d", ip.data(), port);
return 0;
}
#endif
Expand All @@ -70,9 +71,9 @@ int GetSockName(int sock, char* out, int out_len) {
}

std::string GetSockName(int fd) {
char out[256];
GetSockName(fd, out, sizeof(out));
return std::string(out);
std::array<char, 256> out;
GetSockName(fd, out.data(), sizeof(out));
return std::string(out.data());
}

} // namespace distributed
Expand Down
7 changes: 4 additions & 3 deletions paddle/phi/core/enforce.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License. */

#include "paddle/phi/core/enforce.h"

#include <array>
#include <map>
#include <memory>
#include <unordered_map>
Expand Down Expand Up @@ -141,9 +142,9 @@ std::string GetCurrentTraceBackString(bool for_signal) {
#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL)
static constexpr int TRACE_STACK_LIMIT = 100;

void* call_stack[TRACE_STACK_LIMIT];
auto size = backtrace(call_stack, TRACE_STACK_LIMIT);
auto symbols = backtrace_symbols(call_stack, size);
std::array<void*, TRACE_STACK_LIMIT> call_stack;
auto size = backtrace(call_stack.data(), TRACE_STACK_LIMIT);
auto symbols = backtrace_symbols(call_stack.data(), size);
Dl_info info;
int idx = 0;
// `for_signal` used to remove the stack trace introduced by
Expand Down
8 changes: 4 additions & 4 deletions paddle/phi/core/flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ PHI_DEFINE_EXPORTED_double(
* Example:
* Note: For selecting allocator policy of PaddlePaddle.
*/
static constexpr char kDefaultAllocatorStrategy[] = "auto_growth";
static constexpr char kDefaultAllocatorStrategy[] = "auto_growth"; // NOLINT
PHI_DEFINE_EXPORTED_string(
allocator_strategy,
kDefaultAllocatorStrategy,
Expand Down Expand Up @@ -1060,13 +1060,13 @@ DEFINE_int32(record_pool_max_size,
2000000,
"SlotRecordDataset slot record pool max size");
DEFINE_int32(slotpool_thread_num, 1, "SlotRecordDataset slot pool thread num");
DEFINE_bool(enable_slotpool_wait_release,
DEFINE_bool(enable_slotpool_wait_release, // NOLINT
false,
"enable slotrecord object wait release, default false");
DEFINE_bool(enable_slotrecord_reset_shrink,
DEFINE_bool(enable_slotrecord_reset_shrink, // NOLINT
false,
"enable slotrecord object reset shrink memory, default false");
DEFINE_bool(enable_ins_parser_file,
DEFINE_bool(enable_ins_parser_file, // NOLINT
false,
"enable parser ins file, default false");
PHI_DEFINE_EXPORTED_bool(
Expand Down
54 changes: 28 additions & 26 deletions paddle/phi/core/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include <array>
#include <cstdint>
#include <map>
#include <string>
Expand Down Expand Up @@ -81,11 +82,11 @@
} /* NOLINT */ \
using fL##shorttype::FLAGS_##name

#define PHI_DEFINE_bool(name, val, txt) \
namespace fLB { \
typedef ::fLB::CompileAssert FLAG_##name##_value_is_not_a_bool \
[(sizeof(::fLB::IsBoolFlag(val)) != sizeof(double)) ? 1 : -1]; \
} \
#define PHI_DEFINE_bool(name, val, txt) \
namespace fLB { \
typedef ::fLB::CompileAssert FLAG_##name##_value_is_not_a_bool /* NOLINT */ \
[(sizeof(::fLB::IsBoolFlag(val)) != sizeof(double)) ? 1 : -1]; \
} \
PHI_DEFINE_VARIABLE(bool, B, name, val, txt)

#define PHI_DEFINE_int32(name, val, txt) \
Expand All @@ -103,27 +104,28 @@
#define PHI_DEFINE_double(name, val, txt) \
PHI_DEFINE_VARIABLE(double, D, name, val, txt)

#define PHI_DEFINE_string(name, val, txt) \
namespace fLS { \
using ::fLS::clstring; \
using ::fLS::StringFlagDestructor; \
static union { \
void* align; \
char s[sizeof(clstring)]; \
} s_##name[2]; \
clstring* const FLAGS_no##name = \
::fLS::dont_pass0toDEFINE_string(s_##name[0].s, val); \
static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \
#name, \
MAYBE_STRIPPED_HELP(txt), \
__FILE__, \
FLAGS_no##name, \
new (s_##name[1].s) clstring(*FLAGS_no##name)); \
static StringFlagDestructor d_##name(s_##name[0].s, s_##name[1].s); \
extern PHI_EXPORT_FLAG clstring& FLAGS_##name; \
using fLS::FLAGS_##name; \
clstring& FLAGS_##name = *FLAGS_no##name; \
} /* NOLINT */ \
#define PHI_DEFINE_string(name, val, txt) \
namespace fLS { \
using ::fLS::clstring; \
using ::fLS::StringFlagDestructor; \
static union { /* NOLINT */ \
void* align; \
std::array<char, sizeof(clstring)> s; \
} s_##name[2]; /* NOLINT */ \
clstring* const FLAGS_no##name = \
::fLS::dont_pass0toDEFINE_string(s_##name[0].s.data(), val); \
static GFLAGS_NAMESPACE::FlagRegisterer o_##name( \
#name, \
MAYBE_STRIPPED_HELP(txt), \
__FILE__, \
FLAGS_no##name, \
new (s_##name[1].s.data()) clstring(*FLAGS_no##name)); \
static StringFlagDestructor d_##name(s_##name[0].s.data(), \
s_##name[1].s.data()); \
extern PHI_EXPORT_FLAG clstring& FLAGS_##name; \
using fLS::FLAGS_##name; \
clstring& FLAGS_##name = *FLAGS_no##name; \
} /* NOLINT */ \
using fLS::FLAGS_##name

namespace phi {
Expand Down
Loading

0 comments on commit c000091

Please sign in to comment.