Skip to content

Commit 96e5990

Browse files
authored
Fix #771 (Add cppcheck self check and fix cppcheck warnings) (#772)
1 parent 76d62d1 commit 96e5990

3 files changed

Lines changed: 71 additions & 14 deletions

File tree

‎.github/workflows/cppcheck.yml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
2+
# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
3+
name: cppcheck
4+
5+
on: [push, pull_request]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
selfcheck:
12+
13+
runs-on: ubuntu-26.04
14+
15+
defaults:
16+
run:
17+
shell: bash -euo pipefail {0}
18+
19+
env:
20+
CPPCHECK_VERSION: '2.22.0'
21+
CPPCHECK_INSTALL_DIR: ${{ github.workspace }}/cppcheck-install
22+
23+
steps:
24+
- uses: actions/checkout@v6
25+
with:
26+
persist-credentials: false
27+
28+
- name: Cache cppcheck
29+
uses: actions/cache@v4
30+
id: cache-cppcheck
31+
with:
32+
path: ${{ env.CPPCHECK_INSTALL_DIR }}
33+
key: ${{ runner.os }}-cppcheck-${{ env.CPPCHECK_VERSION }}
34+
35+
- name: Build cppcheck
36+
if: steps.cache-cppcheck.outputs.cache-hit != 'true'
37+
run: |
38+
git clone --depth 1 --branch "$CPPCHECK_VERSION" https://github.com/danmar/cppcheck.git cppcheck-src
39+
cmake -S cppcheck-src -B cppcheck-src/build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$CPPCHECK_INSTALL_DIR"
40+
cmake --build cppcheck-src/build -j "$(nproc)"
41+
cmake --install cppcheck-src/build
42+
43+
- name: Self check
44+
run: |
45+
"$CPPCHECK_INSTALL_DIR/bin/cppcheck" --version
46+
"$CPPCHECK_INSTALL_DIR/bin/cppcheck" \
47+
--enable=style,information \
48+
--inconclusive \
49+
--inline-suppr \
50+
--check-level=exhaustive \
51+
--error-exitcode=1 \
52+
--suppress=missingMemberCopy \
53+
--suppress=constParameterReference \
54+
simplecpp.cpp

‎simplecpp.cpp‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <fstream>
3131
#include <iostream>
3232
#include <istream>
33+
#include <iterator>
3334
#include <limits>
3435
#include <list>
3536
#include <map>
@@ -290,7 +291,6 @@ void simplecpp::Token::printOut() const
290291
std::cout << std::endl;
291292
}
292293

293-
// cppcheck-suppress noConstructor - we call init() in the inherited to initialize the private members
294294
class simplecpp::TokenList::Stream {
295295
public:
296296
virtual ~Stream() = default;
@@ -424,6 +424,7 @@ class simplecpp::TokenList::Stream {
424424
return 0;
425425
}
426426

427+
// cppcheck-suppress uninitMemberVarPrivate - FP this class is a abstract base class and its members are initialized in init()
427428
unsigned short bom;
428429

429430
/** characters that are read but not used yet. The next character is at the back. */
@@ -435,11 +436,12 @@ class simplecpp::TokenList::Stream {
435436
/** get() has returned EOF */
436437
bool inputEnd{false};
437438
protected:
439+
// cppcheck-suppress uninitMemberVarNoCtor - FP this class is a abstract base class and its members are initialized in init()
438440
bool isUtf16;
439441
};
440442

441443
namespace {
442-
class StdIStream : public simplecpp::TokenList::Stream {
444+
class StdIStream final : public simplecpp::TokenList::Stream {
443445
public:
444446
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
445447
explicit StdIStream(std::istream &istr)
@@ -463,7 +465,7 @@ namespace {
463465
std::istream &istr;
464466
};
465467

466-
class StdCharBufStream : public simplecpp::TokenList::Stream {
468+
class StdCharBufStream final : public simplecpp::TokenList::Stream {
467469
public:
468470
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
469471
StdCharBufStream(const unsigned char* str, std::size_t size)
@@ -494,7 +496,7 @@ namespace {
494496
std::size_t pos{};
495497
};
496498

497-
class FileStream : public simplecpp::TokenList::Stream {
499+
class FileStream final : public simplecpp::TokenList::Stream {
498500
public:
499501
/**
500502
* @throws simplecpp::Output thrown if file is not found
@@ -859,7 +861,7 @@ static simplecpp::TokenString readLineComment(simplecpp::TokenList::Stream &stre
859861
return currentToken;
860862
}
861863

862-
simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline)
864+
simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline) const
863865
{
864866
TokenString currentToken = "/*";
865867
(void)stream.readChar();
@@ -1844,10 +1846,11 @@ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor(int maxsize
18441846

18451847
unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)
18461848
{
1847-
for (unsigned int i = 0; i < files.size(); ++i) {
1848-
if (files[i] == filename)
1849-
return i;
1850-
}
1849+
const auto it = std::find_if(files.cbegin(), files.cend(), [&filename](const std::string &f) {
1850+
return f == filename;
1851+
});
1852+
if (it != files.cend())
1853+
return static_cast<unsigned int>(std::distance(files.cbegin(), it));
18511854
files.emplace_back(filename);
18521855
return files.size() - 1U;
18531856
}
@@ -2675,16 +2678,16 @@ namespace simplecpp {
26752678

26762679
/** Expand while the expansion result in @p temp ends with the name of a
26772680
* function-like macro whose arguments are supplied by the tokens after @p tok.
2678-
* Each round consumes that macro call from the token stream. The @p gatherCall
2681+
* Each round consumes that macro call from the token stream. The @p gather
26792682
* callback copies the macro name and its call into its list and returns the
26802683
* last token of the call, or nullptr when no complete call is available (see
26812684
* gatherCall and gatherCallAfter). Returns the first token that was not
26822685
* consumed (tok->next when no call was expanded). */
26832686
template<class GatherCall>
2684-
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 {
2687+
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 {
26852688
while (const Macro * const calledMacro = callableMacro(temp.cback(), macros, expandedmacros)) {
26862689
TokenList call(files);
2687-
const Token * const last = gatherCall(call, temp.cback(), tok);
2690+
const Token * const last = gather(call, temp.cback(), tok);
26882691
if (!last)
26892692
break;
26902693
output.takeTokens(temp);

‎simplecpp.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ namespace simplecpp {
413413

414414
void combineOperators();
415415

416-
void constFoldUnaryNotPosNeg(Token *tok);
416+
void constFoldUnaryNotPosNeg(Token *tok1);
417417
/**
418418
* @throws std::overflow_error thrown on overflow or division by zero
419419
*/
@@ -444,7 +444,7 @@ namespace simplecpp {
444444
* @param multiline incremented for each newline that is removed
445445
* @return the comment text
446446
*/
447-
TokenString readBlockComment(Stream &stream, unsigned int &multiline);
447+
TokenString readBlockComment(Stream &stream, unsigned int &multiline) const;
448448
/**
449449
* Read the text of a #error or #warning directive, up to but not
450450
* including the newline, and add it as one token. A backslash-newline

0 commit comments

Comments
 (0)