From aa658b7dbccdf262f46f63e1a574ff990599e1eb Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 12 Sep 2022 21:28:02 +0200 Subject: [PATCH] sys: Make sure constexpr variables don't get copied onto the stack --- lib/libimhex/include/hex/api/keybinding.hpp | 8 ++++---- lib/libimhex/include/hex/helpers/disassembler.hpp | 2 +- lib/libimhex/include/hex/helpers/net.hpp | 2 +- lib/libimhex/source/helpers/utils.cpp | 2 +- main/source/window/win_window.cpp | 10 +++++----- main/source/window/window.cpp | 2 +- .../builtin/source/content/data_processor_nodes.cpp | 2 +- .../builtin/source/content/helpers/pattern_drawer.cpp | 4 ++-- plugins/builtin/source/content/tools_entries.cpp | 4 ++-- plugins/builtin/source/content/views/view_about.cpp | 2 +- .../source/content/views/view_command_palette.cpp | 2 +- .../builtin/source/content/views/view_hex_editor.cpp | 10 +++++----- .../source/content/views/view_pattern_editor.cpp | 6 +++--- plugins/builtin/source/content/welcome_screen.cpp | 2 +- 14 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/libimhex/include/hex/api/keybinding.hpp b/lib/libimhex/include/hex/api/keybinding.hpp index 85e89b843..551e4206e 100644 --- a/lib/libimhex/include/hex/api/keybinding.hpp +++ b/lib/libimhex/include/hex/api/keybinding.hpp @@ -179,10 +179,10 @@ namespace hex { return result; } - static constexpr auto CTRL = Key(static_cast(0x1000'0000)); - static constexpr auto ALT = Key(static_cast(0x2000'0000)); - static constexpr auto SHIFT = Key(static_cast(0x4000'0000)); - static constexpr auto SUPER = Key(static_cast(0x8000'0000)); + constexpr static auto CTRL = Key(static_cast(0x1000'0000)); + constexpr static auto ALT = Key(static_cast(0x2000'0000)); + constexpr static auto SHIFT = Key(static_cast(0x4000'0000)); + constexpr static auto SUPER = Key(static_cast(0x8000'0000)); class ShortcutManager { public: diff --git a/lib/libimhex/include/hex/helpers/disassembler.hpp b/lib/libimhex/include/hex/helpers/disassembler.hpp index 27208e1d7..1d7f7ae55 100644 --- a/lib/libimhex/include/hex/helpers/disassembler.hpp +++ b/lib/libimhex/include/hex/helpers/disassembler.hpp @@ -31,7 +31,7 @@ namespace hex { class Disassembler { public: - static constexpr cs_arch toCapstoneArchitecture(Architecture architecture) { + constexpr static cs_arch toCapstoneArchitecture(Architecture architecture) { return static_cast(architecture); } diff --git a/lib/libimhex/include/hex/helpers/net.hpp b/lib/libimhex/include/hex/helpers/net.hpp index 138813389..c0a2d860d 100644 --- a/lib/libimhex/include/hex/helpers/net.hpp +++ b/lib/libimhex/include/hex/helpers/net.hpp @@ -35,7 +35,7 @@ namespace hex { Net(); ~Net(); - static constexpr u32 DefaultTimeout = 2'000; + constexpr static u32 DefaultTimeout = 2'000; std::future> getString(const std::string &url, u32 timeout = DefaultTimeout); std::future> getJson(const std::string &url, u32 timeout = DefaultTimeout); diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index a45cd8e37..e137466f3 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -233,7 +233,7 @@ namespace hex { } std::string toEngineeringString(double value) { - constexpr std::array Suffixes = { "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E" }; + constexpr static std::array Suffixes = { "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E" }; int8_t suffixIndex = 6; diff --git a/main/source/window/win_window.cpp b/main/source/window/win_window.cpp index 7a727a86f..8219336b6 100644 --- a/main/source/window/win_window.cpp +++ b/main/source/window/win_window.cpp @@ -106,11 +106,11 @@ namespace hex { return HTNOWHERE; } - constexpr auto RegionClient = 0b0000; - constexpr auto RegionLeft = 0b0001; - constexpr auto RegionRight = 0b0010; - constexpr auto RegionTop = 0b0100; - constexpr auto RegionBottom = 0b1000; + constexpr static auto RegionClient = 0b0000; + constexpr static auto RegionLeft = 0b0001; + constexpr static auto RegionRight = 0b0010; + constexpr static auto RegionTop = 0b0100; + constexpr static auto RegionBottom = 0b1000; const auto result = RegionLeft * (cursor.x < (window.left + border.x)) | diff --git a/main/source/window/window.cpp b/main/source/window/window.cpp index 7c5a329ae..2cc93ea91 100644 --- a/main/source/window/window.cpp +++ b/main/source/window/window.cpp @@ -129,7 +129,7 @@ namespace hex { glfwSetWindowTitle(this->m_window, title.c_str()); }); - constexpr auto CrashBackupFileName = "crash_backup.hexproj"; + constexpr static auto CrashBackupFileName = "crash_backup.hexproj"; EventManager::subscribe(this, [this, CrashBackupFileName](int) { ImGui::SaveIniSettingsToDisk(this->m_imguiSettingsPath.string().c_str()); diff --git a/plugins/builtin/source/content/data_processor_nodes.cpp b/plugins/builtin/source/content/data_processor_nodes.cpp index fa670e571..3ec72c4ab 100644 --- a/plugins/builtin/source/content/data_processor_nodes.cpp +++ b/plugins/builtin/source/content/data_processor_nodes.cpp @@ -32,7 +32,7 @@ namespace hex::plugin::builtin { NodeBuffer() : Node("hex.builtin.nodes.constants.buffer.header", { dp::Attribute(dp::Attribute::IOType::Out, dp::Attribute::Type::Buffer, "") }) { } void drawNode() override { - constexpr int StepSize = 1, FastStepSize = 10; + constexpr static int StepSize = 1, FastStepSize = 10; ImGui::PushItemWidth(100); ImGui::InputScalar("hex.builtin.nodes.constants.buffer.size"_lang, ImGuiDataType_U32, &this->m_size, &StepSize, &FastStepSize); diff --git a/plugins/builtin/source/content/helpers/pattern_drawer.cpp b/plugins/builtin/source/content/helpers/pattern_drawer.cpp index f4496e253..069ff450f 100644 --- a/plugins/builtin/source/content/helpers/pattern_drawer.cpp +++ b/plugins/builtin/source/content/helpers/pattern_drawer.cpp @@ -29,8 +29,8 @@ namespace hex { namespace { - constexpr auto DisplayEndDefault = 50u; - constexpr auto DisplayEndStep = 50u; + constexpr auto DisplayEndDefault = 50U; + constexpr auto DisplayEndStep = 50U; using namespace ::std::literals::string_literals; diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 3e5ff9307..2d9b98f6d 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -974,7 +974,7 @@ namespace hex::plugin::builtin { return; } - constexpr auto BufferSize = 0xFF'FFFF; + constexpr static auto BufferSize = 0xFF'FFFF; for (u64 partOffset = 0; partOffset < splitSize; partOffset += BufferSize) { partFile.write(file.readBytes(std::min(BufferSize, splitSize - partOffset))); partFile.flush(); @@ -1103,7 +1103,7 @@ namespace hex::plugin::builtin { return; } - constexpr auto BufferSize = 0xFF'FFFF; + constexpr static auto BufferSize = 0xFF'FFFF; auto inputSize = input.getSize(); for (u64 inputOffset = 0; inputOffset < inputSize; inputOffset += BufferSize) { output.write(input.readBytes(std::min(BufferSize, inputSize - inputOffset))); diff --git a/plugins/builtin/source/content/views/view_about.cpp b/plugins/builtin/source/content/views/view_about.cpp index 373b3d37b..92ac982fc 100644 --- a/plugins/builtin/source/content/views/view_about.cpp +++ b/plugins/builtin/source/content/views/view_about.cpp @@ -147,7 +147,7 @@ namespace hex::plugin::builtin { ImGui::TableSetupColumn("Type"); ImGui::TableSetupColumn("Paths"); - constexpr std::array, 8> PathTypes = { + constexpr static std::array, 8> PathTypes = { {{ "Resources", fs::ImHexPath::Resources }, { "Config", fs::ImHexPath::Config }, { "Magic", fs::ImHexPath::Magic }, diff --git a/plugins/builtin/source/content/views/view_command_palette.cpp b/plugins/builtin/source/content/views/view_command_palette.cpp index 343972d02..c36b4745b 100644 --- a/plugins/builtin/source/content/views/view_command_palette.cpp +++ b/plugins/builtin/source/content/views/view_command_palette.cpp @@ -75,7 +75,7 @@ namespace hex::plugin::builtin { } std::vector ViewCommandPalette::getCommandResults(const std::string &input) { - constexpr auto MatchCommand = [](const std::string &currCommand, const std::string &commandToMatch) -> std::pair { + constexpr static auto MatchCommand = [](const std::string &currCommand, const std::string &commandToMatch) -> std::pair { if (currCommand.empty()) { return { MatchType::InfoMatch, "" }; } else if (currCommand.size() <= commandToMatch.size()) { diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 6ea2dcc0a..0376ca8c9 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1002,14 +1002,14 @@ namespace hex::plugin::builtin { if ((ImGui::IsMouseDown(ImGuiMouseButton_Left) && this->m_selectionStart != this->m_selectionEnd)) { auto fractionPerLine = 1.0 / (this->m_visibleRowCount + 1); - if (y == u64(clipper.DisplayStart + 2)) { - if (i128(this->m_selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) <= (i64(clipper.DisplayStart + 2) * this->m_bytesPerRow)) { + if (y == u64(clipper.DisplayStart + 3)) { + if (i128(this->m_selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) <= (i64(clipper.DisplayStart + 3) * this->m_bytesPerRow)) { this->m_shouldScrollToSelection = false; - ImGui::SetScrollHereY(fractionPerLine * 4); + ImGui::SetScrollHereY(fractionPerLine * 5); } - } else if (y == u64(clipper.DisplayEnd - 2)) { - if (i128(this->m_selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) >= (i64(clipper.DisplayEnd - 2) * this->m_bytesPerRow)) { + } else if (y == u64(clipper.DisplayEnd - 3)) { + if (i128(this->m_selectionEnd - provider->getBaseAddress() - provider->getCurrentPageAddress()) >= (i64(clipper.DisplayEnd - 3) * this->m_bytesPerRow)) { this->m_shouldScrollToSelection = false; ImGui::SetScrollHereY(fractionPerLine * (this->m_visibleRowCount - 1)); } diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 2edb6c0bc..9cdc51e18 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -26,13 +26,13 @@ namespace hex::plugin::builtin { static bool initialized = false; static TextEditor::LanguageDefinition langDef; if (!initialized) { - static constexpr std::array keywords = { + constexpr static std::array keywords = { "using", "struct", "union", "enum", "bitfield", "be", "le", "if", "else", "false", "true", "this", "parent", "addressof", "sizeof", "$", "while", "for", "fn", "return", "break", "continue", "namespace", "in", "out" }; for (auto &k : keywords) langDef.mKeywords.insert(k); - static constexpr std::array builtInTypes = { + constexpr static std::array builtInTypes = { "u8", "u16", "u24", "u32", "u48", "u64", "u96", "u128", "s8", "s16", "s24", "s32", "s48", "s64", "s96", "s128", "float", "double", "char", "char16", "bool", "padding", "str", "auto" }; for (const auto name : builtInTypes) { @@ -524,7 +524,7 @@ namespace hex::plugin::builtin { ON_SCOPE_EXIT { ImGui::PopID(); }; ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x); - constexpr const char *Types[] = { "I", "F", "S", "B" }; + constexpr static const char *Types[] = { "I", "F", "S", "B" }; if (ImGui::BeginCombo("", Types[static_cast(type)])) { for (auto i = 0; i < IM_ARRAYSIZE(Types); i++) { if (ImGui::Selectable(Types[i])) diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index e298b93e0..7d94c3bc6 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -528,7 +528,7 @@ namespace hex::plugin::builtin { }); - constexpr auto CrashBackupFileName = "crash_backup.hexproj"; + constexpr static auto CrashBackupFileName = "crash_backup.hexproj"; for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) { if (auto filePath = std::fs::path(path) / CrashBackupFileName; fs::exists(filePath)) { s_safetyBackupPath = filePath;