From e38f5927efeea027f16c8f93e1299d284839fab4 Mon Sep 17 00:00:00 2001 From: Anton van Wezenbeek Date: Sun, 3 Sep 2023 14:43:19 +0200 Subject: [PATCH 1/2] cppcheck updates --- ci/CMakeLists.txt | 3 +- src/ex/vi/commands-motion.cpp | 2 +- src/factory/process-imp.cpp | 6 ++-- src/factory/process-imp.h | 6 ++-- src/stc/scope.cpp | 4 +-- src/stc/scope.h | 4 +-- src/syntax/lexer.cpp | 14 ++++---- src/syntax/wex/lex-lilypond-util.h | 54 ++++++++++++++++------------ src/syntax/wex/lex-lilypond.cpp | 4 +-- src/syntax/wex/lex-rfw.cpp | 1 - src/ui/statusbar.cpp | 5 ++- src/ui/toolbar.cpp | 58 ++++++++++++++++++------------ 12 files changed, 90 insertions(+), 71 deletions(-) diff --git a/ci/CMakeLists.txt b/ci/CMakeLists.txt index e28f7ee845..4cfcba8303 100644 --- a/ci/CMakeLists.txt +++ b/ci/CMakeLists.txt @@ -11,11 +11,12 @@ if (NOT WIN32) add_custom_target(cppcheck COMMAND cppcheck - --std=c++${WEX_CXX_STANDARD}20 + --std=c++${WEX_CXX_STANDARD} --quiet --enable=all --suppress=missingInclude --suppress=cppcheckError --suppress=cstyleCast + --suppress=missingIncludeSystem --suppress=internalAstError --suppress=invalidFunctionArg --suppress=noExplicitConstructor diff --git a/src/ex/vi/commands-motion.cpp b/src/ex/vi/commands-motion.cpp index 5f9b24b7c5..76bb58ca0d 100644 --- a/src/ex/vi/commands-motion.cpp +++ b/src/ex/vi/commands-motion.cpp @@ -747,7 +747,7 @@ void wex::vi::visual_extend(int begin_pos, int end_pos) const default: get_stc()->SetCurrentPos(end_pos); get_stc() - ->SelectNone(); // appearently previous call selects to the new pos .. + ->SelectNone(); // apparently previous call selects to the new pos .. break; } } diff --git a/src/factory/process-imp.cpp b/src/factory/process-imp.cpp index 23767debf4..665b5f7e39 100644 --- a/src/factory/process-imp.cpp +++ b/src/factory/process-imp.cpp @@ -113,7 +113,7 @@ bool wex::factory::process_imp::stop(wxEvtHandler* e) return false; } -void wex::factory::process_imp::thread_error(process* p) +void wex::factory::process_imp::thread_error(const process* p) { std::thread v( [debug = m_debug.load(), @@ -144,7 +144,7 @@ void wex::factory::process_imp::thread_error(process* p) v.detach(); } -void wex::factory::process_imp::thread_input(process* p) +void wex::factory::process_imp::thread_input(const process* p) { std::thread t( [debug = m_debug.load(), @@ -196,7 +196,7 @@ void wex::factory::process_imp::thread_input(process* p) t.detach(); } -void wex::factory::process_imp::thread_output(process* p) +void wex::factory::process_imp::thread_output(const process* p) { std::thread u( [debug = m_debug.load(), diff --git a/src/factory/process-imp.h b/src/factory/process-imp.h index 519c198770..3e4943194a 100644 --- a/src/factory/process-imp.h +++ b/src/factory/process-imp.h @@ -45,9 +45,9 @@ class process_imp private: void boost_async_system(process* p, const process_data& data); - void thread_error(process* p); - void thread_input(process* p); - void thread_output(process* p); + void thread_error(const process* p); + void thread_input(const process* p); + void thread_output(const process* p); std::shared_ptr m_io; std::shared_ptr> m_queue; diff --git a/src/stc/scope.cpp b/src/stc/scope.cpp index 835278f850..8a3049162b 100644 --- a/src/stc/scope.cpp +++ b/src/stc/scope.cpp @@ -2,7 +2,7 @@ // Name: scope.cpp // Purpose: Implementation of class wex::scope // Author: Anton van Wezenbeek -// Copyright: (c) 2020-2021 Anton van Wezenbeek +// Copyright: (c) 2020-2023 Anton van Wezenbeek //////////////////////////////////////////////////////////////////////////////// #include @@ -17,7 +17,7 @@ wex::scope::scope(stc* s) { } -void wex::scope::check_levels(check_t type) +void wex::scope::check_levels(const check_t& type) { bool changed = false; const auto level(m_stc->get_fold_level()); diff --git a/src/stc/scope.h b/src/stc/scope.h index 0ea109be5b..ee3cfd7f51 100644 --- a/src/stc/scope.h +++ b/src/stc/scope.h @@ -2,7 +2,7 @@ // Name: scope.h // Purpose: Implementation of class wex::scope // Author: Anton van Wezenbeek -// Copyright: (c) 2020-2022 Anton van Wezenbeek +// Copyright: (c) 2020-2023 Anton van Wezenbeek //////////////////////////////////////////////////////////////////////////////// #pragma once @@ -62,7 +62,7 @@ class scope typedef std::bitset<2> check_t; - void check_levels(check_t type = check_t().set()); + void check_levels(const check_t& type = check_t().set()); /// Finds text in scope (from current down), returns iterator. map_t::const_iterator iterator(const std::string& text) const; diff --git a/src/syntax/lexer.cpp b/src/syntax/lexer.cpp index 3a80c68048..14ee5c437c 100644 --- a/src/syntax/lexer.cpp +++ b/src/syntax/lexer.cpp @@ -286,15 +286,15 @@ bool wex::lexer::apply() const int wex::lexer::attrib(const std::string& name) const { - for (const auto& a : m_attribs) - { - if (std::get<0>(a) == name) + const auto& a = std::find_if( + m_attribs.begin(), + m_attribs.end(), + [&](auto const& i) { - return std::get<1>(a); - } - } + return std::get<0>(i) == name; + }); - return -1; + return a != m_attribs.end() ? std::get<1>(*a) : -1; } void wex::lexer::auto_match(const std::string& lexer) diff --git a/src/syntax/wex/lex-lilypond-util.h b/src/syntax/wex/lex-lilypond-util.h index 3b1e62ec8e..3f53f901ca 100644 --- a/src/syntax/wex/lex-lilypond-util.h +++ b/src/syntax/wex/lex-lilypond-util.h @@ -7,6 +7,8 @@ #pragma once +#include + namespace Scintilla { /// Offers some general methods. @@ -22,6 +24,15 @@ class lilypond /// Constructor. lilypond(LexAccessor& s) : m_styler(s) + , m_words{ + "align", + "alignat", + "flalign", + "gather", + "multiline", + "displaymath", + "eqnarray", + "equation"} { ; } @@ -41,7 +52,8 @@ class lilypond bool next_not_blank_is(Sci_Position i, char needle) const; private: - LexAccessor& m_styler; + LexAccessor& m_styler; + const std::vector m_words; }; // inline implementation lex_rfw_access @@ -134,15 +146,19 @@ inline bool lilypond::last_word_check( } else { - for (const auto& w : checks) - { - if (last_word_is(match, std::string("{" + w + "}").c_str())) + return std::any_of( + checks.begin(), + checks.end(), + [this, &match, &state, &start](const auto& w) { - m_styler.ColourTo(start - 1, state); - state = SCE_L_COMMAND; - return true; - } - } + if (last_word_is(match, std::string("{" + w + "}").c_str())) + { + m_styler.ColourTo(start - 1, state); + state = SCE_L_COMMAND; + return true; + } + return false; + }); } } } @@ -198,21 +214,13 @@ inline bool lilypond::last_word_is_match_env(Sci_Position pos) const if (s[j - 1] == '*') s[--j] = '\0'; - for (const auto& word : std::vector{ - "align", - "alignat", - "flalign", - "gather", - "multiline", - "displaymath", - "eqnarray", - "equation"}) - { - if (word == s) + return std::any_of( + m_words.begin(), + m_words.end(), + [&s](const auto& w) { - return true; - } - } + return w == s; + }); return false; } diff --git a/src/syntax/wex/lex-lilypond.cpp b/src/syntax/wex/lex-lilypond.cpp index 3642db41f2..52147a33c0 100644 --- a/src/syntax/wex/lex-lilypond.cpp +++ b/src/syntax/wex/lex-lilypond.cpp @@ -127,7 +127,7 @@ void SCI_METHOD lex_lilypond::Fold( do { - char ch, buf[16]; + char buf[16]; Sci_Position i, j; int lev = -1; bool needFold = false; @@ -135,7 +135,7 @@ void SCI_METHOD lex_lilypond::Fold( i < static_cast(endPos); ++i) { - ch = styler.SafeGetCharAt(i); + const char ch = styler.SafeGetCharAt(i); if (ch == '\r' || ch == '\n') break; if (ch == '{') diff --git a/src/syntax/wex/lex-rfw.cpp b/src/syntax/wex/lex-rfw.cpp index abce33d0c2..b09393ac16 100644 --- a/src/syntax/wex/lex-rfw.cpp +++ b/src/syntax/wex/lex-rfw.cpp @@ -6,7 +6,6 @@ //////////////////////////////////////////////////////////////////////////////// #include -#include #include "lex-rfw.h" diff --git a/src/ui/statusbar.cpp b/src/ui/statusbar.cpp index e8e1c63249..8d604baa4f 100644 --- a/src/ui/statusbar.cpp +++ b/src/ui/statusbar.cpp @@ -234,10 +234,9 @@ void wex::statusbar::pane_dialog() std::tuple wex::statusbar::pane_info(const std::string& pane) const { - const std::string use_pane = pane.empty() ? "PaneText" : pane; - int shown_pane_no = 0; + const std::string use_pane = pane.empty() ? "PaneText" : pane; - for (int pane_no = 0; const auto& it : m_panes) + for (int pane_no = 0, shown_pane_no = 0; const auto& it : m_panes) { if (it.is_shown()) { diff --git a/src/ui/toolbar.cpp b/src/ui/toolbar.cpp index 3ca11b46fc..caa074af6c 100644 --- a/src/ui/toolbar.cpp +++ b/src/ui/toolbar.cpp @@ -2,7 +2,7 @@ // Name: toolbar.cpp // Purpose: Implementation of wex::toolbar class // Author: Anton van Wezenbeek -// Copyright: (c) 2021-2023 Anton van Wezenbeek +// Copyright: (c) 2010-2023 Anton van Wezenbeek //////////////////////////////////////////////////////////////////////////////// #include @@ -312,31 +312,43 @@ bool wex::toolbar::add_tool( const std::vector& v, bool realize) { - for (const auto& it : v) - { - if (const stockart art(it.id()); art.get_bitmap(wxART_TOOLBAR).IsOk()) - { - if (!AddTool( - it.id(), - wxEmptyString, // no label + if (!std::all_of( + v.begin(), + v.end(), + [this](const auto& it) + { + if (const stockart art(it.id()); art.get_bitmap(wxART_TOOLBAR).IsOk()) + { + if (!AddTool( + it.id(), + wxEmptyString, // no label #ifdef __WXGTK__ - art.get_bitmap(wxART_TOOLBAR), + art.get_bitmap(wxART_TOOLBAR), #else - art.get_bitmap(wxART_MENU, wxSize(16, 16)), + art.get_bitmap(wxART_MENU, wxSize(16, 16)), #endif - wxGetStockLabel(it.id(), wxSTOCK_NOFLAGS), // short help - it.kind())) - { - return false; - } - } - else if (it.bitmap().IsOk()) - { - if (!AddTool(it.id(), it.label(), it.bitmap(), it.help(), it.kind())) - { - return false; - } - } + wxGetStockLabel(it.id(), wxSTOCK_NOFLAGS), // short help + it.kind())) + { + return false; + } + } + else if (it.bitmap().IsOk()) + { + if (!AddTool( + it.id(), + it.label(), + it.bitmap(), + it.help(), + it.kind())) + { + return false; + } + } + return true; + })) + { + return false; } if (realize) From 33800c33170c2962076c94d8cf2dffe51d937161 Mon Sep 17 00:00:00 2001 From: Anton van Wezenbeek Date: Sun, 3 Sep 2023 18:59:43 +0200 Subject: [PATCH 2/2] fix some asan detected leaks --- include/wex/core/app.h | 4 ++++ sample/frame.cpp | 5 +++++ sample/frame.h | 5 +++++ src/core/app.cpp | 9 ++++++--- src/ui/frame.cpp | 2 ++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/wex/core/app.h b/include/wex/core/app.h index 54e9759180..eda18a5a6d 100644 --- a/include/wex/core/app.h +++ b/include/wex/core/app.h @@ -12,6 +12,8 @@ namespace wex { +class file_translations_loader; + /// Offers the application, with lib specific init and exit, /// and provides access to the locale and the catalog dir. /// Your application should be derived from this class. @@ -48,6 +50,8 @@ class app : public wxApp wxLanguage m_language; + file_translations_loader* m_loader{nullptr}; + static int m_first_init; }; }; // namespace wex diff --git a/sample/frame.cpp b/sample/frame.cpp index 451d705098..bdc54633b1 100644 --- a/sample/frame.cpp +++ b/sample/frame.cpp @@ -125,6 +125,11 @@ frame::frame() {"PaneMacro", 50}}); } +frame::~frame() +{ + delete m_process; +} + wex::del::listview* frame::activate(wex::data::listview::type_t type, const wex::lexer* lexer) { diff --git a/sample/frame.h b/sample/frame.h index 4b1fc6a184..9657225983 100644 --- a/sample/frame.h +++ b/sample/frame.h @@ -13,8 +13,13 @@ class frame : public wex::del::frame { public: + /// Default constructor. frame(); + /// Destructor. + ~frame(); + + /// Update from app. void update(app* a); private: diff --git a/src/core/app.cpp b/src/core/app.cpp index 0e19d493dd..69903048d8 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -59,6 +59,9 @@ int wex::app::OnExit() std::cout << e.what() << "\n"; } + delete wxTranslations::Get(); + delete m_loader; + return wxApp::OnExit(); } @@ -75,13 +78,13 @@ bool wex::app::OnInit() if (m_language != wxLANGUAGE_UNKNOWN && m_language != wxLANGUAGE_DEFAULT) { - auto* loader = new file_translations_loader(); + m_loader = new file_translations_loader(); wxUILocale::FromTag(wxUILocale::GetLanguageCanonicalName(m_language)); wxTranslations::Set(new wxTranslations()); wxTranslations::Get()->SetLanguage(m_language); - wxTranslations::Get()->SetLoader(loader); + wxTranslations::Get()->SetLoader(m_loader); - loader->add_catalogs(m_language); + m_loader->add_catalogs(m_language); } // Necessary for auto_complete images. diff --git a/src/ui/frame.cpp b/src/ui/frame.cpp index df8c772969..ba4f07d9a1 100644 --- a/src/ui/frame.cpp +++ b/src/ui/frame.cpp @@ -327,6 +327,8 @@ wex::frame::~frame() { delete m_ofs; } + + delete m_ex_commandline; } bool wex::frame::add_toolbar_panes(const panes_t& panes)