From a7100e269400284afd87aea6987e2cb7ee565610 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 24 Sep 2025 19:58:22 +0200 Subject: [PATCH 1/3] 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 8f1879ef..79650a55 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 e164fa90..e3447ce8 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -71,6 +71,65 @@ namespace simplecpp { class Macro; class FileDataCache; + // 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 { @@ -364,8 +423,8 @@ namespace simplecpp { unsigned int fileIndex(const std::string &filename); - Token *frontToken; - Token *backToken; + constness_ptr frontToken; + constness_ptr backToken; std::vector &files; }; From bbfc7263e7b0143f946eaaa73b30149289a7708c Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 25 Sep 2025 08:27:28 +0200 Subject: [PATCH 2/3] s --- simplecpp.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simplecpp.h b/simplecpp.h index e3447ce8..d7fc0a81 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 b00c0421ccb78db8e77e9fe65b390d8b0f8840c2 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 27 Sep 2025 10:48:04 +0200 Subject: [PATCH 3/3] const --- simplecpp.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 79650a55..8511c84f 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1354,7 +1354,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() != "?") @@ -1950,7 +1949,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;