Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/cppcheck.yml
Original file line number Diff line number Diff line change
@@ -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
32 changes: 15 additions & 17 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <fstream>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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)
Expand Down Expand Up @@ -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<std::string> &files)
: file(fopen(filename.c_str(), "rb"))
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<unsigned int>(std::distance(files.cbegin(), it));
files.emplace_back(filename);
return files.size() - 1U;
}
Expand Down Expand Up @@ -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<class GatherCall>
const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &expandedmacros, OutputList *outputList, GatherCall gatherCall) const {
const Token *expandTrailingCalls(TokenList &output, TokenList &temp, const Location &loc, const Token *tok, const MacroMap &macros, const std::set<TokenString> &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);
Expand Down
4 changes: 2 additions & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
Loading