From 9c67af058a7e02a7c58d0e4b07e48c884183fb0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Jul 2013 12:31:04 +0200 Subject: [PATCH] SymbolDatabase: Renamed Variable::varId() to Variable::declarationId() to make it more clear how it works. --- lib/checkassignif.cpp | 2 +- lib/checkbufferoverrun.cpp | 124 ++++++++++++++++++------------------- lib/checkbufferoverrun.h | 14 ++--- lib/checkclass.cpp | 4 +- lib/checkio.cpp | 8 +-- lib/checkmemoryleak.cpp | 24 +++---- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 16 ++--- lib/checkstl.cpp | 10 +-- lib/checkuninitvar.cpp | 20 +++--- lib/checkunusedvar.cpp | 14 ++--- lib/symboldatabase.cpp | 12 ++-- lib/symboldatabase.h | 6 +- 13 files changed, 128 insertions(+), 128 deletions(-) diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 5195e16ff..69474f6a8 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -70,7 +70,7 @@ void CheckAssignIf::assignIf() if (num < 0 && bitop == '|') continue; - assignIfParseScope(tok, tok->tokAt(4), var->varId(), var->isLocal(), bitop, num); + assignIfParseScope(tok, tok->tokAt(4), var->declarationId(), var->isLocal(), bitop, num); } } } diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 1cbb5609c..f42b9e9ed 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -438,7 +438,7 @@ static bool for_bailout(const Token * const tok1, unsigned int varid) void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &arrayInfo, const std::string &strindex, bool condition_out_of_bounds, unsigned int counter_varid, const std::string &min_counter_value, const std::string &max_counter_value) { - const std::string pattern = (arrayInfo.varid() ? std::string("%varid%") : arrayInfo.varname()) + " [ " + strindex + " ]"; + const std::string pattern = (arrayInfo.declarationId() ? std::string("%varid%") : arrayInfo.varname()) + " [ " + strindex + " ]"; for (const Token* tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) { // TestBufferOverrun::array_index_for_question @@ -477,16 +477,16 @@ void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &array } if (Token::Match(tok2, "if|switch")) { - if (bailoutIfSwitch(tok2, arrayInfo.varid())) + if (bailoutIfSwitch(tok2, arrayInfo.declarationId())) break; } - if (condition_out_of_bounds && Token::Match(tok2, pattern.c_str(), arrayInfo.varid())) { + if (condition_out_of_bounds && Token::Match(tok2, pattern.c_str(), arrayInfo.declarationId())) { bufferOverrunError(tok2, arrayInfo.varname()); break; } - else if (arrayInfo.varid() && tok2->varId() && counter_varid > 0 && !min_counter_value.empty() && !max_counter_value.empty()) { + else if (arrayInfo.declarationId() && tok2->varId() && counter_varid > 0 && !min_counter_value.empty() && !max_counter_value.empty()) { // Is the loop variable used to calculate the array index? // In this scope it is determined if such calculated // array indexes are out of bounds. @@ -499,7 +499,7 @@ void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &array // Maximum calculated array index int max_index = 0; - if (Token::Match(tok2, "%varid% [ %var% +|-|*|/ %num% ]", arrayInfo.varid()) && + if (Token::Match(tok2, "%varid% [ %var% +|-|*|/ %num% ]", arrayInfo.declarationId()) && tok2->tokAt(2)->varId() == counter_varid) { // operator: +-*/ const char action = tok2->strAt(3)[0]; @@ -511,7 +511,7 @@ void CheckBufferOverrun::parse_for_body(const Token *tok, const ArrayInfo &array //printf("max_index: %s %c %s\n", max_counter_value.c_str(), action, second.c_str()); min_index = std::atoi(MathLib::calculate(min_counter_value, second, action).c_str()); max_index = std::atoi(MathLib::calculate(max_counter_value, second, action).c_str()); - } else if (Token::Match(tok2, "%varid% [ %num% +|-|*|/ %var% ]", arrayInfo.varid()) && + } else if (Token::Match(tok2, "%varid% [ %num% +|-|*|/ %var% ]", arrayInfo.declarationId()) && tok2->tokAt(4)->varId() == counter_varid) { // operator: +-*/ const char action = tok2->strAt(3)[0]; @@ -643,14 +643,14 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p // No variable id occur for instance when: // - Variable function arguments: "void f(...)" // - Unnamed parameter: "void f(char *)" - if (parameter->varId() == 0) + if (parameter->declarationId() == 0) return; // Check the parameter usage in the function scope.. for (const Token* ftok = func->functionScope->classStart; ftok != func->functionScope->classEnd; ftok = ftok->next()) { if (Token::Match(ftok, "if|for|while (")) { // bailout if there is buffer usage.. - if (bailoutIfSwitch(ftok, parameter->varId())) { + if (bailoutIfSwitch(ftok, parameter->declarationId())) { break; } @@ -669,7 +669,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p if (ftok->str() == "}") break; - if (ftok->varId() == parameter->varId()) { + if (ftok->varId() == parameter->declarationId()) { if (Token::Match(ftok->previous(), "-- %var%") || Token::Match(ftok, "%var% --")) break; @@ -691,7 +691,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p // Calling function.. if (Token::Match(ftok, "%var% (")) { ArrayInfo ai(arrayInfo); - ai.varid(parameter->varId()); + ai.declarationId(parameter->declarationId()); checkFunctionCall(ftok, ai, callstack); } } @@ -749,18 +749,18 @@ void CheckBufferOverrun::checkFunctionCall(const Token *tok, const ArrayInfo &ar const Token *tok2 = tok->tokAt(2); // 1st parameter.. - if (Token::Match(tok2, "%varid% ,|)", arrayInfo.varid())) + if (Token::Match(tok2, "%varid% ,|)", arrayInfo.declarationId())) checkFunctionParameter(*tok, 1, arrayInfo, callstack); - else if (Token::Match(tok2, "%varid% + %num% ,|)", arrayInfo.varid())) { + else if (Token::Match(tok2, "%varid% + %num% ,|)", arrayInfo.declarationId())) { const ArrayInfo ai(arrayInfo.limit(MathLib::toLongNumber(tok2->strAt(2)))); checkFunctionParameter(*tok, 1, ai, callstack); } // goto 2nd parameter and check it.. tok2 = tok2->nextArgument(); - if (Token::Match(tok2, "%varid% ,|)", arrayInfo.varid())) + if (Token::Match(tok2, "%varid% ,|)", arrayInfo.declarationId())) checkFunctionParameter(*tok, 2, arrayInfo, callstack); - else if (Token::Match(tok2, "%varid% + %num% ,|)", arrayInfo.varid())) { + else if (Token::Match(tok2, "%varid% + %num% ,|)", arrayInfo.declarationId())) { const ArrayInfo ai(arrayInfo.limit(MathLib::toLongNumber(tok2->strAt(2)))); checkFunctionParameter(*tok, 2, ai, callstack); } @@ -880,7 +880,7 @@ void CheckBufferOverrun::arrayIndexInForLoop(const Token *tok, const ArrayInfo & const Token *useToken = NULL; bool incrementInLoop = false; for (const Token *loopTok = tok3->tokAt(3); loopTok != endToken; loopTok = loopTok->next()) { - if (Token::Match(loopTok, "%varid% [ %var% ++| ]", arrayInfo.varid()) && loopTok->tokAt(2)->varId() == counter_varid) + if (Token::Match(loopTok, "%varid% [ %var% ++| ]", arrayInfo.declarationId()) && loopTok->tokAt(2)->varId() == counter_varid) useToken = loopTok; if (Token::Match(loopTok, "%varid% ++", counter_varid)) incrementInLoop = true; @@ -898,7 +898,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "%varid% [ %num% ]", varid)) || - (varid == 0 && Token::Match(tok, (varnames + " [ %num% ]").c_str()))) { + if ((declarationId > 0 && Token::Match(tok, "%varid% [ %num% ]", declarationId)) || + (declarationId == 0 && Token::Match(tok, (varnames + " [ %num% ]").c_str()))) { const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2 + varc)); if (index >= size) { std::vector indexes; @@ -928,22 +928,22 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vectorscope()->classEnd; tok != end; tok = tok->next()) { - if (varid != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", varid)) { + if (declarationId != 0 && Token::Match(tok, "%varid% = new|malloc|realloc", declarationId)) { // Abort break; } // reassign buffer - if (varid > 0 && Token::Match(tok, "[;{}] %varid% = %any%", varid)) { + if (declarationId > 0 && Token::Match(tok, "[;{}] %varid% = %any%", declarationId)) { // using varid .. bailout - if (tok->tokAt(3)->varId() != varid) + if (tok->tokAt(3)->varId() != declarationId) break; pointerIsOutOfBounds = false; } // Array index.. - if ((varid > 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [ %num% ]", varid))) || - (varid == 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && (Token::Match(tok->next(), (varnames + " [ %num% ]").c_str()) || Token::Match(tok->next(), (varname[0] +" [ %num% ] . " + varname[1] + " [ %num% ]").c_str()))))) { + if ((declarationId > 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [ %num% ]", declarationId))) || + (declarationId == 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && (Token::Match(tok->next(), (varnames + " [ %num% ]").c_str()) || Token::Match(tok->next(), (varname[0] +" [ %num% ] . " + varname[1] + " [ %num% ]").c_str()))))) { std::vector indexes; const Token *tok2 = tok->tokAt(2 + varc); for (; Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) { @@ -1019,7 +1019,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0) { + if (declarationId == 0 && size > 0) { std::list callstack; callstack.push_back(tok); if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str())) @@ -1030,7 +1030,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", varid)) || - (varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) { + if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", declarationId)) || + (declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) { const std::size_t len = Token::getStrLength(tok->tokAt(varc + 4)); if (total_size > 0 && len >= (unsigned int)total_size) { - bufferOverrunError(tok, varid > 0 ? std::string() : varnames); + bufferOverrunError(tok, declarationId > 0 ? std::string() : varnames); continue; } - } else if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", varid)) || - (varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %var% )").c_str()))) { + } else if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", declarationId)) || + (declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %var% )").c_str()))) { const Variable *var = tok->tokAt(4)->variable(); if (var && var->isArray() && var->dimensions().size() == 1) { const std::size_t len = (std::size_t)var->dimension(0); @@ -1060,12 +1060,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0 ? std::string("strcat ( %varid% , %str% ) ;") : ("strcat ( " + varnames + " , %str% ) ;"); - if (Token::Match(tok, strcatPattern.c_str(), varid)) { + const std::string strcatPattern = declarationId > 0 ? std::string("strcat ( %varid% , %str% ) ;") : ("strcat ( " + varnames + " , %str% ) ;"); + if (Token::Match(tok, strcatPattern.c_str(), declarationId)) { std::size_t charactersAppend = 0; const Token *tok2 = tok; - while (Token::Match(tok2, strcatPattern.c_str(), varid)) { + while (Token::Match(tok2, strcatPattern.c_str(), declarationId)) { charactersAppend += Token::getStrLength(tok2->tokAt(4 + varc)); if (charactersAppend >= static_cast(total_size)) { bufferOverrunError(tok2); @@ -1077,14 +1077,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0" check. - const std::string sprintfPattern = varid > 0 ? std::string("sprintf ( %varid% , %str% [,)]") : ("sprintf ( " + varnames + " , %str% [,)]"); - if (Token::Match(tok, sprintfPattern.c_str(), varid) && total_size > 0) { + const std::string sprintfPattern = declarationId > 0 ? std::string("sprintf ( %varid% , %str% [,)]") : ("sprintf ( " + varnames + " , %str% [,)]"); + if (Token::Match(tok, sprintfPattern.c_str(), declarationId) && total_size > 0) { checkSprintfCall(tok, static_cast(total_size)); } // snprintf.. - const std::string snprintfPattern = varid > 0 ? std::string("snprintf ( %varid% , %num% ,") : ("snprintf ( " + varnames + " , %num% ,"); - if (Token::Match(tok, snprintfPattern.c_str(), varid)) { + const std::string snprintfPattern = declarationId > 0 ? std::string("snprintf ( %varid% , %num% ,") : ("snprintf ( " + varnames + " , %num% ,"); + if (Token::Match(tok, snprintfPattern.c_str(), declarationId)) { const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4 + varc)); if (n > total_size) outOfBoundsError(tok->tokAt(4 + varc), "snprintf size", true, n, total_size); @@ -1093,24 +1093,24 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector 0) { // No varid => function calls are not handled - if (varid == 0) + if (declarationId == 0) continue; - const ArrayInfo arrayInfo1(varid, varnames, total_size / size, size); + const ArrayInfo arrayInfo1(declarationId, varnames, total_size / size, size); const std::list callstack; checkFunctionCall(tok, arrayInfo1, callstack); } // undefined behaviour: result of pointer arithmetic is out of bounds - else if (varid && Token::Match(tok, "= %varid% + %num% ;", varid)) { + else if (declarationId && Token::Match(tok, "= %varid% + %num% ;", declarationId)) { const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3)); if (index > size && _settings->isEnabled("portability")) pointerOutOfBoundsError(tok->next(), "buffer"); - if (index >= size && Token::Match(tok->tokAt(-2), "[;{}] %varid% =", varid)) + if (index >= size && Token::Match(tok->tokAt(-2), "[;{}] %varid% =", declarationId)) pointerIsOutOfBounds = true; } - else if (pointerIsOutOfBounds && Token::Match(tok, "[;{}=] * %varid% [;=]", varid)) { + else if (pointerIsOutOfBounds && Token::Match(tok, "[;{}=] * %varid% [;=]", declarationId)) { outOfBoundsError(tok->tokAt(2), tok->strAt(2), false, 0, 0); } } @@ -1131,7 +1131,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo continue; } - else if (Token::Match(tok, "%varid% [ %num% ]", arrayInfo.varid())) { + else if (Token::Match(tok, "%varid% [ %num% ]", arrayInfo.declarationId())) { std::vector indexes; for (const Token *tok2 = tok->next(); Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) { const MathLib::bigint index = MathLib::toLongNumber(tok2->strAt(1)); @@ -1214,7 +1214,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo checkFunctionCall(tok, arrayInfo, callstack); } - if (Token::Match(tok, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", arrayInfo.varid())) { + if (Token::Match(tok, "strncpy|memcpy|memmove ( %varid% , %str% , %num% )", arrayInfo.declarationId())) { unsigned int num = (unsigned int)MathLib::toLongNumber(tok->strAt(6)); if (Token::getStrLength(tok->tokAt(4)) >= (unsigned int)total_size && (unsigned int)total_size == num) { if (_settings->inconclusive) @@ -1222,7 +1222,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } } - if ((Token::Match(tok, "strncpy|strncat ( %varid% ,", arrayInfo.varid()) && Token::Match(tok->linkAt(1)->tokAt(-2), ", %num% )"))) { + if ((Token::Match(tok, "strncpy|strncat ( %varid% ,", arrayInfo.declarationId()) && Token::Match(tok->linkAt(1)->tokAt(-2), ", %num% )"))) { const Token* param3 = tok->linkAt(1)->previous(); // check for strncpy which is not terminated @@ -1253,7 +1253,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } // Dangerous usage of strncpy + strncat.. - if (Token::Match(param3->tokAt(2), "; strncat ( %varid% ,", arrayInfo.varid()) && Token::Match(param3->linkAt(4)->tokAt(-2), ", %num% )")) { + if (Token::Match(param3->tokAt(2), "; strncat ( %varid% ,", arrayInfo.declarationId()) && Token::Match(param3->linkAt(4)->tokAt(-2), ", %num% )")) { const MathLib::bigint n = MathLib::toLongNumber(param3->str()) + MathLib::toLongNumber(param3->linkAt(4)->strAt(-1)); if (n > total_size) strncatUsageError(param3->tokAt(3)); @@ -1261,7 +1261,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } // Writing data into array.. - if (Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", arrayInfo.varid())) { + if (Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", arrayInfo.declarationId())) { const std::size_t len = Token::getStrLength(tok->tokAt(4)); if (total_size > 0 && len >= (unsigned int)total_size) { bufferOverrunError(tok, arrayInfo.varname()); @@ -1270,11 +1270,11 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } // Detect few strcat() calls - if (total_size > 0 && Token::Match(tok, "strcat ( %varid% , %str% ) ;", arrayInfo.varid())) { + if (total_size > 0 && Token::Match(tok, "strcat ( %varid% , %str% ) ;", arrayInfo.declarationId())) { std::size_t charactersAppend = 0; const Token *tok2 = tok; - while (tok2 && Token::Match(tok2, "strcat ( %varid% , %str% ) ;", arrayInfo.varid())) { + while (tok2 && Token::Match(tok2, "strcat ( %varid% , %str% ) ;", arrayInfo.declarationId())) { charactersAppend += Token::getStrLength(tok2->tokAt(4)); if (charactersAppend >= (unsigned int)total_size) { bufferOverrunError(tok2, arrayInfo.varname()); @@ -1285,12 +1285,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo } - if (Token::Match(tok, "sprintf ( %varid% , %str% [,)]", arrayInfo.varid())) { + if (Token::Match(tok, "sprintf ( %varid% , %str% [,)]", arrayInfo.declarationId())) { checkSprintfCall(tok, total_size); } // snprintf.. - if (total_size > 0 && Token::Match(tok, "snprintf ( %varid% , %num% ,", arrayInfo.varid())) { + if (total_size > 0 && Token::Match(tok, "snprintf ( %varid% , %num% ,", arrayInfo.declarationId())) { const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4)); if (n > total_size) outOfBoundsError(tok->tokAt(4), "snprintf size", true, n, total_size); @@ -1298,14 +1298,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo // readlink() / readlinkat() buffer usage if (_settings->standards.posix) { - if (Token::simpleMatch(tok, "readlink (") && Token::Match(tok->tokAt(2)->nextArgument(), "%varid% , %num% )", arrayInfo.varid())) + if (Token::simpleMatch(tok, "readlink (") && Token::Match(tok->tokAt(2)->nextArgument(), "%varid% , %num% )", arrayInfo.declarationId())) checkReadlinkBufferUsage(tok, scope_begin, total_size, false); - else if (Token::simpleMatch(tok, "readlinkat (") && Token::Match(tok->tokAt(2)->nextArgument()->nextArgument(), "%varid% , %num% )", arrayInfo.varid())) + else if (Token::simpleMatch(tok, "readlinkat (") && Token::Match(tok->tokAt(2)->nextArgument()->nextArgument(), "%varid% , %num% )", arrayInfo.declarationId())) checkReadlinkBufferUsage(tok, scope_begin, total_size, true); } // undefined behaviour: result of pointer arithmetic is out of bounds - if (_settings->isEnabled("portability") && Token::Match(tok, "= %varid% + %num% ;", arrayInfo.varid())) { + if (_settings->isEnabled("portability") && Token::Match(tok, "= %varid% + %num% ;", arrayInfo.declarationId())) { const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3)); if (index < 0 || index > arrayInfo.num(0)) { pointerOutOfBoundsError(tok->next(), "array"); @@ -1485,7 +1485,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() continue; std::vector v; - ArrayInfo temp(var->varId(), tok->next()->str(), total_size / size, size); + ArrayInfo temp(var->declarationId(), tok->next()->str(), total_size / size, size); checkScope(tok->tokAt(nextTok), v, temp); } } @@ -1653,7 +1653,7 @@ void CheckBufferOverrun::checkStructVariable() // Check variable usage.. ArrayInfo temp = arrayInfo; - temp.varid(0); // do variable lookup by variable and member names rather than varid + temp.declarationId(0); // do variable lookup by variable and member names rather than varid std::string varnames; // use class and member name for messages for (unsigned int k = 0; k < varname.size(); ++k) varnames += (k == 0 ? "" : ".") + varname[k]; @@ -1969,7 +1969,7 @@ void CheckBufferOverrun::negativeIndex() CheckBufferOverrun::ArrayInfo::ArrayInfo() - : _element_size(0), _varid(0) + : _element_size(0), _declarationId(0) { } @@ -1978,8 +1978,8 @@ CheckBufferOverrun::ArrayInfo::ArrayInfo(const CheckBufferOverrun::ArrayInfo &ai *this = ai; } -CheckBufferOverrun::ArrayInfo::ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcevarid) - : _varname(var->name()), _varid((forcevarid == 0U) ? var->varId() : forcevarid) +CheckBufferOverrun::ArrayInfo::ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcedeclid) + : _varname(var->name()), _declarationId((forcedeclid == 0U) ? var->declarationId() : forcedeclid) { for (std::size_t i = 0; i < var->dimensions().size(); i++) _num.push_back(var->dimension(i)); @@ -1996,7 +1996,7 @@ CheckBufferOverrun::ArrayInfo & CheckBufferOverrun::ArrayInfo::operator=(const C if (&ai != this) { _element_size = ai._element_size; _num = ai._num; - _varid = ai._varid; + _declarationId = ai._declarationId; _varname = ai._varname; } return *this; @@ -2009,7 +2009,7 @@ CheckBufferOverrun::ArrayInfo & CheckBufferOverrun::ArrayInfo::operator=(const C * this will not be needed as the declare can be used instead. */ CheckBufferOverrun::ArrayInfo::ArrayInfo(unsigned int id, const std::string &name, MathLib::bigint size1, MathLib::bigint n) - : _varname(name), _element_size(size1), _varid(id) + : _varname(name), _element_size(size1), _declarationId(id) { _num.push_back(n); } @@ -2022,7 +2022,7 @@ CheckBufferOverrun::ArrayInfo CheckBufferOverrun::ArrayInfo::limit(MathLib::bigi n *= _num[i]; if (uvalue > n) n = uvalue; - return ArrayInfo(_varid, _varname, _element_size, n - uvalue); + return ArrayInfo(_declarationId, _varname, _element_size, n - uvalue); } diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index a739e4a2e..226cb3aac 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -125,13 +125,13 @@ public: /** size of each element in array */ MathLib::bigint _element_size; - /** variable id */ - unsigned int _varid; + /** declaration id */ + unsigned int _declarationId; public: ArrayInfo(); ArrayInfo(const ArrayInfo &); - ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcevarid = 0); + ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcedeclid = 0); ArrayInfo & operator=(const ArrayInfo &ai); /** @@ -164,11 +164,11 @@ public: } /** Variable name */ - unsigned int varid() const { - return _varid; + unsigned int declarationId() const { + return _declarationId; } - void varid(unsigned int id) { - _varid = id; + void declarationId(unsigned int id) { + _declarationId = id; } /** Variable name */ diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 6d8ffd125..987f6afd5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -517,7 +517,7 @@ void CheckClass::initializeVarList(const Function &func, std::listnext(), "%var% . %var% (")) { std::list::const_iterator var; for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { - if (var->varId() == ftok->next()->varId()) { + if (var->declarationId() == ftok->next()->varId()) { /** @todo false negative: we assume function changes variable state */ assignVar(ftok->next()->str(), scope, usage); break; @@ -1334,7 +1334,7 @@ void CheckClass::virtualDestructor() for (std::size_t k = 0; k < symbolDatabase->getVariableListSize(); k++) { const Variable* var = symbolDatabase->getVariableFromVarId(k); if (var && var->isPointer() && var->type() == derivedFrom) - basepointer.insert(var->varId()); + basepointer.insert(var->declarationId()); } // pointer variables of type 'Base *' that should not be deleted diff --git a/lib/checkio.cpp b/lib/checkio.cpp index c42273f3c..b36759799 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -107,16 +107,16 @@ void CheckIO::checkFileUsage() std::size_t varListSize = symbolDatabase->getVariableListSize(); for (std::size_t i = 1; i < varListSize; ++i) { const Variable* var = symbolDatabase->getVariableFromVarId(i); - if (!var || !var->varId() || var->isArray() || !Token::simpleMatch(var->typeStartToken(), "FILE *")) + if (!var || !var->declarationId() || var->isArray() || !Token::simpleMatch(var->typeStartToken(), "FILE *")) continue; if (var->isLocal()) { if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor" - filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN))); + filepointers.insert(std::make_pair(var->declarationId(), Filepointer(UNKNOWN))); else - filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED))); + filepointers.insert(std::make_pair(var->declarationId(), Filepointer(CLOSED))); } else { - filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN))); + filepointers.insert(std::make_pair(var->declarationId(), Filepointer(UNKNOWN))); // TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode } } diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 368013fa4..d422c573a 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -487,14 +487,14 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int // Check if pointer is allocated. int realloc = 0; for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) { - if (tok->varId() == arg->varId()) { + if (tok->varId() == arg->declarationId()) { if (Token::Match(tok->tokAt(-3), "free ( * %var% )")) { realloc = 1; allocType = No; } else if (Token::Match(tok->previous(), "* %var% =")) { - allocType = getAllocationType(tok->tokAt(2), arg->varId()); + allocType = getAllocationType(tok->tokAt(2), arg->declarationId()); if (allocType == No) { - allocType = getReallocationType(tok->tokAt(2), arg->varId()); + allocType = getReallocationType(tok->tokAt(2), arg->declarationId()); } if (allocType != No) { if (realloc) @@ -681,7 +681,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::listfunctionScope) return "use"; - Token *func = getcode(function->functionScope->classStart->next(), callstack, param->varId(), alloctype, dealloctype, false, sz); + Token *func = getcode(function->functionScope->classStart->next(), callstack, param->declarationId(), alloctype, dealloctype, false, sz); //simplifycode(func); const Token *func_ = func; while (func_ && func_->str() == ";") @@ -2528,12 +2528,12 @@ void CheckMemoryLeakStructMember::check() bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable) { - const unsigned int varid(variable->varId()); + const unsigned int declarationId(variable->declarationId()); bool alloc = false; for (const Token *tok2 = variable->nameToken(); tok2 != variable->scope()->classEnd; tok2 = tok2->next()) { - if (Token::Match(tok2, "= %varid% [;=]", varid)) { + if (Token::Match(tok2, "= %varid% [;=]", declarationId)) { return false; - } else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", varid)) { + } else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) { alloc = true; } } @@ -2576,14 +2576,14 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var // Unknown usage of struct /** @todo Check how the struct is used. Only bail out if necessary */ - else if (Token::Match(tok2, "[(,] %varid% [,)]", variable->varId())) + else if (Token::Match(tok2, "[(,] %varid% [,)]", variable->declarationId())) break; // Struct member is allocated => check if it is also properly deallocated.. - else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->varId()) - || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->varId()) - || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->varId())) { - const unsigned int structid(variable->varId()); + else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->declarationId()) + || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->declarationId()) + || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->declarationId())) { + const unsigned int structid(variable->declarationId()); const unsigned int structmemberid(tok2->tokAt(2)->varId()); // This struct member is allocated.. check that it is deallocated diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 3ab593453..f074e95f2 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1441,7 +1441,7 @@ private: // Pointer declaration declaration? const Variable *var = tok.variable(); if (var && var->isPointer() && var->nameToken() == &tok) - checks.push_back(new Nullpointer(owner, var->varId(), var->name(), symbolDatabase, library)); + checks.push_back(new Nullpointer(owner, var->declarationId(), var->name(), symbolDatabase, library)); } if (Token::simpleMatch(&tok, "try {")) { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b7fc9b75a..b387dcfb7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1744,7 +1744,7 @@ void CheckOther::checkVariableScope() continue; const Token* tok = var->nameToken()->next(); - if (Token::Match(tok, "; %varid% = %any% ;", var->varId())) { + if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) { tok = tok->tokAt(3); if (!tok->isNumber() && tok->type() != Token::eString && tok->type() != Token::eChar && !tok->isBoolean()) continue; @@ -1768,7 +1768,7 @@ void CheckOther::checkVariableScope() } tok = tok->link(); - } else if (tok->varId() == var->varId() || tok->str() == "goto") { + } else if (tok->varId() == var->declarationId() || tok->str() == "goto") { reduce = false; break; } @@ -1817,7 +1817,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (tok == forHeadEnd) forHeadEnd = 0; - if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope. + if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->declarationId())) { // Assigned in outer scope. loopVariable = false; unsigned int indent = 0; for (const Token* tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { // Ensure that variable isn't used on right side of =, too @@ -1829,23 +1829,23 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us indent--; } else if (tok2->str() == ";") break; - else if (tok2->varId() == var->varId()) { + else if (tok2->varId() == var->declarationId()) { loopVariable = true; break; } } } - if (loopVariable && Token::Match(tok, "%varid% !!=", var->varId())) // Variable used in loop + if (loopVariable && Token::Match(tok, "%varid% !!=", var->declarationId())) // Variable used in loop return false; - if (Token::Match(tok, "& %varid%", var->varId())) // Taking address of variable + if (Token::Match(tok, "& %varid%", var->declarationId())) // Taking address of variable return false; - if (Token::Match(tok, "= %varid%", var->varId()) && (var->isArray() || var->isPointer())) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope + if (Token::Match(tok, "= %varid%", var->declarationId()) && (var->isArray() || var->isPointer())) // Create a copy of array/pointer. Bailout, because the memory it points to might be necessary in outer scope return false; - if (tok->varId() == var->varId()) { + if (tok->varId() == var->declarationId()) { used = true; if (scope->type == Scope::eSwitch && scope == tok->scope()) return false; // Used in outer switch scope - unsafe or impossible to reduce scope diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 860c54300..85130e6af 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -103,7 +103,7 @@ void CheckStl::iterators() validIterator = true; // Is iterator compared against different container? - if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->varId()) { + if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->declarationId()) { iteratorsError(tok2, container->name(), tok2->strAt(2)); tok2 = tok2->tokAt(6); } @@ -123,7 +123,7 @@ void CheckStl::iterators() // If insert/erase is used on different container then // report an error - if (container && tok2->varId() != container->varId()) { + if (container && tok2->varId() != container->declarationId()) { // skip error message if container is a set.. const Variable *variableInfo = tok2->variable(); const Token *decltok = variableInfo ? variableInfo->typeStartToken() : NULL; @@ -312,13 +312,13 @@ void CheckStl::stlOutOfBounds() continue; // variable id for loop variable. - unsigned int numId = tok->next()->varId(); + const unsigned int numId = tok->next()->varId(); // variable id for the container variable - unsigned int varId = container->varId(); + const unsigned int declarationId = container->declarationId(); for (const Token *tok3 = tok->tokAt(8); tok3 && tok3 != i->classEnd; tok3 = tok3->next()) { - if (tok3->varId() == varId) { + if (tok3->varId() == declarationId) { if (Token::Match(tok3->next(), ". size|length ( )")) break; else if (Token::Match(tok3->next(), "[ %varid% ]", numId)) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 577d664cc..004c264ad 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -69,7 +69,7 @@ private: /** internal constructor for creating extra checks */ UninitVar(Check *c, const Variable* v, const SymbolDatabase* db, const Library *lib, bool isc) - : ExecutionPath(c, v->varId()), symbolDatabase(db), library(lib), isC(isc), var(v), alloc(false), strncpy_(false), memset_nonzero(false) { + : ExecutionPath(c, v->declarationId()), symbolDatabase(db), library(lib), isC(isc), var(v), alloc(false), strncpy_(false), memset_nonzero(false) { } /** is other execution path equal? */ @@ -420,7 +420,7 @@ private: while (parent) { for (std::list::const_iterator j = parent->varlist.begin(); j != parent->varlist.end(); ++j) { if (j->name() == var2->name()) { - ExecutionPath::bailOutVar(checks, j->varId()); // If there is a variable with the same name in other scopes, this might cause false positives, if there are unexpanded macros + ExecutionPath::bailOutVar(checks, j->declarationId()); // If there is a variable with the same name in other scopes, this might cause false positives, if there are unexpanded macros break; } } @@ -1309,7 +1309,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, const Token *end = tok->next()->link(); // If address of variable is taken in the block then bail out - if (Token::findmatch(tok->tokAt(2), "& %varid%", end, var.varId())) + if (Token::findmatch(tok->tokAt(2), "& %varid%", end, var.declarationId())) return true; // Skip block @@ -1370,7 +1370,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, while (tok && tok->str() != ";") { // variable is seen.. - if (tok->varId() == var.varId()) { + if (tok->varId() == var.declarationId()) { if (!membervar.empty()) { if (Token::Match(tok, "%var% . %var% ;|%cop%") && tok->strAt(2) == membervar) uninitStructMemberError(tok, tok->str() + "." + membervar); @@ -1403,7 +1403,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, } // variable is seen.. - if (tok->varId() == var.varId()) { + if (tok->varId() == var.declarationId()) { if (!membervar.empty()) { if (isMemberVariableAssignment(tok, membervar)) { checkRhs(tok, var, membervar); @@ -1439,7 +1439,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va { const Token * const endpar = startparentheses->link(); for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) { - if (tok->varId() == var.varId()) { + if (tok->varId() == var.declarationId()) { if (Token::Match(tok, "%var% . %var%")) { if (tok->strAt(2) == membervar) { if (isMemberVariableAssignment(tok, membervar)) @@ -1474,7 +1474,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const assert(tok->str() == "{"); for (const Token * const end = tok->link(); tok != end; tok = tok->next()) { - if (tok->varId() == var.varId()) { + if (tok->varId() == var.declarationId()) { if (!membervar.empty()) { if (isMemberVariableAssignment(tok, membervar)) { bool assign = true; @@ -1484,7 +1484,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const rhs = true; if (tok2->str() == ";") break; - if (rhs && tok2->varId() == var.varId() && isMemberVariableUsage(tok2, var.isPointer(), membervar)) { + if (rhs && tok2->varId() == var.declarationId() && isMemberVariableUsage(tok2, var.isPointer(), membervar)) { assign = false; break; } @@ -1504,7 +1504,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const bool assign = true; if (tok->strAt(1) == "=") { for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { - if (tok2->varId() == var.varId()) { + if (tok2->varId() == var.declarationId()) { assign = false; break; } @@ -1541,7 +1541,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, const std:: while (NULL != (tok = tok->next())) { if (tok->str() == "=") rhs = true; - else if (rhs && tok->varId() == var.varId()) { + else if (rhs && tok->varId() == var.declarationId()) { if (membervar.empty() && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP())) uninitvarError(tok, tok->str()); else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), membervar)) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 10ed07cbc..dce1bef92 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -57,7 +57,7 @@ public: /** variable is used.. set both read+write */ void use(std::list > & varReadInScope) { - varReadInScope.back().insert(_var->varId()); + varReadInScope.back().insert(_var->declarationId()); _read = true; _write = true; } @@ -161,7 +161,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2, bool replace) VariableUsage *temp = find(*i); if (temp) - temp->_aliases.erase(var1->_var->varId()); + temp->_aliases.erase(var1->_var->declarationId()); } // remove all aliases from var1 @@ -196,7 +196,7 @@ void Variables::clearAliases(unsigned int varid) VariableUsage *temp = find(*i); if (temp) - temp->_aliases.erase(usage->_var->varId()); + temp->_aliases.erase(usage->_var->declarationId()); } // remove all aliases from usage @@ -226,9 +226,9 @@ void Variables::addVar(const Variable *var, VariableType type, bool write_) { - if (var->varId() > 0) { - _varAddedInScope.back().insert(var->varId()); - _varUsage.insert(std::make_pair(var->varId(), VariableUsage(var, type, false, write_, false))); + if (var->declarationId() > 0) { + _varAddedInScope.back().insert(var->declarationId()); + _varUsage.insert(std::make_pair(var->declarationId(), VariableUsage(var, type, false, write_, false))); } } @@ -705,7 +705,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } } if (i->isArray() && i->isClass()) // Array of class/struct members. Initialized by ctor. - variables.write(i->varId(), i->nameToken()); + variables.write(i->declarationId(), i->nameToken()); if (i->isArray() && Token::Match(i->nameToken(), "%var% [ %var% ]")) // Array index variable read. variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken()); diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 0e454fe74..dc9379f43 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -832,7 +832,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti // add all variables std::list::const_iterator var; for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { - unsigned int varId = var->varId(); + unsigned int varId = var->declarationId(); if (varId) _variableList[varId] = &(*var); } @@ -843,10 +843,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti std::list::const_iterator arg; for (arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) { // check for named parameters - if (arg->nameToken() && arg->varId()) { - unsigned int varId = arg->varId(); - if (varId) - _variableList[varId] = &(*arg); + if (arg->nameToken() && arg->declarationId()) { + const unsigned int declarationId = arg->declarationId(); + if (declarationId > 0U) + _variableList[declarationId] = &(*arg); } } } @@ -1574,7 +1574,7 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons std::cout << indent << "_name: " << var->nameToken(); if (var->nameToken()) { std::cout << " " << var->name() << " " << _tokenizer->list.fileLine(var->nameToken()) << std::endl; - std::cout << indent << " varId: " << var->varId() << std::endl; + std::cout << indent << " declarationId: " << var->declarationId() << std::endl; } else std::cout << std::endl; std::cout << indent << "_start: " << var->typeStartToken() << " " << var->typeStartToken()->str() diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 03b7ad090..ead2c1e38 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -195,10 +195,10 @@ public: } /** - * Get variable ID. - * @return variable ID + * Get declaration ID (varId used for variable in its declaration). + * @return declaration ID */ - unsigned int varId() const { + unsigned int declarationId() const { // name may not exist for function arguments if (_name) return _name->varId();