astyle changes, missed from previous commits

This commit is contained in:
Reijo Tomperi 2009-01-08 21:08:14 +00:00
parent 66547e7ddf
commit 42b661630b
4 changed files with 59 additions and 39 deletions

View File

@ -59,7 +59,7 @@ void CheckOther::WarningOldStylePointerCast()
if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
continue; continue;
_errorLogger->reportErr( ErrorMessage::cstyleCast(_tokenizer, tok) ); _errorLogger->reportErr(ErrorMessage::cstyleCast(_tokenizer, tok));
} }
} }
@ -140,7 +140,7 @@ void CheckOther::WarningRedundantCode()
if (err) if (err)
{ {
_errorLogger->reportErr( ErrorMessage::redundantIfDelete0(_tokenizer, tok) ); _errorLogger->reportErr(ErrorMessage::redundantIfDelete0(_tokenizer, tok));
} }
} }
@ -176,7 +176,7 @@ void CheckOther::redundantCondition2()
var2->str() == var3->str() && var2->str() == var3->str() &&
any1->str() == any2->str()) any1->str() == any2->str())
{ {
_errorLogger->reportErr( ErrorMessage::redundantIfRemove(_tokenizer, tok) ); _errorLogger->reportErr(ErrorMessage::redundantIfRemove(_tokenizer, tok));
} }
tok = Token::findmatch(tok->next(), pattern); tok = Token::findmatch(tok->next(), pattern);
@ -324,40 +324,40 @@ void CheckOther::InvalidFunctionUsage()
} }
} }
} }
// sprintf|snprintf overlapping data // sprintf|snprintf overlapping data
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
// Get variable id of target buffer.. // Get variable id of target buffer..
unsigned int varid = 0; unsigned int varid = 0;
if ( Token::Match(tok, "sprintf|snprintf ( %var% ,") ) if (Token::Match(tok, "sprintf|snprintf ( %var% ,"))
varid = tok->tokAt(2)->varId(); varid = tok->tokAt(2)->varId();
else if ( Token::Match(tok, "sprintf|snprintf ( %var% . %var% ,") ) else if (Token::Match(tok, "sprintf|snprintf ( %var% . %var% ,"))
varid = tok->tokAt(4)->varId(); varid = tok->tokAt(4)->varId();
if ( varid == 0 ) if (varid == 0)
continue; continue;
// goto "," // goto ","
const Token *tok2 = tok->tokAt(3); const Token *tok2 = tok->tokAt(3);
while ( tok2 && tok2->str() != "," ) while (tok2 && tok2->str() != ",")
tok2 = tok2->next(); tok2 = tok2->next();
// is any source buffer overlapping the target buffer? // is any source buffer overlapping the target buffer?
unsigned int parlevel = 0; unsigned int parlevel = 0;
while ( (tok2 = tok2->next()) != NULL ) while ((tok2 = tok2->next()) != NULL)
{ {
if ( tok2->str() == "(" ) if (tok2->str() == "(")
++parlevel; ++parlevel;
else if ( tok2->str() == ")" ) else if (tok2->str() == ")")
{ {
--parlevel; --parlevel;
if ( parlevel < 0 ) if (parlevel < 0)
break; break;
} }
else if ( tok2->varId() == varid ) else if (tok2->varId() == varid)
{ {
std::ostringstream ostr; std::ostringstream ostr;
ostr << _tokenizer->fileLine(tok2) << ": Overlapping data buffer " << tok2->str(); ostr << _tokenizer->fileLine(tok2) << ": Overlapping data buffer " << tok2->str();

View File

@ -273,7 +273,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Memory leak // Memory leak
CheckMemoryLeakClass checkMemoryLeak(&_tokenizer, _settings, this); CheckMemoryLeakClass checkMemoryLeak(&_tokenizer, _settings, this);
if ( ErrorMessage::memleak(_settings) ) if (ErrorMessage::memleak(_settings))
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
// Check that all class constructors are ok. // Check that all class constructors are ok.
@ -310,7 +310,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Warning upon c-style pointer casts // Warning upon c-style pointer casts
if ( ErrorMessage::cstyleCast(_settings) ) if (ErrorMessage::cstyleCast(_settings))
{ {
const char *ext = strrchr(FileName, '.'); const char *ext = strrchr(FileName, '.');
if (ext && strcmp(ext, ".cpp") == 0) if (ext && strcmp(ext, ".cpp") == 0)
@ -318,7 +318,7 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
} }
// if (a) delete a; // if (a) delete a;
if ( ErrorMessage::redundantIfDelete0(_settings) ) if (ErrorMessage::redundantIfDelete0(_settings))
checkOther.WarningRedundantCode(); checkOther.WarningRedundantCode();

View File

@ -31,29 +31,49 @@ private:
static std::string msg1(const Tokenizer *tokenizer, const Token *Location); static std::string msg1(const Tokenizer *tokenizer, const Token *Location);
public: public:
static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{ return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; } {
return msg1(tokenizer, Location) + "Memory leak: " + varname + "";
}
static bool memleak(const Settings &s) static bool memleak(const Settings &s)
{ return true; } {
return true;
}
static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{ return msg1(tokenizer, Location) + "Resource leak: " + varname + ""; } {
return msg1(tokenizer, Location) + "Resource leak: " + varname + "";
}
static bool resourceLeak(const Settings &s) static bool resourceLeak(const Settings &s)
{ return true; } {
return true;
}
static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location) static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location)
{ return msg1(tokenizer, Location) + "C-style pointer casting"; } {
return msg1(tokenizer, Location) + "C-style pointer casting";
}
static bool cstyleCast(const Settings &s) static bool cstyleCast(const Settings &s)
{ return & s._checkCodingStyle; } {
return & s._checkCodingStyle;
}
static std::string redundantIfDelete0(const Tokenizer *tokenizer, const Token *Location) static std::string redundantIfDelete0(const Tokenizer *tokenizer, const Token *Location)
{ return msg1(tokenizer, Location) + "Redundant condition. It is safe to deallocate a NULL pointer"; } {
return msg1(tokenizer, Location) + "Redundant condition. It is safe to deallocate a NULL pointer";
}
static bool redundantIfDelete0(const Settings &s) static bool redundantIfDelete0(const Settings &s)
{ return & s._checkCodingStyle; } {
return & s._checkCodingStyle;
}
static std::string redundantIfRemove(const Tokenizer *tokenizer, const Token *Location) static std::string redundantIfRemove(const Tokenizer *tokenizer, const Token *Location)
{ return msg1(tokenizer, Location) + "Redundant condition. The remove function in the STL will not do anything if element doesn't exist"; } {
return msg1(tokenizer, Location) + "Redundant condition. The remove function in the STL will not do anything if element doesn't exist";
}
static bool redundantIfRemove(const Settings &s) static bool redundantIfRemove(const Settings &s)
{ return & s._checkCodingStyle; } {
return & s._checkCodingStyle;
}
}; };
#endif #endif

View File

@ -36,7 +36,7 @@ private:
{ {
TEST_CASE(delete1); TEST_CASE(delete1);
TEST_CASE(delete2); TEST_CASE(delete2);
TEST_CASE(sprintf1); // Dangerous usage of sprintf TEST_CASE(sprintf1); // Dangerous usage of sprintf
} }
@ -86,9 +86,9 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str()); ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. It is safe to deallocate a NULL pointer\n"), errout.str());
} }
void sprintfUsage(const char code[]) void sprintfUsage(const char code[])
{ {
// Tokenize.. // Tokenize..
@ -102,16 +102,16 @@ private:
// Check for redundant code.. // Check for redundant code..
CheckOther checkOther(&tokenizer, this); CheckOther checkOther(&tokenizer, this);
checkOther.InvalidFunctionUsage(); checkOther.InvalidFunctionUsage();
} }
void sprintf1() void sprintf1()
{ {
sprintfUsage( "void foo()\n" sprintfUsage("void foo()\n"
"{\n" "{\n"
" char buf[100];\n" " char buf[100];\n"
" sprintf(buf,\"%s\",buf);\n" " sprintf(buf,\"%s\",buf);\n"
"}\n"); "}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: Overlapping data buffer buf\n"), errout.str()); ASSERT_EQUALS(std::string("[test.cpp:4]: Overlapping data buffer buf\n"), errout.str());
} }
}; };