code cleanups and refactorings

This commit is contained in:
PKEuS 2011-11-26 21:02:04 +01:00 committed by Daniel Marjamäki
parent 50c320ef27
commit 6b6f780057
8 changed files with 59 additions and 118 deletions

View File

@ -219,7 +219,7 @@ private:
static bool bailoutIfSwitch(const Token *tok, const unsigned int varid) static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
{ {
// Used later to check if the body belongs to a "if" // Used later to check if the body belongs to a "if"
const std::string str1(tok->str()); bool is_if = tok->str() == "if";
// Count { and } // Count { and }
unsigned int indentlevel = 0; unsigned int indentlevel = 0;
@ -239,7 +239,7 @@ static bool bailoutIfSwitch(const Token *tok, const unsigned int varid)
} }
// If scanning a "if" block then bailout for "break" // If scanning a "if" block then bailout for "break"
else if (str1 == "if" && tok->str() == "break") else if (is_if && tok->str() == "break")
return true; return true;
// bailout for "return" // bailout for "return"
@ -874,7 +874,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
tok3 = tok3->tokAt(-2); tok3 = tok3->tokAt(-2);
// just taking the address? // just taking the address?
const bool addr(tok3 && (Token::simpleMatch(tok3, "&") || const bool addr(tok3 && (tok3->str() == "&" ||
Token::simpleMatch(tok3->previous(), "& ("))); Token::simpleMatch(tok3->previous(), "& (")));
// taking address of 1 past end? // taking address of 1 past end?
@ -1065,8 +1065,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
tok2 = tok2->tokAt(-2); tok2 = tok2->tokAt(-2);
// just taking the address? // just taking the address?
const bool addr(Token::simpleMatch(tok2, "&") || const bool addr(tok2 && (tok2->str() == "&" ||
Token::simpleMatch(tok2->previous(), "& (")); Token::simpleMatch(tok2->previous(), "& (")));
// taking address of 1 past end? // taking address of 1 past end?
if (addr && totalIndex == totalElements) if (addr && totalIndex == totalElements)
@ -1278,6 +1278,9 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
else if (tok->str() == "}") else if (tok->str() == "}")
--indentlevel; --indentlevel;
if (indentlevel <= 0)
continue;
// size : Max array index // size : Max array index
MathLib::bigint size = 0; MathLib::bigint size = 0;
@ -1298,25 +1301,24 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
"Check (BufferOverrun::checkGlobalAndLocalVariable)", "Check (BufferOverrun::checkGlobalAndLocalVariable)",
tok->progressValue()); tok->progressValue());
if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) { if (Token::Match(tok, "[*;{}] %var% = new %type% [ %num% ]")) {
size = MathLib::toLongNumber(tok->strAt(6)); size = MathLib::toLongNumber(tok->strAt(6));
type = tok->strAt(4); type = tok->strAt(4);
varid = tok->next()->varId(); varid = tok->next()->varId();
nextTok = 8; nextTok = 8;
} else if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = new %type% ( %num% )")) { } else if (Token::Match(tok, "[*;{}] %var% = new %type% ( %num% )")) {
size = 1; size = 1;
type = tok->strAt(4); type = tok->strAt(4);
varid = tok->next()->varId(); varid = tok->next()->varId();
nextTok = 8; nextTok = 8;
} else if (indentlevel > 0 && } else if (Token::Match(tok, "[;{}] %var% = %str% ;") &&
Token::Match(tok, "[;{}] %var% = %str% ;") &&
tok->next()->varId() > 0 && tok->next()->varId() > 0 &&
NULL != Token::findmatch(_tokenizer->tokens(), "[;{}] const| %type% * %varid% ;", tok->next()->varId())) { NULL != Token::findmatch(_tokenizer->tokens(), "[;{}] const| %type% * %varid% ;", tok->next()->varId())) {
size = 1 + int(tok->tokAt(3)->strValue().size()); size = 1 + int(tok->tokAt(3)->strValue().size());
type = "char"; type = "char";
varid = tok->next()->varId(); varid = tok->next()->varId();
nextTok = 4; nextTok = 4;
} else if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = malloc|alloca ( %num% ) ;")) { } else if (Token::Match(tok, "[*;{}] %var% = malloc|alloca ( %num% ) ;")) {
size = MathLib::toLongNumber(tok->strAt(5)); size = MathLib::toLongNumber(tok->strAt(5));
type = "char"; // minimum type, typesize=1 type = "char"; // minimum type, typesize=1
varid = tok->next()->varId(); varid = tok->next()->varId();

View File

@ -1337,25 +1337,13 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok)
static unsigned int countParameters(const Token *tok) static unsigned int countParameters(const Token *tok)
{ {
if (Token::Match(tok->tokAt(2), "void| )")) tok = tok->tokAt(2);
if (tok->str() == ")")
return 0; return 0;
unsigned int numpar = 1; unsigned int numpar = 1;
unsigned int parlevel = 0; while ((tok = tok->nextArgument()))
for (; tok; tok = tok->next()) { numpar++;
if (tok->str() == "(")
++parlevel;
else if (tok->str() == ")") {
if (parlevel <=1)
break;
--parlevel;
}
else if (parlevel==1 && tok->str() == ",") {
++numpar;
}
}
return numpar; return numpar;
} }
@ -1364,7 +1352,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok)
{ {
unsigned int args = countParameters(tok); unsigned int args = countParameters(tok);
std::list<Function>::const_iterator func; std::list<Function>::const_iterator func;
unsigned int matches = 0; unsigned int matches = 0;
unsigned int consts = 0; unsigned int consts = 0;

View File

@ -626,31 +626,17 @@ bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid, boo
} }
static int countParameters(const Token *tok) static unsigned int countParameters(const Token *tok)
{ {
if (!Token::Match(tok, "%var% (")) tok = tok->tokAt(2);
return -1; if (tok->str() == ")")
if (Token::Match(tok->tokAt(2), "void| )"))
return 0; return 0;
int numpar = 1; unsigned int numpar = 1;
int parlevel = 0; while (tok = tok->nextArgument())
for (; tok; tok = tok->next()) { numpar++;
if (tok->str() == "(")
++parlevel;
else if (tok->str() == ")") { return numpar;
if (parlevel <= 1)
return numpar;
--parlevel;
}
else if (parlevel == 1 && tok->str() == ",") {
++numpar;
}
}
return -1;
} }
bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname) bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname)
@ -729,8 +715,8 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
} }
// how many parameters is there in the function call? // how many parameters is there in the function call?
int numpar = countParameters(tok); unsigned int numpar = countParameters(tok);
if (numpar <= 0) { if (numpar == 0) {
// Taking return value => it is not a noreturn function // Taking return value => it is not a noreturn function
if (tok->strAt(-1) == "=") if (tok->strAt(-1) == "=")
return NULL; return NULL;
@ -2885,7 +2871,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
#include "checkuninitvar.h" // CheckUninitVar::analyse #include "checkuninitvar.h" // CheckUninitVar::analyse
void CheckMemoryLeakNoVar::check() void CheckMemoryLeakNoVar::check()
{ {

View File

@ -336,24 +336,14 @@ void CheckNullPointer::nullPointerLinkedList()
// } // }
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) { for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) {
// search for a "for" token.. // search for a "for" token..
if (!Token::simpleMatch(tok1, "for (")) if (tok1->str() != "for")
continue; continue;
// is there any dereferencing occurring in the for statement // is there any dereferencing occurring in the for statement
// parlevel2 counts the parentheses when using tok2. const Token* end2 = tok1->tokAt(1)->link();
unsigned int parlevel2 = 1; for (const Token *tok2 = tok1->tokAt(2); tok2 != end2; tok2 = tok2->next()) {
for (const Token *tok2 = tok1->tokAt(2); tok2; tok2 = tok2->next()) {
// Parentheses..
if (tok2->str() == "(")
++parlevel2;
else if (tok2->str() == ")") {
if (parlevel2 <= 1)
break;
--parlevel2;
}
// Dereferencing a variable inside the "for" parentheses.. // Dereferencing a variable inside the "for" parentheses..
else if (Token::Match(tok2, "%var% . %var%")) { if (Token::Match(tok2, "%var% . %var%")) {
// Variable id for dereferenced variable // Variable id for dereferenced variable
const unsigned int varid(tok2->varId()); const unsigned int varid(tok2->varId());
if (varid == 0) if (varid == 0)

View File

@ -22,7 +22,7 @@
#include <cstring> #include <cstring>
#include "path.h" #include "path.h"
std::string Path::toNativeSeparators(const std::string &path) std::string Path::toNativeSeparators(std::string path)
{ {
#if defined(_WIN32) #if defined(_WIN32)
char separ = '/'; char separ = '/';
@ -31,18 +31,16 @@ std::string Path::toNativeSeparators(const std::string &path)
char separ = '\\'; char separ = '\\';
char native = '/'; char native = '/';
#endif #endif
std::string modified(path); std::replace(path.begin(), path.end(), separ, native);
std::replace(modified.begin(), modified.end(), separ, native); return path;
return modified;
} }
std::string Path::fromNativeSeparators(const std::string &path) std::string Path::fromNativeSeparators(std::string path)
{ {
char nonnative = '\\'; char nonnative = '\\';
char newsepar = '/'; char newsepar = '/';
std::string modified(path); std::replace(path.begin(), path.end(), nonnative, newsepar);
std::replace(modified.begin(), modified.end(), nonnative, newsepar); return path;
return modified;
} }
std::string Path::simplifyPath(const char *originalPath) std::string Path::simplifyPath(const char *originalPath)
@ -109,11 +107,10 @@ bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
#endif #endif
} }
std::string Path::removeQuotationMarks(const std::string &path) std::string Path::removeQuotationMarks(std::string path)
{ {
std::string editPath(path); path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
editPath.erase(std::remove(editPath.begin(), editPath.end(), '\"'), editPath.end()); return path;
return editPath;
} }
std::string Path::getFilenameExtension(const std::string &path) std::string Path::getFilenameExtension(const std::string &path)

View File

@ -38,14 +38,14 @@ public:
* @param path Path string to convert. * @param path Path string to convert.
* @return converted path. * @return converted path.
*/ */
static std::string toNativeSeparators(const std::string &path); static std::string toNativeSeparators(std::string path);
/** /**
* Convert path to use internal path separators. * Convert path to use internal path separators.
* @param path Path string to convert. * @param path Path string to convert.
* @return converted path. * @return converted path.
*/ */
static std::string fromNativeSeparators(const std::string &path); static std::string fromNativeSeparators(std::string path);
/** /**
* @brief Simplify path "foo/bar/.." => "foo" * @brief Simplify path "foo/bar/.." => "foo"
@ -68,7 +68,7 @@ public:
* @param path path to be cleaned. * @param path path to be cleaned.
* @return Cleaned path without quotation marks. * @return Cleaned path without quotation marks.
*/ */
static std::string removeQuotationMarks(const std::string &path); static std::string removeQuotationMarks(std::string path);
/** /**
* @brief Get an extension of the filename. * @brief Get an extension of the filename.

View File

@ -1107,9 +1107,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
} }
if (from_negation) { if (from_negation) {
ndeflist.push_back(deflist.back()); ndeflist.push_back(deflist.back());
deflist.pop_back(); deflist.back() = "!";
std::string nmark("!");
deflist.push_back(nmark);
} }
if (std::find(ret.begin(), ret.end(), def) == ret.end()) { if (std::find(ret.begin(), ret.end(), def) == ret.end()) {
@ -1119,13 +1117,11 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) { else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) {
if (deflist.back() == "!") { if (deflist.back() == "!") {
deflist.pop_back(); deflist.back() = ndeflist.back();
deflist.push_back(ndeflist.back());
ndeflist.pop_back(); ndeflist.pop_back();
} else { } else {
std::string tempDef((deflist.back() == "1") ? "0" : "1"); std::string tempDef((deflist.back() == "1") ? "0" : "1");
deflist.pop_back(); deflist.back() = tempDef;
deflist.push_back(tempDef);
} }
} }
@ -2701,11 +2697,8 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
} }
} }
{ for (std::map<std::string, PreprocessorMacro *>::iterator it = macros.begin(); it != macros.end(); ++it)
std::map<std::string, PreprocessorMacro *>::iterator it; delete it->second;
for (it = macros.begin(); it != macros.end(); ++it)
delete it->second;
}
return ostr.str(); return ostr.str();
} }

View File

@ -826,7 +826,7 @@ static Token *processFunc(Token *tok2, bool inOperator)
tok2 = tok2->linkAt(4)->next(); tok2 = tok2->linkAt(4)->next();
else if (Token::Match(tok2->next(), "* ( * %type% (")) else if (Token::Match(tok2->next(), "* ( * %type% ("))
tok2 = tok2->linkAt(5)->next(); tok2 = tok2->linkAt(5)->next();
else if (Token::Match(tok2->next(), "* [") && else if (Token::simpleMatch(tok2->next(), "* [") &&
Token::simpleMatch(tok2->linkAt(2), "] ;")) Token::simpleMatch(tok2->linkAt(2), "] ;"))
tok2 = tok2->next(); tok2 = tok2->next();
else { else {
@ -3450,7 +3450,7 @@ void Tokenizer::setVarId()
tok = tok->next(); tok = tok->next();
// skip global namespace prefix // skip global namespace prefix
if (Token::simpleMatch(tok, "::")) if (tok && tok->str() == "::")
tok = tok->next(); tok = tok->next();
while (Token::Match(tok, "%var% ::")) while (Token::Match(tok, "%var% ::"))
@ -8389,9 +8389,9 @@ const char *Tokenizer::getParameterName(const Token *ftok, unsigned int par)
for (; ftok; ftok = ftok->next()) { for (; ftok; ftok = ftok->next()) {
if (ftok->str() == ")") if (ftok->str() == ")")
break; break;
if (ftok->str() == ",") else if (ftok->str() == ",")
++_par; ++_par;
if (par == _par && Token::Match(ftok, "%var% [,)]")) else if (par == _par && Token::Match(ftok, "%var% [,)]"))
return ftok->str().c_str(); return ftok->str().c_str();
} }
return NULL; return NULL;
@ -9145,25 +9145,18 @@ void Tokenizer::simplifyStructDecl()
void Tokenizer::simplifyCallingConvention() void Tokenizer::simplifyCallingConvention()
{ {
const char * pattern = "__cdecl|__stdcall|__fastcall|__thiscall|__clrcall|__syscall|__pascal|__fortran|__far|__near|WINAPI|APIENTRY|CALLBACK"; const char pattern[] = "__cdecl|__stdcall|__fastcall|__thiscall|__clrcall|__syscall|__pascal|__fortran|__far|__near|WINAPI|APIENTRY|CALLBACK";
while (Token::Match(_tokens, pattern)) {
_tokens->deleteThis();
}
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = _tokens; tok; tok = tok->next()) {
while (Token::Match(tok->next(), pattern)) { while (Token::Match(tok, pattern)) {
tok->deleteNext(); tok->deleteThis();
} }
} }
} }
void Tokenizer::simplifyDeclspec() void Tokenizer::simplifyDeclspec()
{ {
while (Token::simpleMatch(_tokens, "__declspec (") && _tokens->next()->link() && _tokens->next()->link()->next()) {
Token::eraseTokens(_tokens, _tokens->next()->link()->next());
_tokens->deleteThis();
}
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "__declspec (") && tok->next()->link() && tok->next()->link()->next()) { while (Token::simpleMatch(tok, "__declspec (") && tok->next()->link() && tok->next()->link()->next()) {
Token::eraseTokens(tok, tok->next()->link()->next()); Token::eraseTokens(tok, tok->next()->link()->next());
tok->deleteThis(); tok->deleteThis();
} }
@ -9172,12 +9165,8 @@ void Tokenizer::simplifyDeclspec()
void Tokenizer::simplifyAttribute() void Tokenizer::simplifyAttribute()
{ {
while (Token::simpleMatch(_tokens, "__attribute__ (") && _tokens->next()->link() && _tokens->next()->link()->next()) {
Token::eraseTokens(_tokens, _tokens->next()->link()->next());
_tokens->deleteThis();
}
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = _tokens; tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next()) { while (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next()) {
if (Token::simpleMatch(tok->tokAt(2), "( unused )")) { if (Token::simpleMatch(tok->tokAt(2), "( unused )")) {
// check if after variable name // check if after variable name
if (Token::Match(tok->next()->link()->next(), ";|=")) { if (Token::Match(tok->next()->link()->next(), ";|=")) {
@ -9192,7 +9181,6 @@ void Tokenizer::simplifyAttribute()
Token::eraseTokens(tok, tok->next()->link()->next()); Token::eraseTokens(tok, tok->next()->link()->next());
tok->deleteThis(); tok->deleteThis();
tok = tok->previous();
} }
} }
} }
@ -9201,12 +9189,9 @@ void Tokenizer::simplifyAttribute()
void Tokenizer::simplifyKeyword() void Tokenizer::simplifyKeyword()
{ {
const char pattern[] = "volatile|inline|__inline|__forceinline|register|restrict|__restrict|__restrict__"; const char pattern[] = "volatile|inline|__inline|__forceinline|register|restrict|__restrict|__restrict__";
while (Token::Match(_tokens, pattern)) {
_tokens->deleteThis();
}
for (Token *tok = _tokens; tok; tok = tok->next()) { for (Token *tok = _tokens; tok; tok = tok->next()) {
while (Token::Match(tok->next(), pattern)) { while (Token::Match(tok, pattern)) {
tok->deleteNext(); tok->deleteThis();
} }
} }
} }