From adda20a6fe8d61dbce912d8c8760ec0bfbe6c0fd Mon Sep 17 00:00:00 2001 From: InoriRus Date: Wed, 25 May 2022 20:07:51 +1000 Subject: [PATCH] add support of clang 14.0.3 --- appveyor.yml | 2 +- source/.clang-tidy | 2 +- source/3rdparty/cpuinfo/CMakeLists.txt | 2 +- source/3rdparty/sdl2/CMakeLists.txt | 2 +- source/3rdparty/sqlite/CMakeLists.txt | 4 +- source/CMakeLists.txt | 14 +- .../include/Emulator/Graphics/AsyncJob.h | 13 +- source/emulator/src/Audio.cpp | 28 +- source/emulator/src/Elf.cpp | 6 +- source/emulator/src/Graphics/Graphics.cpp | 10 +- .../emulator/src/Graphics/GraphicsRender.cpp | 19 +- source/emulator/src/Graphics/GraphicsRun.cpp | 18 +- .../src/Graphics/Objects/GpuMemory.cpp | 7 +- source/emulator/src/Graphics/Shader.cpp | 9 +- source/emulator/src/Graphics/Tile.cpp | 12 +- source/emulator/src/Kyty.cpp | 3 +- source/emulator/src/Libs/LibC.cpp | 4 +- source/emulator/src/Libs/LibSaveData.cpp | 8 +- source/emulator/src/Libs/LibUserService.cpp | 4 +- source/emulator/src/Libs/Printf.cpp | 5 +- source/emulator/src/Log.cpp | 4 +- source/emulator/src/Network.cpp | 12 +- source/emulator/src/RuntimeLinker.cpp | 40 +- source/emulator/src/SymbolDatabase.cpp | 2 +- source/emulator/src/VirtualMemory.cpp | 3 +- source/include/Kyty/Core/ByteBuffer.h | 2 +- source/include/Kyty/Core/Common.h | 13 +- source/include/Kyty/Core/Database.h | 4 +- source/include/Kyty/Core/DateTime.h | 14 +- source/include/Kyty/Core/DbgAssert.h | 4 + source/include/Kyty/Core/Hashmap.h | 5 +- source/include/Kyty/Core/LinkList.h | 9 +- source/include/Kyty/Core/SimpleArray.h | 13 +- source/include/Kyty/Core/String.h | 4 +- source/include/Kyty/Core/Vector.h | 6 +- source/include/Kyty/Sys/SysSwapByteOrder.h | 6 +- .../launcher/src/ConfigurationEditDialog.cpp | 12 +- source/launcher/src/MainDialog.cpp | 3 +- source/launcher/ts/launcher_ru_RU.ts | 20 +- source/lib/Core/src/Compression.cpp | 4 +- source/lib/Core/src/Database.cpp | 8 +- source/lib/Core/src/DateTime.cpp | 27 +- source/lib/Core/src/Debug.cpp | 12 +- source/lib/Core/src/File.cpp | 5 +- source/lib/Core/src/MSpace.cpp | 9 +- source/lib/Core/src/MemoryAlloc.cpp | 8 +- source/lib/Core/src/String.cpp | 16 +- source/lib/Core/src/Threads.cpp | 13 +- source/lib/Scripts/src/Scripts.cpp | 10 +- source/lib/Sys/src/SysWindowsDbg.cpp | 3 + source/lib/Sys/src/SysWindowsFileIO.cpp | 13 +- source/unit_test/.clang-tidy | 2 +- .../src/core/UnitTestCoreDateTime.cpp | 413 ++++++++++++++++++ 53 files changed, 681 insertions(+), 210 deletions(-) create mode 100644 source/unit_test/src/core/UnitTestCoreDateTime.cpp diff --git a/appveyor.yml b/appveyor.yml index b16e9af..9a94746 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 0.1.0.build-{build} +version: 0.1.1.build-{build} image: Visual Studio 2019 environment: matrix: diff --git a/source/.clang-tidy b/source/.clang-tidy index e8ff46b..dc61d59 100644 --- a/source/.clang-tidy +++ b/source/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,google-*,llvm-*,-llvm-header-guard,misc-*,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-uppercase-literal-suffix,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,hicpp-*,-hicpp-vararg,-hicpp-no-array-decay,-hicpp-avoid-c-arrays,-hicpp-uppercase-literal-suffix,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-static-cast-downcast,-modernize-pass-by-value,-cert-dcl50-cpp,-misc-no-recursion,-cppcoreguidelines-avoid-non-const-global-variables,-performance-no-int-to-ptr' +Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,google-*,llvm-*,-llvm-header-guard,misc-*,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-uppercase-literal-suffix,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,hicpp-*,-hicpp-vararg,-hicpp-no-array-decay,-hicpp-avoid-c-arrays,-hicpp-uppercase-literal-suffix,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-static-cast-downcast,-modernize-pass-by-value,-cert-dcl50-cpp,-misc-no-recursion,-cppcoreguidelines-avoid-non-const-global-variables,-performance-no-int-to-ptr,-readability-identifier-length,-bugprone-easily-swappable-parameters,-bugprone-throw-keyword-missing' CheckOptions: - key: readability-function-cognitive-complexity.Threshold value: 50 diff --git a/source/3rdparty/cpuinfo/CMakeLists.txt b/source/3rdparty/cpuinfo/CMakeLists.txt index 353b375..d170e84 100644 --- a/source/3rdparty/cpuinfo/CMakeLists.txt +++ b/source/3rdparty/cpuinfo/CMakeLists.txt @@ -20,7 +20,7 @@ if (MINGW) if (CLANG) set_source_files_properties(${cpuinfo_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-variable -Wno-implicit-function-declaration") else() - set_source_files_properties(${cpuinfo_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-variable -Wno-implicit-function-declaration -Wno-format -Wno-format-extra-args") + set_source_files_properties(${cpuinfo_src} PROPERTIES COMPILE_FLAGS "-Wno-maybe-uninitialized -Wno-unused-variable -Wno-implicit-function-declaration -Wno-format -Wno-format-extra-args") endif() endif() diff --git a/source/3rdparty/sdl2/CMakeLists.txt b/source/3rdparty/sdl2/CMakeLists.txt index a521ab0..789add0 100644 --- a/source/3rdparty/sdl2/CMakeLists.txt +++ b/source/3rdparty/sdl2/CMakeLists.txt @@ -97,7 +97,7 @@ file(GLOB sdl2_src if (MINGW) if (CLANG) - set_source_files_properties(${sdl2_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-function -Wno-pragma-pack -Wno-shadow -Wno-unknown-pragmas -Wno-unused-local-typedefs -DHAVE_LIBC -DUSING_GENERATED_CONFIG_H -D_FLOAT_H___") + set_source_files_properties(${sdl2_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-but-set-variable -Wno-unused-function -Wno-pragma-pack -Wno-shadow -Wno-unknown-pragmas -Wno-unused-local-typedefs -DHAVE_LIBC -DUSING_GENERATED_CONFIG_H -D_FLOAT_H___") else() set_source_files_properties(${sdl2_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-function -Wno-shadow -Wno-unused-but-set-variable -Wno-unknown-pragmas -Wno-unused-local-typedefs -Wno-maybe-uninitialized -DHAVE_LIBC -DUSING_GENERATED_CONFIG_H") endif() diff --git a/source/3rdparty/sqlite/CMakeLists.txt b/source/3rdparty/sqlite/CMakeLists.txt index 8e3b11b..6f25b24 100644 --- a/source/3rdparty/sqlite/CMakeLists.txt +++ b/source/3rdparty/sqlite/CMakeLists.txt @@ -5,9 +5,9 @@ file(GLOB sqlite_src if (MINGW) if (CLANG) - set_source_files_properties(${sqlite_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-function -Wno-unknown-pragmas -Wno-unused-variable -Wno-sometimes-uninitialized") + set_source_files_properties(${sqlite_src} PROPERTIES COMPILE_FLAGS "-Wno-unused-but-set-variable -Wno-unused-function -Wno-unknown-pragmas -Wno-unused-variable -Wno-sometimes-uninitialized") else() - set_source_files_properties(${sqlite_src} PROPERTIES COMPILE_FLAGS "-Wno-return-local-addr -Wno-unused-but-set-variable -Wno-unused-function -Wno-unknown-pragmas -Wno-unused-variable -Wno-maybe-uninitialized -Wno-array-bounds") + set_source_files_properties(${sqlite_src} PROPERTIES COMPILE_FLAGS "-Wno-misleading-indentation -Wno-return-local-addr -Wno-unused-but-set-variable -Wno-unused-function -Wno-unknown-pragmas -Wno-unused-variable -Wno-maybe-uninitialized -Wno-array-bounds") endif() endif() diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 19e3d3c..6ff7cfe 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -82,7 +82,7 @@ if (KYTY_LINKER STREQUAL LD) set(KYTY_LD_OPTIONS "-Wl,--image-base=0x100000000000") endif() -project(Kyty${KYTY_PROJECT_NAME}${CMAKE_BUILD_TYPE}${KYTY_COMPILER} VERSION 0.1.0) +project(Kyty${KYTY_PROJECT_NAME}${CMAKE_BUILD_TYPE}${KYTY_COMPILER} VERSION 0.1.1) include(src_script.cmake) @@ -119,20 +119,20 @@ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0.0) list(APPEND KYTY_IWYU emulator_obj core_obj - #math_obj - #scripts_obj - #sys_obj - launcher + math_obj + scripts_obj + sys_obj + #launcher unit_test ) list(APPEND KYTY_CLANG_TYDY emulator_obj - core_obj + #core_obj #math_obj #scripts_obj #sys_obj launcher - unit_test + #unit_test ) endif() diff --git a/source/emulator/include/Emulator/Graphics/AsyncJob.h b/source/emulator/include/Emulator/Graphics/AsyncJob.h index 38b2d9c..2e4be68 100644 --- a/source/emulator/include/Emulator/Graphics/AsyncJob.h +++ b/source/emulator/include/Emulator/Graphics/AsyncJob.h @@ -15,7 +15,7 @@ class AsyncJob { public: using func_t = std::function; - explicit AsyncJob(const char* name): m_name(name) { m_thread = new Core::Thread(ThreadRun, this); } + explicit AsyncJob(const char* name): m_name(name), m_thread(new Core::Thread(ThreadRun, this)) {} virtual ~AsyncJob() { EXIT_IF(m_thread == nullptr); @@ -52,12 +52,11 @@ private: Core::Mutex m_mutex; Core::CondVar m_cond_var1; Core::CondVar m_cond_var2; - Core::Thread* m_thread = nullptr; - const char* m_name = nullptr; - - func_t m_func = nullptr; - void* m_arg = nullptr; - bool m_need_exit = false; + const char* m_name = nullptr; + func_t m_func = nullptr; + void* m_arg = nullptr; + bool m_need_exit = false; + Core::Thread* m_thread = nullptr; static void ThreadRun(void* data) { diff --git a/source/emulator/src/Audio.cpp b/source/emulator/src/Audio.cpp index 6a22cad..911421e 100644 --- a/source/emulator/src/Audio.cpp +++ b/source/emulator/src/Audio.cpp @@ -41,7 +41,7 @@ public: private: Id() = default; - static Id Invalid() { return Id(); } + static Id Invalid() { return {}; } static Id Create(int audio_id) { Id r; @@ -710,15 +710,15 @@ static void draw_fake_frame(uint32_t width, uint32_t height, void* data, float l { constexpr int STRIPS_NUM = 5; - uint32_t luma_width = width; - uint32_t luma_height = height; - uint32_t chroma_width = luma_width / 2; - uint32_t chroma_height = luma_height / 2; - auto* buffer = static_cast(data); - auto* luma = buffer; - auto* chroma = buffer + luma_width * luma_height; - uint32_t luma_strip_size = luma_height / STRIPS_NUM; - uint32_t chroma_strip_size = chroma_height / STRIPS_NUM; + size_t luma_width = width; + size_t luma_height = height; + size_t chroma_width = luma_width / 2; + size_t chroma_height = luma_height / 2; + auto* buffer = static_cast(data); + auto* luma = buffer; + auto* chroma = buffer + luma_width * luma_height; + size_t luma_strip_size = luma_height / STRIPS_NUM; + size_t chroma_strip_size = chroma_height / STRIPS_NUM; uint8_t color[STRIPS_NUM][3] = {}; @@ -728,9 +728,9 @@ static void draw_fake_frame(uint32_t width, uint32_t height, void* data, float l rgb_to_yuv(0, 0, 0, &color[3][0], &color[3][1], &color[3][2]); rgb_to_yuv(l, l, l, &color[4][0], &color[4][1], &color[4][2]); - for (uint32_t y = 0; y < luma_strip_size; y++) + for (size_t y = 0; y < luma_strip_size; y++) { - for (uint32_t x = 0; x < luma_width; x++) + for (size_t x = 0; x < luma_width; x++) { for (int si = 0; si < STRIPS_NUM; si++) { @@ -738,9 +738,9 @@ static void draw_fake_frame(uint32_t width, uint32_t height, void* data, float l } } } - for (uint32_t y = 0; y < chroma_strip_size; y++) + for (size_t y = 0; y < chroma_strip_size; y++) { - for (uint32_t x = 0; x < chroma_width; x++) + for (size_t x = 0; x < chroma_width; x++) { for (int si = 0; si < STRIPS_NUM; si++) { diff --git a/source/emulator/src/Elf.cpp b/source/emulator/src/Elf.cpp index 004dabb..2aa02cc 100644 --- a/source/emulator/src/Elf.cpp +++ b/source/emulator/src/Elf.cpp @@ -379,7 +379,8 @@ void Elf64::DbgDump(const String& folder) } char str[512]; - sprintf(str, "phdr_%03d", i); + int s = snprintf(str, 512, "phdr_%03d", i); + EXIT_NOT_IMPLEMENTED(s >= 512); Core::File fout; fout.Create(folder_str + str); @@ -406,7 +407,8 @@ void Elf64::DbgDump(const String& folder) } char str[512]; - sprintf(str, "shdr_%03d", i); + int s = snprintf(str, 512, "shdr_%03d", i); + EXIT_NOT_IMPLEMENTED(s >= 512); Core::File fout; fout.Create(folder_str + str); diff --git a/source/emulator/src/Graphics/Graphics.cpp b/source/emulator/src/Graphics/Graphics.cpp index bed2326..eb679f8 100644 --- a/source/emulator/src/Graphics/Graphics.cpp +++ b/source/emulator/src/Graphics/Graphics.cpp @@ -149,7 +149,7 @@ int KYTY_SYSV_ABI GraphicsSetPsShader(uint32_t* cmd, uint64_t size, const uint32 printf("\t ps_regs.m_cbShaderMask = %08" PRIx32 "\n", ps_regs[11]); cmd[0] = KYTY_PM4(size, Pm4::IT_NOP, Pm4::R_PS); - memcpy(&cmd[1], ps_regs, 12 * 4); + memcpy(&cmd[1], ps_regs, static_cast(12) * 4); } return OK; @@ -190,7 +190,7 @@ int KYTY_SYSV_ABI GraphicsSetPsShader350(uint32_t* cmd, uint64_t size, const uin printf("\t ps_regs.m_cbShaderMask = %08" PRIx32 "\n", ps_regs[11]); cmd[0] = KYTY_PM4(size, Pm4::IT_NOP, Pm4::R_PS); - memcpy(&cmd[1], ps_regs, 12 * 4); + memcpy(&cmd[1], ps_regs, static_cast(12) * 4); } // printf("ok\n"); @@ -222,7 +222,7 @@ int KYTY_SYSV_ABI GraphicsUpdatePsShader(uint32_t* cmd, uint64_t size, const uin printf("\t ps_regs.m_cbShaderMask = %08" PRIx32 "\n", ps_regs[11]); cmd[0] = KYTY_PM4(size, Pm4::IT_NOP, Pm4::R_PS_UPDATE); - memcpy(&cmd[1], ps_regs, 12 * 4); + memcpy(&cmd[1], ps_regs, static_cast(12) * 4); return OK; } @@ -251,7 +251,7 @@ int KYTY_SYSV_ABI GraphicsUpdatePsShader350(uint32_t* cmd, uint64_t size, const printf("\t ps_regs.m_cbShaderMask = %08" PRIx32 "\n", ps_regs[11]); cmd[0] = KYTY_PM4(size, Pm4::IT_NOP, Pm4::R_PS_UPDATE); - memcpy(&cmd[1], ps_regs, 12 * 4); + memcpy(&cmd[1], ps_regs, static_cast(12) * 4); return OK; } @@ -276,7 +276,7 @@ int KYTY_SYSV_ABI GraphicsSetCsShaderWithModifier(uint32_t* cmd, uint64_t size, cmd[0] = KYTY_PM4(size, Pm4::IT_NOP, Pm4::R_CS); cmd[1] = shader_modifier; - memcpy(&cmd[2], cs_regs, 7 * 4); + memcpy(&cmd[2], cs_regs, static_cast(7) * 4); return OK; } diff --git a/source/emulator/src/Graphics/GraphicsRender.cpp b/source/emulator/src/Graphics/GraphicsRender.cpp index 6ecfeb9..2293e4e 100644 --- a/source/emulator/src/Graphics/GraphicsRender.cpp +++ b/source/emulator/src/Graphics/GraphicsRender.cpp @@ -1,5 +1,6 @@ #include "Emulator/Graphics/GraphicsRender.h" +#include "Kyty/Core/Common.h" #include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/File.h" #include "Kyty/Core/Hash.h" @@ -49,6 +50,7 @@ struct VulkanDescriptor VkDescriptorSet descriptor_set = nullptr; }; +// Pack structs to guarantee the uniquess of object representation #pragma pack(push, 1) struct PipelineStencilStaticState @@ -1813,11 +1815,11 @@ static VulkanPipeline* CreatePipelineInternal(VkRenderPass render_pass, const Sh create_info.pNext = nullptr; create_info.flags = 0; - create_info.codeSize = vs_shader.Size() * 4; + create_info.codeSize = static_cast(vs_shader.Size()) * 4; create_info.pCode = vs_shader.GetDataConst(); vkCreateShaderModule(gctx->device, &create_info, nullptr, &vert_shader_module); - create_info.codeSize = ps_shader.Size() * 4; + create_info.codeSize = static_cast(ps_shader.Size()) * 4; create_info.pCode = ps_shader.GetDataConst(); vkCreateShaderModule(gctx->device, &create_info, nullptr, &frag_shader_module); @@ -2169,7 +2171,7 @@ static VulkanPipeline* CreatePipelineInternal(const ShaderComputeInputInfo* inpu create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; create_info.pNext = nullptr; create_info.flags = 0; - create_info.codeSize = cs_shader.Size() * 4; + create_info.codeSize = static_cast(cs_shader.Size()) * 4; create_info.pCode = cs_shader.GetDataConst(); vkCreateShaderModule(gctx->device, &create_info, nullptr, &comp_shader_module); @@ -2231,6 +2233,7 @@ static VulkanPipeline* CreatePipelineInternal(const ShaderComputeInputInfo* inpu bool PipelineStaticParameters::operator==(const PipelineStaticParameters& other) const { + // NOLINTNEXTLINE(bugprone-suspicious-memory-comparison,cert-exp42-c,cert-flp37-c) return (0 == memcmp(this, &other, sizeof(struct PipelineStaticParameters))); } @@ -4162,7 +4165,7 @@ static void PrepareGdsPointers(const ShaderGdsResources& gds_pointers, uint32_t* if (gds_pointers.pointers_num > 0) { - (*sgprs) += 4 * ((gds_pointers.pointers_num - 1) / 4 + 1); + (*sgprs) += static_cast(4 * ((gds_pointers.pointers_num - 1) / 4 + 1)); } } @@ -4351,11 +4354,11 @@ void GraphicsRenderDrawIndex(uint64_t submit_id, CommandBuffer* buffer, HW::Hard { case 0: index_type = VK_INDEX_TYPE_UINT16; - index_size = 2 * index_count; + index_size = 2 * static_cast(index_count); break; case 1: index_type = VK_INDEX_TYPE_UINT32; - index_size = 4 * index_count; + index_size = 4 * static_cast(index_count); break; default: EXIT("unknown index_type_and_size: %u\n", index_type_and_size); } @@ -4395,7 +4398,7 @@ void GraphicsRenderDrawIndex(uint64_t submit_id, CommandBuffer* buffer, HW::Hard { const auto& b = vs_input_info.buffers[i]; uint64_t addr = b.addr; - uint64_t size = b.stride * b.num_records; + uint64_t size = static_cast(b.stride) * b.num_records; auto* vertices = static_cast( GpuMemoryCreateObject(submit_id, g_render_ctx->GetGraphicCtx(), nullptr, addr, size, VertexBufferGpuObject())); @@ -4520,7 +4523,7 @@ void GraphicsRenderDrawIndexAuto(uint64_t submit_id, CommandBuffer* buffer, HW:: { const auto& b = vs_input_info.buffers[i]; uint64_t addr = b.addr; - uint64_t size = b.stride * b.num_records; + uint64_t size = static_cast(b.stride) * b.num_records; auto* vertices = static_cast( GpuMemoryCreateObject(submit_id, g_render_ctx->GetGraphicCtx(), nullptr, addr, size, VertexBufferGpuObject())); diff --git a/source/emulator/src/Graphics/GraphicsRun.cpp b/source/emulator/src/Graphics/GraphicsRun.cpp index 6ba409c..fdaf7bc 100644 --- a/source/emulator/src/Graphics/GraphicsRun.cpp +++ b/source/emulator/src/Graphics/GraphicsRun.cpp @@ -616,18 +616,18 @@ void CommandProcessor::WriteConstRam(uint32_t offset, const uint32_t* src, uint3 { Core::LockGuard lock(m_mutex); - memcpy(m_const_ram + offset / 4, src, dw_num * 4); + memcpy(m_const_ram + offset / 4, src, static_cast(dw_num) * 4); } void CommandProcessor::DumpConstRam(uint32_t* dst, uint32_t offset, uint32_t dw_num) { Core::LockGuard lock(m_mutex); - GpuMemoryCheckAccessViolation(reinterpret_cast(dst), dw_num * 4); + GpuMemoryCheckAccessViolation(reinterpret_cast(dst), static_cast(dw_num) * 4); - memcpy(dst, m_const_ram + offset / 4, dw_num * 4); + memcpy(dst, m_const_ram + offset / 4, static_cast(dw_num) * 4); - GraphicsRenderMemoryFlush(reinterpret_cast(dst), dw_num * 4); + GraphicsRenderMemoryFlush(reinterpret_cast(dst), static_cast(dw_num) * 4); } void CommandProcessor::WaitRegMem(uint32_t func, bool me, bool mem, const uint32_t* addr, uint32_t ref, uint32_t mask, uint32_t poll) @@ -651,11 +651,11 @@ void CommandProcessor::WriteData(uint32_t* dst, const uint32_t* src, uint32_t dw EXIT_NOT_IMPLEMENTED(write_control != 0x04100500); - GpuMemoryCheckAccessViolation(reinterpret_cast(dst), dw_num * 4); + GpuMemoryCheckAccessViolation(reinterpret_cast(dst), static_cast(dw_num) * 4); - memcpy(dst, src, dw_num * 4); + memcpy(dst, src, static_cast(dw_num) * 4); - GraphicsRenderMemoryFlush(reinterpret_cast(dst), dw_num * 4); + GraphicsRenderMemoryFlush(reinterpret_cast(dst), static_cast(dw_num) * 4); } void GraphicsRing::Submit(uint32_t* cmd_draw_buffer, uint32_t num_draw_dw, uint32_t* cmd_const_buffer, uint32_t num_const_dw, int handle, @@ -940,7 +940,7 @@ void CommandProcessor::Run(uint32_t* data, uint32_t num_dw) if (num_dw > 0) { - GraphicsRenderMemoryFree(reinterpret_cast(data), num_dw * 4); + GraphicsRenderMemoryFree(reinterpret_cast(data), static_cast(num_dw) * 4); } auto* cmd = data; @@ -1596,7 +1596,7 @@ KYTY_HW_CTX_PARSER(hw_ctx_set_aa_sample_control) HW::AaSampleControl r; - memcpy(r.locations, buffer, 16 * 4); + memcpy(r.locations, buffer, static_cast(16) * 4); r.centroid_priority = static_cast(buffer[18]) | (static_cast(buffer[19]) << 32u); diff --git a/source/emulator/src/Graphics/Objects/GpuMemory.cpp b/source/emulator/src/Graphics/Objects/GpuMemory.cpp index 7ddefe4..402449d 100644 --- a/source/emulator/src/Graphics/Objects/GpuMemory.cpp +++ b/source/emulator/src/Graphics/Objects/GpuMemory.cpp @@ -59,13 +59,10 @@ static OverlapType GetOverlapType(uint64_t vaddr_a, uint64_t size_a, uint64_t va return OverlapType::Equals; } - uint64_t vaddr_last_a = vaddr_a + size_a - 1; - uint64_t vaddr_last_b = vaddr_b + size_b - 1; - bool a_b = addr_in_block(vaddr_a, size_a, vaddr_b); - bool a_lb = addr_in_block(vaddr_a, size_a, vaddr_last_b); + bool a_lb = addr_in_block(vaddr_a, size_a, vaddr_b + size_b - 1); bool b_a = addr_in_block(vaddr_b, size_b, vaddr_a); - bool b_la = addr_in_block(vaddr_b, size_b, vaddr_last_a); + bool b_la = addr_in_block(vaddr_b, size_b, vaddr_a + size_a - 1); if (a_b && a_lb) { diff --git a/source/emulator/src/Graphics/Shader.cpp b/source/emulator/src/Graphics/Shader.cpp index 205170a..e93281b 100644 --- a/source/emulator/src/Graphics/Shader.cpp +++ b/source/emulator/src/Graphics/Shader.cpp @@ -2144,8 +2144,11 @@ static bool SpirvDisassemble(const uint32_t* src_binary, size_t src_binary_size, std::string disassembly; if (!core.Disassemble(src_binary, src_binary_size, &disassembly, - uint32_t(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER) | SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES | - SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT | SPV_BINARY_TO_TEXT_OPTION_COLOR)) + static_cast(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER) | + static_cast(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES) | + static_cast(SPV_BINARY_TO_TEXT_OPTION_COMMENT) | + static_cast(SPV_BINARY_TO_TEXT_OPTION_INDENT) | + static_cast(SPV_BINARY_TO_TEXT_OPTION_COLOR))) { *dst_disassembly = disassembly.c_str(); @@ -2242,7 +2245,7 @@ static const ShaderBinaryInfo* GetBinaryInfo(const uint32_t* code) if (code[0] == 0xBEEB03FF) { - return reinterpret_cast(code + (code[1] + 1) * 2); + return reinterpret_cast(code + static_cast(code[1] + 1) * 2); } return nullptr; diff --git a/source/emulator/src/Graphics/Tile.cpp b/source/emulator/src/Graphics/Tile.cpp index ef9a573..4932aa4 100644 --- a/source/emulator/src/Graphics/Tile.cpp +++ b/source/emulator/src/Graphics/Tile.cpp @@ -162,8 +162,8 @@ public: if (tile_bytes > 512) { - tile_split_slice = element_offset / (512 * 8); - element_offset %= (512 * 8); + tile_split_slice = element_offset / (static_cast(512) * 8); + element_offset %= (static_cast(512) * 8); tile_bytes = 512; } @@ -304,7 +304,7 @@ static void Detile32(const Tiler32* t, uint32_t width, uint32_t height, uint32_t uint32_t start_y = p->start_y; uint32_t width = p->width; uint32_t height = p->height; - uint32_t dst_pitch = p->dst_pitch; + uint64_t dst_pitch = p->dst_pitch; bool neo = p->neo; for (uint32_t y = start_y; y < height; y++) @@ -359,7 +359,7 @@ static void Detile32(const Tiler1d* t, uint32_t width, uint32_t height, uint32_t for (uint32_t y = 0; y < height; y++) { uint32_t x = 0; - uint64_t linear_offset = y * dst_pitch * 4; + uint64_t linear_offset = y * static_cast(dst_pitch) * 4; for (; x + 1 < width; x += 2) { @@ -382,7 +382,7 @@ static void Detile64(const Tiler1d* t, uint32_t width, uint32_t height, uint32_t for (uint32_t y = 0; y < height; y++) { uint32_t x = 0; - uint64_t linear_offset = y * dst_pitch * 8; + uint64_t linear_offset = y * static_cast(dst_pitch) * 8; for (; x + 1 < width; x += 2) { @@ -405,7 +405,7 @@ static void Detile128(const Tiler1d* t, uint32_t width, uint32_t height, uint32_ for (uint32_t y = 0; y < height; y++) { uint32_t x = 0; - uint64_t linear_offset = y * dst_pitch * 16; + uint64_t linear_offset = y * static_cast(dst_pitch) * 16; for (; x + 1 < width; x += 2) { diff --git a/source/emulator/src/Kyty.cpp b/source/emulator/src/Kyty.cpp index 2474272..e3ede4b 100644 --- a/source/emulator/src/Kyty.cpp +++ b/source/emulator/src/Kyty.cpp @@ -138,7 +138,8 @@ KYTY_SCRIPT_FUNC(kyty_init_func) print_system_info(); - atexit(kyty_close); + int ok = atexit(kyty_close); + EXIT_NOT_IMPLEMENTED(ok != 0); return 0; } diff --git a/source/emulator/src/Libs/LibC.cpp b/source/emulator/src/Libs/LibC.cpp index 3042fd3..23cbf6f 100644 --- a/source/emulator/src/Libs/LibC.cpp +++ b/source/emulator/src/Libs/LibC.cpp @@ -55,7 +55,9 @@ static KYTY_SYSV_ABI int atexit(void (*func)()) ::printf("func = %" PRIx64 "\n", reinterpret_cast(func)); - ::atexit(func); + int ok = ::atexit(func); + + EXIT_NOT_IMPLEMENTED(ok != 0); return 0; } diff --git a/source/emulator/src/Libs/LibSaveData.cpp b/source/emulator/src/Libs/LibSaveData.cpp index f2d47ab..bd79880 100644 --- a/source/emulator/src/Libs/LibSaveData.cpp +++ b/source/emulator/src/Libs/LibSaveData.cpp @@ -173,7 +173,9 @@ int KYTY_SYSV_ABI SaveDataMount(const SaveDataMount* mount, SaveDataMountResult* mount_result->mount_status = 1; } - snprintf(mount_result->mount_point.data, 16, "%s", mount_point.C_Str()); + int s = snprintf(mount_result->mount_point.data, 16, "%s", mount_point.C_Str()); + + EXIT_NOT_IMPLEMENTED(s >= 16); mount_result->required_blocks = 0; @@ -234,7 +236,9 @@ int KYTY_SYSV_ABI SaveDataMount2(const SaveDataMount2* mount, SaveDataMountResul mount_result->mount_status = 1; } - snprintf(mount_result->mount_point.data, 16, "%s", mount_point.C_Str()); + int s = snprintf(mount_result->mount_point.data, 16, "%s", mount_point.C_Str()); + + EXIT_NOT_IMPLEMENTED(s >= 16); mount_result->required_blocks = 0; diff --git a/source/emulator/src/Libs/LibUserService.cpp b/source/emulator/src/Libs/LibUserService.cpp index 344ef2b..dea677d 100644 --- a/source/emulator/src/Libs/LibUserService.cpp +++ b/source/emulator/src/Libs/LibUserService.cpp @@ -89,7 +89,9 @@ static KYTY_SYSV_ABI int UserServiceGetUserName(int user_id, char* name, size_t EXIT_NOT_IMPLEMENTED(user_id != 1); EXIT_NOT_IMPLEMENTED(size < 5); - snprintf(name, size, "%s", "Kyty"); + int s = snprintf(name, size, "%s", "Kyty"); + + EXIT_NOT_IMPLEMENTED(static_cast(s) >= size); return OK; } diff --git a/source/emulator/src/Libs/Printf.cpp b/source/emulator/src/Libs/Printf.cpp index a83d91c..5d73bcd 100644 --- a/source/emulator/src/Libs/Printf.cpp +++ b/source/emulator/src/Libs/Printf.cpp @@ -8,6 +8,7 @@ #include "Emulator/Libs/Printf.h" #include "Kyty/Core/Common.h" +#include "Kyty/Core/DbgAssert.h" #include "Kyty/Core/Vector.h" #include "Emulator/Common.h" @@ -15,7 +16,6 @@ #include #include -#include #ifdef KYTY_EMU_ENABLED @@ -856,7 +856,8 @@ static int kyty_printf_internal(bool sn, char* sn_s, size_t sn_n, const char* fo if (sn) { - snprintf(sn_s, sn_n, "%s", buffer.GetDataConst()); + int s = snprintf(sn_s, sn_n, "%s", buffer.GetDataConst()); + EXIT_NOT_IMPLEMENTED(static_cast(s) >= sn_n); } else { printf(FG_BRIGHT_MAGENTA "%s" DEFAULT, buffer.GetDataConst()); diff --git a/source/emulator/src/Log.cpp b/source/emulator/src/Log.cpp index 23f8996..4335989 100644 --- a/source/emulator/src/Log.cpp +++ b/source/emulator/src/Log.cpp @@ -11,10 +11,12 @@ #include "Emulator/Config.h" #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS -#include +#include // IWYU pragma: keep // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include #endif #ifdef KYTY_EMU_ENABLED diff --git a/source/emulator/src/Network.cpp b/source/emulator/src/Network.cpp index 3f1248a..f8a079f 100644 --- a/source/emulator/src/Network.cpp +++ b/source/emulator/src/Network.cpp @@ -55,7 +55,7 @@ public: private: Id() = default; - static Id Invalid() { return Id(); } + static Id Invalid() { return {}; } static Id Create(int net_id, Type type) { Id r; @@ -1226,7 +1226,10 @@ int KYTY_SYSV_ABI NpGetNpId(int user_id, NpId* np_id) EXIT_NOT_IMPLEMENTED(np_id == nullptr); - snprintf(np_id->handle.data, 16, "Kyty"); + int s = snprintf(np_id->handle.data, 16, "Kyty"); + + EXIT_NOT_IMPLEMENTED(s >= 16); + np_id->handle.term = 0; return OK; @@ -1240,7 +1243,10 @@ int KYTY_SYSV_ABI NpGetOnlineId(int user_id, NpOnlineId* online_id) EXIT_NOT_IMPLEMENTED(online_id == nullptr); - snprintf(online_id->data, 16, "Kyty"); + int s = snprintf(online_id->data, 16, "Kyty"); + + EXIT_NOT_IMPLEMENTED(s >= 16); + online_id->term = 0; return OK; diff --git a/source/emulator/src/RuntimeLinker.cpp b/source/emulator/src/RuntimeLinker.cpp index 4b41a65..1feeab3 100644 --- a/source/emulator/src/RuntimeLinker.cpp +++ b/source/emulator/src/RuntimeLinker.cpp @@ -171,7 +171,7 @@ static void KYTY_SYSV_ABI stackwalk_x86(uint64_t rbp, void** stack, int* depth, for (; i < d; i++) { - if (!(uintptr_t(frame) >= stack_addr && uintptr_t(frame) < stack_addr + stack_size)) + if (!(reinterpret_cast(frame) >= stack_addr && reinterpret_cast(frame) < stack_addr + stack_size)) { break; } @@ -765,22 +765,6 @@ void RuntimeLinker::SaveProgram(Program* program, const String& elf_name) } } -void RuntimeLinker::Clear() -{ - // EXIT_NOT_IMPLEMENTED(!Core::Thread::IsMainThread()); - - Core::LockGuard lock(m_mutex); - - for (auto* p: m_programs) - { - DeleteProgram(p); - } - m_programs.Clear(); - delete m_symbols; - m_symbols = nullptr; - m_relocated = false; -} - void RuntimeLinker::Execute() { KYTY_PROFILER_THREAD("Thread_Main"); @@ -797,7 +781,7 @@ void RuntimeLinker::Execute() // Reserve some stack. There may be jumps over guard page. To prevent segfault we need to expand committed area. size_t expanded_size = 0; - size_t expanded_max = 256 * 1024; + size_t expanded_max = static_cast(256) * 1024; while (expanded_size < expanded_max) { @@ -819,6 +803,22 @@ void RuntimeLinker::Execute() } } +void RuntimeLinker::Clear() +{ + // EXIT_NOT_IMPLEMENTED(!Core::Thread::IsMainThread()); + + Core::LockGuard lock(m_mutex); + + for (auto* p: m_programs) + { + DeleteProgram(p); + } + m_programs.Clear(); + delete m_symbols; + m_symbols = nullptr; + m_relocated = false; +} + void RuntimeLinker::Resolve(const String& name, SymbolType type, Program* program, SymbolRecord* out_info, bool* bind_self) { KYTY_PROFILER_FUNCTION(); @@ -1117,7 +1117,7 @@ void RuntimeLinker::LoadProgramToMemory(Program* program) EXIT_IF(phdr == nullptr || ehdr == nullptr); program->base_size = calc_base_size(ehdr, phdr); - program->base_size_aligned = (program->base_size & ~(uint64_t(0x1000) - 1)) + 0x1000; + program->base_size_aligned = (program->base_size & ~(static_cast(0x1000) - 1)) + 0x1000; uint64_t exception_handler_size = VirtualMemory::ExceptionHandler::GetSize(); uint64_t tls_handler_size = is_shared ? 0 : Jit::SafeCall::GetSize(); @@ -1360,7 +1360,7 @@ static void InstallRelocateHandler(Program* program) KYTY_PROFILER_FUNCTION(); uint64_t pltgot_vaddr = program->dynamic_info->pltgot_vaddr + program->base_vaddr; - uint64_t pltgot_size = 3 * 8; + uint64_t pltgot_size = static_cast(3) * 8; void** pltgot = reinterpret_cast(pltgot_vaddr); VirtualMemory::Mode old_mode {}; diff --git a/source/emulator/src/SymbolDatabase.cpp b/source/emulator/src/SymbolDatabase.cpp index 80d5708..b7605a8 100644 --- a/source/emulator/src/SymbolDatabase.cpp +++ b/source/emulator/src/SymbolDatabase.cpp @@ -64,7 +64,7 @@ void SymbolDatabase::DbgDump(const String& folder, const String& file_name) const SymbolRecord* SymbolDatabase::Find(const SymbolResolve& s) const { - auto index = m_map.Get(GenerateName(s), uint32_t(-1)); + auto index = m_map.Get(GenerateName(s), decltype(m_symbols)::INVALID_INDEX); if (!m_symbols.IndexValid(index)) { return nullptr; diff --git a/source/emulator/src/VirtualMemory.cpp b/source/emulator/src/VirtualMemory.cpp index 30d7a0e..8986b90 100644 --- a/source/emulator/src/VirtualMemory.cpp +++ b/source/emulator/src/VirtualMemory.cpp @@ -26,6 +26,7 @@ // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include //#include @@ -169,7 +170,7 @@ ExceptionHandler::~ExceptionHandler() uint64_t ExceptionHandler::GetSize() { - return (sizeof(ExceptionHandlerPrivate::HandlerInfo) & ~(uint64_t(0x1000) - 1)) + 0x1000; + return (sizeof(ExceptionHandlerPrivate::HandlerInfo) & ~(static_cast(0x1000) - 1)) + 0x1000; } bool ExceptionHandler::Install(uint64_t base_address, uint64_t handler_addr, uint64_t image_size, handler_func_t func) diff --git a/source/include/Kyty/Core/ByteBuffer.h b/source/include/Kyty/Core/ByteBuffer.h index eecd9ab..404f241 100644 --- a/source/include/Kyty/Core/ByteBuffer.h +++ b/source/include/Kyty/Core/ByteBuffer.h @@ -25,7 +25,7 @@ public: } } - ByteBuffer(const void* buf, uint32_t size): ByteBuffer(size, false) // @suppress("Ambiguous problem") + explicit ByteBuffer(const void* buf, uint32_t size): ByteBuffer(size, false) // @suppress("Ambiguous problem") { Byte* values_ptr = GetData(); std::memcpy(values_ptr, buf, size); diff --git a/source/include/Kyty/Core/Common.h b/source/include/Kyty/Core/Common.h index 7556939..3af4934 100644 --- a/source/include/Kyty/Core/Common.h +++ b/source/include/Kyty/Core/Common.h @@ -17,6 +17,7 @@ // IWYU pragma: begin_exports #include +#include #include #include // IWYU pragma: end_exports @@ -31,16 +32,16 @@ #define KYTY_CLASS_NO_COPY(name) \ public: \ - name(const name&) = delete; /* NOLINT(bugprone-macro-parentheses) */ \ - name& operator=(const name&) = delete; /* NOLINT(bugprone-macro-parentheses) */ \ - name(name&&) noexcept = delete; /* NOLINT(bugprone-macro-parentheses) */ \ + name(const name&) = delete; /* NOLINT(bugprone-macro-parentheses) */ \ + name& operator=(const name&) = delete; /* NOLINT(bugprone-macro-parentheses) */ \ + name(name&&) noexcept = delete; /* NOLINT(bugprone-macro-parentheses) */ \ name& operator=(name&&) noexcept = delete; /* NOLINT(bugprone-macro-parentheses) */ #define KYTY_CLASS_DEFAULT_COPY(name) \ public: \ - name(const name&) = default; /* NOLINT(bugprone-macro-parentheses) */ \ - name& operator=(const name&) = default; /* NOLINT(bugprone-macro-parentheses) */ \ - name(name&&) noexcept = default; /* NOLINT(bugprone-macro-parentheses) */ \ + name(const name&) = default; /* NOLINT(bugprone-macro-parentheses) */ \ + name& operator=(const name&) = default; /* NOLINT(bugprone-macro-parentheses) */ \ + name(name&&) noexcept = default; /* NOLINT(bugprone-macro-parentheses) */ \ name& operator=(name&&) noexcept = default; /* NOLINT(bugprone-macro-parentheses) */ #if (KYTY_COMPILER == KYTY_COMPILER_MINGW || KYTY_COMPILER == KYTY_COMPILER_GCC) diff --git a/source/include/Kyty/Core/Database.h b/source/include/Kyty/Core/Database.h index 797d663..711e4a7 100644 --- a/source/include/Kyty/Core/Database.h +++ b/source/include/Kyty/Core/Database.h @@ -13,7 +13,7 @@ struct ConnectionPrivate; void Init(); -class Statement +class Statement final { public: enum class State @@ -79,7 +79,7 @@ public: friend class Connection; protected: - virtual ~Statement(); + ~Statement(); explicit Statement(ConnectionPrivate* c); private: diff --git a/source/include/Kyty/Core/DateTime.h b/source/include/Kyty/Core/DateTime.h index dc29557..f01b0f7 100644 --- a/source/include/Kyty/Core/DateTime.h +++ b/source/include/Kyty/Core/DateTime.h @@ -33,7 +33,7 @@ public: explicit Date(jd_t d): m_jd(d) {} Date(const Date& d) = default; Date(Date&& d) noexcept = default; - Date(int year, int month, int day); // month in [1...12], day in [1...31] + explicit Date(int year, int month, int day); // month in [1...12], day in [1...31] ~Date() = default; static Date FromSystem(); @@ -70,7 +70,7 @@ public: bool operator>(const Date& other) const { return m_jd > other.m_jd; } bool operator>=(const Date& other) const { return m_jd >= other.m_jd; } - Date& operator=(const Date& other) = default; + Date& operator=(const Date& other) = default; Date& operator=(Date&& other) noexcept = default; Date operator+(int days) const { return Date(m_jd + days); } Date operator-(int days) const { return Date(m_jd - days); } @@ -102,7 +102,7 @@ public: } Time(const Time& t) = default; Time(Time&& t) noexcept = default; - Time(int hour24, int minute, int second, int msec = 0); + explicit Time(int hour24, int minute, int second, int msec = 0); ~Time() = default; static Time FromSystem(); @@ -133,7 +133,7 @@ public: bool operator>(const Time& other) const { return m_ms > other.m_ms; } bool operator>=(const Time& other) const { return m_ms >= other.m_ms; } - Time& operator=(const Time& other) = default; + Time& operator=(const Time& other) = default; Time& operator=(Time&& other) noexcept = default; Time operator+(int secs) const; Time operator-(int secs) const; @@ -150,8 +150,8 @@ public: DateTime() = default; explicit DateTime(const Date& d): m_date(d) {} explicit DateTime(const Time& t): m_time(t) {} - DateTime(const Date& d, const Time& t): m_date(d), m_time(t) {} - DateTime(const Time& t, const Date& d): m_date(d), m_time(t) {} + explicit DateTime(const Date& d, const Time& t): m_date(d), m_time(t) {} + explicit DateTime(const Time& t, const Date& d): m_date(d), m_time(t) {} DateTime(const DateTime& dt) = default; DateTime(DateTime&& dt) noexcept = default; ~DateTime() = default; @@ -178,7 +178,7 @@ public: String ToString(const char* format = "YYYY.MM.DD HH24:MI:SS", LanguageId lang_id = LanguageId::English) const; - DateTime& operator=(const DateTime& other) = default; + DateTime& operator=(const DateTime& other) = default; DateTime& operator=(DateTime&& other) noexcept = default; bool operator==(const DateTime& other) const { return m_date == other.m_date && m_time == other.m_time; } diff --git a/source/include/Kyty/Core/DbgAssert.h b/source/include/Kyty/Core/DbgAssert.h index c1e9b4e..56a9025 100644 --- a/source/include/Kyty/Core/DbgAssert.h +++ b/source/include/Kyty/Core/DbgAssert.h @@ -27,6 +27,10 @@ bool dbg_is_debugger_present(); } // namespace Kyty::Core +//#if KYTY_COMPILER == KYTY_COMPILER_MINGW +//#pragma GCC diagnostic ignored "-Warray-bounds" +//#endif + #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS #define ASSERT_HALT() \ (Kyty::Core::dbg_is_debugger_present() ? (::fflush(nullptr), *(reinterpret_cast(1)) = 0) : (std::_Exit(321), 1)) diff --git a/source/include/Kyty/Core/Hashmap.h b/source/include/Kyty/Core/Hashmap.h index 9be53df..82c7208 100644 --- a/source/include/Kyty/Core/Hashmap.h +++ b/source/include/Kyty/Core/Hashmap.h @@ -111,7 +111,7 @@ public: V& operator[](const K& key) { - V def; + V def {}; return *(static_cast(m_b.OperatorSquareBrackets(&key, &def))); } @@ -155,7 +155,8 @@ public: return false; } bool ok = true; - auto callback = [&ok, &other](const K* key, const V* value) { + auto callback = [&ok, &other](const K* key, const V* value) + { if (!(other.Find(*key) && other.Get(*key) == *value)) { ok = false; diff --git a/source/include/Kyty/Core/LinkList.h b/source/include/Kyty/Core/LinkList.h index 411040c..d893d38 100644 --- a/source/include/Kyty/Core/LinkList.h +++ b/source/include/Kyty/Core/LinkList.h @@ -27,7 +27,7 @@ using ListIndex = struct index = (list).Prev(index)) /* NOLINT(bugprone-macro-parentheses) */ template -class ListNode +class ListNode final { /*private:*/ @@ -35,7 +35,7 @@ class ListNode explicit ListNode(const T& v): m_value(v), m_next(this), m_prev(this) {} - virtual ~ListNode() { Remove(); } + /*virtual*/ ~ListNode() { Remove(); } void Remove() { @@ -263,7 +263,10 @@ public: if (this != &list) { Clear(); - FOR_LIST(index, list) { add(list[index]); } + FOR_LIST(index, list) + { + add(list[index]); + } } return *this; } diff --git a/source/include/Kyty/Core/SimpleArray.h b/source/include/Kyty/Core/SimpleArray.h index 2ea884f..f288e3e 100644 --- a/source/include/Kyty/Core/SimpleArray.h +++ b/source/include/Kyty/Core/SimpleArray.h @@ -38,7 +38,8 @@ public: using SortSwapFunc = void (*)(T*, int32_t, int32_t, void*); using SortCompareFunc = bool (*)(const T&, const T&); - static constexpr size_t SIZEOF_T = sizeof(T); // NOLINT(bugprone-sizeof-expression) + static constexpr size_t SIZEOF_T = sizeof(T); // NOLINT(bugprone-sizeof-expression) + static constexpr uint32_t INVALID_INDEX = static_cast(-1); SimpleArray() = default; @@ -191,7 +192,7 @@ public: return index; } } - return uint32_t(-1); + return INVALID_INDEX; } template @@ -204,7 +205,7 @@ public: return index; } } - return uint32_t(-1); + return INVALID_INDEX; } template @@ -217,7 +218,7 @@ public: return index; } } - return uint32_t(-1); + return INVALID_INDEX; } template @@ -242,13 +243,13 @@ public: return index; } } - return uint32_t(-1); + return INVALID_INDEX; } bool Remove(const T& t) { uint32_t index = Find(t); - if (index == uint32_t(-1)) + if (index == INVALID_INDEX) { return false; } diff --git a/source/include/Kyty/Core/String.h b/source/include/Kyty/Core/String.h index 8d3a783..fe59277 100644 --- a/source/include/Kyty/Core/String.h +++ b/source/include/Kyty/Core/String.h @@ -251,12 +251,12 @@ public: [[nodiscard]] const_iterator cend() const { return GetDataConst() + Size(); } // NOLINT(readability-identifier-naming) private: - String(const char32_t* array, uint32_t size); + explicit String(const char32_t* array, uint32_t size); // String(const uint8_t *utf8_str); // String(const char16_t *utf16_str); template - String(const T* str, OP&& read, uint32_t size = 0, bool with_size = false): m_data(new DataType) + explicit String(const T* str, OP&& read, uint32_t size = 0, bool with_size = false): m_data(new DataType) { if (str == nullptr) { diff --git a/source/include/Kyty/Core/Vector.h b/source/include/Kyty/Core/Vector.h index b0e3006..72ae1f9 100644 --- a/source/include/Kyty/Core/Vector.h +++ b/source/include/Kyty/Core/Vector.h @@ -18,6 +18,8 @@ public: using SortSwapFunc = typename A::SortSwapFunc; using SortCompareFunc = typename A::SortCompareFunc; + static constexpr uint32_t INVALID_INDEX = static_cast(-1); + VectorBase(): m_data(new DataType) {} VectorBase(const VectorType& src) { src.m_data->CopyPtr(&m_data, src.m_data); } @@ -162,12 +164,12 @@ public: return m_data->Find(t2, t3, op_eq); } - [[nodiscard]] bool Contains(const T& t) const { return Find(t) != uint32_t(-1); } + [[nodiscard]] bool Contains(const T& t) const { return Find(t) != INVALID_INDEX; } template bool Contains(const T2& t, OP&& op_eq) const { - return Find(t, op_eq) != uint32_t(-1); + return Find(t, op_eq) != INVALID_INDEX; } bool Remove(const T& t) diff --git a/source/include/Kyty/Sys/SysSwapByteOrder.h b/source/include/Kyty/Sys/SysSwapByteOrder.h index d264e2b..c347403 100644 --- a/source/include/Kyty/Sys/SysSwapByteOrder.h +++ b/source/include/Kyty/Sys/SysSwapByteOrder.h @@ -53,7 +53,7 @@ inline void SwapByteOrder(T& x) { if (std::is_signed_v) { - x = std::make_signed_t(SwapByteOrder16(std::make_unsigned_t(x))); + x = static_cast>(SwapByteOrder16(static_cast>(x))); } else { x = SwapByteOrder16(x); @@ -63,7 +63,7 @@ inline void SwapByteOrder(T& x) { if (std::is_signed_v) { - x = std::make_signed_t(SwapByteOrder32(std::make_unsigned_t(x))); + x = static_cast>(SwapByteOrder32(static_cast>(x))); } else { x = SwapByteOrder32(x); @@ -73,7 +73,7 @@ inline void SwapByteOrder(T& x) { if (std::is_signed_v) { - x = std::make_signed_t(SwapByteOrder64(std::make_unsigned_t(x))); + x = static_cast>(SwapByteOrder64(static_cast>(x))); } else { x = SwapByteOrder64(x); diff --git a/source/launcher/src/ConfigurationEditDialog.cpp b/source/launcher/src/ConfigurationEditDialog.cpp index 939f72d..de918b9 100644 --- a/source/launcher/src/ConfigurationEditDialog.cpp +++ b/source/launcher/src/ConfigurationEditDialog.cpp @@ -45,7 +45,7 @@ static void ChangeColor(QListWidgetItem* item) } ConfigurationEditDialog::ConfigurationEditDialog(Kyty::Configuration* info, ConfigurationListWidget* parent) - : QDialog(parent, Qt::WindowCloseButtonHint), m_ui(new Ui::ConfigurationEditDialog), m_parent(parent) + : QDialog(parent, Qt::WindowCloseButtonHint), m_ui(new Ui::ConfigurationEditDialog), m_info(info), m_parent(parent) { m_ui->setupUi(this); @@ -83,8 +83,6 @@ ConfigurationEditDialog::ConfigurationEditDialog(Kyty::Configuration* info, Conf restoreGeometry(g_last_geometry); - m_info = info; - Init(); scan_libs(); @@ -280,7 +278,7 @@ void ConfigurationEditDialog::scan_elfs() m_ui->listWidget_elfs->clear(); - int selected_num = 0; + // int selected_num = 0; for (const auto& elf: m_info->elfs) { @@ -299,7 +297,7 @@ void ConfigurationEditDialog::scan_elfs() item->setCheckState(selected ? Qt::Checked : Qt::Unchecked); } - selected_num += (selected ? 1 : 0); + // selected_num += (selected ? 1 : 0); } for (const auto& elf: elfs) @@ -323,7 +321,7 @@ void ConfigurationEditDialog::scan_libs() m_ui->listWidget_libs->clear(); - int selected_num = 0; + // int selected_num = 0; for (const auto& lib: m_info->libs) { @@ -342,7 +340,7 @@ void ConfigurationEditDialog::scan_libs() item->setCheckState(selected ? Qt::Checked : Qt::Unchecked); } - selected_num += (selected ? 1 : 0); + // selected_num += (selected ? 1 : 0); } for (const auto& lib: libs) diff --git a/source/launcher/src/MainDialog.cpp b/source/launcher/src/MainDialog.cpp index d70f43e..64446e7 100644 --- a/source/launcher/src/MainDialog.cpp +++ b/source/launcher/src/MainDialog.cpp @@ -27,10 +27,11 @@ #include "ui_main_dialog.h" -#include +#include // IWYU pragma: keep // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include class QWidget; diff --git a/source/launcher/ts/launcher_ru_RU.ts b/source/launcher/ts/launcher_ru_RU.ts index 4256e9a..e9cbf79 100644 --- a/source/launcher/ts/launcher_ru_RU.ts +++ b/source/launcher/ts/launcher_ru_RU.ts @@ -214,17 +214,17 @@ - + Save failed - + Please fill all mandatory fields - + Open Directory @@ -322,12 +322,12 @@ - + Error - + Can't create file: @@ -336,27 +336,27 @@ MainDialogPrivate - + Settings file: - + Emulator: - + Version: - + Error - + Can't find emulator diff --git a/source/lib/Core/src/Compression.cpp b/source/lib/Core/src/Compression.cpp index 0ea1076..1c8cfed 100644 --- a/source/lib/Core/src/Compression.cpp +++ b/source/lib/Core/src/Compression.cpp @@ -1235,7 +1235,7 @@ ByteBuffer ZipReader::ExtractFile(int file_index) if (file_index < 0 || IsFileDirectory(file_index)) { - return ByteBuffer(); + return {}; } ZipFileStat s {}; @@ -1243,7 +1243,7 @@ ByteBuffer ZipReader::ExtractFile(int file_index) if (s.m_uncomp_size == 0) { - return ByteBuffer(); + return {}; } void* ptr = mz_zip_reader_extract_to_heap(&m_p->zip, file_index, &size, 0); diff --git a/source/lib/Core/src/Database.cpp b/source/lib/Core/src/Database.cpp index c846938..b5090ab 100644 --- a/source/lib/Core/src/Database.cpp +++ b/source/lib/Core/src/Database.cpp @@ -28,7 +28,7 @@ using Math::Rand; struct StatementPrivate { - explicit StatementPrivate(ConnectionPrivate* p): p_stmt(nullptr), m_p(p), text(nullptr) {} + explicit StatementPrivate(ConnectionPrivate* p): m_p(p) {} void Prepare(const char* sql_text); [[nodiscard]] Statement::State Step() const; @@ -46,9 +46,9 @@ struct StatementPrivate void DbgTest() const; - sqlite3_stmt* p_stmt; - ConnectionPrivate* m_p; - const char* text; + sqlite3_stmt* p_stmt = nullptr; + ConnectionPrivate* m_p = nullptr; + const char* text = nullptr; }; struct ConnectionPrivate diff --git a/source/lib/Core/src/DateTime.cpp b/source/lib/Core/src/DateTime.cpp index 2783c06..177be0d 100644 --- a/source/lib/Core/src/DateTime.cpp +++ b/source/lib/Core/src/DateTime.cpp @@ -389,7 +389,7 @@ Date Date::FromMacros(const String& date) if (lst.Size() != 3) { - return Date(); + return {}; } static const char* month_str[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; @@ -406,7 +406,7 @@ Date Date::FromMacros(const String& date) if (month < 0) { - return Date(); + return {}; } return Date(lst.At(2).ToInt32(), month, lst.At(1).ToInt32()); @@ -673,7 +673,7 @@ Time Time::operator+(int secs) const if (IsInvalid()) { - return Time(); + return {}; } int r = m_ms + ms_secs; @@ -698,7 +698,7 @@ Time Time::operator-(int secs) const if (IsInvalid()) { - return Time(); + return {}; } int r = m_ms - ms_secs; @@ -768,7 +768,7 @@ DateTime DateTime::FromSystem() if (t.is_invalid) { - return DateTime(); + return {}; } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); @@ -781,7 +781,7 @@ DateTime DateTime::FromSystemUTC() if (t.is_invalid) { - return DateTime(); + return {}; } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); @@ -848,26 +848,29 @@ uint64_t DateTime::DistanceMs(const DateTime& other) const jd_t j1 = other.m_date.JulianDay(); jd_t j2 = m_date.JulianDay(); - return int64_t(j2 - j1 - 1) * int64_t(TIME_MS_IN_DAY) + (int64_t(TIME_MS_IN_DAY) - int64_t(other.m_time.MsecTotal())) + - int64_t(m_time.MsecTotal()); + return static_cast(j2 - j1 - 1) * static_cast(TIME_MS_IN_DAY) + + (static_cast(TIME_MS_IN_DAY) - static_cast(other.m_time.MsecTotal())) + + static_cast(m_time.MsecTotal()); } DateTime DateTime::FromSQLiteJulian(double jd) { double i = NAN; double f = modf(jd + 0.5, &i); - return DateTime(Date(jd_t(i)), Time(int(f * double(TIME_MS_IN_DAY)))); + return DateTime(Date(static_cast(i)), Time(static_cast(f * static_cast(TIME_MS_IN_DAY)))); } double DateTime::ToSQLiteJulian() const { - return -0.5 + double(GetDate().JulianDay()) + double(GetTime().MsecTotal()) / double(TIME_MS_IN_DAY); + return -0.5 + static_cast(GetDate().JulianDay()) + + static_cast(GetTime().MsecTotal()) / static_cast(TIME_MS_IN_DAY); } int64_t DateTime::ToSQLiteJulianInt64() const { - return -(static_cast(TIME_MS_IN_DAY)) / 2 + int64_t(GetDate().JulianDay()) * (static_cast(TIME_MS_IN_DAY)) + - int64_t(GetTime().MsecTotal()); + return -(static_cast(TIME_MS_IN_DAY)) / 2 + + static_cast(GetDate().JulianDay()) * (static_cast(TIME_MS_IN_DAY)) + + static_cast(GetTime().MsecTotal()); } DateTime DateTime::FromUnix(double seconds) diff --git a/source/lib/Core/src/Debug.cpp b/source/lib/Core/src/Debug.cpp index 3bfd71a..51037aa 100644 --- a/source/lib/Core/src/Debug.cpp +++ b/source/lib/Core/src/Debug.cpp @@ -16,9 +16,9 @@ #endif #ifdef KYTY_UNDECORATE -//String unDName(const String &mangled, +// String unDName(const String &mangled, // void* (*memget)(size_t), void (*memfree)(void*), -// unsigned short int flags); +// unsigned short int flags); #endif namespace Kyty::Core { @@ -199,7 +199,7 @@ void DebugMap::LoadCsv() #endif - //EXIT("1"); + // EXIT("1"); #endif } @@ -269,7 +269,7 @@ void DebugMap::LoadMsvcLink(const String& name, int mode) if (DBG_PRINTF) { printf("%016" PRIx64 "; %s; %s\n", static_cast(addr), func.utf8_str().GetData(), obj.utf8_str().GetData()); - fflush(stdout); + // int ok = fflush(stdout); } DebugFunctionInfo inf = {addr, 0, func.utf8_str(), obj.utf8_str()}; @@ -365,7 +365,7 @@ void DebugMap::LoadMsvcLldLink(const String& name, int mode) if (DBG_PRINTF) { printf("%016" PRIx64 "; %s; %s\n", static_cast(addr), func.utf8_str().GetData(), obj.utf8_str().GetData()); - fflush(stdout); + // fflush(stdout); } DebugFunctionInfo inf = {addr, 0, func.utf8_str(), obj.utf8_str()}; @@ -417,7 +417,7 @@ void DebugMap::LoadMsvcLldLink(const String& name, int mode) { m_p->data[i - 1].length = m_p->data[i].addr - m_p->data[i - 1].addr; } - + #ifdef KYTY_UNDECORATE #endif } diff --git a/source/lib/Core/src/File.cpp b/source/lib/Core/src/File.cpp index 25f01f7..664072a 100644 --- a/source/lib/Core/src/File.cpp +++ b/source/lib/Core/src/File.cpp @@ -13,6 +13,7 @@ // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS #ifdef CreateDirectory @@ -931,7 +932,7 @@ DateTime File::GetLastAccessTimeUTC(const String& name) if (t.is_invalid) { - return DateTime(); + return {}; } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); @@ -944,7 +945,7 @@ DateTime File::GetLastWriteTimeUTC(const String& name) if (t.is_invalid) { - return DateTime(); + return {}; } return DateTime(Date(t.Year, t.Month, t.Day), Time(t.Hour, t.Minute, t.Second, t.Milliseconds)); diff --git a/source/lib/Core/src/MSpace.cpp b/source/lib/Core/src/MSpace.cpp index 9550722..3a9883a 100644 --- a/source/lib/Core/src/MSpace.cpp +++ b/source/lib/Core/src/MSpace.cpp @@ -426,6 +426,13 @@ static bool MSpaceInternalInit(MSpaceContext& ctx, const char* name, void* base, ctx = MSpaceContext(); + int s = snprintf(ctx.name, sizeof(ctx.name), "%s", name); + + if (static_cast(s) >= sizeof(ctx.name)) + { + return false; + } + ctx.base = static_cast(base); ctx.capacity = (capacity / sizeof(MSpaceBlock)) - 2; @@ -438,8 +445,6 @@ static bool MSpaceInternalInit(MSpaceContext& ctx, const char* name, void* base, ctx.mutex = (thread_safe ? new Core::Mutex : nullptr); ctx.dbg_callback = dbg_callback; - snprintf(ctx.name, sizeof(ctx.name), "%s", name); - return true; } diff --git a/source/lib/Core/src/MemoryAlloc.cpp b/source/lib/Core/src/MemoryAlloc.cpp index ed020a2..d5259db 100644 --- a/source/lib/Core/src/MemoryAlloc.cpp +++ b/source/lib/Core/src/MemoryAlloc.cpp @@ -99,7 +99,11 @@ public: KYTY_CLASS_NO_COPY(MemLock); #ifdef MEM_TRACKER - [[nodiscard]] bool IsRecursive() const { return g_mem_depth > 1; } // NOLINT(readability-convert-member-functions-to-static) + // NOLINTNEXTLINE(readability-convert-member-functions-to-static) + [[nodiscard]] bool IsRecursive() const + { + return g_mem_depth > 1; + } #endif }; @@ -193,7 +197,7 @@ void core_memory_init() void* mem_alloc_check_alignment(void* ptr) { #ifdef MEM_ALLOC_ALIGNED - if ((uintptr_t(ptr) & uintptr_t(MEM_ALLOC_ALIGN - 1)) != 0u) + if ((reinterpret_cast(ptr) & static_cast(MEM_ALLOC_ALIGN - 1)) != 0u) { EXIT("mem alloc not aligned!\n"); } diff --git a/source/lib/Core/src/String.cpp b/source/lib/Core/src/String.cpp index 0bea601..bb8579a 100644 --- a/source/lib/Core/src/String.cpp +++ b/source/lib/Core/src/String.cpp @@ -1155,7 +1155,7 @@ String String::Mid(uint32_t first, uint32_t count) const if (first >= size) { - return String(); + return {}; } if (first + count > size) @@ -1225,7 +1225,7 @@ String String::TrimRight() const return Mid(0, size - i); } } - return String(); + return {}; } String String::TrimLeft() const @@ -1240,7 +1240,7 @@ String String::TrimLeft() const return Mid(i, size - i); } } - return String(); + return {}; } String String::Trim() const @@ -1902,7 +1902,7 @@ String String::DirectoryWithoutFilename() const if (index == STRING_INVALID_INDEX) { - return String(); + return {}; } return Left(index + 1); @@ -1938,7 +1938,7 @@ String String::ExtensionWithoutFilename() const if (index == STRING_INVALID_INDEX) { - return String(); + return {}; } return Mid(index); @@ -1966,7 +1966,7 @@ String String::RemoveLast(uint32_t num) const if (num >= size) { - return String(); + return {}; } return Left(size - num); @@ -1978,7 +1978,7 @@ String String::RemoveFirst(uint32_t num) const if (num >= size) { - return String(); + return {}; } return Right(size - num); @@ -2097,7 +2097,7 @@ String String::SortChars() const if (IsEmpty()) { - return String(); + return {}; } auto size = m_data->Size() - 1; Vector d(size); diff --git a/source/lib/Core/src/Threads.cpp b/source/lib/Core/src/Threads.cpp index 3293d27..e792156 100644 --- a/source/lib/Core/src/Threads.cpp +++ b/source/lib/Core/src/Threads.cpp @@ -23,7 +23,8 @@ //#define KYTY_DEBUG_LOCKS_TIMED #if !(defined(KYTY_DEBUG_LOCKS) || defined(KYTY_DEBUG_LOCKS_TIMED)) && defined(KYTY_WIN_CS) -#include +#include // IWYU pragma: keep +// IWYU pragma: no_include constexpr DWORD KYTY_CS_SPIN_COUNT = 4000; // IWYU pragma: no_include @@ -84,8 +85,14 @@ struct MutexPrivate std::recursive_timed_mutex m_mutex; #else #ifdef KYTY_WIN_CS - MutexPrivate() { InitializeCriticalSectionAndSpinCount(&m_cs, KYTY_CS_SPIN_COUNT); } - ~MutexPrivate() { DeleteCriticalSection(&m_cs); } + MutexPrivate() + { + InitializeCriticalSectionAndSpinCount(&m_cs, KYTY_CS_SPIN_COUNT); + } + ~MutexPrivate() + { + DeleteCriticalSection(&m_cs); + } KYTY_CLASS_NO_COPY(MutexPrivate); CRITICAL_SECTION m_cs {}; #else diff --git a/source/lib/Scripts/src/Scripts.cpp b/source/lib/Scripts/src/Scripts.cpp index e45c739..4c148b9 100644 --- a/source/lib/Scripts/src/Scripts.cpp +++ b/source/lib/Scripts/src/Scripts.cpp @@ -555,7 +555,7 @@ bool ScriptVar::IsInteger() const #else bool ScriptVar::IsInteger() const { - return m_p->is_double && double(int32_t(m_p->val_double)) == m_p->val_double; + return m_p->is_double && static_cast(static_cast(m_p->val_double)) == m_p->val_double; } #endif bool ScriptVar::IsString() const @@ -575,7 +575,7 @@ int64_t ScriptVar::ToInteger() const #else int32_t ScriptVar::ToInteger() const { - return IsInteger() ? int32_t(m_p->val_double) : 0; + return IsInteger() ? static_cast(m_p->val_double) : 0; } #endif double ScriptVar::ToDouble() const @@ -747,7 +747,7 @@ void PushInteger(int32_t i) { lua_init(); - PushDouble(double(i)); + PushDouble(static_cast(i)); } #endif @@ -781,7 +781,7 @@ ScriptVar ScriptTable::At(const String& key) const return p.GetValue(); } } - return ScriptVar(); + return {}; } ScriptVar ScriptTable::At(double key) const @@ -797,7 +797,7 @@ ScriptVar ScriptTable::At(double key) const return p.GetValue(); } } - return ScriptVar(); + return {}; } void ScriptTable::DbgPrint(int depth) const diff --git a/source/lib/Sys/src/SysWindowsDbg.cpp b/source/lib/Sys/src/SysWindowsDbg.cpp index 3c13c6d..3dcbb76 100644 --- a/source/lib/Sys/src/SysWindowsDbg.cpp +++ b/source/lib/Sys/src/SysWindowsDbg.cpp @@ -8,6 +8,9 @@ // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include +// IWYU pragma: no_include #if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS //#error "KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS" diff --git a/source/lib/Sys/src/SysWindowsFileIO.cpp b/source/lib/Sys/src/SysWindowsFileIO.cpp index e7c8e02..55d7039 100644 --- a/source/lib/Sys/src/SysWindowsFileIO.cpp +++ b/source/lib/Sys/src/SysWindowsFileIO.cpp @@ -12,12 +12,13 @@ #include "Kyty/Sys/SysFileIO.h" #include "Kyty/Sys/SysTimer.h" -#include +#include // IWYU pragma: keep // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include // IWYU pragma: no_include +// IWYU pragma: no_include namespace Kyty { @@ -188,8 +189,8 @@ sys_file_t* sys_file_create(const String& file_name) HANDLE h_file = nullptr; h_file = CreateFileW(reinterpret_cast(file_name.utf16_str().GetData()), - static_cast(GENERIC_READ) | static_cast(GENERIC_WRITE), 0, nullptr, CREATE_ALWAYS, - FILE_ATTRIBUTE_NORMAL, nullptr); + static_cast(GENERIC_READ) | static_cast(GENERIC_WRITE), 0, nullptr, CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, nullptr); ret->handle = h_file; ret->type = SYS_FILE_FILE; @@ -250,7 +251,7 @@ sys_file_t* sys_file_open_w(const String& file_name, sys_file_cache_type_t cache HANDLE h_file = nullptr; h_file = CreateFileW(reinterpret_cast(file_name.utf16_str().GetData()), GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, - get_cache_access_type(cache_type), nullptr); + get_cache_access_type(cache_type), nullptr); if (h_file == KYTY_INVALID_HANDLE_VALUE()) { @@ -271,8 +272,8 @@ sys_file_t* sys_file_open_rw(const String& file_name, sys_file_cache_type_t cach HANDLE h_file = nullptr; h_file = CreateFileW(reinterpret_cast(file_name.utf16_str().GetData()), - static_cast(GENERIC_READ) | static_cast(GENERIC_WRITE), 0, nullptr, OPEN_EXISTING, - get_cache_access_type(cache_type), nullptr); + static_cast(GENERIC_READ) | static_cast(GENERIC_WRITE), 0, nullptr, OPEN_EXISTING, + get_cache_access_type(cache_type), nullptr); if (h_file == KYTY_INVALID_HANDLE_VALUE()) { diff --git a/source/unit_test/.clang-tidy b/source/unit_test/.clang-tidy index 868fe3a..e5ddcc5 100644 --- a/source/unit_test/.clang-tidy +++ b/source/unit_test/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,google-*,llvm-*,-llvm-header-guard,misc-*,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-uppercase-literal-suffix,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,hicpp-*,-hicpp-vararg,-hicpp-no-array-decay,-hicpp-avoid-c-arrays,-hicpp-uppercase-literal-suffix,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-static-cast-downcast,-modernize-pass-by-value,-cert-dcl50-cpp,-cert-err58-cpp,-bugprone-undefined-memory-manipulation,-*-function-size,-misc-no-recursion,-cppcoreguidelines-avoid-non-const-global-variables,-readability-function-cognitive-complexity,-performance-no-int-to-ptr' +Checks: '-*,bugprone-*,cert-*,clang-analyzer-*,google-*,llvm-*,-llvm-header-guard,misc-*,modernize-*,-modernize-use-trailing-return-type,-modernize-avoid-c-arrays,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-uppercase-literal-suffix,cppcoreguidelines-*,-cppcoreguidelines-pro-type-vararg,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-cppcoreguidelines-pro-type-reinterpret-cast,-cppcoreguidelines-avoid-c-arrays,hicpp-*,-hicpp-vararg,-hicpp-no-array-decay,-hicpp-avoid-c-arrays,-hicpp-uppercase-literal-suffix,-cppcoreguidelines-pro-bounds-constant-array-index,-cppcoreguidelines-pro-type-union-access,-cppcoreguidelines-pro-type-static-cast-downcast,-modernize-pass-by-value,-cert-dcl50-cpp,-cert-err58-cpp,-bugprone-undefined-memory-manipulation,-*-function-size,-misc-no-recursion,-cppcoreguidelines-avoid-non-const-global-variables,-readability-function-cognitive-complexity,-performance-no-int-to-ptr,-readability-identifier-length' CheckOptions: - key: readability-redundant-access-specifiers.CheckFirstDeclaration value: 1 diff --git a/source/unit_test/src/core/UnitTestCoreDateTime.cpp b/source/unit_test/src/core/UnitTestCoreDateTime.cpp new file mode 100644 index 0000000..f2b48f8 --- /dev/null +++ b/source/unit_test/src/core/UnitTestCoreDateTime.cpp @@ -0,0 +1,413 @@ +#include "Kyty/Core/DateTime.h" +#include "Kyty/Core/Language.h" +#include "Kyty/Core/String.h" +#include "Kyty/UnitTest.h" + +#include +#include + +UT_BEGIN(CoreDateTime); + +namespace Language = Core::Language; +using Core::Date; +using Core::DateTime; +using Core::jd_t; +using Core::Time; + +// NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) +struct TestDateT +{ + int year; + int month; + int day; + bool Invalid; // NOLINT(readability-identifier-naming) + int Day; // NOLINT(readability-identifier-naming) + int Month; // NOLINT(readability-identifier-naming) + int Year; // NOLINT(readability-identifier-naming) + jd_t Jd; // NOLINT(readability-identifier-naming) + int DaysInMonth; // NOLINT(readability-identifier-naming) + bool LeapYear; // NOLINT(readability-identifier-naming) + int DaysInYear; // NOLINT(readability-identifier-naming) + int DayOfWeek; // NOLINT(readability-identifier-naming) + int DayOfYear; // NOLINT(readability-identifier-naming) + int QuarterOfYear; // NOLINT(readability-identifier-naming) +}; + +// NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) +struct TestTimeT +{ + int hour; + int minute; + int second; + int msec; + bool Invalid; // NOLINT(readability-identifier-naming) + int Hour24; // NOLINT(readability-identifier-naming) + int Hour12; // NOLINT(readability-identifier-naming) + bool IsAM; // NOLINT(readability-identifier-naming) + bool IsPM; // NOLINT(readability-identifier-naming) + int Minute; // NOLINT(readability-identifier-naming) + int Second; // NOLINT(readability-identifier-naming) + int Msec; // NOLINT(readability-identifier-naming) + int Ms; // NOLINT(readability-identifier-naming) +}; + +static TestDateT g_data_date[] = {{-100, 1, 21, false, 21, 1, -100, 1684921, 31, false, 365, 1, 21, 1}, + {-1, 2, 21, false, 21, 2, -1, 1721111, 29, true, 366, 1, 52, 1}, + {0, 3, 21, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {1, 4, 21, false, 21, 4, 1, 1721536, 30, false, 365, 6, 111, 2}, + {100, 5, 21, false, 21, 5, 100, 1757725, 31, false, 365, 5, 141, 2}, + {2000, 6, 30, false, 30, 6, 2000, 2451726, 30, true, 366, 5, 182, 2}, + {2004, 7, 21, false, 21, 7, 2004, 2453208, 31, true, 366, 3, 203, 3}, + {2100, 8, 21, false, 21, 8, 2100, 2488302, 31, false, 365, 6, 233, 3}, + {2004, 0, 21, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {2004, 13, 21, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {2004, -1, 21, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {2000, 6, 0, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {2000, 6, -1, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}, + {2000, 6, 31, true, 0, 0, 0, Core::DATE_JD_INVALID, 0, false, 0, 0, 0, 0}}; +//#define data_date_num (int)(sizeof(data_date) / sizeof(data_date[0])) + +static TestTimeT g_data_time[] = { + {10, 20, 30, 40, false, 10, 10, true, false, 20, 30, 40, 37230040}, + {20, 20, 30, 40, false, 20, 8, false, true, 20, 30, 40, 73230040}, + {13, 20, 30, 40, false, 13, 1, false, true, 20, 30, 40, 48030040}, + {12, 20, 30, 40, false, 12, 12, false, true, 20, 30, 40, 44430040}, + {-1, 20, 30, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {0, 20, 30, 40, false, 0, 12, true, false, 20, 30, 40, 1230040}, + {24, 20, 30, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, -1, 30, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, 0, 30, 40, false, 10, 10, true, false, 0, 30, 40, 36030040}, + {10, 60, 30, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, 20, -1, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, 20, 0, 40, false, 10, 10, true, false, 20, 0, 40, 37200040}, + {10, 20, 60, 40, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, 20, 30, -1, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, + {10, 20, 30, 0, false, 10, 10, true, false, 20, 30, 0, 37230000}, + {10, 20, 30, 1000, true, -1, -1, false, false, -1, -1, -1, Core::TIME_MS_INVALID}, +}; +//#define data_time_num (int)(sizeof(data_time) / sizeof(data_time[0])) + +static void test_date() +{ + Date invalid; + EXPECT_TRUE(invalid.IsInvalid()); + EXPECT_EQ(invalid.Day(), 0); + EXPECT_EQ(invalid.Month(), 0); + EXPECT_EQ(invalid.Year(), 0); + EXPECT_EQ(invalid.JulianDay(), (jd_t)Core::DATE_JD_INVALID); + EXPECT_EQ(invalid.DaysInMonth(), 0); + EXPECT_FALSE(invalid.IsLeapYear()); + + for (auto& i: g_data_date) + { + Date d1(i.year, i.month, i.day); + Date d2; + d2.Set(i.year, i.month, i.day); + Date d3(i.Jd); + Date d4; + d4.Set(i.Jd); + + EXPECT_EQ(d1, d2); + EXPECT_EQ(d1, d3); + EXPECT_EQ(d1, d4); + + EXPECT_EQ(d1.IsInvalid(), i.Invalid); + EXPECT_EQ(d1.Day(), i.Day); + EXPECT_EQ(d1.Month(), i.Month); + EXPECT_EQ(d1.Year(), i.Year); + EXPECT_EQ(d1.JulianDay(), i.Jd); + EXPECT_EQ(d1.DaysInMonth(), i.DaysInMonth); + EXPECT_EQ(d1.IsLeapYear(), i.LeapYear); + EXPECT_EQ(d1.DaysInYear(), i.DaysInYear); + EXPECT_EQ(d1.DayOfWeek(), i.DayOfWeek); + EXPECT_EQ(d1.DayOfYear(), i.DayOfYear); + EXPECT_EQ(d1.QuarterOfYear(), i.QuarterOfYear); + + int y = 0; + int m = 0; + int d = 0; + d4.Get(&y, &m, &d); + EXPECT_EQ(d, i.Day); + EXPECT_EQ(m, i.Month); + EXPECT_EQ(y, i.Year); + } + + EXPECT_EQ(Date::DaysInMonth(Core::MONTH_SEPTEMBER), 30); + EXPECT_TRUE(Date::IsLeapYear(2016)); + EXPECT_FALSE(Date::IsLeapYear(1981)); + EXPECT_EQ(Date::DaysInYear(2016), 366); + EXPECT_EQ(Date::DaysInYear(1981), 365); + EXPECT_TRUE(Date::IsValid(2016, 1, 28)); + EXPECT_FALSE(Date::IsValid(2016, 28, 1)); + + EXPECT_TRUE(Date(1981, 4, 21) == Date(1981, 4, 21)); + EXPECT_TRUE(Date(1981, 4, 21) != Date(1981, 4, 22)); + EXPECT_TRUE(Date(1981, 4, 21) < Date(1982, 4, 21)); + EXPECT_TRUE(Date(1981, 4, 21) <= Date(1981, 4, 21)); + EXPECT_TRUE(Date(1981, 4, 21) <= Date(1982, 4, 21)); + EXPECT_TRUE(Date(1981, 4, 21) > Date(1981, 4, 20)); + EXPECT_TRUE(Date(1981, 4, 21) >= Date(1981, 4, 21)); + EXPECT_TRUE(Date(1981, 4, 21) >= Date(1981, 4, 20)); + + EXPECT_EQ(Date(1981, 4, 21).ToString(), String(U"1981.04.21")); + + EXPECT_EQ(Date(1981, 4, 21).ToString("YYYY YYY YY Y Q MM MON MONTH D DAY DY DD DDD J"), + U"1981 981 81 1 2 04 Apr April 2 Tuesday Tue 21 111 2444716"); + + EXPECT_EQ(Date(1981, 4, 21).ToString("YYYY YYY YY Y Q MM MON MONTH D DAY DY DD DDD J", Language::GetId(U"ru")), + U"1981 981 81 1 2 04 Апр Апрель 2 Вторник Втн 21 111 2444716"); + + Date s(1981, 4, 21); + s += 20; + s -= 10; + Date ss; + ss = s; + EXPECT_EQ(ss, Date(1981, 5, 1)); + EXPECT_EQ(ss - 10, Date(1981, 4, 21)); + EXPECT_EQ(ss + 10, Date(1981, 5, 11)); + + Date s1 = Date::FromSystem(); + Date s2 = Date::FromSystemUTC(); + + EXPECT_TRUE(s1 < Date(2023, 1, 1)); + EXPECT_TRUE(s1 > Date(2021, 12, 31)); + EXPECT_TRUE(s2 < Date(2023, 1, 1)); + EXPECT_TRUE(s2 > Date(2021, 12, 31)); + + EXPECT_TRUE(!Date::FromMacros(U"" __DATE__).IsInvalid()); + EXPECT_EQ(Date::FromMacros(U"Dec 07 2017"), Date(2017, 12, 7)); + + // printf("sysdate (loc) = %s\n", s1.ToString().C_Str()); + // printf("sysdate (utc) = %s\n", s2.ToString().C_Str()); +} + +static void test_time() +{ + Time invalid; + EXPECT_TRUE(invalid.IsInvalid()); + + Time invalid2(24 * 3600 * 1000 + 1); + EXPECT_TRUE(invalid2.IsInvalid()); + + Time invalid3(-1); + EXPECT_TRUE(invalid3.IsInvalid()); + + for (auto& i: g_data_time) + { + Time t1(i.hour, i.minute, i.second, i.msec); + Time t2; + t2.Set(i.hour, i.minute, i.second, i.msec); + Time t3(i.Ms); + Time t4; + t4.Set(i.Ms); + + EXPECT_EQ(t1, t2); + EXPECT_EQ(t1, t3); + EXPECT_EQ(t1, t4); + + EXPECT_EQ(t1.IsInvalid(), i.Invalid); + EXPECT_EQ(t1.Hour24(), i.Hour24); + EXPECT_EQ(t1.Hour12(), i.Hour12); + EXPECT_EQ(t1.IsAM(), i.IsAM); + EXPECT_EQ(t1.IsPM(), i.IsPM); + EXPECT_EQ(t1.Minute(), i.Minute); + EXPECT_EQ(t1.Second(), i.Second); + EXPECT_EQ(t1.Msec(), i.Msec); + EXPECT_EQ(t1.MsecTotal(), i.Ms); + + int h = 0; + int m = 0; + int s = 0; + int ms = 0; + t4.Get(&h, &m, &s, &ms); + EXPECT_EQ(h, i.Hour24); + EXPECT_EQ(m, i.Minute); + EXPECT_EQ(s, i.Second); + EXPECT_EQ(ms, i.Msec); + } + + EXPECT_TRUE(Time::IsValid(14, 59, 25)); + EXPECT_FALSE(Time::IsValid(25, 59, 25)); + + EXPECT_TRUE(Time(14, 59, 25) == Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 24) != Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 26) > Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 26) >= Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 25) >= Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 24) < Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 24) <= Time(14, 59, 25)); + EXPECT_TRUE(Time(14, 59, 24) <= Time(14, 59, 24)); + + EXPECT_EQ(Time(1, 2, 3).ToString("HH HH12 HH24 MI SS SSSSS AM A.M."), U"01 01 01 02 03 03723 AM A.M."); + EXPECT_EQ(Time(13, 2, 3).ToString("HH HH12 HH24 MI SS SSSSS AM A.M."), U"01 01 13 02 03 46923 PM P.M."); + + // printf("%s\n", Time(1, 2, 3).ToString("HH HH12 HH24 MI SS SSSSS AM A.M.").C_Str()); + // printf("%s\n", Time(13, 2, 3).ToString("HH HH12 HH24 MI SS SSSSS AM A.M.").C_Str()); + + { + Time s(15, 17, 55); + s += 20; + s -= 10; + Time ss; + ss = s; + EXPECT_EQ(ss, Time(15, 18, 05)); + EXPECT_EQ(ss - 10, Time(15, 17, 55)); + EXPECT_EQ(ss + 10, Time(15, 18, 15)); + } + + { + Time s(23, 59, 55); + s += 20; + s -= 10; + Time ss; + ss = s; + EXPECT_EQ(ss, Time(0, 0, 5)); + EXPECT_EQ(ss - 10, Time(23, 59, 55)); + } + + { + Time s(0, 0, 5); + s -= 20; + s += 10; + Time ss; + ss = s; + EXPECT_EQ(ss, Time(23, 59, 55)); + EXPECT_EQ(ss + 10, Time(0, 0, 5)); + } + + Time t1 = Time::FromSystem(); + Time t2 = Time::FromSystemUTC(); + + // EXPECT_TRUE(t1 != t2); + + EXPECT_TRUE(t1 < Time(23, 59, 59)); + EXPECT_TRUE(t1 > Time(0, 0, 0)); + EXPECT_TRUE(t2 < Time(23, 59, 59)); + EXPECT_TRUE(t2 > Time(0, 0, 0)); + + // printf("systime (loc) = %s\n", t1.ToString().C_Str()); + // printf("systime (utc) = %s\n", t2.ToString().C_Str()); +} + +static void test_datetime() +{ + DateTime dt1; + DateTime dt2(Date(2016, 2, 1)); + DateTime dt3(Time(12, 4, 7)); + DateTime dt4(Date(2016, 2, 1), Time(12, 4, 7)); + DateTime dt5(Time(12, 4, 7), Date(2016, 2, 1)); + + EXPECT_TRUE(dt1.IsInvalid()); + EXPECT_TRUE(dt2.IsInvalid()); + EXPECT_TRUE(dt3.IsInvalid()); + EXPECT_TRUE(!dt4.IsInvalid()); + EXPECT_TRUE(!dt5.IsInvalid()); + + EXPECT_EQ(dt4, dt5); + + dt2.SetTime(dt4.GetTime()); + dt3.SetDate(dt5.GetDate()); + + EXPECT_EQ(dt4, dt2); + EXPECT_EQ(dt4, dt3); + + DateTime sdt1 = DateTime::FromSystem(); + DateTime sdt2 = DateTime::FromSystemUTC(); + + EXPECT_TRUE(!sdt1.IsInvalid()); + EXPECT_TRUE(!sdt2.IsInvalid()); + + // EXPECT_TRUE(sdt1 != sdt2); + + EXPECT_TRUE(sdt1.GetTime() < Time(23, 59, 59)); + EXPECT_TRUE(sdt1.GetTime() > Time(0, 0, 0)); + EXPECT_TRUE(sdt2.GetTime() < Time(23, 59, 59)); + EXPECT_TRUE(sdt2.GetTime() > Time(0, 0, 0)); + + EXPECT_TRUE(sdt1.GetDate() < Date(2023, 1, 1)); + EXPECT_TRUE(sdt1.GetDate() > Date(2021, 12, 31)); + EXPECT_TRUE(sdt2.GetDate() < Date(2023, 1, 1)); + EXPECT_TRUE(sdt2.GetDate() > Date(2021, 12, 31)); + + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) == DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) != DateTime(Date(2015, 12, 31), Time(14, 59, 24))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 26)) > DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 26)) > DateTime(Date(2015, 12, 30), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) >= DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 26)) >= DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) >= DateTime(Date(2015, 12, 30), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) < DateTime(Date(2015, 12, 31), Time(14, 59, 26))); + EXPECT_TRUE(DateTime(Date(2015, 12, 30), Time(14, 59, 25)) < DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) <= DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + EXPECT_TRUE(DateTime(Date(2015, 12, 31), Time(14, 59, 25)) <= DateTime(Date(2015, 12, 31), Time(14, 59, 26))); + EXPECT_TRUE(DateTime(Date(2015, 12, 30), Time(14, 59, 25)) <= DateTime(Date(2015, 12, 31), Time(14, 59, 25))); + + // printf("sysdate (loc) = %s(%d)\n", sdt1.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt1.GetTime().Msec()); + // printf("sysdate (utc) = %s(%d)\n", sdt2.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt2.GetTime().Msec()); + + auto sdt1_s = DateTime::FromSQLiteJulian(sdt1.ToSQLiteJulian()); + auto sdt2_s = DateTime::FromSQLiteJulian(sdt2.ToSQLiteJulian()); + + EXPECT_LE(abs(sdt1_s.GetTime().MsecTotal() - sdt1.GetTime().MsecTotal()), 2); + EXPECT_LE(abs(sdt2_s.GetTime().MsecTotal() - sdt2.GetTime().MsecTotal()), 2); + EXPECT_EQ(sdt1_s.GetDate(), sdt1.GetDate()); + EXPECT_EQ(sdt2_s.GetDate(), sdt2.GetDate()); + + // printf("sqldate (loc) = %s(%d)\n", sdt1_s.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt1_s.GetTime().Msec()); + // printf("sqldate (utc) = %s(%d)\n", sdt2_s.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt2_s.GetTime().Msec()); + + sdt1_s = DateTime::FromUnix(sdt1.ToUnix()); + sdt2_s = DateTime::FromUnix(sdt2.ToUnix()); + + EXPECT_LE(abs(sdt1_s.GetTime().MsecTotal() - sdt1.GetTime().MsecTotal()), 2); + EXPECT_LE(abs(sdt2_s.GetTime().MsecTotal() - sdt2.GetTime().MsecTotal()), 2); + EXPECT_EQ(sdt1_s.GetDate(), sdt1.GetDate()); + EXPECT_EQ(sdt2_s.GetDate(), sdt2.GetDate()); + + // printf("Unix (loc) = %s(%d)\n", sdt1_s.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt1_s.GetTime().Msec()); + // printf("Unix (utc) = %s(%d)\n", sdt2_s.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), sdt2_s.GetTime().Msec()); + + DateTime current(Date(2021, 6, 14), Time(4, 35, 0, 0)); + auto unix = current.ToUnix(); + EXPECT_LE(fabs(unix - 1623645300.0), 1.0); + // printf("Unix = %s(%d)\n", current.ToString("YYYY.MM.DD HH24:MI:SS").C_Str(), current.GetTime().Msec()); + // printf("Unix = %f\n", unix); +} + +void test_dist() +{ + DateTime t1 = DateTime::FromSystem(); + + DateTime t2 = t1; + + EXPECT_EQ(t1.DistanceMs(t2), 0U); + + t2.GetTime() += 5; + + EXPECT_EQ(t1.DistanceMs(t2), 5000U); + EXPECT_EQ(t2.DistanceMs(t1), 5000U); + + t2.GetDate() += 1; + + EXPECT_EQ(t1.DistanceMs(t2), Core::TIME_MS_IN_DAY + 5000U); + EXPECT_EQ(t2.DistanceMs(t1), Core::TIME_MS_IN_DAY + 5000U); + + t2.GetDate() += 1; + + EXPECT_EQ(t1.DistanceMs(t2), Core::TIME_MS_IN_DAY * 2 + 5000U); + EXPECT_EQ(t2.DistanceMs(t1), Core::TIME_MS_IN_DAY * 2 + 5000U); +} + +TEST(Core, DateTime) +{ + UT_MEM_CHECK_INIT(); + + test_date(); + test_time(); + test_datetime(); + test_dist(); + + UT_MEM_CHECK(); +} + +UT_END();