diff --git a/Source/.clang-tidy b/Source/.clang-tidy new file mode 100644 index 00000000..55e472a8 --- /dev/null +++ b/Source/.clang-tidy @@ -0,0 +1,37 @@ +Checks: > + -*, + bugprone-*, + -bugprone-branch-clone, + -bugprone-easily-swappable-parameters, + -bugprone-misplaced-widening-cast, + google-*, + -google-readability-braces-around-statements, + hicpp-*, + -hicpp-avoid-c-arrays, + -hicpp-braces-around-statements, + -hicpp-uppercase-literal-suffix, + -hicpp-use-auto, + -hicpp-use-equals-default, + misc-*, + -misc-const-correctness, + -misc-include-cleaner, + -misc-no-recursion, + -misc-non-private-member-variables-in-classes, + -misc-unused-parameters, + -misc-use-anonymous-namespace, + modernize-*, + -modernize-avoid-c-arrays, + -modernize-use-auto, + -modernize-use-nodiscard, + -modernize-use-trailing-return-type, + performance-*, + -performance-no-int-to-ptr, + readability-*, + -readability-braces-around-statements, + -readability-function-cognitive-complexity, + -readability-identifier-length, + -readability-implicit-bool-conversion, + -readability-magic-numbers, + -readability-uppercase-literal-suffix + +FormatStyle: file diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 23b3a896..39374330 100755 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -4,6 +4,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(GCC_min_version 10) project(dolphin-memory-engine) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + if(WIN32) set(DolphinProcessSrc DolphinProcess/Windows/WindowsDolphinProcess.cpp) set(ExeIconSrc Resources/exeicon.rc) diff --git a/Source/CheatEngineParser/CheatEngineParser.cpp b/Source/CheatEngineParser/CheatEngineParser.cpp index b8eadda4..b4f3088a 100644 --- a/Source/CheatEngineParser/CheatEngineParser.cpp +++ b/Source/CheatEngineParser/CheatEngineParser.cpp @@ -39,7 +39,6 @@ MemWatchTreeNode* CheatEngineParser::parseCTFile(QIODevice* CTFileIODevice, if (m_xmlReader->readNextStartElement()) { - std::string test = m_xmlReader->name().toString().toStdString(); if (m_xmlReader->name() == QString("CheatTable")) { MemWatchTreeNode* rootNode = new MemWatchTreeNode(nullptr); @@ -71,7 +70,6 @@ MemWatchTreeNode* CheatEngineParser::parseCheatTable(MemWatchTreeNode* rootNode, { if (m_xmlReader->readNextStartElement()) { - std::string test = m_xmlReader->name().toString().toStdString(); if (m_xmlReader->name() == QString("CheatEntries")) parseCheatEntries(rootNode, useDolphinPointer); } @@ -95,7 +93,6 @@ MemWatchTreeNode* CheatEngineParser::parseCheatEntries(MemWatchTreeNode* node, { if (m_xmlReader->readNextStartElement()) { - std::string test = m_xmlReader->name().toString().toStdString(); if (m_xmlReader->name() == QString("CheatEntry")) parseCheatEntry(node, useDolphinPointer); } @@ -362,7 +359,7 @@ void CheatEngineParser::verifyCheatEntryParsingErrors(cheatEntryParsingState sta } } -QString CheatEngineParser::formatImportedEntryBasicInfo(const MemWatchEntry* entry) const +QString CheatEngineParser::formatImportedEntryBasicInfo(const MemWatchEntry* const entry) { QString formatedEntry = ""; formatedEntry += "Label: " + entry->getLabel() + "\n"; diff --git a/Source/CheatEngineParser/CheatEngineParser.h b/Source/CheatEngineParser/CheatEngineParser.h index d92b955d..a607e75b 100644 --- a/Source/CheatEngineParser/CheatEngineParser.h +++ b/Source/CheatEngineParser/CheatEngineParser.h @@ -38,7 +38,7 @@ class CheatEngineParser void parseCheatEntry(MemWatchTreeNode* node, const bool useDolphinPointer); void verifyCheatEntryParsingErrors(cheatEntryParsingState state, MemWatchEntry* entry, bool isGroup, const bool useDolphinPointer); - QString formatImportedEntryBasicInfo(const MemWatchEntry* entry) const; + static QString formatImportedEntryBasicInfo(const MemWatchEntry* entry); u64 m_tableStartAddress = 0; QString m_errorMessages = ""; diff --git a/Source/Common/CommonTypes.h b/Source/Common/CommonTypes.h index 29495cbc..c9ea09dd 100755 --- a/Source/Common/CommonTypes.h +++ b/Source/Common/CommonTypes.h @@ -2,12 +2,12 @@ #include -typedef uint64_t u64; -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; +using u64 = uint64_t; +using u32 = uint32_t; +using u16 = uint16_t; +using u8 = uint8_t; -typedef int64_t s64; -typedef int32_t s32; -typedef int16_t s16; -typedef int8_t s8; +using s64 = int64_t; +using s32 = int32_t; +using s16 = int16_t; +using s8 = int8_t; diff --git a/Source/Common/CommonUtils.h b/Source/Common/CommonUtils.h index 36802cfc..cbe28efb 100644 --- a/Source/Common/CommonUtils.h +++ b/Source/Common/CommonUtils.h @@ -21,6 +21,19 @@ namespace Common { +template +To bit_cast(From from) // To be replaced with std::bit_cast when available +{ + static_assert(sizeof(From) == sizeof(To), "Type sizes do not match"); + union + { + From f; + To t; + } u; + u.f = from; + return u.t; +} + #ifdef _WIN32 inline u16 bSwap16(u16 data) { diff --git a/Source/Common/MemoryCommon.cpp b/Source/Common/MemoryCommon.cpp index 321ef3ca..aa2ba8ab 100644 --- a/Source/Common/MemoryCommon.cpp +++ b/Source/Common/MemoryCommon.cpp @@ -125,16 +125,17 @@ int getNbrBytesAlignmentForType(const MemType type) } char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLength, - const std::string inputString, const MemBase base, const MemType type, - const size_t length) + const std::string_view inputString, const MemBase base, + const MemType type, const size_t length) { - if (inputString.length() == 0) + if (inputString.empty()) { returnCode = MemOperationReturnCode::invalidInput; return nullptr; } - std::stringstream ss(inputString); + std::stringstream ss; + ss << inputString; switch (base) { case MemBase::base_octal: @@ -158,19 +159,20 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen u8 theByte = 0; if (base == MemBase::base_binary) { - unsigned long long input = 0; + u8 input{}; try { - input = std::bitset(inputString).to_ullong(); + input = static_cast( + std::bitset(inputString.data(), inputString.size()).to_ullong()); } - catch (std::invalid_argument) + catch (const std::invalid_argument&) { delete[] buffer; buffer = nullptr; returnCode = MemOperationReturnCode::invalidInput; return buffer; } - theByte = static_cast(input); + theByte = input; } else { @@ -196,19 +198,20 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen u16 theHalfword = 0; if (base == MemBase::base_binary) { - unsigned long long input = 0; + u16 input{}; try { - input = std::bitset(inputString).to_ullong(); + input = static_cast( + std::bitset(inputString.data(), inputString.size()).to_ullong()); } - catch (std::invalid_argument) + catch (const std::invalid_argument&) { delete[] buffer; buffer = nullptr; returnCode = MemOperationReturnCode::invalidInput; return buffer; } - theHalfword = static_cast(input); + theHalfword = input; } else { @@ -232,19 +235,20 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen u32 theWord = 0; if (base == MemBase::base_binary) { - unsigned long long input = 0; + u32 input{}; try { - input = std::bitset(inputString).to_ullong(); + input = static_cast( + std::bitset(inputString.data(), inputString.size()).to_ullong()); } - catch (std::invalid_argument) + catch (const std::invalid_argument&) { delete[] buffer; buffer = nullptr; returnCode = MemOperationReturnCode::invalidInput; return buffer; } - theWord = static_cast(input); + theWord = input; } else { @@ -270,10 +274,11 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen u32 theWord = 0; if (base == MemBase::base_binary) { - unsigned long long input = 0; + u32 input{}; try { - input = std::bitset(inputString).to_ullong(); + input = static_cast( + std::bitset(inputString.data(), inputString.size()).to_ullong()); } catch (const std::invalid_argument&) { @@ -282,7 +287,7 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen returnCode = MemOperationReturnCode::invalidInput; return buffer; } - theWord = static_cast(input); + theWord = input; } else { @@ -323,10 +328,11 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen u64 theDoubleWord = 0; if (base == MemBase::base_binary) { - unsigned long long input = 0; + u64 input{}; try { - input = std::bitset(inputString).to_ullong(); + input = static_cast( + std::bitset(inputString.data(), inputString.size()).to_ullong()); } catch (const std::invalid_argument&) { @@ -335,7 +341,7 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen returnCode = MemOperationReturnCode::invalidInput; return buffer; } - theDoubleWord = static_cast(input); + theDoubleWord = input; } else { @@ -378,7 +384,7 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen returnCode = MemOperationReturnCode::inputTooLong; return buffer; } - std::memcpy(buffer, inputString.c_str(), length); + std::memcpy(buffer, inputString.data(), length); actualLength = length; break; } @@ -419,7 +425,6 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen int index = 0; for (const auto& i : bytes) { - std::stringstream byteStream(i); ss >> std::hex; u8 theByte = 0; int theByteInt = 0; diff --git a/Source/Common/MemoryCommon.h b/Source/Common/MemoryCommon.h index 0d0bd38b..3c2fa68b 100644 --- a/Source/Common/MemoryCommon.h +++ b/Source/Common/MemoryCommon.h @@ -2,6 +2,7 @@ #include #include +#include #include "CommonTypes.h" @@ -58,8 +59,7 @@ size_t getSizeForType(const MemType type, const size_t length); bool shouldBeBSwappedForType(const MemType type); int getNbrBytesAlignmentForType(const MemType type); char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLength, - const std::string inputString, const MemBase base, const MemType type, - const size_t length); + std::string_view inputString, MemBase base, MemType type, size_t length); std::string formatMemoryToString(const char* memory, const MemType type, const size_t length, const MemBase base, const bool isUnsigned, const bool withBSwap = false); diff --git a/Source/DolphinProcess/IDolphinProcess.h b/Source/DolphinProcess/IDolphinProcess.h index 2609c02b..f2f819f3 100644 --- a/Source/DolphinProcess/IDolphinProcess.h +++ b/Source/DolphinProcess/IDolphinProcess.h @@ -10,7 +10,7 @@ namespace DolphinComm class IDolphinProcess { public: - virtual ~IDolphinProcess() {} + virtual ~IDolphinProcess() = default; virtual bool findPID() = 0; virtual bool obtainEmuRAMInformations() = 0; virtual bool readFromRAM(const u32 offset, char* buffer, const size_t size, diff --git a/Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp b/Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp index bae5a6d4..56afb504 100644 --- a/Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp +++ b/Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp @@ -36,7 +36,7 @@ bool LinuxDolphinProcess::obtainEmuRAMInformations() continue; bool foundDevShmDolphin = false; - for (auto str : lineData) + for (const std::string& str : lineData) { if (str.substr(0, 19) == "/dev/shm/dolphinmem" || str.substr(0, 20) == "/dev/shm/dolphin-emu") { @@ -163,7 +163,7 @@ bool LinuxDolphinProcess::readFromRAM(const u32 offset, char* buffer, const size local.iov_base = buffer; local.iov_len = size; - remote.iov_base = (void*)RAMAddress; + remote.iov_base = reinterpret_cast(RAMAddress); remote.iov_len = size; nread = process_vm_readv(m_PID, &local, 1, &remote, 1, 0); @@ -232,7 +232,7 @@ bool LinuxDolphinProcess::writeToRAM(const u32 offset, const char* buffer, const local.iov_base = bufferCopy; local.iov_len = size; - remote.iov_base = (void*)RAMAddress; + remote.iov_base = reinterpret_cast(RAMAddress); remote.iov_len = size; if (withBSwap) diff --git a/Source/DolphinProcess/Linux/LinuxDolphinProcess.h b/Source/DolphinProcess/Linux/LinuxDolphinProcess.h index 58026f65..0e2ed0a9 100644 --- a/Source/DolphinProcess/Linux/LinuxDolphinProcess.h +++ b/Source/DolphinProcess/Linux/LinuxDolphinProcess.h @@ -13,7 +13,7 @@ namespace DolphinComm class LinuxDolphinProcess : public IDolphinProcess { public: - LinuxDolphinProcess() {} + LinuxDolphinProcess() = default; bool findPID() override; bool obtainEmuRAMInformations() override; bool readFromRAM(const u32 offset, char* buffer, size_t size, const bool withBSwap) override; diff --git a/Source/DolphinProcess/Mac/MacDolphinProcess.h b/Source/DolphinProcess/Mac/MacDolphinProcess.h index 4b235254..a0f4e8c1 100644 --- a/Source/DolphinProcess/Mac/MacDolphinProcess.h +++ b/Source/DolphinProcess/Mac/MacDolphinProcess.h @@ -10,7 +10,7 @@ namespace DolphinComm class MacDolphinProcess : public IDolphinProcess { public: - MacDolphinProcess() {} + MacDolphinProcess() = default; bool findPID() override; bool obtainEmuRAMInformations() override; bool readFromRAM(const u32 offset, char* buffer, size_t size, const bool withBSwap) override; diff --git a/Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp b/Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp index 1e640d98..8564eb5c 100644 --- a/Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp +++ b/Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp @@ -173,7 +173,8 @@ bool WindowsDolphinProcess::readFromRAM(const u32 offset, char* buffer, const si } SIZE_T nread = 0; - bool bResult = ReadProcessMemory(m_hDolphin, (void*)RAMAddress, buffer, size, &nread); + const bool bResult{static_cast( + ReadProcessMemory(m_hDolphin, reinterpret_cast(RAMAddress), buffer, size, &nread))}; if (bResult && nread == size) { if (withBSwap) @@ -265,7 +266,8 @@ bool WindowsDolphinProcess::writeToRAM(const u32 offset, const char* buffer, con } } - bool bResult = WriteProcessMemory(m_hDolphin, (void*)RAMAddress, bufferCopy, size, &nread); + const bool bResult{static_cast(WriteProcessMemory( + m_hDolphin, reinterpret_cast(RAMAddress), bufferCopy, size, &nread))}; delete[] bufferCopy; return (bResult && nread == size); } diff --git a/Source/DolphinProcess/Windows/WindowsDolphinProcess.h b/Source/DolphinProcess/Windows/WindowsDolphinProcess.h index b12705ab..4c013a33 100644 --- a/Source/DolphinProcess/Windows/WindowsDolphinProcess.h +++ b/Source/DolphinProcess/Windows/WindowsDolphinProcess.h @@ -11,7 +11,7 @@ namespace DolphinComm class WindowsDolphinProcess : public IDolphinProcess { public: - WindowsDolphinProcess() {} + WindowsDolphinProcess() = default; bool findPID() override; bool obtainEmuRAMInformations() override; bool readFromRAM(const u32 offset, char* buffer, const size_t size, diff --git a/Source/GUI/MemCopy/DlgCopy.cpp b/Source/GUI/MemCopy/DlgCopy.cpp index 2b65a4f9..34cd215f 100644 --- a/Source/GUI/MemCopy/DlgCopy.cpp +++ b/Source/GUI/MemCopy/DlgCopy.cpp @@ -6,6 +6,8 @@ #include #include #include + +#include #include #include #include "../../Common/CommonUtils.h" @@ -154,30 +156,24 @@ bool DlgCopy::copyMemory() void DlgCopy::updateMemoryText() { - m_spnWatcherCopyOutput->setText(QString::fromStdString( - charToHexString(m_Data.data(), m_Data.size(), - (DlgCopy::ByteStringFormats)m_cmbViewerBytesSeparator->currentIndex()))); + m_spnWatcherCopyOutput->setText(QString::fromStdString(charToHexString( + m_Data.data(), m_Data.size(), + static_cast(m_cmbViewerBytesSeparator->currentIndex())))); } -bool DlgCopy::isHexString(std::string str) +bool DlgCopy::isHexString(std::string_view str) { if (str.length() > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) { str = str.substr(2); } - for (char c : str) - { - if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) - { - continue; - } - return false; - } - return true; + return std::ranges::all_of(str.cbegin(), str.cend(), [](const char c) { + return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'); + }); } -bool DlgCopy::hexStringToU32(std::string str, u32& output) +bool DlgCopy::hexStringToU32(const std::string_view str, u32& output) { // if (str.empty() || str.length() % 2 == 1) if (str.empty()) @@ -186,7 +182,8 @@ bool DlgCopy::hexStringToU32(std::string str, u32& output) if (!isHexString(str)) return false; - std::stringstream ss(str); + std::stringstream ss; + ss << str; ss >> std::hex; ss >> output; @@ -194,42 +191,36 @@ bool DlgCopy::hexStringToU32(std::string str, u32& output) return true; } -bool DlgCopy::isUnsignedIntegerString(std::string str) +bool DlgCopy::isUnsignedIntegerString(const std::string_view str) { - for (char c : str) - { - if (c >= '0' && c <= '9') - { - continue; - } - return false; - } - return true; + return std::ranges::all_of(str.cbegin(), str.cend(), + [](const char c) { return '0' <= c && c <= '9'; }); } -bool DlgCopy::uintStringToU32(std::string str, u32& output) +bool DlgCopy::uintStringToU32(const std::string_view str, u32& output) { if (!isUnsignedIntegerString(str) || str.empty() || str.length() > 10) return false; - u64 u = std::stoll(str); + const auto u{static_cast(std::stoll(std::string{str}))}; if (u > ULONG_MAX) return false; - output = (u32)u; + output = static_cast(u); return true; } -std::string DlgCopy::charToHexString(char* input, size_t count, DlgCopy::ByteStringFormats format) +std::string DlgCopy::charToHexString(const char* const input, const size_t count, + const DlgCopy::ByteStringFormats format) { std::stringstream ss; const char convert[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - std::string beforeAll = ""; - std::string beforeByte = ""; - std::string betweenBytes = ""; - std::string afterAll = ""; + std::string beforeAll; + std::string beforeByte; + std::string betweenBytes; + std::string afterAll; switch (format) { diff --git a/Source/GUI/MemCopy/DlgCopy.h b/Source/GUI/MemCopy/DlgCopy.h index 45499811..63bb4649 100644 --- a/Source/GUI/MemCopy/DlgCopy.h +++ b/Source/GUI/MemCopy/DlgCopy.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -30,11 +32,11 @@ class DlgCopy : public QDialog bool copyMemory(); void updateMemoryText(); - static bool hexStringToU32(std::string str, u32& output); - static bool isHexString(std::string str); - static bool isUnsignedIntegerString(std::string str); - static bool uintStringToU32(std::string str, u32& output); - static std::string charToHexString(char* input, size_t count, ByteStringFormats format); + static bool hexStringToU32(std::string_view str, u32& output); + static bool isHexString(std::string_view str); + static bool isUnsignedIntegerString(std::string_view str); + static bool uintStringToU32(std::string_view str, u32& output); + static std::string charToHexString(const char* input, size_t count, ByteStringFormats format); QLineEdit* m_spnWatcherCopyAddress; QLineEdit* m_spnWatcherCopySize; diff --git a/Source/GUI/MemScanner/ResultsListModel.cpp b/Source/GUI/MemScanner/ResultsListModel.cpp index 2d4b747f..4664652d 100644 --- a/Source/GUI/MemScanner/ResultsListModel.cpp +++ b/Source/GUI/MemScanner/ResultsListModel.cpp @@ -5,9 +5,7 @@ ResultsListModel::ResultsListModel(QObject* parent, MemScanner* scanner) { } -ResultsListModel::~ResultsListModel() -{ -} +ResultsListModel::~ResultsListModel() = default; int ResultsListModel::columnCount(const QModelIndex& parent) const { diff --git a/Source/GUI/MemViewer/MemViewer.cpp b/Source/GUI/MemViewer/MemViewer.cpp index 7e845ca8..cf5fe23b 100644 --- a/Source/GUI/MemViewer/MemViewer.cpp +++ b/Source/GUI/MemViewer/MemViewer.cpp @@ -730,8 +730,9 @@ bool MemViewer::writeCharacterToSelectedMemory(char byteToWrite) byteToWrite = selectedMemoryValue & 0x0F | (byteToWrite << 4); } - u32 offsetToWrite = Common::dolphinAddrToOffset(m_currentFirstAddress + (u32)memoryOffset, - DolphinComm::DolphinAccessor::isARAMAccessible()); + const u32 offsetToWrite{ + Common::dolphinAddrToOffset(m_currentFirstAddress + static_cast(memoryOffset), + DolphinComm::DolphinAccessor::isARAMAccessible())}; return DolphinComm::DolphinAccessor::writeToRAM(offsetToWrite, &byteToWrite, 1, false); } @@ -915,9 +916,10 @@ void MemViewer::determineMemoryTextRenderProperties(const int rowIndex, const in { QColor baseColor = QColor(Qt::red); float alphaPercentage = - (1000 - (m_elapsedTimer.elapsed() - - m_memoryMsElapsedLastChange[rowIndex * m_numColumns + columnIndex])) / - (1000 / 100); + (1000.0f - + static_cast(m_elapsedTimer.elapsed() - + m_memoryMsElapsedLastChange[rowIndex * m_numColumns + columnIndex])) / + (1000.0f / 100.0f); int newAlpha = std::trunc(baseColor.alpha() * (alphaPercentage / 100)); bgColor = QColor(baseColor.red(), baseColor.green(), baseColor.blue(), newAlpha); } @@ -951,7 +953,7 @@ void MemViewer::renderASCIIText(QPainter& painter, const int rowIndex, const int std::string asciiStr = Common::formatMemoryToString( m_updatedRawMemoryData + ((rowIndex * m_numColumns) + columnIndex), Common::MemType::type_string, 1, Common::MemBase::base_none, true); - int asciiByte = (int)asciiStr[0]; + const int asciiByte{static_cast(static_cast(asciiStr[0]))}; if (asciiByte > 0x7E || asciiByte < 0x20) asciiStr = "."; QRect* currentCharRect = new QRect( diff --git a/Source/GUI/MemWatcher/Dialogs/DlgAddWatchEntry.cpp b/Source/GUI/MemWatcher/Dialogs/DlgAddWatchEntry.cpp index 666362e7..d9cdb75c 100644 --- a/Source/GUI/MemWatcher/Dialogs/DlgAddWatchEntry.cpp +++ b/Source/GUI/MemWatcher/Dialogs/DlgAddWatchEntry.cpp @@ -185,7 +185,6 @@ void DlgAddWatchEntry::addPointerOffset() int level = static_cast(m_entry->getPointerLevel()); QLabel* lblLevel = new QLabel(QString::fromStdString("Level " + std::to_string(level + 1) + ":")); QLineEdit* txbOffset = new QLineEdit(); - txbOffset->setText(0); m_offsets.append(txbOffset); QLabel* lblAddressOfPath = new QLabel(" -> "); m_addressPath.append(lblAddressOfPath); diff --git a/Source/GUI/MemWatcher/MemWatchModel.cpp b/Source/GUI/MemWatcher/MemWatchModel.cpp index 7902338a..aa4d4ad4 100644 --- a/Source/GUI/MemWatcher/MemWatchModel.cpp +++ b/Source/GUI/MemWatcher/MemWatchModel.cpp @@ -2,13 +2,33 @@ #include #include + #include #include #include #include "../../CheatEngineParser/CheatEngineParser.h" +#include "../../Common/CommonUtils.h" #include "../GUICommon.h" +namespace +{ +QString getAddressString(const MemWatchEntry* const entry) +{ + std::stringstream ss; + if (entry->isBoundToPointer()) + { + int level = static_cast(entry->getPointerLevel()); + std::string strAddress = entry->getAddressStringForPointerLevel(level); + ss << "(" << level << "*)" << std::hex << std::uppercase << strAddress; + return QString::fromStdString(ss.str()); + } + u32 address = entry->getConsoleAddress(); + ss << std::hex << std::uppercase << address; + return QString::fromStdString(ss.str()); +} +} // namespace + MemWatchModel::MemWatchModel(QObject* parent) : QAbstractItemModel(parent) { m_rootNode = new MemWatchTreeNode(nullptr); @@ -37,13 +57,13 @@ bool MemWatchModel::updateNodeValueRecursive(MemWatchTreeNode* node, const QMode QVector children = node->getChildren(); if (children.count() > 0) { - for (auto i : children) + for (MemWatchTreeNode* const child : children) { - QModelIndex theIndex = index(i->getRow(), WATCH_COL_VALUE, parent); - readSucess = updateNodeValueRecursive(i, theIndex, readSucess); + QModelIndex theIndex = index(child->getRow(), WATCH_COL_VALUE, parent); + readSucess = updateNodeValueRecursive(child, theIndex, readSucess); if (!readSucess) return false; - if (!GUICommon::g_valueEditing && !i->isGroup()) + if (!GUICommon::g_valueEditing && !child->isGroup()) emit dataChanged(theIndex, theIndex); } } @@ -61,10 +81,10 @@ bool MemWatchModel::freezeNodeValueRecursive(MemWatchTreeNode* node, const QMode QVector children = node->getChildren(); if (children.count() > 0) { - for (auto i : children) + for (MemWatchTreeNode* const child : children) { - QModelIndex theIndex = index(i->getRow(), WATCH_COL_VALUE, parent); - writeSucess = freezeNodeValueRecursive(i, theIndex, writeSucess); + QModelIndex theIndex = index(child->getRow(), WATCH_COL_VALUE, parent); + writeSucess = freezeNodeValueRecursive(child, theIndex, writeSucess); if (!writeSucess) return false; } @@ -92,7 +112,7 @@ void MemWatchModel::changeType(const QModelIndex& index, Common::MemType type, s emit dataChanged(index, index); } -MemWatchEntry* MemWatchModel::getEntryFromIndex(const QModelIndex& index) const +MemWatchEntry* MemWatchModel::getEntryFromIndex(const QModelIndex& index) { MemWatchTreeNode* node = static_cast(index.internalPointer()); return node->getEntry(); @@ -397,16 +417,16 @@ int MemWatchModel::getNodeDeepness(const MemWatchTreeNode* node) const } MemWatchTreeNode* -MemWatchModel::getLeastDeepNodeFromList(const QList nodes) const +MemWatchModel::getLeastDeepNodeFromList(const QList& nodes) const { int leastLevelFound = std::numeric_limits::max(); MemWatchTreeNode* returnNode = new MemWatchTreeNode(nullptr); - for (auto i : nodes) + for (MemWatchTreeNode* const node : nodes) { - int deepness = getNodeDeepness(i); + int deepness = getNodeDeepness(node); if (deepness < leastLevelFound) { - returnNode = i; + returnNode = node; leastLevelFound = deepness; } } @@ -427,15 +447,14 @@ QMimeData* MemWatchModel::mimeData(const QModelIndexList& indexes) const if (!nodes.contains(node)) nodes << node; } - qulonglong leastDeepPointer = 0; MemWatchTreeNode* leastDeepNode = getLeastDeepNodeFromList(nodes); - std::memcpy(&leastDeepPointer, &leastDeepNode, sizeof(MemWatchTreeNode*)); + const qulonglong leastDeepPointer{ + Common::bit_cast(leastDeepNode)}; stream << leastDeepPointer; stream << static_cast(nodes.count()); foreach (MemWatchTreeNode* node, nodes) { - qulonglong pointer = 0; - std::memcpy(&pointer, &node, sizeof(node)); + const auto pointer{Common::bit_cast(node)}; stream << pointer; } mimeData->setData("application/x-memwatchtreenode", data); @@ -456,10 +475,9 @@ bool MemWatchModel::dropMimeData(const QMimeData* data, Qt::DropAction action, i else destParentNode = static_cast(parent.internalPointer()); - qlonglong leastDeepNodePtr; + qulonglong leastDeepNodePtr{}; stream >> leastDeepNodePtr; - MemWatchTreeNode* leastDeepNode = nullptr; - std::memcpy(&leastDeepNode, &leastDeepNodePtr, sizeof(leastDeepNodePtr)); + auto* const leastDeepNode{Common::bit_cast(leastDeepNodePtr)}; if (row == -1) { @@ -483,10 +501,9 @@ bool MemWatchModel::dropMimeData(const QMimeData* data, Qt::DropAction action, i for (int i = 0; i < count; ++i) { - qlonglong nodePtr; + qulonglong nodePtr{}; stream >> nodePtr; - MemWatchTreeNode* srcNode = nullptr; - std::memcpy(&srcNode, &nodePtr, sizeof(nodePtr)); + auto* const srcNode{Common::bit_cast(nodePtr)}; // Since beginMoveRows uses the same row format then the one received, we want to keep that, but // still use the correct row number for inserting. @@ -615,26 +632,13 @@ void MemWatchModel::sortRecursive(int column, Qt::SortOrder order, MemWatchTreeN parent->setChildren(children); - for (auto i : parent->getChildren()) + for (MemWatchTreeNode* const child : parent->getChildren()) { - if (i->isGroup()) - sortRecursive(column, order, i); - } -} - -QString MemWatchModel::getAddressString(const MemWatchEntry* entry) const -{ - std::stringstream ss; - if (entry->isBoundToPointer()) - { - int level = static_cast(entry->getPointerLevel()); - std::string strAddress = entry->getAddressStringForPointerLevel(level); - ss << "(" << level << "*)" << std::hex << std::uppercase << strAddress; - return QString::fromStdString(ss.str()); + if (child->isGroup()) + { + sortRecursive(column, order, child); + } } - u32 address = entry->getConsoleAddress(); - ss << std::hex << std::uppercase << address; - return QString::fromStdString(ss.str()); } void MemWatchModel::loadRootFromJsonRecursive(const QJsonObject& json) @@ -685,7 +689,7 @@ MemWatchTreeNode* MemWatchModel::getRootNode() const return m_rootNode; } -MemWatchTreeNode* MemWatchModel::getTreeNodeFromIndex(const QModelIndex& index) const +MemWatchTreeNode* MemWatchModel::getTreeNodeFromIndex(const QModelIndex& index) { return static_cast(index.internalPointer()); } diff --git a/Source/GUI/MemWatcher/MemWatchModel.h b/Source/GUI/MemWatcher/MemWatchModel.h index 1e89fead..20425b2d 100644 --- a/Source/GUI/MemWatcher/MemWatchModel.h +++ b/Source/GUI/MemWatcher/MemWatchModel.h @@ -49,7 +49,7 @@ class MemWatchModel : public QAbstractItemModel const QModelIndex& parent) override; void changeType(const QModelIndex& index, const Common::MemType type, const size_t length); - MemWatchEntry* getEntryFromIndex(const QModelIndex& index) const; + static MemWatchEntry* getEntryFromIndex(const QModelIndex& index); void addGroup(const QString& name); void addEntry(MemWatchEntry* entry); void editEntry(MemWatchEntry* entry, const QModelIndex& index); @@ -65,7 +65,7 @@ class MemWatchModel : public QAbstractItemModel QString writeRootToCSVStringRecursive() const; bool hasAnyNodes() const; MemWatchTreeNode* getRootNode() const; - MemWatchTreeNode* getTreeNodeFromIndex(const QModelIndex& index) const; + static MemWatchTreeNode* getTreeNodeFromIndex(const QModelIndex& index); bool editData(const QModelIndex& index, const QVariant& value, int role, bool emitEdit = false); signals: @@ -79,8 +79,7 @@ class MemWatchModel : public QAbstractItemModel const bool readSucess = true); bool freezeNodeValueRecursive(MemWatchTreeNode* node, const QModelIndex& parent = QModelIndex(), const bool writeSucess = true); - QString getAddressString(const MemWatchEntry* entry) const; - MemWatchTreeNode* getLeastDeepNodeFromList(const QList nodes) const; + MemWatchTreeNode* getLeastDeepNodeFromList(const QList& nodes) const; int getNodeDeepness(const MemWatchTreeNode* node) const; MemWatchTreeNode* m_rootNode; diff --git a/Source/GUI/MemWatcher/MemWatchWidget.cpp b/Source/GUI/MemWatcher/MemWatchWidget.cpp index 0d0761d8..51ece341 100644 --- a/Source/GUI/MemWatcher/MemWatchWidget.cpp +++ b/Source/GUI/MemWatcher/MemWatchWidget.cpp @@ -345,17 +345,17 @@ void MemWatchWidget::pasteWatchFromClipBoard(MemWatchTreeNode* node, int row) if (copiedRootNode->hasChildren()) { int numberIterated = 0; - for (auto i : copiedRootNode->getChildren()) + for (MemWatchTreeNode* const child : copiedRootNode->getChildren()) { if (node == nullptr) node = m_watchModel->getRootNode(); if (node->isGroup() || (node->getParent() == nullptr)) { - node->appendChild(i); + node->appendChild(child); } else { - node->getParent()->insertChild(row + numberIterated, i); + node->getParent()->insertChild(row + numberIterated, child); } numberIterated++; } diff --git a/Source/MemoryScanner/MemoryScanner.cpp b/Source/MemoryScanner/MemoryScanner.cpp index c3f7f45c..c3d60710 100644 --- a/Source/MemoryScanner/MemoryScanner.cpp +++ b/Source/MemoryScanner/MemoryScanner.cpp @@ -1,11 +1,33 @@ #include "MemoryScanner.h" +#include + #include "../Common/CommonUtils.h" #include "../DolphinProcess/DolphinAccessor.h" -MemScanner::MemScanner() : m_resultsConsoleAddr(std::vector()) +namespace +{ +std::string addSpacesToBytesArrays(const std::string_view bytesArray) { + std::string result(bytesArray); + int spacesAdded = 0; + for (int i = 2; i < bytesArray.length(); i += 2) + { + if (bytesArray[i] != ' ') + { + result.insert(i + spacesAdded, 1, ' '); + spacesAdded++; + } + else + { + i++; + } + } + return result; } +} // namespace + +MemScanner::MemScanner() = default; MemScanner::~MemScanner() { @@ -473,7 +495,7 @@ bool MemScanner::setSearchRangeEnd(u32 endRange) return true; } -int MemScanner::getTermsNumForFilter(const MemScanner::ScanFiter filter) const +int MemScanner::getTermsNumForFilter(const MemScanner::ScanFiter filter) { if (filter == MemScanner::ScanFiter::between) return 2; @@ -485,7 +507,7 @@ int MemScanner::getTermsNumForFilter(const MemScanner::ScanFiter filter) const return 0; } -bool MemScanner::typeSupportsAdditionalOptions(const Common::MemType type) const +bool MemScanner::typeSupportsAdditionalOptions(const Common::MemType type) { return (type == Common::MemType::type_byte || type == Common::MemType::type_halfword || type == Common::MemType::type_word); @@ -524,25 +546,6 @@ void MemScanner::removeResultAt(int index) m_resultCount--; } -std::string MemScanner::addSpacesToBytesArrays(const std::string& bytesArray) const -{ - std::string result(bytesArray); - int spacesAdded = 0; - for (int i = 2; i < bytesArray.length(); i += 2) - { - if (bytesArray[i] != ' ') - { - result.insert(i + spacesAdded, 1, ' '); - spacesAdded++; - } - else - { - i++; - } - } - return result; -} - bool MemScanner::undoScan() { if (m_undoCount > 0) diff --git a/Source/MemoryScanner/MemoryScanner.h b/Source/MemoryScanner/MemoryScanner.h index dbc50807..073399e7 100644 --- a/Source/MemoryScanner/MemoryScanner.h +++ b/Source/MemoryScanner/MemoryScanner.h @@ -1,8 +1,10 @@ #pragma once +#include #include #include #include +#include #include #include "../Common/CommonTypes.h" @@ -48,7 +50,7 @@ class MemScanner bool bswapSecond, size_t length) const; template - inline T convertMemoryToType(const char* memory, bool invert) const + T convertMemoryToType(const char* memory, bool invert) const { T theType; std::memcpy(&theType, memory, sizeof(T)); @@ -58,9 +60,9 @@ class MemScanner } template - inline CompareResult compareMemoryAsNumbersWithType(const char* first, const char* second, - const char* offset, bool offsetInvert, - bool bswapSecond) const + CompareResult compareMemoryAsNumbersWithType(const char* first, const char* second, + const char* offset, bool offsetInvert, + bool bswapSecond) const { T firstByte; T secondByte; @@ -113,8 +115,9 @@ class MemScanner } } - if (firstByte != firstByte) - return CompareResult::nan; + if constexpr (std::is_floating_point::value) + if (std::isnan(firstByte)) + return CompareResult::nan; if (firstByte < (secondByte + convertMemoryToType(offset, offsetInvert))) return CompareResult::smaller; @@ -129,15 +132,15 @@ class MemScanner void setEnforceMemAlignment(const bool enforceAlignment); void setIsSigned(const bool isSigned); void resetSearchRange(); - bool setSearchRangeBegin(u32 beginIndex); - bool setSearchRangeEnd(u32 endIndex); - bool setSearchRange(u32 beginIndex, u32 endIndex); + bool setSearchRangeBegin(u32 beginRange); + bool setSearchRangeEnd(u32 endRange); + bool setSearchRange(u32 beginRange, u32 endRange); std::vector getResultsConsoleAddr() const; size_t getResultCount() const; bool hasUndo() const; size_t getUndoCount() const; - int getTermsNumForFilter(const ScanFiter filter) const; + static int getTermsNumForFilter(ScanFiter filter); Common::MemType getType() const; Common::MemBase getBase() const; size_t getLength() const; @@ -145,7 +148,7 @@ class MemScanner std::string getFormattedScannedValueAt(const int index) const; std::string getFormattedCurrentValueAt(int index) const; void removeResultAt(int index); - bool typeSupportsAdditionalOptions(const Common::MemType type) const; + static bool typeSupportsAdditionalOptions(Common::MemType type); bool hasScanStarted() const; private: @@ -153,7 +156,6 @@ class MemScanner const char* memoryToCompare2, const char* noOffset, const char* newerRAMCache, const size_t realSize, const u32 consoleOffset) const; - std::string addSpacesToBytesArrays(const std::string& bytesArray) const; bool m_searchInRangeBegin = false; bool m_searchInRangeEnd = false; diff --git a/Source/MemoryWatch/MemWatchEntry.cpp b/Source/MemoryWatch/MemWatchEntry.cpp index e6bacd31..86a414a4 100644 --- a/Source/MemoryWatch/MemWatchEntry.cpp +++ b/Source/MemoryWatch/MemWatchEntry.cpp @@ -6,16 +6,16 @@ #include #include #include +#include #include "../Common/CommonUtils.h" #include "../DolphinProcess/DolphinAccessor.h" -MemWatchEntry::MemWatchEntry(const QString label, const u32 consoleAddress, - const Common::MemType type, const Common::MemBase base, - const bool isUnsigned, const size_t length, +MemWatchEntry::MemWatchEntry(QString label, const u32 consoleAddress, const Common::MemType type, + const Common::MemBase base, const bool isUnsigned, const size_t length, const bool isBoundToPointer) - : m_label(label), m_consoleAddress(consoleAddress), m_type(type), m_isUnsigned(isUnsigned), - m_base(base), m_boundToPointer(isBoundToPointer), m_length(length) + : m_label(std::move(label)), m_consoleAddress(consoleAddress), m_type(type), + m_isUnsigned(isUnsigned), m_base(base), m_boundToPointer(isBoundToPointer), m_length(length) { m_memory = new char[getSizeForType(m_type, m_length)]; } diff --git a/Source/MemoryWatch/MemWatchEntry.h b/Source/MemoryWatch/MemWatchEntry.h index 781bbd6d..83af72ea 100644 --- a/Source/MemoryWatch/MemWatchEntry.h +++ b/Source/MemoryWatch/MemWatchEntry.h @@ -12,10 +12,9 @@ class MemWatchEntry { public: MemWatchEntry(); - MemWatchEntry(const QString label, const u32 consoleAddress, const Common::MemType type, - const Common::MemBase = Common::MemBase::base_decimal, - const bool m_isUnsigned = false, const size_t length = 1, - const bool isBoundToPointer = false); + MemWatchEntry(QString label, u32 consoleAddress, Common::MemType type, + Common::MemBase = Common::MemBase::base_decimal, bool m_isUnsigned = false, + size_t length = 1, bool isBoundToPointer = false); MemWatchEntry(MemWatchEntry* entry); ~MemWatchEntry(); diff --git a/Source/MemoryWatch/MemWatchTreeNode.cpp b/Source/MemoryWatch/MemWatchTreeNode.cpp index 926ed94d..6c88f888 100644 --- a/Source/MemoryWatch/MemWatchTreeNode.cpp +++ b/Source/MemoryWatch/MemWatchTreeNode.cpp @@ -3,12 +3,13 @@ #include #include +#include #include "../GUI/GUICommon.h" -MemWatchTreeNode::MemWatchTreeNode(MemWatchEntry* entry, MemWatchTreeNode* parent, - const bool isGroup, const QString& groupName) - : m_entry(entry), m_parent(parent), m_isGroup(isGroup), m_groupName(groupName) +MemWatchTreeNode::MemWatchTreeNode(MemWatchEntry* const entry, MemWatchTreeNode* const parent, + const bool isGroup, QString groupName) + : m_entry(entry), m_parent(parent), m_isGroup(isGroup), m_groupName(std::move(groupName)) { } @@ -83,7 +84,7 @@ QVector MemWatchTreeNode::getChildren() const void MemWatchTreeNode::setChildren(QVector children) { - m_children = children; + m_children = std::move(children); } MemWatchTreeNode* MemWatchTreeNode::getParent() const @@ -189,10 +190,10 @@ void MemWatchTreeNode::writeToJson(QJsonObject& json) const { json["groupName"] = m_groupName; QJsonArray entries; - for (auto i : m_children) + for (MemWatchTreeNode* const child : m_children) { QJsonObject theNode; - i->writeToJson(theNode); + child->writeToJson(theNode); entries.append(theNode); } json["groupEntries"] = entries; @@ -202,10 +203,10 @@ void MemWatchTreeNode::writeToJson(QJsonObject& json) const if (m_parent == nullptr) { QJsonArray watchList; - for (auto i : m_children) + for (MemWatchTreeNode* const child : m_children) { QJsonObject theNode; - i->writeToJson(theNode); + child->writeToJson(theNode); watchList.append(theNode); } json["watchList"] = watchList; @@ -243,9 +244,9 @@ QString MemWatchTreeNode::writeAsCSV() const if (isGroup() || m_parent == nullptr) { QString rootCsv; - for (auto i : m_children) + for (MemWatchTreeNode* const child : m_children) { - QString theCsvLine = i->writeAsCSV(); + QString theCsvLine = child->writeAsCSV(); rootCsv.append(theCsvLine); } return rootCsv; diff --git a/Source/MemoryWatch/MemWatchTreeNode.h b/Source/MemoryWatch/MemWatchTreeNode.h index ef9f4bf0..9e30ffa5 100644 --- a/Source/MemoryWatch/MemWatchTreeNode.h +++ b/Source/MemoryWatch/MemWatchTreeNode.h @@ -9,8 +9,8 @@ class MemWatchTreeNode { public: - MemWatchTreeNode(MemWatchEntry* entry, MemWatchTreeNode* parent = nullptr, - const bool isGroup = false, const QString& groupName = ""); + explicit MemWatchTreeNode(MemWatchEntry* entry, MemWatchTreeNode* parent = nullptr, + bool isGroup = false, QString groupName = {}); MemWatchTreeNode(const MemWatchTreeNode& node); ~MemWatchTreeNode();