Skip to content

Commit

Permalink
fixed Cppcheck warnings (#315)
Browse files Browse the repository at this point in the history
* fixed `missingOverride` Cppcheck warnings

* fixed `operatorEqVarError` Cppcheck warning

* fixed `noCopyConstructor` Cppcheck warning

* fixed `noOperatorEq` Cppcheck warning

* fixed `noExplicitConstructor` Cppcheck warnings

* suppressed `noConstructor` Cppcheck warning

* fixed `constParameterPointer` Cppcheck warnings

* suppressed `selfAssignment` Cppcheck warnings

* suppressed `duplicateExpressionTernary` Cppcheck warning

* suppressed `uninitDerivedMemberVar` Cppcheck warnings

* fixed `-Winconsistent-missing-destructor-override` Clang warning
  • Loading branch information
firewave authored Sep 11, 2023
1 parent adeff2a commit 62a8c7b
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
#undef ERROR
#endif

#if __cplusplus >= 201103L
#define OVERRIDE override
#define EXPLICIT explicit
#else
#define OVERRIDE
#define EXPLICIT
#endif

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#define nullptr NULL
#endif
Expand Down Expand Up @@ -236,6 +244,7 @@ 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() {}
Expand Down Expand Up @@ -354,23 +363,24 @@ class simplecpp::TokenList::Stream {

class StdIStream : public simplecpp::TokenList::Stream {
public:
StdIStream(std::istream &istr)
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
EXPLICIT StdIStream(std::istream &istr)
: istr(istr)
{
assert(istr.good());
init();
}

virtual int get() {
virtual int get() OVERRIDE {
return istr.get();
}
virtual int peek() {
virtual int peek() OVERRIDE {
return istr.peek();
}
virtual void unget() {
virtual void unget() OVERRIDE {
istr.unget();
}
virtual bool good() {
virtual bool good() OVERRIDE {
return istr.good();
}

Expand All @@ -380,7 +390,8 @@ class StdIStream : public simplecpp::TokenList::Stream {

class FileStream : public simplecpp::TokenList::Stream {
public:
FileStream(const std::string &filename)
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
EXPLICIT FileStream(const std::string &filename)
: file(fopen(filename.c_str(), "rb"))
, lastCh(0)
, lastStatus(0)
Expand All @@ -389,25 +400,25 @@ class FileStream : public simplecpp::TokenList::Stream {
init();
}

~FileStream() {
~FileStream() OVERRIDE {
fclose(file);
file = nullptr;
}

virtual int get() {
virtual int get() OVERRIDE {
lastStatus = lastCh = fgetc(file);
return lastCh;
}
virtual int peek() {
virtual int peek() OVERRIDE{
// keep lastCh intact
const int ch = fgetc(file);
unget_internal(ch);
return ch;
}
virtual void unget() {
virtual void unget() OVERRIDE {
unget_internal(lastCh);
}
virtual bool good() {
virtual bool good() OVERRIDE {
return lastStatus != EOF;
}

Expand All @@ -422,6 +433,9 @@ class FileStream : public simplecpp::TokenList::Stream {
ungetc(ch, file);
}

FileStream(const FileStream&);
FileStream &operator=(const FileStream&);

FILE *file;
int lastCh;
int lastStatus;
Expand Down Expand Up @@ -1437,6 +1451,7 @@ namespace simplecpp {
tokenListDefine = other.tokenListDefine;
parseDefine(tokenListDefine.cfront());
}
usageList = other.usageList;
}
return *this;
}
Expand Down Expand Up @@ -2502,6 +2517,7 @@ namespace simplecpp {
if (unc)
path = '/' + path;

// cppcheck-suppress duplicateExpressionTernary - platform-dependent implementation
return strpbrk(path.c_str(), "*?") == nullptr ? realFilename(path) : path;
}
}
Expand Down Expand Up @@ -2595,6 +2611,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI

for (simplecpp::Token *headerToken = tok1->next; headerToken != tok3; headerToken = headerToken->next)
header += headerToken->str();
// cppcheck-suppress selfAssignment - platform-dependent implementation
header = realFilename(header);
}
else {
Expand Down Expand Up @@ -3164,14 +3181,14 @@ static void getLocaltime(struct tm &ltime)
#endif
}

static std::string getDateDefine(struct tm *timep)
static std::string getDateDefine(const struct tm *timep)
{
char buf[] = "??? ?? ????";
strftime(buf, sizeof(buf), "%b %d %Y", timep);
return std::string("\"").append(buf).append("\"");
}

static std::string getTimeDefine(struct tm *timep)
static std::string getTimeDefine(const struct tm *timep)
{
char buf[] = "??:??:??";
strftime(buf, sizeof(buf), "%T", timep);
Expand Down Expand Up @@ -3484,6 +3501,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
if (systemheader) {
while ((tok = tok->next) && tok->op != '>')
header += tok->str();
// cppcheck-suppress selfAssignment - platform-dependent implementation
header = realFilename(header);
if (tok && tok->op == '>')
closingAngularBracket = true;
Expand Down

0 comments on commit 62a8c7b

Please sign in to comment.