From 21530f5be182614337c6cf7999c7afdc77de7f23 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 24 Sep 2025 19:58:22 +0200 Subject: [PATCH 1/4] added `constness_ptr` as pointer wrapper to ensure actual method constness --- simplecpp.cpp | 2 +- simplecpp.h | 67 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 8e10ca54..e9a18f5e 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -540,7 +540,7 @@ void simplecpp::TokenList::clear() backToken = nullptr; while (frontToken) { Token * const next = frontToken->next; - delete frontToken; + delete frontToken.get(); frontToken = next; } sizeOfType.clear(); diff --git a/simplecpp.h b/simplecpp.h index 15187063..6c823991 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -71,6 +71,65 @@ namespace simplecpp { using TokenString = std::string; class Macro; + // as std::optional behaves similarly we could use that instead if we ever move to C++17. + // it is not a simple drop-in as our "operator bool()" indicates if the pointer is non-null + // whereas std::optional indicates if a value is set. + // + // This is similar to std::experimental::propagate_const + // see https://en.cppreference.com/w/cpp/experimental/propagate_const + template + class constness_ptr + { + public: + explicit constness_ptr(T* p) + : mPtr(p) + {} + + constness_ptr &operator=(T* p) { + mPtr = p; + return *this; + } + + T* get() noexcept { + return mPtr; + } + + const T* get() const noexcept { + return mPtr; + } + + operator T*() noexcept { + return mPtr; + } + + operator const T*() const noexcept { + return mPtr; + } + + T* operator->() noexcept { + return mPtr; + } + + const T* operator->() const noexcept { + return mPtr; + } + + T& operator*() noexcept { + return *mPtr; + } + + const T& operator*() const noexcept { + return *mPtr; + } + + explicit operator bool() const noexcept { + return mPtr != nullptr; + } + + private: + T* mPtr; + }; + /** * Location in source code */ @@ -155,8 +214,8 @@ namespace simplecpp { bool number; bool whitespaceahead; Location location; - Token *previous{}; - Token *next{}; + constness_ptr previous; + constness_ptr next; mutable const Token *nextcond{}; const Token *previousSkipComments() const { @@ -363,8 +422,8 @@ namespace simplecpp { unsigned int fileIndex(const std::string &filename); - Token *frontToken; - Token *backToken; + constness_ptr frontToken; + constness_ptr backToken; std::vector &files; }; From 20c6d66e4fa9bf3e8fff3e6ec7e34ff2e59e71ad Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 25 Sep 2025 08:27:28 +0200 Subject: [PATCH 2/4] s --- simplecpp.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simplecpp.h b/simplecpp.h index 6c823991..a88d7e30 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -81,7 +81,10 @@ namespace simplecpp { class constness_ptr { public: - explicit constness_ptr(T* p) +#ifndef _MSC_VER + explicit +#endif + constness_ptr(T* p) : mPtr(p) {} From 1226bf4aa51e8c641b8f7cf7a869c9b21e8af93f Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 27 Sep 2025 10:48:04 +0200 Subject: [PATCH 3/4] const --- simplecpp.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index e9a18f5e..2f9f8584 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1357,7 +1357,6 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok) void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1) { bool gotoTok1 = false; - // NOLINTNEXTLINE(misc-const-correctness) - technically correct but used to access non-const data for (Token *tok = tok1; tok && tok->op != ')'; tok = gotoTok1 ? tok1 : tok->next) { gotoTok1 = false; if (tok->str() != "?") @@ -1954,7 +1953,6 @@ namespace simplecpp { } } - // NOLINTNEXTLINE(misc-const-correctness) - technically correct but used to access non-const data Token * const output_end_1 = output.back(); const Token *valueToken2; From ff8e9e3e1bd5fc81f2b2e723f75a05169e2874af Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 4 Dec 2025 14:03:41 +0100 Subject: [PATCH 4/4] s --- simplecpp.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simplecpp.h b/simplecpp.h index a88d7e30..573b728f 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -81,6 +81,7 @@ namespace simplecpp { class constness_ptr { public: + constness_ptr() = default; #ifndef _MSC_VER explicit #endif @@ -130,7 +131,7 @@ namespace simplecpp { } private: - T* mPtr; + T* mPtr{}; }; /**