From edda024285f01f47df3fefde59a270a00e39f758 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 14 Aug 2020 12:34:44 +0100 Subject: [PATCH] Fix up some Coverity and CLion warnings. --- common/libeval_compiler/libeval_compiler.cpp | 35 +++++++-------- include/libeval_compiler/libeval_compiler.h | 46 ++++++++++++-------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/common/libeval_compiler/libeval_compiler.cpp b/common/libeval_compiler/libeval_compiler.cpp index 7f454a54aa..f4f66f3960 100644 --- a/common/libeval_compiler/libeval_compiler.cpp +++ b/common/libeval_compiler/libeval_compiler.cpp @@ -166,7 +166,7 @@ wxString UCODE::Dump() const }; -wxString TOKENIZER::GetChars( std::function cond ) const +wxString TOKENIZER::GetChars( const std::function& cond ) const { wxString rv; size_t p = m_pos; @@ -180,9 +180,10 @@ wxString TOKENIZER::GetChars( std::function cond ) const return rv; } -bool TOKENIZER::MatchAhead( const wxString& match, std::function stopCond ) const +bool TOKENIZER::MatchAhead( const wxString& match, + const std::function& stopCond ) const { - int remaining = m_str.Length() - m_pos; + int remaining = (int) m_str.Length() - m_pos; if( remaining < (int) match.length() ) return false; @@ -266,9 +267,7 @@ bool COMPILER::Compile( const wxString& aString, UCODE* aCode, CONTEXT* aPreflig m_tree = nullptr; m_parseFinished = false; - T_TOKEN tok; - - tok.value.str = nullptr; + T_TOKEN tok( defaultToken ); 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] ) freeTree( tree->leaf[1] ); - if( tree->uop ) - { - delete tree->uop; - } + delete tree->uop; } + void TREE_NODE::SetUop( int aOp, double aValue ) { - if( uop ) - delete uop; + delete uop; std::unique_ptr val( new VALUE( aValue ) ); uop = new UOP( aOp, std::move( val ) ); } + void TREE_NODE::SetUop( int aOp, const wxString& aValue ) { - if( uop ) - delete uop; + delete uop; std::unique_ptr val( new VALUE( aValue ) ); uop = new UOP( aOp, std::move( val ) ); } + void TREE_NODE::SetUop( int aOp, std::unique_ptr aRef ) { - if( uop ) - delete uop; + delete uop; uop = new UOP( aOp, std::move( aRef ) ); } + void TREE_NODE::SetUop( int aOp, FUNC_CALL_REF aFunc, std::unique_ptr aRef ) { - if( uop ) - delete uop; + delete uop; uop = new UOP( aOp, std::move( aFunc ), std::move( aRef ) ); } - static void prepareTree( LIBEVAL::TREE_NODE *node ) { node->isVisited = false; @@ -828,7 +823,7 @@ bool COMPILER::generateUCode( UCODE* aCode, CONTEXT* aPreflightContext ) 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, (int) loc - 1 ); } diff --git a/include/libeval_compiler/libeval_compiler.h b/include/libeval_compiler/libeval_compiler.h index 4c33e1e6a5..992dfe2dc4 100644 --- a/include/libeval_compiler/libeval_compiler.h +++ b/include/libeval_compiler/libeval_compiler.h @@ -67,10 +67,9 @@ struct ERROR_STATUS { bool pendingError = false; - - COMPILATION_STAGE stage; - wxString message; // Note: use wxString for GUI-related strings - int srcPos; + COMPILATION_STAGE stage; + wxString message; + int srcPos; }; @@ -84,6 +83,7 @@ enum VAR_TYPE_T enum TOKEN_TYPE_T { + TR_UNDEFINED = 0, TR_NUMBER = 1, TR_IDENTIFIER = 2, TR_ASSIGN = 3, @@ -101,19 +101,25 @@ typedef std::function FUNC_CALL_REF; struct T_TOKEN_VALUE { - wxString *str; - double num; - int idx; + wxString* str; + double num; + 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 }; + struct T_TOKEN { - int token; + int token; 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 { 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 { @@ -351,9 +358,9 @@ public: wxString Format() const; private: - int m_op; + int m_op; - FUNC_CALL_REF m_func; + FUNC_CALL_REF m_func; std::unique_ptr m_ref; std::unique_ptr m_value; }; @@ -396,13 +403,14 @@ public: return m_pos; } - wxString GetChars( std::function cond ) const; + wxString GetChars( const std::function& cond ) const; - bool MatchAhead( const wxString& match, std::function stopCond ) const; + bool MatchAhead( const wxString& match, + const std::function& stopCond ) const; private: wxString m_str; - size_t m_pos; + size_t m_pos = 0; }; @@ -431,7 +439,7 @@ public: void SetErrorCallback( std::function aCallback ) { - m_errorCallback = aCallback; + m_errorCallback = std::move( aCallback ); } bool IsErrorPending() const { return m_errorStatus.pendingError; } @@ -473,12 +481,14 @@ protected: int m_sourcePos; bool m_parseFinished; + ERROR_STATUS m_errorStatus; + + std::function m_errorCallback; TREE_NODE* m_tree; - ERROR_STATUS m_errorStatus; + std::vector m_gcItems; - std::vector m_gcStrings; - std::function m_errorCallback; + std::vector m_gcStrings; };