Skip to content

Commit 8096560

Browse files
committed
extracted code for actual check implementation from Check into CheckImpl
1 parent 966a89d commit 8096560

58 files changed

Lines changed: 3531 additions & 3392 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: 33 additions & 29 deletions
Large diffs are not rendered by default.

lib/check.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,6 @@ void Check::writeToErrorList(const ErrorMessage &errmsg)
6060
std::cout << errmsg.toXML() << std::endl;
6161
}
6262

63-
64-
void Check::reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
65-
{
66-
const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
67-
if (mErrorLogger)
68-
mErrorLogger->reportErr(errmsg);
69-
else
70-
writeToErrorList(errmsg);
71-
}
72-
73-
void Check::reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
74-
{
75-
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
76-
if (mErrorLogger)
77-
mErrorLogger->reportErr(errmsg);
78-
else
79-
writeToErrorList(errmsg);
80-
}
81-
82-
bool Check::wrongData(const Token *tok, const char *str)
83-
{
84-
if (mSettings->daca)
85-
reportError(tok, Severity::debug, "DacaWrongData", "Wrong data detected by condition " + std::string(str));
86-
return true;
87-
}
88-
8963
std::list<Check *> &Check::instances()
9064
{
9165
#ifdef __SVR4
@@ -98,36 +72,3 @@ std::list<Check *> &Check::instances()
9872
return _instances;
9973
#endif
10074
}
101-
102-
std::string Check::getMessageId(const ValueFlow::Value &value, const char id[])
103-
{
104-
if (value.condition != nullptr)
105-
return id + std::string("Cond");
106-
if (value.safe)
107-
return std::string("safe") + (char)std::toupper(id[0]) + (id + 1);
108-
return id;
109-
}
110-
111-
ErrorPath Check::getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const
112-
{
113-
ErrorPath errorPath;
114-
if (!value) {
115-
errorPath.emplace_back(errtok, std::move(bug));
116-
} else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) {
117-
errorPath = value->errorPath;
118-
errorPath.emplace_back(errtok, std::move(bug));
119-
} else {
120-
if (value->condition)
121-
errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'");
122-
//else if (!value->isKnown() || value->defaultArg)
123-
// errorPath = value->callstack;
124-
errorPath.emplace_back(errtok, std::move(bug));
125-
}
126-
return errorPath;
127-
}
128-
129-
void Check::logChecker(const char id[])
130-
{
131-
reportError(nullptr, Severity::none, "logChecker", id);
132-
}
133-

lib/check.h

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,8 @@ class CPPCHECKLIB Check {
6161
/** This constructor is used when registering the CheckClass */
6262
explicit Check(const std::string &aname);
6363

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-
public:
7064
virtual ~Check() {
71-
if (!mTokenizer)
72-
instances().remove(this);
65+
instances().remove(this);
7366
}
7467

7568
Check(const Check &) = delete;
@@ -129,45 +122,6 @@ class CPPCHECKLIB Check {
129122
return false;
130123
}
131124

132-
protected:
133-
static std::string getMessageId(const ValueFlow::Value &value, const char id[]);
134-
135-
const Tokenizer* const mTokenizer{};
136-
const Settings* const mSettings{};
137-
ErrorLogger* const mErrorLogger{};
138-
139-
/** report an error */
140-
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg) {
141-
reportError(tok, severity, id, msg, CWE(0U), Certainty::normal);
142-
}
143-
144-
/** report an error */
145-
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty) {
146-
const std::list<const Token *> callstack(1, tok);
147-
reportError(callstack, severity, id, msg, cwe, certainty);
148-
}
149-
150-
/** report an error */
151-
void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg) {
152-
reportError(callstack, severity, id, msg, CWE(0U), Certainty::normal);
153-
}
154-
155-
/** report an error */
156-
void reportError(const std::list<const Token *> &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty);
157-
158-
void reportError(const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty);
159-
160-
/** log checker */
161-
void logChecker(const char id[]);
162-
163-
ErrorPath getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const;
164-
165-
/**
166-
* Use WRONG_DATA in checkers when you check for wrong data. That
167-
* will call this method
168-
*/
169-
bool wrongData(const Token *tok, const char *str);
170-
171125
private:
172126
const std::string mName;
173127
};

lib/check64bit.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "check64bit.h"
2424

25+
#include "checkimpl.h"
2526
#include "errortypes.h"
2627
#include "settings.h"
2728
#include "symboldatabase.h"
@@ -40,7 +41,22 @@ namespace {
4041
Check64BitPortability instance;
4142
}
4243

43-
void Check64BitPortability::pointerassignment()
44+
class Check64BitPortabilityImpl : public CheckImpl {
45+
public:
46+
/** This constructor is used when running checks. */
47+
Check64BitPortabilityImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
48+
: CheckImpl(tokenizer, settings, errorLogger) {}
49+
50+
/** Check for pointer assignment */
51+
void pointerassignment();
52+
53+
void assignmentAddressToIntegerError(const Token *tok);
54+
void assignmentIntegerToAddressError(const Token *tok);
55+
void returnIntegerError(const Token *tok);
56+
void returnPointerError(const Token *tok);
57+
};
58+
59+
void Check64BitPortabilityImpl::pointerassignment()
4460
{
4561
if (!mSettings->severity.isEnabled(Severity::portability))
4662
return;
@@ -118,7 +134,7 @@ void Check64BitPortability::pointerassignment()
118134
}
119135
}
120136

121-
void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
137+
void Check64BitPortabilityImpl::assignmentAddressToIntegerError(const Token *tok)
122138
{
123139
reportError(tok, Severity::portability,
124140
"AssignmentAddressToInteger",
@@ -129,7 +145,7 @@ void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
129145
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
130146
}
131147

132-
void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
148+
void Check64BitPortabilityImpl::assignmentIntegerToAddressError(const Token *tok)
133149
{
134150
reportError(tok, Severity::portability,
135151
"AssignmentIntegerToAddress",
@@ -140,7 +156,7 @@ void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
140156
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
141157
}
142158

143-
void Check64BitPortability::returnPointerError(const Token *tok)
159+
void Check64BitPortabilityImpl::returnPointerError(const Token *tok)
144160
{
145161
reportError(tok, Severity::portability,
146162
"CastAddressToIntegerAtReturn",
@@ -151,7 +167,7 @@ void Check64BitPortability::returnPointerError(const Token *tok)
151167
"to 32-bit integer. The safe way is to always return an integer.", CWE758, Certainty::normal);
152168
}
153169

154-
void Check64BitPortability::returnIntegerError(const Token *tok)
170+
void Check64BitPortabilityImpl::returnIntegerError(const Token *tok)
155171
{
156172
reportError(tok, Severity::portability,
157173
"CastIntegerToAddressAtReturn",
@@ -161,3 +177,20 @@ void Check64BitPortability::returnIntegerError(const Token *tok)
161177
"and Linux they are of different width. In worst case you end up casting 64-bit integer down to 32-bit pointer. "
162178
"The safe way is to always return a pointer.", CWE758, Certainty::normal);
163179
}
180+
181+
182+
/** @brief Run checks against the normal token list */
183+
void Check64BitPortability::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
184+
{
185+
Check64BitPortabilityImpl c(&tokenizer, tokenizer.getSettings(), errorLogger);
186+
c.pointerassignment();
187+
}
188+
189+
void Check64BitPortability::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
190+
{
191+
Check64BitPortabilityImpl c(nullptr, settings, errorLogger);
192+
c.assignmentAddressToIntegerError(nullptr);
193+
c.assignmentIntegerToAddressError(nullptr);
194+
c.returnIntegerError(nullptr);
195+
c.returnPointerError(nullptr);
196+
}

lib/check64bit.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@
2424

2525
#include "check.h"
2626
#include "config.h"
27-
#include "tokenize.h"
2827

2928
#include <string>
3029

3130
class ErrorLogger;
3231
class Settings;
33-
class Token;
34-
32+
class Tokenizer;
3533

3634
/// @addtogroup Checks
3735
/// @{
@@ -45,38 +43,13 @@ class CPPCHECKLIB Check64BitPortability : public Check {
4543

4644
public:
4745
/** This constructor is used when registering the Check64BitPortability */
48-
Check64BitPortability() : Check(myName()) {}
46+
Check64BitPortability() : Check("64-bit portability") {}
4947

5048
private:
51-
/** This constructor is used when running checks. */
52-
Check64BitPortability(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
53-
: Check(myName(), tokenizer, settings, errorLogger) {}
54-
5549
/** @brief Run checks against the normal token list */
56-
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
57-
Check64BitPortability check64BitPortability(&tokenizer, tokenizer.getSettings(), errorLogger);
58-
check64BitPortability.pointerassignment();
59-
}
60-
61-
/** Check for pointer assignment */
62-
void pointerassignment();
50+
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
6351

64-
void assignmentAddressToIntegerError(const Token *tok);
65-
void assignmentIntegerToAddressError(const Token *tok);
66-
void returnIntegerError(const Token *tok);
67-
void returnPointerError(const Token *tok);
68-
69-
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
70-
Check64BitPortability c(nullptr, settings, errorLogger);
71-
c.assignmentAddressToIntegerError(nullptr);
72-
c.assignmentIntegerToAddressError(nullptr);
73-
c.returnIntegerError(nullptr);
74-
c.returnPointerError(nullptr);
75-
}
76-
77-
static std::string myName() {
78-
return "64-bit portability";
79-
}
52+
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
8053

8154
std::string classInfo() const override {
8255
return "Check if there is 64-bit portability issues:\n"

lib/checkassert.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "checkassert.h"
2424

25+
#include "checkimpl.h"
2526
#include "errortypes.h"
2627
#include "settings.h"
2728
#include "symboldatabase.h"
@@ -39,7 +40,21 @@ namespace {
3940
CheckAssert instance;
4041
}
4142

42-
void CheckAssert::assertWithSideEffects()
43+
class CheckAssertImpl: public CheckImpl {
44+
public:
45+
CheckAssertImpl(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
46+
: CheckImpl(tokenizer, settings, errorLogger) {}
47+
48+
void assertWithSideEffects();
49+
50+
void checkVariableAssignment(const Token* assignTok, const Scope *assertionScope);
51+
static bool inSameScope(const Token* returnTok, const Token* assignTok);
52+
53+
void sideEffectInAssertError(const Token *tok, const std::string& functionName);
54+
void assignmentInAssertError(const Token *tok, const std::string &varname);
55+
};
56+
57+
void CheckAssertImpl::assertWithSideEffects()
4358
{
4459
if (!mSettings->severity.isEnabled(Severity::warning))
4560
return;
@@ -99,7 +114,7 @@ void CheckAssert::assertWithSideEffects()
99114
//---------------------------------------------------------------------------
100115

101116

102-
void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& functionName)
117+
void CheckAssertImpl::sideEffectInAssertError(const Token *tok, const std::string& functionName)
103118
{
104119
reportError(tok, Severity::warning,
105120
"assertWithSideEffect",
@@ -111,7 +126,7 @@ void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& f
111126
"builds, this is a bug.", CWE398, Certainty::normal);
112127
}
113128

114-
void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& varname)
129+
void CheckAssertImpl::assignmentInAssertError(const Token *tok, const std::string& varname)
115130
{
116131
reportError(tok, Severity::warning,
117132
"assignmentInAssert",
@@ -124,7 +139,7 @@ void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& v
124139
}
125140

126141
// checks if side effects happen on the variable prior to tmp
127-
void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
142+
void CheckAssertImpl::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
128143
{
129144
if (!assignTok->isAssignmentOp() && assignTok->tokType() != Token::eIncDecOp)
130145
return;
@@ -152,8 +167,21 @@ void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *a
152167
// TODO: function calls on var
153168
}
154169

155-
bool CheckAssert::inSameScope(const Token* returnTok, const Token* assignTok)
170+
bool CheckAssertImpl::inSameScope(const Token* returnTok, const Token* assignTok)
156171
{
157172
// TODO: even if a return is in the same scope, the assignment might not affect it.
158173
return returnTok->scope() == assignTok->scope();
159174
}
175+
176+
void CheckAssert::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
177+
{
178+
CheckAssertImpl checkAssert(&tokenizer, tokenizer.getSettings(), errorLogger);
179+
checkAssert.assertWithSideEffects();
180+
}
181+
182+
void CheckAssert::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
183+
{
184+
CheckAssertImpl c(nullptr, settings, errorLogger);
185+
c.sideEffectInAssertError(nullptr, "function");
186+
c.assignmentInAssertError(nullptr, "var");
187+
}

0 commit comments

Comments
 (0)