Fix up some Coverity and CLion warnings.

This commit is contained in:
Jeff Young 2020-08-14 12:34:44 +01:00
parent 2b0b7a5153
commit edda024285
2 changed files with 43 additions and 38 deletions

View File

@ -166,7 +166,7 @@ wxString UCODE::Dump() const
}; };
wxString TOKENIZER::GetChars( std::function<bool( wxUniChar )> cond ) const wxString TOKENIZER::GetChars( const std::function<bool( wxUniChar )>& cond ) const
{ {
wxString rv; wxString rv;
size_t p = m_pos; size_t p = m_pos;
@ -180,9 +180,10 @@ wxString TOKENIZER::GetChars( std::function<bool( wxUniChar )> cond ) const
return rv; return rv;
} }
bool TOKENIZER::MatchAhead( const wxString& match, std::function<bool( wxUniChar )> stopCond ) const bool TOKENIZER::MatchAhead( const wxString& match,
const std::function<bool( wxUniChar )>& stopCond ) const
{ {
int remaining = m_str.Length() - m_pos; int remaining = (int) m_str.Length() - m_pos;
if( remaining < (int) match.length() ) if( remaining < (int) match.length() )
return false; return false;
@ -266,9 +267,7 @@ bool COMPILER::Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflig
m_tree = nullptr; m_tree = nullptr;
m_parseFinished = false; m_parseFinished = false;
T_TOKEN tok; T_TOKEN tok( defaultToken );
tok.value.str = nullptr;
libeval_dbg(0, "str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() ); libeval_dbg(0, "str: '%s' empty: %d\n", aString.c_str(), !!aString.empty() );
@ -639,48 +638,44 @@ void COMPILER::freeTree( LIBEVAL::TREE_NODE *tree )
if ( tree->leaf[1] ) if ( tree->leaf[1] )
freeTree( tree->leaf[1] ); freeTree( tree->leaf[1] );
if( tree->uop ) delete tree->uop;
{
delete tree->uop;
}
} }
void TREE_NODE::SetUop( int aOp, double aValue ) void TREE_NODE::SetUop( int aOp, double aValue )
{ {
if( uop ) delete uop;
delete uop;
std::unique_ptr<VALUE> val( new VALUE( aValue ) ); std::unique_ptr<VALUE> val( new VALUE( aValue ) );
uop = new UOP( aOp, std::move( val ) ); uop = new UOP( aOp, std::move( val ) );
} }
void TREE_NODE::SetUop( int aOp, const wxString& aValue ) void TREE_NODE::SetUop( int aOp, const wxString& aValue )
{ {
if( uop ) delete uop;
delete uop;
std::unique_ptr<VALUE> val( new VALUE( aValue ) ); std::unique_ptr<VALUE> val( new VALUE( aValue ) );
uop = new UOP( aOp, std::move( val ) ); uop = new UOP( aOp, std::move( val ) );
} }
void TREE_NODE::SetUop( int aOp, std::unique_ptr<VAR_REF> aRef ) void TREE_NODE::SetUop( int aOp, std::unique_ptr<VAR_REF> aRef )
{ {
if( uop ) delete uop;
delete uop;
uop = new UOP( aOp, std::move( aRef ) ); uop = new UOP( aOp, std::move( aRef ) );
} }
void TREE_NODE::SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef ) void TREE_NODE::SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr<VAR_REF> aRef )
{ {
if( uop ) delete uop;
delete uop;
uop = new UOP( aOp, std::move( aFunc ), std::move( aRef ) ); uop = new UOP( aOp, std::move( aFunc ), std::move( aRef ) );
} }
static void prepareTree( LIBEVAL::TREE_NODE *node ) static void prepareTree( LIBEVAL::TREE_NODE *node )
{ {
node->isVisited = false; node->isVisited = false;
@ -828,7 +823,7 @@ bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext )
if( !aPreflightContext->IsErrorPending() ) if( !aPreflightContext->IsErrorPending() )
{ {
size_t loc = node->leaf[1]->leaf[1]->srcPos - node->value.str->Length(); size_t loc = node->leaf[1]->leaf[1]->srcPos - paramStr.Length();
reportError( CST_CODEGEN, aPreflightContext->GetError().message, reportError( CST_CODEGEN, aPreflightContext->GetError().message,
(int) loc - 1 ); (int) loc - 1 );
} }

View File

@ -67,10 +67,9 @@ struct ERROR_STATUS
{ {
bool pendingError = false; bool pendingError = false;
COMPILATION_STAGE stage;
COMPILATION_STAGE stage; wxString message;
wxString message; // Note: use wxString for GUI-related strings int srcPos;
int srcPos;
}; };
@ -84,6 +83,7 @@ enum VAR_TYPE_T
enum TOKEN_TYPE_T enum TOKEN_TYPE_T
{ {
TR_UNDEFINED = 0,
TR_NUMBER = 1, TR_NUMBER = 1,
TR_IDENTIFIER = 2, TR_IDENTIFIER = 2,
TR_ASSIGN = 3, TR_ASSIGN = 3,
@ -101,19 +101,25 @@ typedef std::function<void( CONTEXT*, void* )> FUNC_CALL_REF;
struct T_TOKEN_VALUE struct T_TOKEN_VALUE
{ {
wxString *str; wxString* str;
double num; double num;
int idx; int idx;
}; };
// Lemon can't handle c'tors and d'tors, so we provide a poor-man's version.
constexpr T_TOKEN_VALUE defaultTokenValue = { nullptr, 0.0, 0 }; constexpr T_TOKEN_VALUE defaultTokenValue = { nullptr, 0.0, 0 };
struct T_TOKEN struct T_TOKEN
{ {
int token; int token;
T_TOKEN_VALUE value; T_TOKEN_VALUE value;
}; };
// Lemon can't handle c'tors and d'tors, so we provide a poor-man's version.
constexpr T_TOKEN defaultToken = { TR_UNDEFINED, defaultTokenValue };
class TREE_NODE class TREE_NODE
{ {
public: public:
@ -134,7 +140,8 @@ public:
}; };
TREE_NODE* newNode( LIBEVAL::COMPILER* compiler, int op, const T_TOKEN_VALUE& value = defaultTokenValue); TREE_NODE* newNode( LIBEVAL::COMPILER* compiler, int op,
const T_TOKEN_VALUE& value = defaultTokenValue );
class UNIT_RESOLVER class UNIT_RESOLVER
{ {
@ -351,9 +358,9 @@ public:
wxString Format() const; wxString Format() const;
private: private:
int m_op; int m_op;
FUNC_CALL_REF m_func; FUNC_CALL_REF m_func;
std::unique_ptr<VAR_REF> m_ref; std::unique_ptr<VAR_REF> m_ref;
std::unique_ptr<VALUE> m_value; std::unique_ptr<VALUE> m_value;
}; };
@ -396,13 +403,14 @@ public:
return m_pos; return m_pos;
} }
wxString GetChars( std::function<bool( wxUniChar )> cond ) const; wxString GetChars( const std::function<bool( wxUniChar )>& cond ) const;
bool MatchAhead( const wxString& match, std::function<bool( wxUniChar )> stopCond ) const; bool MatchAhead( const wxString& match,
const std::function<bool( wxUniChar )>& stopCond ) const;
private: private:
wxString m_str; wxString m_str;
size_t m_pos; size_t m_pos = 0;
}; };
@ -431,7 +439,7 @@ public:
void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback ) void SetErrorCallback( std::function<void( const wxString& aMessage, int aOffset )> aCallback )
{ {
m_errorCallback = aCallback; m_errorCallback = std::move( aCallback );
} }
bool IsErrorPending() const { return m_errorStatus.pendingError; } bool IsErrorPending() const { return m_errorStatus.pendingError; }
@ -473,12 +481,14 @@ protected:
int m_sourcePos; int m_sourcePos;
bool m_parseFinished; bool m_parseFinished;
ERROR_STATUS m_errorStatus;
std::function<void( const wxString& aMessage, int aOffset )> m_errorCallback;
TREE_NODE* m_tree; TREE_NODE* m_tree;
ERROR_STATUS m_errorStatus;
std::vector<TREE_NODE*> m_gcItems; std::vector<TREE_NODE*> m_gcItems;
std::vector<wxString*> m_gcStrings; std::vector<wxString*> m_gcStrings;
std::function<void( const wxString& aMessage, int aOffset )> m_errorCallback;
}; };