From 96ea686eb6b3c4fe0c9034171647103d1dc2eb7c Mon Sep 17 00:00:00 2001 From: Herman Semenoff Date: Fri, 18 Oct 2024 08:06:11 +0000 Subject: [PATCH] Fixed return strict const iterator, replace to range-based loop C++17 and code refactor (#548) Signed-off-by: Herman Semenov Co-authored-by: georgemoralis --- .../libraries/kernel/threads/semaphore.cpp | 4 +-- src/core/memory.cpp | 6 ++-- .../frontend/control_flow_graph.cpp | 6 ++-- .../frontend/structured_control_flow.cpp | 30 +++++++++---------- src/shader_recompiler/ir/basic_block.h | 4 +-- src/video_core/buffer_cache/range_set.h | 9 +++--- .../renderer_vulkan/renderer_vulkan.h | 2 +- .../renderer_vulkan/vk_swapchain.cpp | 2 +- 8 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/core/libraries/kernel/threads/semaphore.cpp b/src/core/libraries/kernel/threads/semaphore.cpp index ff536802..59099c1b 100644 --- a/src/core/libraries/kernel/threads/semaphore.cpp +++ b/src/core/libraries/kernel/threads/semaphore.cpp @@ -59,7 +59,7 @@ public: for (auto it = wait_list.begin(); it != wait_list.end();) { auto* waiter = *it; if (waiter->need_count > token_count) { - it++; + ++it; continue; } it = wait_list.erase(it); @@ -148,7 +148,7 @@ public: // Find the first with priority less then us and insert right before it. auto it = wait_list.begin(); while (it != wait_list.end() && (*it)->priority > waiter->priority) { - it++; + ++it; } wait_list.insert(it, waiter); return it; diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 644f9389..471ba585 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -98,7 +98,7 @@ PAddr MemoryManager::Allocate(PAddr search_start, PAddr search_end, size_t size, return dmem_area->second.is_free && remaining_size >= size; }; while (!is_suitable() && dmem_area->second.GetEnd() <= search_end) { - dmem_area++; + ++dmem_area; } ASSERT_MSG(is_suitable(), "Unable to find free direct memory area: size = {:#x}", size); @@ -487,7 +487,7 @@ int MemoryManager::VirtualQuery(VAddr addr, int flags, auto it = FindVMA(addr); if (it->second.type == VMAType::Free && flags == 1) { - it++; + ++it; } if (it->second.type == VMAType::Free) { LOG_WARNING(Kernel_Vmm, "VirtualQuery on free memory region"); @@ -603,7 +603,7 @@ VAddr MemoryManager::SearchFree(VAddr virtual_addr, size_t size, u32 alignment) return remaining_size >= size; }; while (!is_suitable()) { - it++; + ++it; } return virtual_addr; } diff --git a/src/shader_recompiler/frontend/control_flow_graph.cpp b/src/shader_recompiler/frontend/control_flow_graph.cpp index 9d481d32..354196d3 100644 --- a/src/shader_recompiler/frontend/control_flow_graph.cpp +++ b/src/shader_recompiler/frontend/control_flow_graph.cpp @@ -163,10 +163,10 @@ void CFG::EmitDivergenceLabels() { } void CFG::EmitBlocks() { - for (auto it = labels.begin(); it != labels.end(); it++) { + for (auto it = labels.cbegin(); it != labels.cend(); ++it) { const Label start = *it; const auto next_it = std::next(it); - const bool is_last = next_it == labels.end(); + const bool is_last = (next_it == labels.cend()); if (is_last) { // Last label is special. return; @@ -193,7 +193,7 @@ void CFG::EmitBlocks() { void CFG::LinkBlocks() { const auto get_block = [this](u32 address) { auto it = blocks.find(address, Compare{}); - ASSERT_MSG(it != blocks.end() && it->begin == address); + ASSERT_MSG(it != blocks.cend() && it->begin == address); return &*it; }; diff --git a/src/shader_recompiler/frontend/structured_control_flow.cpp b/src/shader_recompiler/frontend/structured_control_flow.cpp index bf5ba6bc..11b40d07 100644 --- a/src/shader_recompiler/frontend/structured_control_flow.cpp +++ b/src/shader_recompiler/frontend/structured_control_flow.cpp @@ -144,32 +144,32 @@ std::string DumpExpr(const Statement* stmt) { [[maybe_unused]] std::string DumpTree(const Tree& tree, u32 indentation = 0) { std::string ret; std::string indent(indentation, ' '); - for (auto stmt = tree.begin(); stmt != tree.end(); ++stmt) { - switch (stmt->type) { + for (const auto& stmt : tree) { + switch (stmt.type) { case StatementType::Code: ret += fmt::format("{} Block {:04x} -> {:04x} (0x{:016x});\n", indent, - stmt->block->begin, stmt->block->end, - reinterpret_cast(stmt->block)); + stmt.block->begin, stmt.block->end, + reinterpret_cast(stmt.block)); break; case StatementType::Goto: - ret += fmt::format("{} if ({}) goto L{};\n", indent, DumpExpr(stmt->cond), - stmt->label->id); + ret += fmt::format("{} if ({}) goto L{};\n", indent, DumpExpr(stmt.cond), + stmt.label->id); break; case StatementType::Label: - ret += fmt::format("{}L{}:\n", indent, stmt->id); + ret += fmt::format("{}L{}:\n", indent, stmt.id); break; case StatementType::If: - ret += fmt::format("{} if ({}) {{\n", indent, DumpExpr(stmt->cond)); - ret += DumpTree(stmt->children, indentation + 4); + ret += fmt::format("{} if ({}) {{\n", indent, DumpExpr(stmt.cond)); + ret += DumpTree(stmt.children, indentation + 4); ret += fmt::format("{} }}\n", indent); break; case StatementType::Loop: ret += fmt::format("{} do {{\n", indent); - ret += DumpTree(stmt->children, indentation + 4); - ret += fmt::format("{} }} while ({});\n", indent, DumpExpr(stmt->cond)); + ret += DumpTree(stmt.children, indentation + 4); + ret += fmt::format("{} }} while ({});\n", indent, DumpExpr(stmt.cond)); break; case StatementType::Break: - ret += fmt::format("{} if ({}) break;\n", indent, DumpExpr(stmt->cond)); + ret += fmt::format("{} if ({}) break;\n", indent, DumpExpr(stmt.cond)); break; case StatementType::Return: ret += fmt::format("{} return;\n", indent); @@ -181,7 +181,7 @@ std::string DumpExpr(const Statement* stmt) { ret += fmt::format("{} unreachable;\n", indent); break; case StatementType::SetVariable: - ret += fmt::format("{} goto_L{} = {};\n", indent, stmt->id, DumpExpr(stmt->op)); + ret += fmt::format("{} goto_L{} = {};\n", indent, stmt.id, DumpExpr(stmt.op)); break; case StatementType::Function: case StatementType::Identity: @@ -625,8 +625,8 @@ private: node.data.block = current_block; }}; Tree& tree{parent.children}; - for (auto it = tree.begin(); it != tree.end(); ++it) { - Statement& stmt{*it}; + for (auto& child : tree) { + Statement& stmt{child}; switch (stmt.type) { case StatementType::Label: // Labels can be ignored diff --git a/src/shader_recompiler/ir/basic_block.h b/src/shader_recompiler/ir/basic_block.h index 11ae969b..74a7d2c5 100644 --- a/src/shader_recompiler/ir/basic_block.h +++ b/src/shader_recompiler/ir/basic_block.h @@ -109,13 +109,13 @@ public: return instructions.begin(); } [[nodiscard]] const_iterator begin() const { - return instructions.begin(); + return instructions.cbegin(); } [[nodiscard]] iterator end() { return instructions.end(); } [[nodiscard]] const_iterator end() const { - return instructions.end(); + return instructions.cend(); } [[nodiscard]] reverse_iterator rbegin() { diff --git a/src/video_core/buffer_cache/range_set.h b/src/video_core/buffer_cache/range_set.h index fe54aff8..2abf6e52 100644 --- a/src/video_core/buffer_cache/range_set.h +++ b/src/video_core/buffer_cache/range_set.h @@ -43,11 +43,10 @@ struct RangeSet { if (m_ranges_set.empty()) { return; } - auto it = m_ranges_set.begin(); - auto end_it = m_ranges_set.end(); - for (; it != end_it; it++) { - const VAddr inter_addr_end = it->upper(); - const VAddr inter_addr = it->lower(); + + for (const auto& set : m_ranges_set) { + const VAddr inter_addr_end = set.upper(); + const VAddr inter_addr = set.lower(); func(inter_addr, inter_addr_end); } } diff --git a/src/video_core/renderer_vulkan/renderer_vulkan.h b/src/video_core/renderer_vulkan/renderer_vulkan.h index 0dd86961..a663622f 100644 --- a/src/video_core/renderer_vulkan/renderer_vulkan.h +++ b/src/video_core/renderer_vulkan/renderer_vulkan.h @@ -70,7 +70,7 @@ public: bool IsVideoOutSurface(const AmdGpu::Liverpool::ColorBuffer& color_buffer) { return std::ranges::find_if(vo_buffers_addr, [&](VAddr vo_buffer) { return vo_buffer == color_buffer.Address(); - }) != vo_buffers_addr.end(); + }) != vo_buffers_addr.cend(); } bool ShowSplash(Frame* frame = nullptr); diff --git a/src/video_core/renderer_vulkan/vk_swapchain.cpp b/src/video_core/renderer_vulkan/vk_swapchain.cpp index 357cd504..86d7d506 100644 --- a/src/video_core/renderer_vulkan/vk_swapchain.cpp +++ b/src/video_core/renderer_vulkan/vk_swapchain.cpp @@ -47,7 +47,7 @@ void Swapchain::Create(u32 width_, u32 height_, vk::SurfaceKHR surface_) { std::find_if(modes.begin(), modes.end(), [&requested](vk::PresentModeKHR mode) { return mode == requested; }); - return it != modes.end(); + return it != modes.cend(); }; const bool has_mailbox = find_mode(vk::PresentModeKHR::eMailbox);