From 969e2d9b6f0d3302d96d0d66e2b56f3ea0de4ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Nov 2026 11:37:21 +0100 Subject: [PATCH 1/4] Fix #771 (Add cppcheck self check and fix cppcheck warnings) --- .github/workflows/cppcheck.yml | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .github/workflows/cppcheck.yml diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml new file mode 100644 index 00000000..efd5ca5f --- /dev/null +++ b/.github/workflows/cppcheck.yml @@ -0,0 +1,55 @@ +# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions +# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners +name: cppcheck + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + selfcheck: + + runs-on: ubuntu-24.04 + + defaults: + run: + shell: bash -euo pipefail {0} + + env: + CPPCHECK_VERSION: '2.22.0' + CPPCHECK_INSTALL_DIR: ${{ github.workspace }}/cppcheck-install + + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - name: Cache cppcheck + uses: actions/cache@v4 + id: cache-cppcheck + with: + path: ${{ env.CPPCHECK_INSTALL_DIR }} + key: ${{ runner.os }}-cppcheck-${{ env.CPPCHECK_VERSION }} + + - name: Build cppcheck + if: steps.cache-cppcheck.outputs.cache-hit != 'true' + run: | + git clone --depth 1 --branch "$CPPCHECK_VERSION" https://github.com/danmar/cppcheck.git cppcheck-src + cmake -S cppcheck-src -B cppcheck-src/build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$CPPCHECK_INSTALL_DIR" + cmake --build cppcheck-src/build -j "$(nproc)" + cmake --install cppcheck-src/build + + - name: Self check + run: | + "$CPPCHECK_INSTALL_DIR/bin/cppcheck" --version + "$CPPCHECK_INSTALL_DIR/bin/cppcheck" \ + --enable=style,information \ + --inconclusive \ + --inline-suppr \ + --check-level=exhaustive \ + --error-exitcode=1 \ + --suppress=missingIncludeSystem \ + --suppress=checkersReport \ + --template='{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]' \ + simplecpp.cpp From bc2763538c1c0d56f316780dddaa5200bddacb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Nov 2026 11:55:19 +0100 Subject: [PATCH 2/4] fixes --- simplecpp.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index eb484884..081523f5 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -290,7 +290,6 @@ void simplecpp::Token::printOut() const std::cout << std::endl; } -// cppcheck-suppress noConstructor - we call init() in the inherited to initialize the private members class simplecpp::TokenList::Stream { public: virtual ~Stream() = default; @@ -424,7 +423,7 @@ class simplecpp::TokenList::Stream { return 0; } - unsigned short bom; + unsigned short bom{0}; /** characters that are read but not used yet. The next character is at the back. */ std::string buffer; @@ -435,13 +434,12 @@ class simplecpp::TokenList::Stream { /** get() has returned EOF */ bool inputEnd{false}; protected: - bool isUtf16; + bool isUtf16{false}; }; namespace { - class StdIStream : public simplecpp::TokenList::Stream { + class StdIStream final : public simplecpp::TokenList::Stream { public: - // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members explicit StdIStream(std::istream &istr) : istr(istr) { assert(istr.good()); @@ -463,9 +461,8 @@ namespace { std::istream &istr; }; - class StdCharBufStream : public simplecpp::TokenList::Stream { + class StdCharBufStream final : public simplecpp::TokenList::Stream { public: - // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members StdCharBufStream(const unsigned char* str, std::size_t size) : str(str) , size(size) @@ -494,12 +491,11 @@ namespace { std::size_t pos{}; }; - class FileStream : public simplecpp::TokenList::Stream { + class FileStream final : public simplecpp::TokenList::Stream { public: /** * @throws simplecpp::Output thrown if file is not found */ - // cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members explicit FileStream(const std::string &filename, std::vector &files) : file(fopen(filename.c_str(), "rb")) { From 08c53a77f8f15be0bbc981eb4943230d90ba783c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Nov 2026 12:56:37 +0100 Subject: [PATCH 3/4] Fix cppcheck warnings and update selfcheck suppressions - make TokenList::readBlockComment const - use the same parameter name in the constFoldUnaryNotPosNeg declaration and definition - rename the expandTrailingCalls callback parameter so it does not shadow gatherCall - use std::find_if in TokenList::fileIndex - selfcheck: suppress missingMemberCopy and constParameterReference, drop the unmatched missingIncludeSystem suppression Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cppcheck.yml | 3 ++- simplecpp.cpp | 17 +++++++++-------- simplecpp.h | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml index efd5ca5f..afe8cf98 100644 --- a/.github/workflows/cppcheck.yml +++ b/.github/workflows/cppcheck.yml @@ -49,7 +49,8 @@ jobs: --inline-suppr \ --check-level=exhaustive \ --error-exitcode=1 \ - --suppress=missingIncludeSystem \ --suppress=checkersReport \ + --suppress=missingMemberCopy \ + --suppress=constParameterReference \ --template='{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]' \ simplecpp.cpp diff --git a/simplecpp.cpp b/simplecpp.cpp index 081523f5..1a12023e 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -855,7 +855,7 @@ static simplecpp::TokenString readLineComment(simplecpp::TokenList::Stream &stre return currentToken; } -simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline) +simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline) const { TokenString currentToken = "/*"; (void)stream.readChar(); @@ -1840,10 +1840,11 @@ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize unsigned int simplecpp::TokenList::fileIndex(const std::string &filename) { - for (unsigned int i = 0; i < files.size(); ++i) { - if (files[i] == filename) - return i; - } + const auto it = std::find_if(files.cbegin(), files.cend(), [&filename](const std::string &f) { + return f == filename; + }); + if (it != files.cend()) + return static_cast(std::distance(files.cbegin(), it)); files.emplace_back(filename); return files.size() - 1U; } @@ -2671,16 +2672,16 @@ namespace simplecpp { /** Expand while the expansion result in @p temp ends with the name of a * function-like macro whose arguments are supplied by the tokens after @p tok. - * Each round consumes that macro call from the token stream. The @p gatherCall + * Each round consumes that macro call from the token stream. The @p gather * callback copies the macro name and its call into its list and returns the * last token of the call, or nullptr when no complete call is available (see * gatherCall and gatherCallAfter). Returns the first token that was not * consumed (tok->next when no call was expanded). */ template - const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set &expandedmacros, OutputList *outputList, GatherCall gatherCall) const { + const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap ¯os, const std::set &expandedmacros, OutputList *outputList, GatherCall gather) const { while (const Macro * const calledMacro = callableMacro(temp.cback(), macros, expandedmacros)) { TokenList call(files); - const Token * const last = gatherCall(call, temp.cback(), tok); + const Token * const last = gather(call, temp.cback(), tok); if (!last) break; output.takeTokens(temp); diff --git a/simplecpp.h b/simplecpp.h index f7d63b37..8d280675 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -413,7 +413,7 @@ namespace simplecpp { void combineOperators(); - void constFoldUnaryNotPosNeg(Token *tok); + void constFoldUnaryNotPosNeg(Token *tok1); /** * @throws std::overflow_error thrown on overflow or division by zero */ @@ -444,7 +444,7 @@ namespace simplecpp { * @param multiline incremented for each newline that is removed * @return the comment text */ - TokenString readBlockComment(Stream &stream, unsigned int &multiline); + TokenString readBlockComment(Stream &stream, unsigned int &multiline) const; /** * Read the text of a #error or #warning directive, up to but not * including the newline, and add it as one token. A backslash-newline From e674f99132e9fb9075a0cc4cee13cc3e453219dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 20 Nov 2026 13:36:20 +0100 Subject: [PATCH 4/4] Include for std::distance Co-Authored-By: Claude Opus 5.5 --- simplecpp.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/simplecpp.cpp b/simplecpp.cpp index 1a12023e..2b4226b9 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include