Skip to content

Commit 14feaa8

Browse files
committed
Refactorizations:
- Fixed lots of cppcheck messages about functions that can be const or static - Fixed possible nullpointer dereference message in symboldatabase.cpp - Replaced tokAt(+-1) by next()/previous()
1 parent 9d88cc6 commit 14feaa8

13 files changed

+93
-93
lines changed

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ bool CheckOther::isUnsigned(const Variable* var) const
19861986
{
19871987
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray() && _tokenizer->sizeOfType(var->typeStartToken()) >= _settings->sizeof_int);
19881988
}
1989-
bool CheckOther::isSigned(const Variable* var) const
1989+
bool CheckOther::isSigned(const Variable* var)
19901990
{
19911991
return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray());
19921992
}
@@ -2943,7 +2943,7 @@ void CheckOther::checkInvalidFree()
29432943
// as a parameter, it might be modified, so we shouldn't report an error
29442944
// if it is later used to free memory
29452945
else if (Token::Match(tok, "%var% (")) {
2946-
const Token* tok2 = Token::findmatch(tok->tokAt(1), "%var%", tok->linkAt(1));
2946+
const Token* tok2 = Token::findmatch(tok->next(), "%var%", tok->linkAt(1));
29472947
while (tok2 != NULL) {
29482948
allocatedVariables.erase(tok2->varId());
29492949
tok2 = Token::findmatch(tok2->next(), "%var%", tok->linkAt(1));
@@ -3011,7 +3011,7 @@ void CheckOther::checkDoubleFree()
30113011
// of previously freed variables.
30123012
// TODO: There are false negatives. This bailout is only needed when the
30133013
// loop will exit without free()'ing the memory on the last iteration.
3014-
else if (tok->str() == "}" && tok->link() && tok->link()->tokAt(-1) &&
3014+
else if (tok->str() == "}" && tok->link() && tok->link()->previous() &&
30153015
tok->link()->linkAt(-1) &&
30163016
Token::Match(tok->link()->linkAt(-1)->previous(), "while|for") &&
30173017
Token::findmatch(tok->link()->linkAt(-1), "break|continue ;", tok) != NULL) {

lib/checkother.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class CPPCHECKLIB CheckOther : public Check {
155155

156156
/** @brief %Check scope of variables */
157157
void checkVariableScope();
158-
bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
158+
static bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
159159

160160
/** @brief %Check for constant function parameter */
161161
void checkConstantFunctionParameter();
@@ -311,7 +311,7 @@ class CPPCHECKLIB CheckOther : public Check {
311311

312312
private:
313313
bool isUnsigned(const Variable *var) const;
314-
bool isSigned(const Variable *var) const;
314+
static bool isSigned(const Variable *var);
315315

316316
// Error messages..
317317
void checkSleepTimeError(const Token *tok, const std::string &strDim);

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ void Variable::evaluate()
10091009
while (tok && tok->str() == "]")
10101010
tok = tok->link()->previous();
10111011
// add array dimensions if present
1012-
if (tok->next()->str() == "[")
1012+
if (tok && tok->next()->str() == "[")
10131013
setFlag(fIsArray, arrayDimensions(_dimensions, tok->next()));
10141014
}
10151015
if (!tok)

lib/tokenlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CPPCHECKLIB TokenList {
5050
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno, bool split = false);
5151
void addtoken(const Token *tok, const unsigned int lineno, const unsigned int fileno);
5252

53-
void insertTokens(Token *dest, const Token *src, unsigned int n);
53+
static void insertTokens(Token *dest, const Token *src, unsigned int n);
5454

5555
/**
5656
* Create tokens from code.

test/testbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,7 @@ class TestBufferOverrun : public TestFixture {
33553355
ASSERT_EQUALS("", errout.str());
33563356
}
33573357

3358-
void counter_test() {
3358+
void counter_test() const {
33593359
std::list<const Token*> unknownParameter;
33603360
unknownParameter.push_back(0);
33613361

test/testcppcheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TestCppcheck : public TestFixture {
5858
TEST_CASE(getErrorMessages);
5959
}
6060

61-
void instancesSorted() {
61+
void instancesSorted() const {
6262
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
6363
std::list<Check *>::const_iterator j = i;
6464
++j;
@@ -68,7 +68,7 @@ class TestCppcheck : public TestFixture {
6868
}
6969
}
7070

71-
void classInfoFormat() {
71+
void classInfoFormat() const {
7272
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
7373
const std::string info = (*i)->classInfo();
7474
if (!info.empty()) {
@@ -80,7 +80,7 @@ class TestCppcheck : public TestFixture {
8080
}
8181
}
8282

83-
void getErrorMessages() {
83+
void getErrorMessages() const {
8484
ErrorLogger2 errorLogger;
8585
CppCheck cppCheck(errorLogger, true);
8686
cppCheck.getErrorMessages();

test/testerrorlogger.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,20 @@ class TestErrorLogger : public TestFixture {
5454
TEST_CASE(suppressUnmatchedSuppressions);
5555
}
5656

57-
void FileLocationDefaults() {
57+
void FileLocationDefaults() const {
5858
ErrorLogger::ErrorMessage::FileLocation loc;
5959
ASSERT_EQUALS("", loc.getfile());
6060
ASSERT_EQUALS(0, loc.line);
6161
}
6262

63-
void FileLocationSetFile() {
63+
void FileLocationSetFile() const {
6464
ErrorLogger::ErrorMessage::FileLocation loc;
6565
loc.setfile("foo.cpp");
6666
ASSERT_EQUALS("foo.cpp", loc.getfile());
6767
ASSERT_EQUALS(0, loc.line);
6868
}
6969

70-
void ErrorMessageConstruct() {
70+
void ErrorMessageConstruct() const {
7171
ErrorLogger::ErrorMessage::FileLocation loc;
7272
loc.setfile("foo.cpp");
7373
loc.line = 5;
@@ -81,7 +81,7 @@ class TestErrorLogger : public TestFixture {
8181
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(true));
8282
}
8383

84-
void ErrorMessageConstructLocations() {
84+
void ErrorMessageConstructLocations() const {
8585
ErrorLogger::ErrorMessage::FileLocation loc;
8686
loc.setfile("foo.cpp");
8787
loc.line = 5;
@@ -99,7 +99,7 @@ class TestErrorLogger : public TestFixture {
9999
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(true));
100100
}
101101

102-
void ErrorMessageVerbose() {
102+
void ErrorMessageVerbose() const {
103103
ErrorLogger::ErrorMessage::FileLocation loc;
104104
loc.setfile("foo.cpp");
105105
loc.line = 5;
@@ -113,7 +113,7 @@ class TestErrorLogger : public TestFixture {
113113
ASSERT_EQUALS("[foo.cpp:5]: (error) Verbose error", msg.toString(true));
114114
}
115115

116-
void ErrorMessageVerboseLocations() {
116+
void ErrorMessageVerboseLocations() const {
117117
ErrorLogger::ErrorMessage::FileLocation loc;
118118
loc.setfile("foo.cpp");
119119
loc.line = 5;
@@ -131,7 +131,7 @@ class TestErrorLogger : public TestFixture {
131131
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Verbose error", msg.toString(true));
132132
}
133133

134-
void CustomFormat() {
134+
void CustomFormat() const {
135135
ErrorLogger::ErrorMessage::FileLocation loc;
136136
loc.setfile("foo.cpp");
137137
loc.line = 5;
@@ -145,7 +145,7 @@ class TestErrorLogger : public TestFixture {
145145
ASSERT_EQUALS("foo.cpp:5,error,errorId,Verbose error", msg.toString(true, "{file}:{line},{severity},{id},{message}"));
146146
}
147147

148-
void CustomFormat2() {
148+
void CustomFormat2() const {
149149
ErrorLogger::ErrorMessage::FileLocation loc;
150150
loc.setfile("foo.cpp");
151151
loc.line = 5;
@@ -159,7 +159,7 @@ class TestErrorLogger : public TestFixture {
159159
ASSERT_EQUALS("Verbose error - foo.cpp(5):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
160160
}
161161

162-
void CustomFormatLocations() {
162+
void CustomFormatLocations() const {
163163
// Check that first location from location stack is used in template
164164
ErrorLogger::ErrorMessage::FileLocation loc;
165165
loc.setfile("foo.cpp");
@@ -178,7 +178,7 @@ class TestErrorLogger : public TestFixture {
178178
ASSERT_EQUALS("Verbose error - bar.cpp(8):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
179179
}
180180

181-
void ToXml() {
181+
void ToXml() const {
182182
ErrorLogger::ErrorMessage::FileLocation loc;
183183
loc.setfile("foo.cpp");
184184
loc.line = 5;
@@ -190,7 +190,7 @@ class TestErrorLogger : public TestFixture {
190190
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
191191
}
192192

193-
void ToXmlLocations() {
193+
void ToXmlLocations() const {
194194
ErrorLogger::ErrorMessage::FileLocation loc;
195195
loc.setfile("foo.cpp");
196196
loc.line = 5;
@@ -206,7 +206,7 @@ class TestErrorLogger : public TestFixture {
206206
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
207207
}
208208

209-
void ToVerboseXml() {
209+
void ToVerboseXml() const {
210210
ErrorLogger::ErrorMessage::FileLocation loc;
211211
loc.setfile("foo.cpp");
212212
loc.line = 5;
@@ -218,7 +218,7 @@ class TestErrorLogger : public TestFixture {
218218
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
219219
}
220220

221-
void ToVerboseXmlLocations() {
221+
void ToVerboseXmlLocations() const {
222222
ErrorLogger::ErrorMessage::FileLocation loc;
223223
loc.setfile("foo.cpp");
224224
loc.line = 5;
@@ -234,7 +234,7 @@ class TestErrorLogger : public TestFixture {
234234
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
235235
}
236236

237-
void ToXmlV2() {
237+
void ToXmlV2() const {
238238
ErrorLogger::ErrorMessage::FileLocation loc;
239239
loc.setfile("foo.cpp");
240240
loc.line = 5;
@@ -253,7 +253,7 @@ class TestErrorLogger : public TestFixture {
253253
ASSERT_EQUALS(message, msg.toXML(false, 2));
254254
}
255255

256-
void ToXmlV2Locations() {
256+
void ToXmlV2Locations() const {
257257
ErrorLogger::ErrorMessage::FileLocation loc;
258258
loc.setfile("foo.cpp");
259259
loc.line = 5;
@@ -277,7 +277,7 @@ class TestErrorLogger : public TestFixture {
277277
ASSERT_EQUALS(message, msg.toXML(false, 2));
278278
}
279279

280-
void InconclusiveXml() {
280+
void InconclusiveXml() const {
281281
// Location
282282
ErrorLogger::ErrorMessage::FileLocation loc;
283283
loc.setfile("foo.cpp");
@@ -298,7 +298,7 @@ class TestErrorLogger : public TestFixture {
298298
msg.toXML(false, 2));
299299
}
300300

301-
void SerializeInconclusiveMessage() {
301+
void SerializeInconclusiveMessage() const {
302302
// Inconclusive error message
303303
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
304304
ErrorMessage msg(locs, Severity::error, "Programming error", "errorId", true);

test/testmemleak.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ class TestMemleakInFunction : public TestFixture {
660660
}
661661
}
662662

663-
CheckMemoryLeakInFunction checkMemoryLeak(NULL, &settings, NULL);
663+
CheckMemoryLeakInFunction checkMemoryLeak(NULL, &settings, this);
664664
checkMemoryLeak.simplifycode(tokens);
665665

666666
return list.front()->stringifyList(0, false);
@@ -804,7 +804,7 @@ class TestMemleakInFunction : public TestFixture {
804804

805805

806806
// is there a leak in given code? if so, return the linenr
807-
unsigned int dofindleak(const char code[]) {
807+
static unsigned int dofindleak(const char code[]) {
808808
// Clear the error buffer..
809809
errout.str("");
810810

@@ -838,7 +838,7 @@ class TestMemleakInFunction : public TestFixture {
838838
return (tok ? tok->linenr() : (unsigned int)(-1));
839839
}
840840

841-
void findleak() {
841+
void findleak() const {
842842
static const unsigned int notfound = (unsigned int)(-1);
843843

844844
ASSERT_EQUALS(1, dofindleak("alloc;"));

test/testsimplifytokens.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7297,7 +7297,7 @@ class TestSimplifyTokens : public TestFixture {
72977297

72987298
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
72997299
const Settings settings;
7300-
Tokenizer tokenizer(&settings, NULL);
7300+
Tokenizer tokenizer(&settings, this);
73017301
std::istringstream istr("x ; return a not_eq x;");
73027302
tokenizer.tokenize(istr, "test.c");
73037303
Token *x_token = tokenizer.list.front()->tokAt(5);

test/testsuppressions.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class TestSuppressions : public TestFixture {
4545
TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction
4646
}
4747

48-
void suppressionsBadId1() {
48+
void suppressionsBadId1() const {
4949
Suppressions suppressions;
5050
std::istringstream s1("123");
5151
ASSERT_EQUALS("Failed to add suppression. Invalid id \"123\"", suppressions.parseFile(s1));
@@ -54,15 +54,15 @@ class TestSuppressions : public TestFixture {
5454
ASSERT_EQUALS("", suppressions.parseFile(s2));
5555
}
5656

57-
void suppressionsDosFormat() {
57+
void suppressionsDosFormat() const {
5858
Suppressions suppressions;
5959
std::istringstream s("abc\r\ndef\r\n");
6060
ASSERT_EQUALS("", suppressions.parseFile(s));
6161
ASSERT_EQUALS(true, suppressions.isSuppressed("abc", "test.cpp", 1));
6262
ASSERT_EQUALS(true, suppressions.isSuppressed("def", "test.cpp", 1));
6363
}
6464

65-
void suppressionsFileNameWithColon() {
65+
void suppressionsFileNameWithColon() const {
6666
Suppressions suppressions;
6767
std::istringstream s("errorid:c:\\foo.cpp\nerrorid:c:\\bar.cpp:12");
6868
ASSERT_EQUALS("", suppressions.parseFile(s));
@@ -71,7 +71,7 @@ class TestSuppressions : public TestFixture {
7171
ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "c:\\bar.cpp", 12));
7272
}
7373

74-
void suppressionsGlob() {
74+
void suppressionsGlob() const {
7575
// Check for syntax errors in glob
7676
{
7777
Suppressions suppressions;
@@ -105,7 +105,7 @@ class TestSuppressions : public TestFixture {
105105
}
106106
}
107107

108-
void suppressionsFileNameWithExtraPath() {
108+
void suppressionsFileNameWithExtraPath() const {
109109
// Ticket #2797
110110
Suppressions suppressions;
111111
suppressions.addSuppression("errorid", "./a.c", 123);
@@ -313,7 +313,7 @@ class TestSuppressions : public TestFixture {
313313
ASSERT_EQUALS("", errout.str());
314314
}
315315

316-
void inlinesuppress_unusedFunction() { // #4210 - wrong report of "unmatchedSuppression" for "unusedFunction"
316+
void inlinesuppress_unusedFunction() const { // #4210 - wrong report of "unmatchedSuppression" for "unusedFunction"
317317
Suppressions suppressions;
318318
suppressions.addSuppression("unusedFunction", "test.c", 3U);
319319
ASSERT_EQUALS(true, suppressions.getUnmatchedLocalSuppressions("test.c").empty());

0 commit comments

Comments
 (0)