diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml new file mode 100644 index 00000000..afe8cf98 --- /dev/null +++ b/.github/workflows/cppcheck.yml @@ -0,0 +1,56 @@ +# 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=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 eb484884..2b4226b9 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -290,7 +291,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 +424,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 +435,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 +462,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 +492,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")) { @@ -859,7 +856,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(); @@ -1844,10 +1841,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; } @@ -2675,16 +2673,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