Skip to content

Commit 7ef91ea

Browse files
committed
extracted code for actual check implementation from Check into CheckImpl
1 parent 2adaafd commit 7ef91ea

10 files changed

Lines changed: 206 additions & 129 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
202202
$(libcppdir)/checkcondition.o \
203203
$(libcppdir)/checkexceptionsafety.o \
204204
$(libcppdir)/checkfunctions.o \
205+
$(libcppdir)/checkimpl.o \
205206
$(libcppdir)/checkinternal.o \
206207
$(libcppdir)/checkio.o \
207208
$(libcppdir)/checkleakautovar.o \
@@ -464,7 +465,7 @@ $(libcppdir)/astutils.o: lib/astutils.cpp lib/astutils.h lib/check.h lib/checkcl
464465
$(libcppdir)/check.o: lib/check.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
465466
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/check.cpp
466467

467-
$(libcppdir)/check64bit.o: lib/check64bit.cpp lib/check.h lib/check64bit.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
468+
$(libcppdir)/check64bit.o: lib/check64bit.cpp lib/check.h lib/check64bit.h lib/checkimpl.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
468469
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/check64bit.cpp
469470

470471
$(libcppdir)/checkassert.o: lib/checkassert.cpp lib/check.h lib/checkassert.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
@@ -494,6 +495,9 @@ $(libcppdir)/checkexceptionsafety.o: lib/checkexceptionsafety.cpp lib/check.h li
494495
$(libcppdir)/checkfunctions.o: lib/checkfunctions.cpp lib/astutils.h lib/check.h lib/checkfunctions.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
495496
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkfunctions.cpp
496497

498+
$(libcppdir)/checkimpl.o: lib/checkimpl.cpp lib/check.h lib/checkimpl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
499+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkimpl.cpp
500+
497501
$(libcppdir)/checkinternal.o: lib/checkinternal.cpp lib/astutils.h lib/check.h lib/checkinternal.h lib/config.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
498502
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkinternal.cpp
499503

lib/check.cpp

Lines changed: 0 additions & 52 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::SeverityType 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::SeverityType 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
@@ -99,29 +73,3 @@ std::list<Check *> &Check::instances()
9973
#endif
10074
}
10175

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-
}

lib/check.h

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

64-
/** This constructor is used when running checks. */
65-
Check(std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
66-
: mTokenizer(tokenizer), mSettings(settings), mErrorLogger(errorLogger), mName(std::move(aname)) {}
67-
6864
virtual ~Check() {
69-
if (!mTokenizer)
70-
instances().remove(this);
65+
instances().remove(this);
7166
}
7267

7368
Check(const Check &) = delete;
@@ -127,42 +122,6 @@ class CPPCHECKLIB Check {
127122
return false;
128123
}
129124

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

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"
@@ -41,7 +42,22 @@ namespace {
4142
Check64BitPortability instance;
4243
}
4344

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

120-
void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
136+
void Check64BitPortabilityImpl::assignmentAddressToIntegerError(const Token *tok)
121137
{
122138
reportError(tok, Severity::portability,
123139
"AssignmentAddressToInteger",
@@ -128,7 +144,7 @@ void Check64BitPortability::assignmentAddressToIntegerError(const Token *tok)
128144
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
129145
}
130146

131-
void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
147+
void Check64BitPortabilityImpl::assignmentIntegerToAddressError(const Token *tok)
132148
{
133149
reportError(tok, Severity::portability,
134150
"AssignmentIntegerToAddress",
@@ -139,7 +155,7 @@ void Check64BitPortability::assignmentIntegerToAddressError(const Token *tok)
139155
"way is to store addresses only in pointer types (or typedefs like uintptr_t).", CWE758, Certainty::normal);
140156
}
141157

142-
void Check64BitPortability::returnPointerError(const Token *tok)
158+
void Check64BitPortabilityImpl::returnPointerError(const Token *tok)
143159
{
144160
reportError(tok, Severity::portability,
145161
"CastAddressToIntegerAtReturn",
@@ -150,7 +166,7 @@ void Check64BitPortability::returnPointerError(const Token *tok)
150166
"to 32-bit integer. The safe way is to always return an integer.", CWE758, Certainty::normal);
151167
}
152168

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

lib/check64bit.h

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,13 @@ class Token;
4343
class CPPCHECKLIB Check64BitPortability : public Check {
4444
public:
4545
/** This constructor is used when registering the Check64BitPortability */
46-
Check64BitPortability() : Check(myName()) {}
47-
48-
/** This constructor is used when running checks. */
49-
Check64BitPortability(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
50-
: Check(myName(), tokenizer, settings, errorLogger) {}
46+
Check64BitPortability() : Check("64-bit portability") {}
5147

5248
/** @brief Run checks against the normal token list */
53-
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
54-
Check64BitPortability check64BitPortability(&tokenizer, tokenizer.getSettings(), errorLogger);
55-
check64BitPortability.pointerassignment();
56-
}
57-
58-
/** Check for pointer assignment */
59-
void pointerassignment();
49+
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override;
6050

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

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

lib/checkimpl.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2023 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "checkimpl.h"
20+
21+
#include "check.h"
22+
23+
#include "errorlogger.h"
24+
#include "settings.h"
25+
#include "token.h"
26+
#include "tokenize.h"
27+
#include "vfvalue.h"
28+
29+
#include <algorithm>
30+
#include <cctype>
31+
#include <iostream>
32+
#include <stdexcept>
33+
#include <utility>
34+
35+
void CheckImpl::reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
36+
{
37+
const ErrorMessage errmsg(callstack, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
38+
if (mErrorLogger)
39+
mErrorLogger->reportErr(errmsg);
40+
else
41+
Check::reportError(errmsg);
42+
}
43+
44+
void CheckImpl::reportError(const ErrorPath &errorPath, Severity::SeverityType severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
45+
{
46+
const ErrorMessage errmsg(errorPath, mTokenizer ? &mTokenizer->list : nullptr, severity, id, msg, cwe, certainty);
47+
if (mErrorLogger)
48+
mErrorLogger->reportErr(errmsg);
49+
else
50+
Check::reportError(errmsg);
51+
}
52+
53+
bool CheckImpl::wrongData(const Token *tok, const char *str)
54+
{
55+
if (mSettings->daca)
56+
reportError(tok, Severity::debug, "DacaWrongData", "Wrong data detected by condition " + std::string(str));
57+
return true;
58+
}
59+
60+
ErrorPath CheckImpl::getErrorPath(const Token* errtok, const ValueFlow::Value* value, std::string bug) const
61+
{
62+
ErrorPath errorPath;
63+
if (!value) {
64+
errorPath.emplace_back(errtok, std::move(bug));
65+
} else if (mSettings->verbose || mSettings->xml || !mSettings->templateLocation.empty()) {
66+
errorPath = value->errorPath;
67+
errorPath.emplace_back(errtok, std::move(bug));
68+
} else {
69+
if (value->condition)
70+
errorPath.emplace_back(value->condition, "condition '" + value->condition->expressionString() + "'");
71+
//else if (!value->isKnown() || value->defaultArg)
72+
// errorPath = value->callstack;
73+
errorPath.emplace_back(errtok, std::move(bug));
74+
}
75+
return errorPath;
76+
}

0 commit comments

Comments
 (0)