Skip to content

Commit 0f3e4ff

Browse files
committed
extracted code for actual check implementation from Check into CheckImpl
1 parent 72547b3 commit 0f3e4ff

68 files changed

Lines changed: 1735 additions & 1744 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 57 additions & 57 deletions
Large diffs are not rendered by default.

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ bool isWithoutSideEffects(const Token* tok, bool checkArrayAccess, bool checkRef
21572157
if (tok && tok->varId()) {
21582158
const Variable* var = tok->variable();
21592159
return var && ((!var->isClass() && (checkReference || !var->isReference())) || var->isPointer() ||
2160-
(checkArrayAccess ? var->isArray() || (var->isStlType() && !var->isStlType(CheckClass::stl_containers_not_const)) : var->isStlType()));
2160+
(checkArrayAccess ? var->isArray() || (var->isStlType() && !var->isStlType(CheckClassImpl::stl_containers_not_const)) : var->isStlType()));
21612161
}
21622162
return true;
21632163
}

lib/check.h

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
//---------------------------------------------------------------------------
2323

2424
#include "config.h"
25-
#include "errortypes.h"
2625

2726
#include <list>
2827
#include <string>
@@ -36,14 +35,8 @@ namespace CTU {
3635
class FileInfo;
3736
}
3837

39-
namespace ValueFlow {
40-
class Value;
41-
}
42-
4338
class Settings;
44-
class Token;
4539
class ErrorLogger;
46-
class ErrorMessage;
4740
class Tokenizer;
4841

4942
/** Use WRONG_DATA in checkers to mark conditions that check that data is correct */
@@ -59,17 +52,9 @@ class Tokenizer;
5952
class CPPCHECKLIB Check {
6053
public:
6154
/** This constructor is used when registering the CheckClass */
62-
explicit Check(std::string aname);
63-
64-
protected:
65-
/** This constructor is used when running checks. */
66-
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
67-
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}
68-
69-
private:
70-
static std::list<Check *> &instances_internal();
71-
72-
public:
55+
explicit Check(std::string aname)
56+
: mName(std::move(aname))
57+
{}
7358
virtual ~Check() = default;
7459

7560
Check(const Check &) = delete;
@@ -89,13 +74,6 @@ class CPPCHECKLIB Check {
8974
/** get information about this class, used to generate documentation */
9075
virtual std::string classInfo() const = 0;
9176

92-
/**
93-
* Write given error to stdout in xml format.
94-
* This is for for printout out the error list with --errorlist
95-
* @param errmsg Error message to write
96-
*/
97-
static void writeToErrorList(const ErrorMessage &errmsg);
98-
9977
/** Base class used for whole-program analysis */
10078
class CPPCHECKLIB FileInfo {
10179
public:
@@ -121,40 +99,6 @@ class CPPCHECKLIB Check {
12199
return false;
122100
}
123101

124-
protected:
125-
static std::string getMessageId(const ValueFlow::Value &value, const char id[]);
126-
127-
const Tokenizer* const mTokenizer{};
128-
const Settings* const mSettings{};
129-
ErrorLogger* const mErrorLogger{};
130-
131-
/** report an error */
132-
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg) {
133-
reportError(tok, severity, id, msg, CWE(0U), Certainty::normal);
134-
}
135-
136-
/** report an error */
137-
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) {
138-
const std::list<const Token *> callstack(1, tok);
139-
reportError(callstack, severity, id, msg, cwe, certainty);
140-
}
141-
142-
/** report an error */
143-
void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty);
144-
145-
void reportError(ErrorPath errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty);
146-
147-
/** log checker */
148-
void logChecker(const char id[]);
149-
150-
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const;
151-
152-
/**
153-
* Use WRONG_DATA in checkers when you check for wrong data. That
154-
* will call this method
155-
*/
156-
bool wrongData(const Token *tok, const char *str);
157-
158102
private:
159103
const std::string mName;
160104
};

lib/check64bit.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static bool isFunctionPointer(const Token* tok)
5151
return Tokenizer::isFunctionPointer(tok->variable()->nameToken());
5252
}
5353

54-
void Check64BitPortability::pointerassignment()
54+
void Check64BitPortabilityImpl::pointerassignment()
5555
{
5656
if (!mSettings->severity.isEnabled(Severity::portability))
5757
return;
@@ -139,7 +139,7 @@ void Check64BitPortability::pointerassignment()
139139
}
140140
}
141141

142-
void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
142+
void Check64BitPortabilityImpl::assignmentAddressToIntegerError(const Token *tok)
143143
{
144144
reportError(tok, Severity::portability,
145145
"AssignmentAddressToInteger",
@@ -150,7 +150,7 @@ void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
150150
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
151151
}
152152

153-
void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
153+
void Check64BitPortabilityImpl::assignmentIntegerToAddressError(const Token *tok)
154154
{
155155
reportError(tok, Severity::portability,
156156
"AssignmentIntegerToAddress",
@@ -161,7 +161,7 @@ void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
161161
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
162162
}
163163

164-
void Check64BitPortability::returnPointerError(const Token *tok)
164+
void Check64BitPortabilityImpl::returnPointerError(const Token *tok)
165165
{
166166
reportError(tok, Severity::portability,
167167
"CastAddressToIntegerAtReturn",
@@ -172,7 +172,7 @@ void Check64BitPortability::returnPointerError(const Token *tok)
172172
"to 32-bit integer. The safe way is to return a type such as intptr_t.", CWE758, Certainty::normal);
173173
}
174174

175-
void Check64BitPortability::returnIntegerError(const Token *tok)
175+
void Check64BitPortabilityImpl::returnIntegerError(const Token *tok)
176176
{
177177
reportError(tok, Severity::portability,
178178
"CastIntegerToAddressAtReturn",
@@ -185,13 +185,13 @@ void Check64BitPortability::returnIntegerError(const Token *tok)
185185

186186
void Check64BitPortability::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
187187
{
188-
Check64BitPortability check64BitPortability(&tokenizer, &tokenizer.getSettings(), errorLogger);
188+
Check64BitPortabilityImpl check64BitPortability(&tokenizer, &tokenizer.getSettings(), errorLogger);
189189
check64BitPortability.pointerassignment();
190190
}
191191

192192
void Check64BitPortability::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
193193
{
194-
Check64BitPortability c(nullptr, settings, errorLogger);
194+
Check64BitPortabilityImpl c(nullptr, settings, errorLogger);
195195
c.assignmentAddressToIntegerError(nullptr);
196196
c.assignmentIntegerToAddressError(nullptr);
197197
c.returnIntegerError(nullptr);

lib/check64bit.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//---------------------------------------------------------------------------
2424

2525
#include "check.h"
26+
#include "checkimpl.h"
2627
#include "config.h"
2728

2829
#include <string>
@@ -44,36 +45,35 @@ class CPPCHECKLIB Check64BitPortability : public Check {
4445

4546
public:
4647
/** This constructor is used when registering the Check64BitPortability */
47-
Check64BitPortability() : Check(myName()) {}
48+
Check64BitPortability() : Check("64-bit portability") {}
4849

4950
private:
50-
/** This constructor is used when running checks. */
51-
Check64BitPortability(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
52-
: Check(myName(), tokenizer, settings, errorLogger) {}
53-
5451
/** @brief Run checks against the normal token list */
5552
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
5653

57-
/** Check for pointer assignment */
58-
void pointerassignment();
59-
60-
void assignmentAddressToIntegerError(const Token *tok);
61-
void assignmentIntegerToAddressError(const Token *tok);
62-
void returnIntegerError(const Token *tok);
63-
void returnPointerError(const Token *tok);
64-
6554
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
6655

67-
static std::string myName() {
68-
return "64-bit portability";
69-
}
70-
7156
std::string classInfo() const override {
7257
return "Check if there is 64-bit portability issues:\n"
7358
"- assign address to/from int/long\n"
7459
"- casting address from/to integer when returning from function\n";
7560
}
7661
};
62+
63+
class Check64BitPortabilityImpl : public CheckImpl {
64+
public:
65+
/** This constructor is used when running checks. */
66+
Check64BitPortabilityImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
67+
: CheckImpl(tokenizer, settings, errorLogger) {}
68+
69+
/** Check for pointer assignment */
70+
void pointerassignment();
71+
72+
void assignmentAddressToIntegerError(const Token *tok);
73+
void assignmentIntegerToAddressError(const Token *tok);
74+
void returnIntegerError(const Token *tok);
75+
void returnPointerError(const Token *tok);
76+
};
7777
/// @}
7878
//---------------------------------------------------------------------------
7979
#endif // check64bitH

lib/checkassert.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// CWE ids used
4040
static const CWE CWE398(398U); // Indicator of Poor Code Quality
4141

42-
void CheckAssert::assertWithSideEffects()
42+
void CheckAssertImpl::assertWithSideEffects()
4343
{
4444
if (!mSettings->severity.isEnabled(Severity::warning))
4545
return;
@@ -117,7 +117,7 @@ void CheckAssert::assertWithSideEffects()
117117
//---------------------------------------------------------------------------
118118

119119

120-
void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& functionName)
120+
void CheckAssertImpl::sideEffectInAssertError(const Token *tok, const std::string& functionName)
121121
{
122122
reportError(tok, Severity::warning,
123123
"assertWithSideEffect",
@@ -129,7 +129,7 @@ void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& f
129129
"builds, this is a bug.", CWE398, Certainty::normal);
130130
}
131131

132-
void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& varname)
132+
void CheckAssertImpl::assignmentInAssertError(const Token *tok, const std::string& varname)
133133
{
134134
reportError(tok, Severity::warning,
135135
"assignmentInAssert",
@@ -142,7 +142,7 @@ void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& v
142142
}
143143

144144
// checks if side effects happen on the variable prior to tmp
145-
void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
145+
void CheckAssertImpl::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
146146
{
147147
if (!assignTok->isAssignmentOp() && assignTok->tokType() != Token::eIncDecOp)
148148
return;
@@ -172,21 +172,21 @@ void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *a
172172
// TODO: function calls on var
173173
}
174174

175-
bool CheckAssert::inSameScope(const Token* returnTok, const Token* assignTok)
175+
bool CheckAssertImpl::inSameScope(const Token* returnTok, const Token* assignTok)
176176
{
177177
// TODO: even if a return is in the same scope, the assignment might not affect it.
178178
return returnTok->scope() == assignTok->scope();
179179
}
180180

181181
void CheckAssert::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
182182
{
183-
CheckAssert checkAssert(&tokenizer, &tokenizer.getSettings(), errorLogger);
183+
CheckAssertImpl checkAssert(&tokenizer, &tokenizer.getSettings(), errorLogger);
184184
checkAssert.assertWithSideEffects();
185185
}
186186

187187
void CheckAssert::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
188188
{
189-
CheckAssert c(nullptr, settings, errorLogger);
189+
CheckAssertImpl c(nullptr, settings, errorLogger);
190190
c.sideEffectInAssertError(nullptr, "function");
191191
c.assignmentInAssertError(nullptr, "var");
192192
}

lib/checkassert.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//---------------------------------------------------------------------------
2424

2525
#include "check.h"
26+
#include "checkimpl.h"
2627
#include "config.h"
2728

2829
#include <string>
@@ -42,14 +43,22 @@ class Tokenizer;
4243

4344
class CPPCHECKLIB CheckAssert : public Check {
4445
public:
45-
CheckAssert() : Check(myName()) {}
46+
CheckAssert() : Check("Assert") {}
4647

4748
private:
48-
CheckAssert(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
49-
: Check(myName(), tokenizer, settings, errorLogger) {}
50-
5149
/** run checks, the token list is not simplified */
5250
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
51+
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
52+
53+
std::string classInfo() const override {
54+
return "Warn if there are side effects in assert statements (since this cause different behaviour in debug/release builds).\n";
55+
}
56+
};
57+
58+
class CheckAssertImpl: public CheckImpl {
59+
public:
60+
CheckAssertImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
61+
: CheckImpl(tokenizer, settings, errorLogger) {}
5362

5463
void assertWithSideEffects();
5564

@@ -58,16 +67,6 @@ class CPPCHECKLIB CheckAssert : public Check {
5867

5968
void sideEffectInAssertError(const Token *tok, const std::string& functionName);
6069
void assignmentInAssertError(const Token *tok, const std::string &varname);
61-
62-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
63-
64-
static std::string myName() {
65-
return "Assert";
66-
}
67-
68-
std::string classInfo() const override {
69-
return "Warn if there are side effects in assert statements (since this cause different behaviour in debug/release builds).\n";
70-
}
7170
};
7271
/// @}
7372
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)