diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 13292adc7..5f93c86f4 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -219,7 +219,7 @@ private: static bool bailoutIfSwitch(const Token *tok, const unsigned int varid) { // 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 } 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" - else if (str1 == "if" && tok->str() == "break") + else if (is_if && tok->str() == "break") return true; // bailout for "return" @@ -874,7 +874,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectortokAt(-2); // just taking the address? - const bool addr(tok3 && (Token::simpleMatch(tok3, "&") || + const bool addr(tok3 && (tok3->str() == "&" || Token::simpleMatch(tok3->previous(), "& ("))); // taking address of 1 past end? @@ -1065,8 +1065,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo tok2 = tok2->tokAt(-2); // just taking the address? - const bool addr(Token::simpleMatch(tok2, "&") || - Token::simpleMatch(tok2->previous(), "& (")); + const bool addr(tok2 && (tok2->str() == "&" || + Token::simpleMatch(tok2->previous(), "& ("))); // taking address of 1 past end? if (addr && totalIndex == totalElements) @@ -1278,6 +1278,9 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() else if (tok->str() == "}") --indentlevel; + if (indentlevel <= 0) + continue; + // size : Max array index MathLib::bigint size = 0; @@ -1298,25 +1301,24 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() "Check (BufferOverrun::checkGlobalAndLocalVariable)", 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)); type = tok->strAt(4); varid = tok->next()->varId(); nextTok = 8; - } else if (indentlevel > 0 && Token::Match(tok, "[*;{}] %var% = new %type% ( %num% )")) { + } else if (Token::Match(tok, "[*;{}] %var% = new %type% ( %num% )")) { size = 1; type = tok->strAt(4); varid = tok->next()->varId(); nextTok = 8; - } else if (indentlevel > 0 && - Token::Match(tok, "[;{}] %var% = %str% ;") && + } else if (Token::Match(tok, "[;{}] %var% = %str% ;") && tok->next()->varId() > 0 && NULL != Token::findmatch(_tokenizer->tokens(), "[;{}] const| %type% * %varid% ;", tok->next()->varId())) { size = 1 + int(tok->tokAt(3)->strValue().size()); type = "char"; varid = tok->next()->varId(); 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)); type = "char"; // minimum type, typesize=1 varid = tok->next()->varId(); diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 660f75677..f18f915b7 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1337,25 +1337,13 @@ bool CheckClass::isMemberVar(const Scope *scope, 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; unsigned int numpar = 1; - unsigned int parlevel = 0; - for (; tok; tok = tok->next()) { - if (tok->str() == "(") - ++parlevel; - - else if (tok->str() == ")") { - if (parlevel <=1) - break; - --parlevel; - } - - else if (parlevel==1 && tok->str() == ",") { - ++numpar; - } - } + while ((tok = tok->nextArgument())) + numpar++; return numpar; } @@ -1364,7 +1352,7 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) { unsigned int args = countParameters(tok); - std::list::const_iterator func; + std::list::const_iterator func; unsigned int matches = 0; unsigned int consts = 0; diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 7f4caa8ba..4c196dc54 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -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% (")) - return -1; - if (Token::Match(tok->tokAt(2), "void| )")) + tok = tok->tokAt(2); + if (tok->str() == ")") return 0; - int numpar = 1; - int parlevel = 0; - for (; tok; tok = tok->next()) { - if (tok->str() == "(") - ++parlevel; + unsigned int numpar = 1; + while (tok = tok->nextArgument()) + numpar++; - else if (tok->str() == ")") { - if (parlevel <= 1) - return numpar; - --parlevel; - } - - else if (parlevel == 1 && tok->str() == ",") { - ++numpar; - } - } - - return -1; + return numpar; } bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname) @@ -729,8 +715,8 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list it is not a noreturn function if (tok->strAt(-1) == "=") 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() { diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index ace4daa7d..a99db8cfd 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -336,24 +336,14 @@ void CheckNullPointer::nullPointerLinkedList() // } for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) { // search for a "for" token.. - if (!Token::simpleMatch(tok1, "for (")) + if (tok1->str() != "for") continue; // is there any dereferencing occurring in the for statement - // parlevel2 counts the parentheses when using tok2. - unsigned int parlevel2 = 1; - 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; - } - + const Token* end2 = tok1->tokAt(1)->link(); + for (const Token *tok2 = tok1->tokAt(2); tok2 != end2; tok2 = tok2->next()) { // 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 const unsigned int varid(tok2->varId()); if (varid == 0) diff --git a/lib/path.cpp b/lib/path.cpp index f1c26aec0..4996f1177 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -22,7 +22,7 @@ #include #include "path.h" -std::string Path::toNativeSeparators(const std::string &path) +std::string Path::toNativeSeparators(std::string path) { #if defined(_WIN32) char separ = '/'; @@ -31,18 +31,16 @@ std::string Path::toNativeSeparators(const std::string &path) char separ = '\\'; char native = '/'; #endif - std::string modified(path); - std::replace(modified.begin(), modified.end(), separ, native); - return modified; + std::replace(path.begin(), path.end(), separ, native); + return path; } -std::string Path::fromNativeSeparators(const std::string &path) +std::string Path::fromNativeSeparators(std::string path) { char nonnative = '\\'; char newsepar = '/'; - std::string modified(path); - std::replace(modified.begin(), modified.end(), nonnative, newsepar); - return modified; + std::replace(path.begin(), path.end(), nonnative, newsepar); + return path; } std::string Path::simplifyPath(const char *originalPath) @@ -109,11 +107,10 @@ bool Path::sameFileName(const std::string &fname1, const std::string &fname2) #endif } -std::string Path::removeQuotationMarks(const std::string &path) +std::string Path::removeQuotationMarks(std::string path) { - std::string editPath(path); - editPath.erase(std::remove(editPath.begin(), editPath.end(), '\"'), editPath.end()); - return editPath; + path.erase(std::remove(path.begin(), path.end(), '\"'), path.end()); + return path; } std::string Path::getFilenameExtension(const std::string &path) diff --git a/lib/path.h b/lib/path.h index 274bfe6c9..5fd71afb8 100644 --- a/lib/path.h +++ b/lib/path.h @@ -38,14 +38,14 @@ public: * @param path Path string to convert. * @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. * @param path Path string to convert. * @return converted path. */ - static std::string fromNativeSeparators(const std::string &path); + static std::string fromNativeSeparators(std::string path); /** * @brief Simplify path "foo/bar/.." => "foo" @@ -68,7 +68,7 @@ public: * @param path path to be cleaned. * @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. diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 57f2a580b..2f1d27729 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1107,9 +1107,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const } if (from_negation) { ndeflist.push_back(deflist.back()); - deflist.pop_back(); - std::string nmark("!"); - deflist.push_back(nmark); + deflist.back() = "!"; } if (std::find(ret.begin(), ret.end(), def) == ret.end()) { @@ -1119,13 +1117,11 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) { if (deflist.back() == "!") { - deflist.pop_back(); - deflist.push_back(ndeflist.back()); + deflist.back() = ndeflist.back(); ndeflist.pop_back(); } else { std::string tempDef((deflist.back() == "1") ? "0" : "1"); - deflist.pop_back(); - deflist.push_back(tempDef); + deflist.back() = tempDef; } } @@ -2701,11 +2697,8 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file } } - { - std::map::iterator it; - for (it = macros.begin(); it != macros.end(); ++it) - delete it->second; - } + for (std::map::iterator it = macros.begin(); it != macros.end(); ++it) + delete it->second; return ostr.str(); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1a0e4f593..0f995f01f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -826,7 +826,7 @@ static Token *processFunc(Token *tok2, bool inOperator) tok2 = tok2->linkAt(4)->next(); else if (Token::Match(tok2->next(), "* ( * %type% (")) tok2 = tok2->linkAt(5)->next(); - else if (Token::Match(tok2->next(), "* [") && + else if (Token::simpleMatch(tok2->next(), "* [") && Token::simpleMatch(tok2->linkAt(2), "] ;")) tok2 = tok2->next(); else { @@ -3450,7 +3450,7 @@ void Tokenizer::setVarId() tok = tok->next(); // skip global namespace prefix - if (Token::simpleMatch(tok, "::")) + if (tok && tok->str() == "::") tok = tok->next(); while (Token::Match(tok, "%var% ::")) @@ -8389,9 +8389,9 @@ const char *Tokenizer::getParameterName(const Token *ftok, unsigned int par) for (; ftok; ftok = ftok->next()) { if (ftok->str() == ")") break; - if (ftok->str() == ",") + else if (ftok->str() == ",") ++_par; - if (par == _par && Token::Match(ftok, "%var% [,)]")) + else if (par == _par && Token::Match(ftok, "%var% [,)]")) return ftok->str().c_str(); } return NULL; @@ -9145,25 +9145,18 @@ void Tokenizer::simplifyStructDecl() void Tokenizer::simplifyCallingConvention() { - const char * pattern = "__cdecl|__stdcall|__fastcall|__thiscall|__clrcall|__syscall|__pascal|__fortran|__far|__near|WINAPI|APIENTRY|CALLBACK"; - while (Token::Match(_tokens, pattern)) { - _tokens->deleteThis(); - } + const char pattern[] = "__cdecl|__stdcall|__fastcall|__thiscall|__clrcall|__syscall|__pascal|__fortran|__far|__near|WINAPI|APIENTRY|CALLBACK"; for (Token *tok = _tokens; tok; tok = tok->next()) { - while (Token::Match(tok->next(), pattern)) { - tok->deleteNext(); + while (Token::Match(tok, pattern)) { + tok->deleteThis(); } } } 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()) { - 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()); tok->deleteThis(); } @@ -9172,12 +9165,8 @@ void Tokenizer::simplifyDeclspec() 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()) { - 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 )")) { // check if after variable name if (Token::Match(tok->next()->link()->next(), ";|=")) { @@ -9192,7 +9181,6 @@ void Tokenizer::simplifyAttribute() Token::eraseTokens(tok, tok->next()->link()->next()); tok->deleteThis(); - tok = tok->previous(); } } } @@ -9201,12 +9189,9 @@ void Tokenizer::simplifyAttribute() void Tokenizer::simplifyKeyword() { 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()) { - while (Token::Match(tok->next(), pattern)) { - tok->deleteNext(); + while (Token::Match(tok, pattern)) { + tok->deleteThis(); } } }