From 2f3a0ea0b5073f54f0c33ec1beb8d7d709875b45 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 29 Apr 2023 09:15:14 -0700 Subject: [PATCH] Debugger: Simplify expression error handling. --- Common/Math/expression_parser.cpp | 38 ++++++++++++++++--------------- Common/Math/expression_parser.h | 3 ++- Core/MIPS/MIPSDebugInterface.cpp | 4 ++-- GPU/Common/GPUDebugInterface.cpp | 7 +++--- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Common/Math/expression_parser.cpp b/Common/Math/expression_parser.cpp index a5f48e64c9..aa9ba4c0a5 100644 --- a/Common/Math/expression_parser.cpp +++ b/Common/Math/expression_parser.cpp @@ -1,3 +1,4 @@ +#include "Common/StringUtils.h" #include "expression_parser.h" #include #include @@ -14,7 +15,7 @@ typedef enum { typedef enum { EXCOMM_CONST, EXCOMM_CONST_FLOAT, EXCOMM_REF, EXCOMM_OP } ExpressionCommand; -static char expressionError[512]; +static std::string expressionError; typedef struct { char Name[4]; @@ -219,7 +220,7 @@ bool isAlphaNum(char c) bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, PostfixExpression& dest) { - expressionError[0] = 0; + expressionError.clear(); int infixPos = 0; int infixLen = (int)strlen(infix); @@ -253,7 +254,7 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf isFloat = true; else if (parseNumber(subStr,16,subPos,value) == false) { - snprintf(expressionError, sizeof(expressionError), "Invalid number \"%s\"",subStr); + expressionError = StringFromFormat("Invalid number \"%s\"", subStr); return false; } @@ -282,14 +283,14 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf continue; } - snprintf(expressionError, sizeof(expressionError), "Invalid symbol \"%s\"",subStr); + expressionError = StringFromFormat("Invalid symbol \"%s\"", subStr); return false; } else { int len; ExpressionOpcodeType type = getExpressionOpcode(&infix[infixPos],len,lastOpcode); if (type == EXOP_NONE) { - snprintf(expressionError, sizeof(expressionError), "Invalid operator at \"%s\"",&infix[infixPos]); + expressionError = StringFromFormat("Invalid operator at \"%s\"", &infix[infixPos]); return false; } @@ -304,7 +305,7 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf { if (opcodeStack.empty()) { - snprintf(expressionError, sizeof(expressionError), "Closing parenthesis without opening one"); + expressionError = "Closing parenthesis without opening one"; return false; } ExpressionOpcodeType t = opcodeStack[opcodeStack.size()-1]; @@ -318,7 +319,7 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf { if (opcodeStack.empty()) { - snprintf(expressionError, sizeof(expressionError), "Closing bracket without opening one"); + expressionError = "Closing bracket without opening one"; return false; } ExpressionOpcodeType t = opcodeStack[opcodeStack.size()-1]; @@ -371,7 +372,7 @@ bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, Postf if (t == EXOP_BRACKETL) // opening bracket without closing one { - snprintf(expressionError, sizeof(expressionError), "Parenthesis not closed"); + expressionError = "Parenthesis not closed"; return false; } dest.push_back(ExpressionPair(EXCOMM_OP,t)); @@ -430,7 +431,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, opcode = exp[num++].second; if (valueStack.size() < ExpressionOpcodes[opcode].args) { - snprintf(expressionError, sizeof(expressionError), "Not enough arguments"); + expressionError = "Not enough arguments"; return false; } for (int l = 0; l < ExpressionOpcodes[opcode].args; l++) @@ -446,12 +447,12 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_MEMSIZE: // must be followed by EXOP_MEM if (exp[num++].second != EXOP_MEM) { - snprintf(expressionError, sizeof(expressionError), "Invalid memsize operator"); + expressionError = "Invalid memsize operator"; return false; } uint32_t val; - if(funcs->getMemoryValue(arg[1],arg[0],val,expressionError, sizeof(expressionError)) == false) + if (funcs->getMemoryValue(arg[1], arg[0], val, &expressionError) == false) { return false; } @@ -460,7 +461,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_MEM: { uint32_t val; - if (funcs->getMemoryValue(arg[0],4,val,expressionError, sizeof(expressionError)) == false) + if (funcs->getMemoryValue(arg[0], 4, val, &expressionError) == false) { return false; } @@ -490,7 +491,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_DIV: // a/b if (arg[0] == 0) { - snprintf(expressionError, sizeof(expressionError), "Division by zero"); + expressionError = "Division by zero"; return false; } if (useFloat) @@ -501,7 +502,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_MOD: // a%b if (arg[0] == 0) { - snprintf(expressionError, sizeof(expressionError), "Modulo by zero"); + expressionError = "Modulo by zero"; return false; } valueStack.push_back(arg[1]%arg[0]); @@ -574,7 +575,7 @@ bool parsePostfixExpression(PostfixExpression& exp, IExpressionFunctions* funcs, case EXOP_TERTELSE: // exp ? exp : exp, else muss zuerst kommen! if (exp[num++].second != EXOP_TERTIF) { - snprintf(expressionError, sizeof(expressionError), "Invalid tertiary operator"); + expressionError = "Invalid tertiary operator"; return false; } valueStack.push_back(arg[2]?arg[1]:arg[0]); @@ -595,8 +596,9 @@ bool parseExpression(const char *exp, IExpressionFunctions *funcs, uint32_t &des return parsePostfixExpression(postfix,funcs,dest); } -const char* getExpressionError() +const char *getExpressionError() { - if (expressionError[0] == 0) snprintf(expressionError, sizeof(expressionError), "Invalid expression"); - return expressionError; + if (expressionError.empty()) + expressionError = "Invalid expression"; + return expressionError.c_str(); } diff --git a/Common/Math/expression_parser.h b/Common/Math/expression_parser.h index fb82fc9446..ee6330a929 100644 --- a/Common/Math/expression_parser.h +++ b/Common/Math/expression_parser.h @@ -2,6 +2,7 @@ #include #include +#include #include typedef std::pair ExpressionPair; @@ -21,7 +22,7 @@ public: virtual bool parseSymbol(char* str, uint32_t& symbolValue) = 0; virtual uint32_t getReferenceValue(uint32_t referenceIndex) = 0; virtual ExpressionType getReferenceType(uint32_t referenceIndex) = 0; - virtual bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char *error, size_t errorBufSize) = 0; + virtual bool getMemoryValue(uint32_t address, int size, uint32_t& dest, std::string *error) = 0; }; bool initPostfixExpression(const char* infix, IExpressionFunctions* funcs, PostfixExpression& dest); diff --git a/Core/MIPS/MIPSDebugInterface.cpp b/Core/MIPS/MIPSDebugInterface.cpp index 9087fd80cd..f2063ddc1d 100644 --- a/Core/MIPS/MIPSDebugInterface.cpp +++ b/Core/MIPS/MIPSDebugInterface.cpp @@ -159,7 +159,7 @@ public: return EXPR_TYPE_UINT; } - bool getMemoryValue(uint32_t address, int size, uint32_t& dest, char* error, size_t errorBufSize) override { + bool getMemoryValue(uint32_t address, int size, uint32_t& dest, std::string *error) override { // We allow, but ignore, bad access. // If we didn't, log/condition statements that reference registers couldn't be configured. uint32_t valid = Memory::ValidSize(address, size); @@ -179,7 +179,7 @@ public: return true; } - snprintf(error, errorBufSize, "Unexpected memory access size %d", size); + *error = StringFromFormat("Unexpected memory access size %d", size); return false; } diff --git a/GPU/Common/GPUDebugInterface.cpp b/GPU/Common/GPUDebugInterface.cpp index c6df47e932..743d884093 100644 --- a/GPU/Common/GPUDebugInterface.cpp +++ b/GPU/Common/GPUDebugInterface.cpp @@ -17,6 +17,7 @@ #include "Common/Log.h" #include "Common/Math/expression_parser.h" +#include "Common/StringUtils.h" #include "Core/Debugger/SymbolMap.h" #include "GPU/Common/GPUDebugInterface.h" #include "GPU/Debugger/Debugger.h" @@ -516,7 +517,7 @@ public: bool parseSymbol(char *str, uint32_t &symbolValue) override; uint32_t getReferenceValue(uint32_t referenceIndex) override; ExpressionType getReferenceType(uint32_t referenceIndex) override; - bool getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error, size_t errorBufSize) override; + bool getMemoryValue(uint32_t address, int size, uint32_t &dest, std::string *error) override; private: bool parseFieldReference(const char *ref, const char *field, uint32_t &referenceIndex); @@ -926,7 +927,7 @@ ExpressionType GEExpressionFunctions::getFieldType(GECmdFormat fmt, GECmdField f return EXPR_TYPE_UINT; } -bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t &dest, char *error, size_t errorBufSize) { +bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t &dest, std::string *error) { // We allow, but ignore, bad access. // If we didn't, log/condition statements that reference registers couldn't be configured. uint32_t valid = Memory::ValidSize(address, size); @@ -946,7 +947,7 @@ bool GEExpressionFunctions::getMemoryValue(uint32_t address, int size, uint32_t return true; } - snprintf(error, errorBufSize, "Unexpected memory access size %d", size); + *error = StringFromFormat("Unexpected memory access size %d", size); return false; }