From 4e9ddeee7d4852fdb2132a507681b91e0405b8db Mon Sep 17 00:00:00 2001 From: Satyanvesh Dittakavi <53337087+satyanveshd@users.noreply.github.com> Date: Thu, 24 Feb 2022 20:57:04 +0530 Subject: [PATCH] SWDEV-306940 - Fix hip errors/warnings by CppCheck (#2486) Change-Id: I91f5e97b0c7ca80b6d21fa66d24e3cf9fde716a9 [ROCm/hip-tests commit: 833e962ddde866ff391f11572430f3aa4c59dfb2] --- .../samples/0_Intro/module_api/launchKernelHcc.cpp | 3 --- .../1_Utils/hipBusBandwidth/ResultDatabase.cpp | 2 +- .../1_Utils/hipBusBandwidth/hipBusBandwidth.cpp | 4 ++-- .../samples/1_Utils/hipCommander/ResultDatabase.cpp | 2 +- .../samples/1_Utils/hipCommander/hipCommander.cpp | 12 ++++++------ .../samples/2_Cookbook/14_gpu_arch/gpuarch.cpp | 11 ++++------- .../15_static_library/device_functions/hipMain2.cpp | 4 ++-- 7 files changed, 16 insertions(+), 22 deletions(-) diff --git a/projects/hip-tests/samples/0_Intro/module_api/launchKernelHcc.cpp b/projects/hip-tests/samples/0_Intro/module_api/launchKernelHcc.cpp index 39d3bd31d2..464f6d8851 100644 --- a/projects/hip-tests/samples/0_Intro/module_api/launchKernelHcc.cpp +++ b/projects/hip-tests/samples/0_Intro/module_api/launchKernelHcc.cpp @@ -69,9 +69,6 @@ int main() { HIP_CHECK(hipModuleLoad(&Module, fileName)); HIP_CHECK(hipModuleGetFunction(&Function, Module, kernel_name)); - uint32_t len = LEN; - uint32_t one = 1; - struct { void* _Ad; void* _Bd; diff --git a/projects/hip-tests/samples/1_Utils/hipBusBandwidth/ResultDatabase.cpp b/projects/hip-tests/samples/1_Utils/hipBusBandwidth/ResultDatabase.cpp index 581f742834..e094f70d07 100644 --- a/projects/hip-tests/samples/1_Utils/hipBusBandwidth/ResultDatabase.cpp +++ b/projects/hip-tests/samples/1_Utils/hipBusBandwidth/ResultDatabase.cpp @@ -401,7 +401,6 @@ void ResultDatabase::DumpCsv(string fileName) { // **************************************************************************** bool ResultDatabase::IsFileEmpty(string fileName) { - bool fileEmpty; ifstream file(fileName.c_str()); @@ -409,6 +408,7 @@ bool ResultDatabase::IsFileEmpty(string fileName) { if (!file.good()) { return true; } else { + bool fileEmpty; fileEmpty = (bool)(file.peek() == ifstream::traits_type::eof()); file.close(); diff --git a/projects/hip-tests/samples/1_Utils/hipBusBandwidth/hipBusBandwidth.cpp b/projects/hip-tests/samples/1_Utils/hipBusBandwidth/hipBusBandwidth.cpp index 8032bd0a20..79d80ce317 100644 --- a/projects/hip-tests/samples/1_Utils/hipBusBandwidth/hipBusBandwidth.cpp +++ b/projects/hip-tests/samples/1_Utils/hipBusBandwidth/hipBusBandwidth.cpp @@ -252,9 +252,9 @@ void RunBenchmark_H2D(ResultDatabase& resultDB) { case MallocUnpinned: if (p_alignedhost) { - delete[] hostMem; - } else { free(hostMem); + } else { + delete[] hostMem; } break; diff --git a/projects/hip-tests/samples/1_Utils/hipCommander/ResultDatabase.cpp b/projects/hip-tests/samples/1_Utils/hipCommander/ResultDatabase.cpp index 1b1ee3a70d..51ced81fae 100644 --- a/projects/hip-tests/samples/1_Utils/hipCommander/ResultDatabase.cpp +++ b/projects/hip-tests/samples/1_Utils/hipCommander/ResultDatabase.cpp @@ -393,7 +393,6 @@ void ResultDatabase::DumpCsv(string fileName) { // **************************************************************************** bool ResultDatabase::IsFileEmpty(string fileName) { - bool fileEmpty; ifstream file(fileName.c_str()); @@ -401,6 +400,7 @@ bool ResultDatabase::IsFileEmpty(string fileName) { if (!file.good()) { return true; } else { + bool fileEmpty; fileEmpty = (bool)(file.peek() == ifstream::traits_type::eof()); file.close(); diff --git a/projects/hip-tests/samples/1_Utils/hipCommander/hipCommander.cpp b/projects/hip-tests/samples/1_Utils/hipCommander/hipCommander.cpp index 37eb0845b1..e95ecd82a1 100644 --- a/projects/hip-tests/samples/1_Utils/hipCommander/hipCommander.cpp +++ b/projects/hip-tests/samples/1_Utils/hipCommander/hipCommander.cpp @@ -282,7 +282,7 @@ class Command { // HCC optimizes away fully NULL kernel calls, so run one that is nearly null: class ModuleKernelCommand : public Command { public: - ModuleKernelCommand(CommandStream* cmdStream, const std::vector args) + ModuleKernelCommand(CommandStream* cmdStream, const std::vector& args) : Command(cmdStream, args), _stream(cmdStream->currentStream()) { hipModule_t module; HIPCHECK(hipModuleLoad(&module, FILENAME)); @@ -316,7 +316,7 @@ class ModuleKernelCommand : public Command { class KernelCommand : public Command { public: enum Type { Null, VectorAdd }; - KernelCommand(CommandStream* cmdStream, const std::vector args, Type kind) + KernelCommand(CommandStream* cmdStream, const std::vector& args, Type kind) : Command(cmdStream, args), _kind(kind), _stream(cmdStream->currentStream()){}; ~KernelCommand(){}; @@ -390,7 +390,7 @@ class CopyCommand : public Command { }; - void dealloc(void* p, MemType memType) { + static void dealloc(void* p, MemType memType) { if (memType == Device) { HIPCHECK(hipFree(p)); } else if (memType == PinnedHost) { @@ -433,7 +433,7 @@ class StreamSyncCommand : public Command { StreamSyncCommand(CommandStream* cmdStream, const std::vector& args) : Command(cmdStream, args), _stream(cmdStream->currentStream()){}; - const char* help() { return "synchronizes the current stream"; }; + static const char* help() { return "synchronizes the current stream"; }; void run() override { HIPCHECK(hipStreamSynchronize(_stream)); }; @@ -537,8 +537,8 @@ CopyCommand::CopyCommand(CommandStream* cmdStream, const std::vectorcurrentStream()) { + _stream(cmdStream->currentStream()), + _kind(kind) { switch (kind) { case hipMemcpyDeviceToHost: _srcType = Device; diff --git a/projects/hip-tests/samples/2_Cookbook/14_gpu_arch/gpuarch.cpp b/projects/hip-tests/samples/2_Cookbook/14_gpu_arch/gpuarch.cpp index ca312c0146..b4c8487b67 100644 --- a/projects/hip-tests/samples/2_Cookbook/14_gpu_arch/gpuarch.cpp +++ b/projects/hip-tests/samples/2_Cookbook/14_gpu_arch/gpuarch.cpp @@ -48,17 +48,14 @@ __global__ void incrementKernel(int32_t* in, int32_t* out, int32_t value, size_t int main() { int32_t incrementValue = 10; - // Host pointers - int32_t* hInput = nullptr; - int32_t* hOutput = nullptr; // Device pointers int32_t* dInput = nullptr; int32_t* dOutput = nullptr; size_t NBytes = SIZE * sizeof(int32_t); - - hInput = static_cast(malloc(NBytes)); - hOutput = static_cast(malloc(NBytes)); + // Host pointers + int32_t* hInput = static_cast(malloc(NBytes)); + int32_t* hOutput = static_cast(malloc(NBytes)); HIP_STATUS_CHECK(hipMalloc(&dInput, NBytes)); HIP_STATUS_CHECK(hipMalloc(&dOutput, NBytes)); @@ -95,4 +92,4 @@ int main() { std::cout << "success\n"; } return 0; -} \ No newline at end of file +} diff --git a/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/hipMain2.cpp b/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/hipMain2.cpp index a93eb141ac..a3c3f8f164 100644 --- a/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/hipMain2.cpp +++ b/projects/hip-tests/samples/2_Cookbook/15_static_library/device_functions/hipMain2.cpp @@ -58,8 +58,8 @@ void run_test2() { HIP_ASSERT(hipFree(A_d)); HIP_ASSERT(hipFree(B_d)); - free(A_h); - free(B_h); + delete [] A_h; + delete [] B_h; std::cout << "Test Passed!\n"; }