Skip to content

Commit 9c67af0

Browse files
committed
SymbolDatabase: Renamed Variable::varId() to Variable::declarationId() to make it more clear how it works.
1 parent 1a2aaa6 commit 9c67af0

13 files changed

Lines changed: 128 additions & 128 deletions

lib/checkassignif.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void CheckAssignIf::assignIf()
7070
if (num < 0 && bitop == '|')
7171
continue;
7272

73-
assignIfParseScope(tok, tok->tokAt(4), var->varId(), var->isLocal(), bitop, num);
73+
assignIfParseScope(tok, tok->tokAt(4), var->declarationId(), var->isLocal(), bitop, num);
7474
}
7575
}
7676
}

lib/checkbufferoverrun.cpp

Lines changed: 62 additions & 62 deletions
Large diffs are not rendered by default.

lib/checkbufferoverrun.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
125125
/** size of each element in array */
126126
MathLib::bigint _element_size;
127127

128-
/** variable id */
129-
unsigned int _varid;
128+
/** declaration id */
129+
unsigned int _declarationId;
130130

131131
public:
132132
ArrayInfo();
133133
ArrayInfo(const ArrayInfo &);
134-
ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcevarid = 0);
134+
ArrayInfo(const Variable *var, const Tokenizer *tokenizer, const unsigned int forcedeclid = 0);
135135
ArrayInfo & operator=(const ArrayInfo &ai);
136136

137137
/**
@@ -164,11 +164,11 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
164164
}
165165

166166
/** Variable name */
167-
unsigned int varid() const {
168-
return _varid;
167+
unsigned int declarationId() const {
168+
return _declarationId;
169169
}
170-
void varid(unsigned int id) {
171-
_varid = id;
170+
void declarationId(unsigned int id) {
171+
_declarationId = id;
172172
}
173173

174174
/** Variable name */

lib/checkclass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
517517
if (Token::Match(ftok->next(), "%var% . %var% (")) {
518518
std::list<Variable>::const_iterator var;
519519
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
520-
if (var->varId() == ftok->next()->varId()) {
520+
if (var->declarationId() == ftok->next()->varId()) {
521521
/** @todo false negative: we assume function changes variable state */
522522
assignVar(ftok->next()->str(), scope, usage);
523523
break;
@@ -1334,7 +1334,7 @@ void CheckClass::virtualDestructor()
13341334
for (std::size_t k = 0; k < symbolDatabase->getVariableListSize(); k++) {
13351335
const Variable* var = symbolDatabase->getVariableFromVarId(k);
13361336
if (var && var->isPointer() && var->type() == derivedFrom)
1337-
basepointer.insert(var->varId());
1337+
basepointer.insert(var->declarationId());
13381338
}
13391339

13401340
// pointer variables of type 'Base *' that should not be deleted

lib/checkio.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,16 @@ void CheckIO::checkFileUsage()
107107
std::size_t varListSize = symbolDatabase->getVariableListSize();
108108
for (std::size_t i = 1; i < varListSize; ++i) {
109109
const Variable* var = symbolDatabase->getVariableFromVarId(i);
110-
if (!var || !var->varId() || var->isArray() || !Token::simpleMatch(var->typeStartToken(), "FILE *"))
110+
if (!var || !var->declarationId() || var->isArray() || !Token::simpleMatch(var->typeStartToken(), "FILE *"))
111111
continue;
112112

113113
if (var->isLocal()) {
114114
if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor"
115-
filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN)));
115+
filepointers.insert(std::make_pair(var->declarationId(), Filepointer(UNKNOWN)));
116116
else
117-
filepointers.insert(std::make_pair(var->varId(), Filepointer(CLOSED)));
117+
filepointers.insert(std::make_pair(var->declarationId(), Filepointer(CLOSED)));
118118
} else {
119-
filepointers.insert(std::make_pair(var->varId(), Filepointer(UNKNOWN)));
119+
filepointers.insert(std::make_pair(var->declarationId(), Filepointer(UNKNOWN)));
120120
// TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode
121121
}
122122
}

lib/checkmemoryleak.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,14 @@ const char *CheckMemoryLeak::functionArgAlloc(const Function *func, unsigned int
487487
// Check if pointer is allocated.
488488
int realloc = 0;
489489
for (tok = func->functionScope->classStart; tok && tok != func->functionScope->classEnd; tok = tok->next()) {
490-
if (tok->varId() == arg->varId()) {
490+
if (tok->varId() == arg->declarationId()) {
491491
if (Token::Match(tok->tokAt(-3), "free ( * %var% )")) {
492492
realloc = 1;
493493
allocType = No;
494494
} else if (Token::Match(tok->previous(), "* %var% =")) {
495-
allocType = getAllocationType(tok->tokAt(2), arg->varId());
495+
allocType = getAllocationType(tok->tokAt(2), arg->declarationId());
496496
if (allocType == No) {
497-
allocType = getReallocationType(tok->tokAt(2), arg->varId());
497+
allocType = getReallocationType(tok->tokAt(2), arg->declarationId());
498498
}
499499
if (allocType != No) {
500500
if (realloc)
@@ -681,7 +681,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
681681
return "use";
682682
if (!function->functionScope)
683683
return "use";
684-
Token *func = getcode(function->functionScope->classStart->next(), callstack, param->varId(), alloctype, dealloctype, false, sz);
684+
Token *func = getcode(function->functionScope->classStart->next(), callstack, param->declarationId(), alloctype, dealloctype, false, sz);
685685
//simplifycode(func);
686686
const Token *func_ = func;
687687
while (func_ && func_->str() == ";")
@@ -2528,12 +2528,12 @@ void CheckMemoryLeakStructMember::check()
25282528

25292529
bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
25302530
{
2531-
const unsigned int varid(variable->varId());
2531+
const unsigned int declarationId(variable->declarationId());
25322532
bool alloc = false;
25332533
for (const Token *tok2 = variable->nameToken(); tok2 != variable->scope()->classEnd; tok2 = tok2->next()) {
2534-
if (Token::Match(tok2, "= %varid% [;=]", varid)) {
2534+
if (Token::Match(tok2, "= %varid% [;=]", declarationId)) {
25352535
return false;
2536-
} else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", varid)) {
2536+
} else if (Token::Match(tok2, "%varid% = malloc|kmalloc (", declarationId)) {
25372537
alloc = true;
25382538
}
25392539
}
@@ -2576,14 +2576,14 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
25762576

25772577
// Unknown usage of struct
25782578
/** @todo Check how the struct is used. Only bail out if necessary */
2579-
else if (Token::Match(tok2, "[(,] %varid% [,)]", variable->varId()))
2579+
else if (Token::Match(tok2, "[(,] %varid% [,)]", variable->declarationId()))
25802580
break;
25812581

25822582
// Struct member is allocated => check if it is also properly deallocated..
2583-
else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->varId())
2584-
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->varId())
2585-
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->varId())) {
2586-
const unsigned int structid(variable->varId());
2583+
else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", variable->declarationId())
2584+
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = new ", variable->declarationId())
2585+
|| Token::Match(tok2->previous(), "[;{}] %varid% . %var% = fopen (", variable->declarationId())) {
2586+
const unsigned int structid(variable->declarationId());
25872587
const unsigned int structmemberid(tok2->tokAt(2)->varId());
25882588

25892589
// This struct member is allocated.. check that it is deallocated

lib/checknullpointer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ class Nullpointer : public ExecutionPath {
14411441
// Pointer declaration declaration?
14421442
const Variable *var = tok.variable();
14431443
if (var && var->isPointer() && var->nameToken() == &tok)
1444-
checks.push_back(new Nullpointer(owner, var->varId(), var->name(), symbolDatabase, library));
1444+
checks.push_back(new Nullpointer(owner, var->declarationId(), var->name(), symbolDatabase, library));
14451445
}
14461446

14471447
if (Token::simpleMatch(&tok, "try {")) {

lib/checkother.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ void CheckOther::checkVariableScope()
17441744
continue;
17451745

17461746
const Token* tok = var->nameToken()->next();
1747-
if (Token::Match(tok, "; %varid% = %any% ;", var->varId())) {
1747+
if (Token::Match(tok, "; %varid% = %any% ;", var->declarationId())) {
17481748
tok = tok->tokAt(3);
17491749
if (!tok->isNumber() && tok->type() != Token::eString && tok->type() != Token::eChar && !tok->isBoolean())
17501750
continue;
@@ -1768,7 +1768,7 @@ void CheckOther::checkVariableScope()
17681768
}
17691769

17701770
tok = tok->link();
1771-
} else if (tok->varId() == var->varId() || tok->str() == "goto") {
1771+
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
17721772
reduce = false;
17731773
break;
17741774
}
@@ -1817,7 +1817,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
18171817
if (tok == forHeadEnd)
18181818
forHeadEnd = 0;
18191819

1820-
if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope.
1820+
if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->declarationId())) { // Assigned in outer scope.
18211821
loopVariable = false;
18221822
unsigned int indent = 0;
18231823
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
18291829
indent--;
18301830
} else if (tok2->str() == ";")
18311831
break;
1832-
else if (tok2->varId() == var->varId()) {
1832+
else if (tok2->varId() == var->declarationId()) {
18331833
loopVariable = true;
18341834
break;
18351835
}
18361836
}
18371837
}
18381838

1839-
if (loopVariable && Token::Match(tok, "%varid% !!=", var->varId())) // Variable used in loop
1839+
if (loopVariable && Token::Match(tok, "%varid% !!=", var->declarationId())) // Variable used in loop
18401840
return false;
18411841

1842-
if (Token::Match(tok, "& %varid%", var->varId())) // Taking address of variable
1842+
if (Token::Match(tok, "& %varid%", var->declarationId())) // Taking address of variable
18431843
return false;
18441844

1845-
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
1845+
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
18461846
return false;
18471847

1848-
if (tok->varId() == var->varId()) {
1848+
if (tok->varId() == var->declarationId()) {
18491849
used = true;
18501850
if (scope->type == Scope::eSwitch && scope == tok->scope())
18511851
return false; // Used in outer switch scope - unsafe or impossible to reduce scope

lib/checkstl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void CheckStl::iterators()
103103
validIterator = true;
104104

105105
// Is iterator compared against different container?
106-
if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->varId()) {
106+
if (Token::Match(tok2, "%varid% !=|== %var% . end|rend|cend|crend ( )", iteratorId) && container && tok2->tokAt(2)->varId() != container->declarationId()) {
107107
iteratorsError(tok2, container->name(), tok2->strAt(2));
108108
tok2 = tok2->tokAt(6);
109109
}
@@ -123,7 +123,7 @@ void CheckStl::iterators()
123123

124124
// If insert/erase is used on different container then
125125
// report an error
126-
if (container && tok2->varId() != container->varId()) {
126+
if (container && tok2->varId() != container->declarationId()) {
127127
// skip error message if container is a set..
128128
const Variable *variableInfo = tok2->variable();
129129
const Token *decltok = variableInfo ? variableInfo->typeStartToken() : NULL;
@@ -312,13 +312,13 @@ void CheckStl::stlOutOfBounds()
312312
continue;
313313

314314
// variable id for loop variable.
315-
unsigned int numId = tok->next()->varId();
315+
const unsigned int numId = tok->next()->varId();
316316

317317
// variable id for the container variable
318-
unsigned int varId = container->varId();
318+
const unsigned int declarationId = container->declarationId();
319319

320320
for (const Token *tok3 = tok->tokAt(8); tok3 && tok3 != i->classEnd; tok3 = tok3->next()) {
321-
if (tok3->varId() == varId) {
321+
if (tok3->varId() == declarationId) {
322322
if (Token::Match(tok3->next(), ". size|length ( )"))
323323
break;
324324
else if (Token::Match(tok3->next(), "[ %varid% ]", numId))

lib/checkuninitvar.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class UninitVar : public ExecutionPath {
6969

7070
/** internal constructor for creating extra checks */
7171
UninitVar(Check *c, const Variable* v, const SymbolDatabase* db, const Library *lib, bool isc)
72-
: ExecutionPath(c, v->varId()), symbolDatabase(db), library(lib), isC(isc), var(v), alloc(false), strncpy_(false), memset_nonzero(false) {
72+
: ExecutionPath(c, v->declarationId()), symbolDatabase(db), library(lib), isC(isc), var(v), alloc(false), strncpy_(false), memset_nonzero(false) {
7373
}
7474

7575
/** is other execution path equal? */
@@ -420,7 +420,7 @@ class UninitVar : public ExecutionPath {
420420
while (parent) {
421421
for (std::list<Variable>::const_iterator j = parent->varlist.begin(); j != parent->varlist.end(); ++j) {
422422
if (j->name() == var2->name()) {
423-
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
423+
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
424424
break;
425425
}
426426
}
@@ -1309,7 +1309,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
13091309
const Token *end = tok->next()->link();
13101310

13111311
// If address of variable is taken in the block then bail out
1312-
if (Token::findmatch(tok->tokAt(2), "& %varid%", end, var.varId()))
1312+
if (Token::findmatch(tok->tokAt(2), "& %varid%", end, var.declarationId()))
13131313
return true;
13141314

13151315
// Skip block
@@ -1370,7 +1370,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
13701370

13711371
while (tok && tok->str() != ";") {
13721372
// variable is seen..
1373-
if (tok->varId() == var.varId()) {
1373+
if (tok->varId() == var.declarationId()) {
13741374
if (!membervar.empty()) {
13751375
if (Token::Match(tok, "%var% . %var% ;|%cop%") && tok->strAt(2) == membervar)
13761376
uninitStructMemberError(tok, tok->str() + "." + membervar);
@@ -1403,7 +1403,7 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
14031403
}
14041404

14051405
// variable is seen..
1406-
if (tok->varId() == var.varId()) {
1406+
if (tok->varId() == var.declarationId()) {
14071407
if (!membervar.empty()) {
14081408
if (isMemberVariableAssignment(tok, membervar)) {
14091409
checkRhs(tok, var, membervar);
@@ -1439,7 +1439,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
14391439
{
14401440
const Token * const endpar = startparentheses->link();
14411441
for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) {
1442-
if (tok->varId() == var.varId()) {
1442+
if (tok->varId() == var.declarationId()) {
14431443
if (Token::Match(tok, "%var% . %var%")) {
14441444
if (tok->strAt(2) == membervar) {
14451445
if (isMemberVariableAssignment(tok, membervar))
@@ -1474,7 +1474,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
14741474
assert(tok->str() == "{");
14751475

14761476
for (const Token * const end = tok->link(); tok != end; tok = tok->next()) {
1477-
if (tok->varId() == var.varId()) {
1477+
if (tok->varId() == var.declarationId()) {
14781478
if (!membervar.empty()) {
14791479
if (isMemberVariableAssignment(tok, membervar)) {
14801480
bool assign = true;
@@ -1484,7 +1484,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
14841484
rhs = true;
14851485
if (tok2->str() == ";")
14861486
break;
1487-
if (rhs && tok2->varId() == var.varId() && isMemberVariableUsage(tok2, var.isPointer(), membervar)) {
1487+
if (rhs && tok2->varId() == var.declarationId() && isMemberVariableUsage(tok2, var.isPointer(), membervar)) {
14881488
assign = false;
14891489
break;
14901490
}
@@ -1504,7 +1504,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
15041504
bool assign = true;
15051505
if (tok->strAt(1) == "=") {
15061506
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
1507-
if (tok2->varId() == var.varId()) {
1507+
if (tok2->varId() == var.declarationId()) {
15081508
assign = false;
15091509
break;
15101510
}
@@ -1541,7 +1541,7 @@ void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, const std::
15411541
while (NULL != (tok = tok->next())) {
15421542
if (tok->str() == "=")
15431543
rhs = true;
1544-
else if (rhs && tok->varId() == var.varId()) {
1544+
else if (rhs && tok->varId() == var.declarationId()) {
15451545
if (membervar.empty() && isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
15461546
uninitvarError(tok, tok->str());
15471547
else if (!membervar.empty() && isMemberVariableUsage(tok, var.isPointer(), membervar))

0 commit comments

Comments
 (0)