Permalink
Browse files

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()
  • Loading branch information...
PKEuS committed Mar 12, 2013
1 parent 9d88cc6 commit 14feaa8d395f2c3161426ee628b98fc1799a5c4e
View
@@ -1986,7 +1986,7 @@ bool CheckOther::isUnsigned(const Variable* var) const
{
return(var && var->typeStartToken()->isUnsigned() && !var->isPointer() && !var->isArray() && _tokenizer->sizeOfType(var->typeStartToken()) >= _settings->sizeof_int);
}
-bool CheckOther::isSigned(const Variable* var) const
+bool CheckOther::isSigned(const Variable* var)
{
return(var && !var->typeStartToken()->isUnsigned() && Token::Match(var->typeEndToken(), "int|char|short|long") && !var->isPointer() && !var->isArray());
}
@@ -2943,7 +2943,7 @@ void CheckOther::checkInvalidFree()
// as a parameter, it might be modified, so we shouldn't report an error
// if it is later used to free memory
else if (Token::Match(tok, "%var% (")) {
- const Token* tok2 = Token::findmatch(tok->tokAt(1), "%var%", tok->linkAt(1));
+ const Token* tok2 = Token::findmatch(tok->next(), "%var%", tok->linkAt(1));
while (tok2 != NULL) {
allocatedVariables.erase(tok2->varId());
tok2 = Token::findmatch(tok2->next(), "%var%", tok->linkAt(1));
@@ -3011,7 +3011,7 @@ void CheckOther::checkDoubleFree()
// of previously freed variables.
// TODO: There are false negatives. This bailout is only needed when the
// loop will exit without free()'ing the memory on the last iteration.
- else if (tok->str() == "}" && tok->link() && tok->link()->tokAt(-1) &&
+ else if (tok->str() == "}" && tok->link() && tok->link()->previous() &&
tok->link()->linkAt(-1) &&
Token::Match(tok->link()->linkAt(-1)->previous(), "while|for") &&
Token::findmatch(tok->link()->linkAt(-1), "break|continue ;", tok) != NULL) {
View
@@ -155,7 +155,7 @@ class CPPCHECKLIB CheckOther : public Check {
/** @brief %Check scope of variables */
void checkVariableScope();
- bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
+ static bool checkInnerScope(const Token *tok, const Variable* var, bool& used);
/** @brief %Check for constant function parameter */
void checkConstantFunctionParameter();
@@ -311,7 +311,7 @@ class CPPCHECKLIB CheckOther : public Check {
private:
bool isUnsigned(const Variable *var) const;
- bool isSigned(const Variable *var) const;
+ static bool isSigned(const Variable *var);
// Error messages..
void checkSleepTimeError(const Token *tok, const std::string &strDim);
View
@@ -1009,7 +1009,7 @@ void Variable::evaluate()
while (tok && tok->str() == "]")
tok = tok->link()->previous();
// add array dimensions if present
- if (tok->next()->str() == "[")
+ if (tok && tok->next()->str() == "[")
setFlag(fIsArray, arrayDimensions(_dimensions, tok->next()));
}
if (!tok)
View
@@ -50,7 +50,7 @@ class CPPCHECKLIB TokenList {
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno, bool split = false);
void addtoken(const Token *tok, const unsigned int lineno, const unsigned int fileno);
- void insertTokens(Token *dest, const Token *src, unsigned int n);
+ static void insertTokens(Token *dest, const Token *src, unsigned int n);
/**
* Create tokens from code.
@@ -3355,7 +3355,7 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("", errout.str());
}
- void counter_test() {
+ void counter_test() const {
std::list<const Token*> unknownParameter;
unknownParameter.push_back(0);
View
@@ -58,7 +58,7 @@ class TestCppcheck : public TestFixture {
TEST_CASE(getErrorMessages);
}
- void instancesSorted() {
+ void instancesSorted() const {
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
std::list<Check *>::const_iterator j = i;
++j;
@@ -68,7 +68,7 @@ class TestCppcheck : public TestFixture {
}
}
- void classInfoFormat() {
+ void classInfoFormat() const {
for (std::list<Check *>::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) {
const std::string info = (*i)->classInfo();
if (!info.empty()) {
@@ -80,7 +80,7 @@ class TestCppcheck : public TestFixture {
}
}
- void getErrorMessages() {
+ void getErrorMessages() const {
ErrorLogger2 errorLogger;
CppCheck cppCheck(errorLogger, true);
cppCheck.getErrorMessages();
View
@@ -54,20 +54,20 @@ class TestErrorLogger : public TestFixture {
TEST_CASE(suppressUnmatchedSuppressions);
}
- void FileLocationDefaults() {
+ void FileLocationDefaults() const {
ErrorLogger::ErrorMessage::FileLocation loc;
ASSERT_EQUALS("", loc.getfile());
ASSERT_EQUALS(0, loc.line);
}
- void FileLocationSetFile() {
+ void FileLocationSetFile() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
ASSERT_EQUALS("foo.cpp", loc.getfile());
ASSERT_EQUALS(0, loc.line);
}
- void ErrorMessageConstruct() {
+ void ErrorMessageConstruct() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -81,7 +81,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("[foo.cpp:5]: (error) Programming error.", msg.toString(true));
}
- void ErrorMessageConstructLocations() {
+ void ErrorMessageConstructLocations() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -99,7 +99,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Programming error.", msg.toString(true));
}
- void ErrorMessageVerbose() {
+ void ErrorMessageVerbose() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -113,7 +113,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("[foo.cpp:5]: (error) Verbose error", msg.toString(true));
}
- void ErrorMessageVerboseLocations() {
+ void ErrorMessageVerboseLocations() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -131,7 +131,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("[foo.cpp:5] -> [bar.cpp:8]: (error) Verbose error", msg.toString(true));
}
- void CustomFormat() {
+ void CustomFormat() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -145,7 +145,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("foo.cpp:5,error,errorId,Verbose error", msg.toString(true, "{file}:{line},{severity},{id},{message}"));
}
- void CustomFormat2() {
+ void CustomFormat2() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -159,7 +159,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("Verbose error - foo.cpp(5):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
}
- void CustomFormatLocations() {
+ void CustomFormatLocations() const {
// Check that first location from location stack is used in template
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
@@ -178,7 +178,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("Verbose error - bar.cpp(8):(error,errorId)", msg.toString(true, "{message} - {file}({line}):({severity},{id})"));
}
- void ToXml() {
+ void ToXml() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -190,7 +190,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
- void ToXmlLocations() {
+ void ToXmlLocations() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -206,7 +206,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Programming error.\"/>", msg.toXML(false,1));
}
- void ToVerboseXml() {
+ void ToVerboseXml() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -218,7 +218,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("<error file=\"foo.cpp\" line=\"5\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
- void ToVerboseXmlLocations() {
+ void ToVerboseXmlLocations() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -234,7 +234,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS("<error file=\"bar.cpp\" line=\"8\" id=\"errorId\" severity=\"error\" msg=\"Verbose error\"/>", msg.toXML(true,1));
}
- void ToXmlV2() {
+ void ToXmlV2() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -253,7 +253,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS(message, msg.toXML(false, 2));
}
- void ToXmlV2Locations() {
+ void ToXmlV2Locations() const {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
loc.line = 5;
@@ -277,7 +277,7 @@ class TestErrorLogger : public TestFixture {
ASSERT_EQUALS(message, msg.toXML(false, 2));
}
- void InconclusiveXml() {
+ void InconclusiveXml() const {
// Location
ErrorLogger::ErrorMessage::FileLocation loc;
loc.setfile("foo.cpp");
@@ -298,7 +298,7 @@ class TestErrorLogger : public TestFixture {
msg.toXML(false, 2));
}
- void SerializeInconclusiveMessage() {
+ void SerializeInconclusiveMessage() const {
// Inconclusive error message
std::list<ErrorLogger::ErrorMessage::FileLocation> locs;
ErrorMessage msg(locs, Severity::error, "Programming error", "errorId", true);
View
@@ -660,7 +660,7 @@ class TestMemleakInFunction : public TestFixture {
}
}
- CheckMemoryLeakInFunction checkMemoryLeak(NULL, &settings, NULL);
+ CheckMemoryLeakInFunction checkMemoryLeak(NULL, &settings, this);
checkMemoryLeak.simplifycode(tokens);
return list.front()->stringifyList(0, false);
@@ -804,7 +804,7 @@ class TestMemleakInFunction : public TestFixture {
// is there a leak in given code? if so, return the linenr
- unsigned int dofindleak(const char code[]) {
+ static unsigned int dofindleak(const char code[]) {
// Clear the error buffer..
errout.str("");
@@ -838,7 +838,7 @@ class TestMemleakInFunction : public TestFixture {
return (tok ? tok->linenr() : (unsigned int)(-1));
}
- void findleak() {
+ void findleak() const {
static const unsigned int notfound = (unsigned int)(-1);
ASSERT_EQUALS(1, dofindleak("alloc;"));
@@ -7297,7 +7297,7 @@ class TestSimplifyTokens : public TestFixture {
void duplicateDefinition() { // #3565 - wrongly detects duplicate definition
const Settings settings;
- Tokenizer tokenizer(&settings, NULL);
+ Tokenizer tokenizer(&settings, this);
std::istringstream istr("x ; return a not_eq x;");
tokenizer.tokenize(istr, "test.c");
Token *x_token = tokenizer.list.front()->tokAt(5);
View
@@ -45,7 +45,7 @@ class TestSuppressions : public TestFixture {
TEST_CASE(inlinesuppress_unusedFunction); // #4210 - unusedFunction
}
- void suppressionsBadId1() {
+ void suppressionsBadId1() const {
Suppressions suppressions;
std::istringstream s1("123");
ASSERT_EQUALS("Failed to add suppression. Invalid id \"123\"", suppressions.parseFile(s1));
@@ -54,15 +54,15 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS("", suppressions.parseFile(s2));
}
- void suppressionsDosFormat() {
+ void suppressionsDosFormat() const {
Suppressions suppressions;
std::istringstream s("abc\r\ndef\r\n");
ASSERT_EQUALS("", suppressions.parseFile(s));
ASSERT_EQUALS(true, suppressions.isSuppressed("abc", "test.cpp", 1));
ASSERT_EQUALS(true, suppressions.isSuppressed("def", "test.cpp", 1));
}
- void suppressionsFileNameWithColon() {
+ void suppressionsFileNameWithColon() const {
Suppressions suppressions;
std::istringstream s("errorid:c:\\foo.cpp\nerrorid:c:\\bar.cpp:12");
ASSERT_EQUALS("", suppressions.parseFile(s));
@@ -71,7 +71,7 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS(true, suppressions.isSuppressed("errorid", "c:\\bar.cpp", 12));
}
- void suppressionsGlob() {
+ void suppressionsGlob() const {
// Check for syntax errors in glob
{
Suppressions suppressions;
@@ -105,7 +105,7 @@ class TestSuppressions : public TestFixture {
}
}
- void suppressionsFileNameWithExtraPath() {
+ void suppressionsFileNameWithExtraPath() const {
// Ticket #2797
Suppressions suppressions;
suppressions.addSuppression("errorid", "./a.c", 123);
@@ -313,7 +313,7 @@ class TestSuppressions : public TestFixture {
ASSERT_EQUALS("", errout.str());
}
- void inlinesuppress_unusedFunction() { // #4210 - wrong report of "unmatchedSuppression" for "unusedFunction"
+ void inlinesuppress_unusedFunction() const { // #4210 - wrong report of "unmatchedSuppression" for "unusedFunction"
Suppressions suppressions;
suppressions.addSuppression("unusedFunction", "test.c", 3U);
ASSERT_EQUALS(true, suppressions.getUnmatchedLocalSuppressions("test.c").empty());
@@ -62,7 +62,7 @@ class TestSymbolDatabase: public TestFixture {
found = false;
}
- const Scope *findFunctionScopeByToken(const SymbolDatabase * db, const Token *tok) const {
+ static const Scope *findFunctionScopeByToken(const SymbolDatabase * db, const Token *tok) {
std::list<Scope>::const_iterator scope;
for (scope = db->scopeList.begin(); scope != db->scopeList.end(); ++scope) {
@@ -74,7 +74,7 @@ class TestSymbolDatabase: public TestFixture {
return 0;
}
- const Function *findFunctionByName(const std::string& str, const Scope* startScope) const {
+ static const Function *findFunctionByName(const std::string& str, const Scope* startScope) {
const Scope* currScope = startScope;
while (currScope && currScope->isExecutable()) {
if (currScope->functionOf)
@@ -196,7 +196,7 @@ class TestSymbolDatabase: public TestFixture {
TEST_CASE(findFunction1);
}
- void array() {
+ void array() const {
std::istringstream code("int a[10+2];");
TokenList list(NULL);
list.createTokens(code, "test.c");
Oops, something went wrong.

0 comments on commit 14feaa8

Please sign in to comment.