SymbolDatabase: Renamed Variable::varId() to Variable::declarationId() to make it more clear how it works.

This commit is contained in:
Daniel Marjamäki 2013-07-20 12:31:04 +02:00
parent 1a2aaa6780
commit 9c67af058a
13 changed files with 128 additions and 128 deletions

View File

@ -70,7 +70,7 @@ void CheckAssignIf::assignIf()
if (num < 0 && bitop == '|') if (num < 0 && bitop == '|')
continue; continue;
assignIfParseScope(tok, tok->tokAt(4), var->varId(), var->isLocal(), bitop, num); assignIfParseScope(tok, tok->tokAt(4), var->declarationId(), var->isLocal(), bitop, num);
} }
} }
} }

View File

@ -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) 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()) { for (const Token* tok2 = tok; tok2 && tok2 != tok->link(); tok2 = tok2->next()) {
// TestBufferOverrun::array_index_for_question // 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 (Token::Match(tok2, "if|switch")) {
if (bailoutIfSwitch(tok2, arrayInfo.varid())) if (bailoutIfSwitch(tok2, arrayInfo.declarationId()))
break; 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()); bufferOverrunError(tok2, arrayInfo.varname());
break; 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? // Is the loop variable used to calculate the array index?
// In this scope it is determined if such calculated // In this scope it is determined if such calculated
// array indexes are out of bounds. // 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 // Maximum calculated array index
int max_index = 0; 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) { tok2->tokAt(2)->varId() == counter_varid) {
// operator: +-*/ // operator: +-*/
const char action = tok2->strAt(3)[0]; 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()); //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()); 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()); 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) { tok2->tokAt(4)->varId() == counter_varid) {
// operator: +-*/ // operator: +-*/
const char action = tok2->strAt(3)[0]; 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: // No variable id occur for instance when:
// - Variable function arguments: "void f(...)" // - Variable function arguments: "void f(...)"
// - Unnamed parameter: "void f(char *)" // - Unnamed parameter: "void f(char *)"
if (parameter->varId() == 0) if (parameter->declarationId() == 0)
return; return;
// Check the parameter usage in the function scope.. // Check the parameter usage in the function scope..
for (const Token* ftok = func->functionScope->classStart; ftok != func->functionScope->classEnd; ftok = ftok->next()) { for (const Token* ftok = func->functionScope->classStart; ftok != func->functionScope->classEnd; ftok = ftok->next()) {
if (Token::Match(ftok, "if|for|while (")) { if (Token::Match(ftok, "if|for|while (")) {
// bailout if there is buffer usage.. // bailout if there is buffer usage..
if (bailoutIfSwitch(ftok, parameter->varId())) { if (bailoutIfSwitch(ftok, parameter->declarationId())) {
break; break;
} }
@ -669,7 +669,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p
if (ftok->str() == "}") if (ftok->str() == "}")
break; break;
if (ftok->varId() == parameter->varId()) { if (ftok->varId() == parameter->declarationId()) {
if (Token::Match(ftok->previous(), "-- %var%") || if (Token::Match(ftok->previous(), "-- %var%") ||
Token::Match(ftok, "%var% --")) Token::Match(ftok, "%var% --"))
break; break;
@ -691,7 +691,7 @@ void CheckBufferOverrun::checkFunctionParameter(const Token &tok, unsigned int p
// Calling function.. // Calling function..
if (Token::Match(ftok, "%var% (")) { if (Token::Match(ftok, "%var% (")) {
ArrayInfo ai(arrayInfo); ArrayInfo ai(arrayInfo);
ai.varid(parameter->varId()); ai.declarationId(parameter->declarationId());
checkFunctionCall(ftok, ai, callstack); checkFunctionCall(ftok, ai, callstack);
} }
} }
@ -749,18 +749,18 @@ void CheckBufferOverrun::checkFunctionCall(const Token *tok, const ArrayInfo &ar
const Token *tok2 = tok->tokAt(2); const Token *tok2 = tok->tokAt(2);
// 1st parameter.. // 1st parameter..
if (Token::Match(tok2, "%varid% ,|)", arrayInfo.varid())) if (Token::Match(tok2, "%varid% ,|)", arrayInfo.declarationId()))
checkFunctionParameter(*tok, 1, arrayInfo, callstack); 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)))); const ArrayInfo ai(arrayInfo.limit(MathLib::toLongNumber(tok2->strAt(2))));
checkFunctionParameter(*tok, 1, ai, callstack); checkFunctionParameter(*tok, 1, ai, callstack);
} }
// goto 2nd parameter and check it.. // goto 2nd parameter and check it..
tok2 = tok2->nextArgument(); tok2 = tok2->nextArgument();
if (Token::Match(tok2, "%varid% ,|)", arrayInfo.varid())) if (Token::Match(tok2, "%varid% ,|)", arrayInfo.declarationId()))
checkFunctionParameter(*tok, 2, arrayInfo, callstack); 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)))); const ArrayInfo ai(arrayInfo.limit(MathLib::toLongNumber(tok2->strAt(2))));
checkFunctionParameter(*tok, 2, ai, callstack); checkFunctionParameter(*tok, 2, ai, callstack);
} }
@ -880,7 +880,7 @@ void CheckBufferOverrun::arrayIndexInForLoop(const Token *tok, const ArrayInfo &
const Token *useToken = NULL; const Token *useToken = NULL;
bool incrementInLoop = false; bool incrementInLoop = false;
for (const Token *loopTok = tok3->tokAt(3); loopTok != endToken; loopTok = loopTok->next()) { 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; useToken = loopTok;
if (Token::Match(loopTok, "%varid% ++", counter_varid)) if (Token::Match(loopTok, "%varid% ++", counter_varid))
incrementInLoop = true; incrementInLoop = true;
@ -898,7 +898,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
return; return;
const MathLib::bigint total_size = arrayInfo.element_size() * arrayInfo.num(0); const MathLib::bigint total_size = arrayInfo.element_size() * arrayInfo.num(0);
unsigned int varid = arrayInfo.varid(); const unsigned int declarationId = arrayInfo.declarationId();
std::string varnames; std::string varnames;
for (unsigned int i = 0; i < varname.size(); ++i) for (unsigned int i = 0; i < varname.size(); ++i)
@ -913,8 +913,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
} }
// Array index.. // Array index..
if ((varid > 0 && Token::Match(tok, "%varid% [ %num% ]", varid)) || if ((declarationId > 0 && Token::Match(tok, "%varid% [ %num% ]", declarationId)) ||
(varid == 0 && Token::Match(tok, (varnames + " [ %num% ]").c_str()))) { (declarationId == 0 && Token::Match(tok, (varnames + " [ %num% ]").c_str()))) {
const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2 + varc)); const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(2 + varc));
if (index >= size) { if (index >= size) {
std::vector<MathLib::bigint> indexes; std::vector<MathLib::bigint> indexes;
@ -928,22 +928,22 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
bool pointerIsOutOfBounds = false; bool pointerIsOutOfBounds = false;
for (const Token* const end = tok->scope()->classEnd; tok != end; tok = tok->next()) { for (const Token* const end = tok->scope()->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 // Abort
break; break;
} }
// reassign buffer // reassign buffer
if (varid > 0 && Token::Match(tok, "[;{}] %varid% = %any%", varid)) { if (declarationId > 0 && Token::Match(tok, "[;{}] %varid% = %any%", declarationId)) {
// using varid .. bailout // using varid .. bailout
if (tok->tokAt(3)->varId() != varid) if (tok->tokAt(3)->varId() != declarationId)
break; break;
pointerIsOutOfBounds = false; pointerIsOutOfBounds = false;
} }
// Array index.. // Array index..
if ((varid > 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [ %num% ]", varid))) || if ((declarationId > 0 && ((tok->str() == "return" || (!tok->isName() && !Token::Match(tok, "[.&]"))) && Token::Match(tok->next(), "%varid% [ %num% ]", declarationId))) ||
(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()))))) { (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<MathLib::bigint> indexes; std::vector<MathLib::bigint> indexes;
const Token *tok2 = tok->tokAt(2 + varc); const Token *tok2 = tok->tokAt(2 + varc);
for (; Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) { for (; Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) {
@ -1019,7 +1019,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
} }
// memset, memcmp, memcpy, strncpy, fgets.. // memset, memcmp, memcpy, strncpy, fgets..
if (varid == 0 && size > 0) { if (declarationId == 0 && size > 0) {
std::list<const Token *> callstack; std::list<const Token *> callstack;
callstack.push_back(tok); callstack.push_back(tok);
if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str())) if (Token::Match(tok, ("%var% ( " + varnames + " ,").c_str()))
@ -1030,7 +1030,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
// Loop.. // Loop..
if (Token::simpleMatch(tok, "for (")) { if (Token::simpleMatch(tok, "for (")) {
const ArrayInfo arrayInfo1(varid, varnames, (unsigned int)size, (unsigned int)total_size); const ArrayInfo arrayInfo1(declarationId, varnames, (unsigned int)size, (unsigned int)total_size);
bool bailout = false; bool bailout = false;
checkScopeForBody(tok, arrayInfo1, bailout); checkScopeForBody(tok, arrayInfo1, bailout);
if (bailout) if (bailout)
@ -1039,15 +1039,15 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
} }
// Writing data into array.. // Writing data into array..
if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", varid)) || if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %str% )", declarationId)) ||
(varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) { (declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %str% )").c_str()))) {
const std::size_t len = Token::getStrLength(tok->tokAt(varc + 4)); const std::size_t len = Token::getStrLength(tok->tokAt(varc + 4));
if (total_size > 0 && len >= (unsigned int)total_size) { if (total_size > 0 && len >= (unsigned int)total_size) {
bufferOverrunError(tok, varid > 0 ? std::string() : varnames); bufferOverrunError(tok, declarationId > 0 ? std::string() : varnames);
continue; continue;
} }
} else if ((varid > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", varid)) || } else if ((declarationId > 0 && Token::Match(tok, "strcpy|strcat ( %varid% , %var% )", declarationId)) ||
(varid == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %var% )").c_str()))) { (declarationId == 0 && Token::Match(tok, ("strcpy|strcat ( " + varnames + " , %var% )").c_str()))) {
const Variable *var = tok->tokAt(4)->variable(); const Variable *var = tok->tokAt(4)->variable();
if (var && var->isArray() && var->dimensions().size() == 1) { if (var && var->isArray() && var->dimensions().size() == 1) {
const std::size_t len = (std::size_t)var->dimension(0); const std::size_t len = (std::size_t)var->dimension(0);
@ -1060,12 +1060,12 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
} }
// Detect few strcat() calls // Detect few strcat() calls
const std::string strcatPattern = varid > 0 ? std::string("strcat ( %varid% , %str% ) ;") : ("strcat ( " + varnames + " , %str% ) ;"); const std::string strcatPattern = declarationId > 0 ? std::string("strcat ( %varid% , %str% ) ;") : ("strcat ( " + varnames + " , %str% ) ;");
if (Token::Match(tok, strcatPattern.c_str(), varid)) { if (Token::Match(tok, strcatPattern.c_str(), declarationId)) {
std::size_t charactersAppend = 0; std::size_t charactersAppend = 0;
const Token *tok2 = tok; 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)); charactersAppend += Token::getStrLength(tok2->tokAt(4 + varc));
if (charactersAppend >= static_cast<std::size_t>(total_size)) { if (charactersAppend >= static_cast<std::size_t>(total_size)) {
bufferOverrunError(tok2); bufferOverrunError(tok2);
@ -1077,14 +1077,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const std::vector<std::str
// sprintf.. // sprintf..
// TODO: change total_size to an unsigned value and remove the "&& total_size > 0" check. // TODO: change total_size to an unsigned value and remove the "&& total_size > 0" check.
const std::string sprintfPattern = varid > 0 ? std::string("sprintf ( %varid% , %str% [,)]") : ("sprintf ( " + varnames + " , %str% [,)]"); const std::string sprintfPattern = declarationId > 0 ? std::string("sprintf ( %varid% , %str% [,)]") : ("sprintf ( " + varnames + " , %str% [,)]");
if (Token::Match(tok, sprintfPattern.c_str(), varid) && total_size > 0) { if (Token::Match(tok, sprintfPattern.c_str(), declarationId) && total_size > 0) {
checkSprintfCall(tok, static_cast<unsigned int>(total_size)); checkSprintfCall(tok, static_cast<unsigned int>(total_size));
} }
// snprintf.. // snprintf..
const std::string snprintfPattern = varid > 0 ? std::string("snprintf ( %varid% , %num% ,") : ("snprintf ( " + varnames + " , %num% ,"); const std::string snprintfPattern = declarationId > 0 ? std::string("snprintf ( %varid% , %num% ,") : ("snprintf ( " + varnames + " , %num% ,");
if (Token::Match(tok, snprintfPattern.c_str(), varid)) { if (Token::Match(tok, snprintfPattern.c_str(), declarationId)) {
const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4 + varc)); const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4 + varc));
if (n > total_size) if (n > total_size)
outOfBoundsError(tok->tokAt(4 + varc), "snprintf size", true, 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<std::str
// Check function call.. // Check function call..
if (Token::Match(tok, "%var% (") && total_size > 0) { if (Token::Match(tok, "%var% (") && total_size > 0) {
// No varid => function calls are not handled // No varid => function calls are not handled
if (varid == 0) if (declarationId == 0)
continue; continue;
const ArrayInfo arrayInfo1(varid, varnames, total_size / size, size); const ArrayInfo arrayInfo1(declarationId, varnames, total_size / size, size);
const std::list<const Token *> callstack; const std::list<const Token *> callstack;
checkFunctionCall(tok, arrayInfo1, callstack); checkFunctionCall(tok, arrayInfo1, callstack);
} }
// undefined behaviour: result of pointer arithmetic is out of bounds // 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)); const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
if (index > size && _settings->isEnabled("portability")) if (index > size && _settings->isEnabled("portability"))
pointerOutOfBoundsError(tok->next(), "buffer"); 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; 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); outOfBoundsError(tok->tokAt(2), tok->strAt(2), false, 0, 0);
} }
} }
@ -1131,7 +1131,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
continue; continue;
} }
else if (Token::Match(tok, "%varid% [ %num% ]", arrayInfo.varid())) { else if (Token::Match(tok, "%varid% [ %num% ]", arrayInfo.declarationId())) {
std::vector<MathLib::bigint> indexes; std::vector<MathLib::bigint> indexes;
for (const Token *tok2 = tok->next(); Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) { for (const Token *tok2 = tok->next(); Token::Match(tok2, "[ %num% ]"); tok2 = tok2->tokAt(3)) {
const MathLib::bigint index = MathLib::toLongNumber(tok2->strAt(1)); 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); 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)); 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 (Token::getStrLength(tok->tokAt(4)) >= (unsigned int)total_size && (unsigned int)total_size == num) {
if (_settings->inconclusive) 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(); const Token* param3 = tok->linkAt(1)->previous();
// check for strncpy which is not terminated // 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.. // 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)); const MathLib::bigint n = MathLib::toLongNumber(param3->str()) + MathLib::toLongNumber(param3->linkAt(4)->strAt(-1));
if (n > total_size) if (n > total_size)
strncatUsageError(param3->tokAt(3)); strncatUsageError(param3->tokAt(3));
@ -1261,7 +1261,7 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
} }
// Writing data into array.. // 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)); const std::size_t len = Token::getStrLength(tok->tokAt(4));
if (total_size > 0 && len >= (unsigned int)total_size) { if (total_size > 0 && len >= (unsigned int)total_size) {
bufferOverrunError(tok, arrayInfo.varname()); bufferOverrunError(tok, arrayInfo.varname());
@ -1270,11 +1270,11 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
} }
// Detect few strcat() calls // 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; std::size_t charactersAppend = 0;
const Token *tok2 = tok; 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)); charactersAppend += Token::getStrLength(tok2->tokAt(4));
if (charactersAppend >= (unsigned int)total_size) { if (charactersAppend >= (unsigned int)total_size) {
bufferOverrunError(tok2, arrayInfo.varname()); 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); checkSprintfCall(tok, total_size);
} }
// snprintf.. // 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)); const MathLib::bigint n = MathLib::toLongNumber(tok->strAt(4));
if (n > total_size) if (n > total_size)
outOfBoundsError(tok->tokAt(4), "snprintf size", true, 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 // readlink() / readlinkat() buffer usage
if (_settings->standards.posix) { 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); 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); checkReadlinkBufferUsage(tok, scope_begin, total_size, true);
} }
// undefined behaviour: result of pointer arithmetic is out of bounds // 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)); const MathLib::bigint index = MathLib::toLongNumber(tok->strAt(3));
if (index < 0 || index > arrayInfo.num(0)) { if (index < 0 || index > arrayInfo.num(0)) {
pointerOutOfBoundsError(tok->next(), "array"); pointerOutOfBoundsError(tok->next(), "array");
@ -1485,7 +1485,7 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable()
continue; continue;
std::vector<std::string> v; std::vector<std::string> 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); checkScope(tok->tokAt(nextTok), v, temp);
} }
} }
@ -1653,7 +1653,7 @@ void CheckBufferOverrun::checkStructVariable()
// Check variable usage.. // Check variable usage..
ArrayInfo temp = arrayInfo; 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 std::string varnames; // use class and member name for messages
for (unsigned int k = 0; k < varname.size(); ++k) for (unsigned int k = 0; k < varname.size(); ++k)
varnames += (k == 0 ? "" : ".") + varname[k]; varnames += (k == 0 ? "" : ".") + varname[k];
@ -1969,7 +1969,7 @@ void CheckBufferOverrun::negativeIndex()
CheckBufferOverrun::ArrayInfo::ArrayInfo() 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; *this = ai;
} }
CheckBufferOverrun::ArrayInfo::ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcevarid) CheckBufferOverrun::ArrayInfo::ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcedeclid)
: _varname(var->name()), _varid((forcevarid == 0U) ? var->varId() : forcevarid) : _varname(var->name()), _declarationId((forcedeclid == 0U) ? var->declarationId() : forcedeclid)
{ {
for (std::size_t i = 0; i < var->dimensions().size(); i++) for (std::size_t i = 0; i < var->dimensions().size(); i++)
_num.push_back(var->dimension(i)); _num.push_back(var->dimension(i));
@ -1996,7 +1996,7 @@ CheckBufferOverrun::ArrayInfo & CheckBufferOverrun::ArrayInfo::operator=(const C
if (&ai != this) { if (&ai != this) {
_element_size = ai._element_size; _element_size = ai._element_size;
_num = ai._num; _num = ai._num;
_varid = ai._varid; _declarationId = ai._declarationId;
_varname = ai._varname; _varname = ai._varname;
} }
return *this; 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. * 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) 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); _num.push_back(n);
} }
@ -2022,7 +2022,7 @@ CheckBufferOverrun::ArrayInfo CheckBufferOverrun::ArrayInfo::limit(MathLib::bigi
n *= _num[i]; n *= _num[i];
if (uvalue > n) if (uvalue > n)
n = uvalue; n = uvalue;
return ArrayInfo(_varid, _varname, _element_size, n - uvalue); return ArrayInfo(_declarationId, _varname, _element_size, n - uvalue);
} }

View File

@ -125,13 +125,13 @@ public:
/** size of each element in array */ /** size of each element in array */
MathLib::bigint _element_size; MathLib::bigint _element_size;
/** variable id */ /** declaration id */
unsigned int _varid; unsigned int _declarationId;
public: public:
ArrayInfo(); ArrayInfo();
ArrayInfo(const 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); ArrayInfo & operator=(const ArrayInfo &ai);
/** /**
@ -164,11 +164,11 @@ public:
} }
/** Variable name */ /** Variable name */
unsigned int varid() const { unsigned int declarationId() const {
return _varid; return _declarationId;
} }
void varid(unsigned int id) { void declarationId(unsigned int id) {
_varid = id; _declarationId = id;
} }
/** Variable name */ /** Variable name */

View File

@ -517,7 +517,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
if (Token::Match(ftok->next(), "%var% . %var% (")) { if (Token::Match(ftok->next(), "%var% . %var% (")) {
std::list<Variable>::const_iterator var; std::list<Variable>::const_iterator var;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++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 */ /** @todo false negative: we assume function changes variable state */
assignVar(ftok->next()->str(), scope, usage); assignVar(ftok->next()->str(), scope, usage);
break; break;
@ -1334,7 +1334,7 @@ void CheckClass::virtualDestructor()
for (std::size_t k = 0; k < symbolDatabase->getVariableListSize(); k++) { for (std::size_t k = 0; k < symbolDatabase->getVariableListSize(); k++) {
const Variable* var = symbolDatabase->getVariableFromVarId(k); const Variable* var = symbolDatabase->getVariableFromVarId(k);
if (var && var->isPointer() && var->type() == derivedFrom) 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 // pointer variables of type 'Base *' that should not be deleted

View File

@ -107,16 +107,16 @@ void CheckIO::checkFileUsage()
std::size_t varListSize = symbolDatabase->getVariableListSize(); std::size_t varListSize = symbolDatabase->getVariableListSize();
for (std::size_t i = 1; i < varListSize; ++i) { for (std::size_t i = 1; i < varListSize; ++i) {
const Variable* var = symbolDatabase->getVariableFromVarId(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; continue;
if (var->isLocal()) { if (var->isLocal()) {
if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor" 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 else
filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED))); filepointers.insert(std::make_pair(var->declarationId(), Filepointer(CLOSED)));
} else { } 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 // TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode
} }
} }

View File

@ -487,14 +487,14 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int
// Check if pointer is allocated. // Check if pointer is allocated.
int realloc = 0; int realloc = 0;
for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) { 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% )")) { if (Token::Match(tok->tokAt(-3), "free ( * %var% )")) {
realloc = 1; realloc = 1;
allocType = No; allocType = No;
} else if (Token::Match(tok->previous(), "* %var% =")) { } else if (Token::Match(tok->previous(), "* %var% =")) {
allocType = getAllocationType(tok->tokAt(2), arg->varId()); allocType = getAllocationType(tok->tokAt(2), arg->declarationId());
if (allocType == No) { if (allocType == No) {
allocType = getReallocationType(tok->tokAt(2), arg->varId()); allocType = getReallocationType(tok->tokAt(2), arg->declarationId());
} }
if (allocType != No) { if (allocType != No) {
if (realloc) if (realloc)
@ -681,7 +681,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
return "use"; return "use";
if (!function->functionScope) if (!function->functionScope)
return "use"; 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); //simplifycode(func);
const Token *func_ = func; const Token *func_ = func;
while (func_ && func_->str() == ";") while (func_ && func_->str() == ";")
@ -2528,12 +2528,12 @@ void CheckMemoryLeakStructMember::check()
bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable) bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
{ {
const unsigned int varid(variable->varId()); const unsigned int declarationId(variable->declarationId());
bool alloc = false; bool alloc = false;
for (const Token *tok2 = variable->nameToken(); tok2 != variable->scope()->classEnd; tok2 = tok2->next()) { 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; return false;
} else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", varid)) { } else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) {
alloc = true; alloc = true;
} }
} }
@ -2576,14 +2576,14 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
// Unknown usage of struct // Unknown usage of struct
/** @todo Check how the struct is used. Only bail out if necessary */ /** @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; break;
// Struct member is allocated => check if it is also properly deallocated.. // Struct member is allocated => check if it is also properly deallocated..
else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->varId()) else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->declarationId())
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->varId()) || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->declarationId())
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->varId())) { || Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->declarationId())) {
const unsigned int structid(variable->varId()); const unsigned int structid(variable->declarationId());
const unsigned int structmemberid(tok2->tokAt(2)->varId()); const unsigned int structmemberid(tok2->tokAt(2)->varId());
// This struct member is allocated.. check that it is deallocated // This struct member is allocated.. check that it is deallocated

View File

@ -1441,7 +1441,7 @@ private:
// Pointer declaration declaration? // Pointer declaration declaration?
const Variable *var = tok.variable(); const Variable *var = tok.variable();
if (var && var->isPointer() && var->nameToken() == &tok) 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 {")) { if (Token::simpleMatch(&tok, "try {")) {

View File

@ -1744,7 +1744,7 @@ void CheckOther::checkVariableScope()
continue; continue;
const Token* tok = var->nameToken()->next(); 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); tok = tok->tokAt(3);
if (!tok->isNumber() && tok->type() != Token::eString && tok->type() != Token::eChar && !tok->isBoolean()) if (!tok->isNumber() && tok->type() != Token::eString && tok->type() != Token::eChar && !tok->isBoolean())
continue; continue;
@ -1768,7 +1768,7 @@ void CheckOther::checkVariableScope()
} }
tok = tok->link(); tok = tok->link();
} else if (tok->varId() == var->varId() || tok->str() == "goto") { } else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
reduce = false; reduce = false;
break; break;
} }
@ -1817,7 +1817,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
if (tok == forHeadEnd) if (tok == forHeadEnd)
forHeadEnd = 0; 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; loopVariable = false;
unsigned int indent = 0; 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 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--; indent--;
} else if (tok2->str() == ";") } else if (tok2->str() == ";")
break; break;
else if (tok2->varId() == var->varId()) { else if (tok2->varId() == var->declarationId()) {
loopVariable = true; loopVariable = true;
break; 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; 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; 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; return false;
if (tok->varId() == var->varId()) { if (tok->varId() == var->declarationId()) {
used = true; used = true;
if (scope->type == Scope::eSwitch && scope == tok->scope()) if (scope->type == Scope::eSwitch && scope == tok->scope())
return false; // Used in outer switch scope - unsafe or impossible to reduce scope return false; // Used in outer switch scope - unsafe or impossible to reduce scope

View File

@ -103,7 +103,7 @@ void CheckStl::iterators()
validIterator = true; validIterator = true;
// Is iterator compared against different container? // 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)); iteratorsError(tok2, container->name(), tok2->strAt(2));
tok2 = tok2->tokAt(6); tok2 = tok2->tokAt(6);
} }
@ -123,7 +123,7 @@ void CheckStl::iterators()
// If insert/erase is used on different container then // If insert/erase is used on different container then
// report an error // report an error
if (container && tok2->varId() != container->varId()) { if (container && tok2->varId() != container->declarationId()) {
// skip error message if container is a set.. // skip error message if container is a set..
const Variable *variableInfo = tok2->variable(); const Variable *variableInfo = tok2->variable();
const Token *decltok = variableInfo ? variableInfo->typeStartToken() : NULL; const Token *decltok = variableInfo ? variableInfo->typeStartToken() : NULL;
@ -312,13 +312,13 @@ void CheckStl::stlOutOfBounds()
continue; continue;
// variable id for loop variable. // variable id for loop variable.
unsigned int numId = tok->next()->varId(); const unsigned int numId = tok->next()->varId();
// variable id for the container variable // 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()) { 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 ( )")) if (Token::Match(tok3->next(), ". size|length ( )"))
break; break;
else if (Token::Match(tok3->next(), "[ %varid% ]", numId)) else if (Token::Match(tok3->next(), "[ %varid% ]", numId))

View File

@ -69,7 +69,7 @@ private:
/** internal constructor for creating extra checks */ /** internal constructor for creating extra checks */
UninitVar(Check *c, const Variable* v, const SymbolDatabase* db, const Library *lib, bool isc) 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? */ /** is other execution path equal? */
@ -420,7 +420,7 @@ private:
while (parent) { while (parent) {
for (std::list<Variable>::const_iterator j = parent->varlist.begin(); j != parent->varlist.end(); ++j) { for (std::list<Variable>::const_iterator j = parent->varlist.begin(); j != parent->varlist.end(); ++j) {
if (j->name() == var2->name()) { 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; break;
} }
} }
@ -1309,7 +1309,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
const Token *end = tok->next()->link(); const Token *end = tok->next()->link();
// If address of variable is taken in the block then bail out // 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; return true;
// Skip block // Skip block
@ -1370,7 +1370,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
while (tok && tok->str() != ";") { while (tok && tok->str() != ";") {
// variable is seen.. // variable is seen..
if (tok->varId() == var.varId()) { if (tok->varId() == var.declarationId()) {
if (!membervar.empty()) { if (!membervar.empty()) {
if (Token::Match(tok, "%var% . %var% ;|%cop%") && tok->strAt(2) == membervar) if (Token::Match(tok, "%var% . %var% ;|%cop%") && tok->strAt(2) == membervar)
uninitStructMemberError(tok, tok->str() + "." + membervar); uninitStructMemberError(tok, tok->str() + "." + membervar);
@ -1403,7 +1403,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
} }
// variable is seen.. // variable is seen..
if (tok->varId() == var.varId()) { if (tok->varId() == var.declarationId()) {
if (!membervar.empty()) { if (!membervar.empty()) {
if (isMemberVariableAssignment(tok, membervar)) { if (isMemberVariableAssignment(tok, membervar)) {
checkRhs(tok, var, membervar); checkRhs(tok, var, membervar);
@ -1439,7 +1439,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
{ {
const Token * const endpar = startparentheses->link(); const Token * const endpar = startparentheses->link();
for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) { 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 (Token::Match(tok, "%var% . %var%")) {
if (tok->strAt(2) == membervar) { if (tok->strAt(2) == membervar) {
if (isMemberVariableAssignment(tok, membervar)) if (isMemberVariableAssignment(tok, membervar))
@ -1474,7 +1474,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
assert(tok->str() == "{"); assert(tok->str() == "{");
for (const Token * const end = tok->link(); tok != end; tok = tok->next()) { 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 (!membervar.empty()) {
if (isMemberVariableAssignment(tok, membervar)) { if (isMemberVariableAssignment(tok, membervar)) {
bool assign = true; bool assign = true;
@ -1484,7 +1484,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
rhs = true; rhs = true;
if (tok2->str() == ";") if (tok2->str() == ";")
break; 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; assign = false;
break; break;
} }
@ -1504,7 +1504,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
bool assign = true; bool assign = true;
if (tok->strAt(1) == "=") { if (tok->strAt(1) == "=") {
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) { for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId() == var.varId()) { if (tok2->varId() == var.declarationId()) {
assign = false; assign = false;
break; break;
} }
@ -1541,7 +1541,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, const std::
while (NULL != (tok = tok->next())) { while (NULL != (tok = tok->next())) {
if (tok->str() == "=") if (tok->str() == "=")
rhs = true; 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())) if (membervar.empty() && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
uninitvarError(tok, tok->str()); uninitvarError(tok, tok->str());
else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), membervar)) else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), membervar))

View File

@ -57,7 +57,7 @@ public:
/** variable is used.. set both read+write */ /** variable is used.. set both read+write */
void use(std::list<std::set<unsigned int> > & varReadInScope) { void use(std::list<std::set<unsigned int> > & varReadInScope) {
varReadInScope.back().insert(_var->varId()); varReadInScope.back().insert(_var->declarationId());
_read = true; _read = true;
_write = true; _write = true;
} }
@ -161,7 +161,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2, bool replace)
VariableUsage *temp = find(*i); VariableUsage *temp = find(*i);
if (temp) if (temp)
temp->_aliases.erase(var1->_var->varId()); temp->_aliases.erase(var1->_var->declarationId());
} }
// remove all aliases from var1 // remove all aliases from var1
@ -196,7 +196,7 @@ void Variables::clearAliases(unsigned int varid)
VariableUsage *temp = find(*i); VariableUsage *temp = find(*i);
if (temp) if (temp)
temp->_aliases.erase(usage->_var->varId()); temp->_aliases.erase(usage->_var->declarationId());
} }
// remove all aliases from usage // remove all aliases from usage
@ -226,9 +226,9 @@ void Variables::addVar(const Variable *var,
VariableType type, VariableType type,
bool write_) bool write_)
{ {
if (var->varId() > 0) { if (var->declarationId() > 0) {
_varAddedInScope.back().insert(var->varId()); _varAddedInScope.back().insert(var->declarationId());
_varUsage.insert(std::make_pair(var->varId(), VariableUsage(var, type, false, write_, false))); _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. 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. if (i->isArray() && Token::Match(i->nameToken(), "%var% [ %var% ]")) // Array index variable read.
variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken()); variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken());

View File

@ -832,7 +832,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
// add all variables // add all variables
std::list<Variable>::const_iterator var; std::list<Variable>::const_iterator var;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
unsigned int varId = var->varId(); unsigned int varId = var->declarationId();
if (varId) if (varId)
_variableList[varId] = &(*var); _variableList[varId] = &(*var);
} }
@ -843,10 +843,10 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
std::list<Variable>::const_iterator arg; std::list<Variable>::const_iterator arg;
for (arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) { for (arg = func->argumentList.begin(); arg != func->argumentList.end(); ++arg) {
// check for named parameters // check for named parameters
if (arg->nameToken() && arg->varId()) { if (arg->nameToken() && arg->declarationId()) {
unsigned int varId = arg->varId(); const unsigned int declarationId = arg->declarationId();
if (varId) if (declarationId > 0U)
_variableList[varId] = &(*arg); _variableList[declarationId] = &(*arg);
} }
} }
} }
@ -1574,7 +1574,7 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
std::cout << indent << "_name: " << var->nameToken(); std::cout << indent << "_name: " << var->nameToken();
if (var->nameToken()) { if (var->nameToken()) {
std::cout << " " << var->name() << " " << _tokenizer->list.fileLine(var->nameToken()) << std::endl; 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 } else
std::cout << std::endl; std::cout << std::endl;
std::cout << indent << "_start: " << var->typeStartToken() << " " << var->typeStartToken()->str() std::cout << indent << "_start: " << var->typeStartToken() << " " << var->typeStartToken()->str()

View File

@ -195,10 +195,10 @@ public:
} }
/** /**
* Get variable ID. * Get declaration ID (varId used for variable in its declaration).
* @return variable ID * @return declaration ID
*/ */
unsigned int varId() const { unsigned int declarationId() const {
// name may not exist for function arguments // name may not exist for function arguments
if (_name) if (_name)
return _name->varId(); return _name->varId();