From ca8e19c96d879bda106ccea9727b94247391487a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Apr 2018 09:38:33 +0200 Subject: [PATCH] SymbolDatabase: Refactor SymbolDatabase: variable list --- lib/checkbufferoverrun.cpp | 3 +-- lib/checkclass.cpp | 3 +-- lib/checkio.cpp | 8 ++----- lib/checkmemoryleak.cpp | 14 +++++------- lib/checkother.cpp | 10 +++------ lib/checkstl.cpp | 18 +++++++-------- lib/checkunusedvar.cpp | 3 +-- lib/checkvaarg.cpp | 3 +-- lib/symboldatabase.h | 4 ++-- lib/tokenize.cpp | 6 ++--- test/testsymboldatabase.cpp | 44 ++++++++++++++++++------------------- 11 files changed, 48 insertions(+), 68 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 60ec1b48d..5e198b1bb 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1106,8 +1106,7 @@ static bool isVLAIndex(const Token *index) void CheckBufferOverrun::negativeArraySize() { - for (unsigned int i = 1; i <= _tokenizer->varIdCount(); i++) { - const Variable * const var = symbolDatabase->getVariableFromVarId(i); + for (const Variable *var : symbolDatabase->variableList()) { if (!var || !var->isArray()) continue; const Token * const nameToken = var->nameToken(); diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 8a1e2a25a..06df1fddf 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1560,8 +1560,7 @@ void CheckClass::virtualDestructor() // pointer variables of type 'Base *' std::set baseClassPointers; - for (std::size_t k = 1; k < symbolDatabase->getVariableListSize(); k++) { - const Variable* var = symbolDatabase->getVariableFromVarId(k); + for (const Variable* var : symbolDatabase->variableList()) { if (var && var->isPointer() && var->type() == derivedFrom) baseClassPointers.insert(var->declarationId()); } diff --git a/lib/checkio.cpp b/lib/checkio.cpp index a47b1bcdb..6839ae79e 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -126,9 +126,7 @@ void CheckIO::checkFileUsage() std::map filepointers; const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase(); - const std::size_t varListSize = symbolDatabase->getVariableListSize(); - for (std::size_t i = 1; i < varListSize; ++i) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || !var->declarationId() || var->isArray() || !Token::simpleMatch(var->typeStartToken(), "FILE *")) continue; @@ -143,9 +141,7 @@ void CheckIO::checkFileUsage() } } - const std::size_t functions = symbolDatabase->functionScopes.size(); - for (std::size_t j = 0; j < functions; ++j) { - const Scope * scope = symbolDatabase->functionScopes[j]; + for (const Scope * scope : symbolDatabase->functionScopes) { unsigned int indent = 0; for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { if (tok->str() == "{") diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 0bb498e65..81c1e7745 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2201,16 +2201,13 @@ static bool isInMemberFunc(const Scope* scope) void CheckMemoryLeakInFunction::check() { // Check locking/unlocking of global resources.. - const std::size_t functions = symbolDatabase->functionScopes.size(); - for (std::size_t i = 0; i < functions; ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; + for (const Scope * scope : symbolDatabase->functionScopes) { if (!scope->hasInlineOrLambdaFunction()) checkScope(scope->bodyStart->next(), emptyString, 0, scope->functionOf != nullptr, 1); } // Check variables.. - for (unsigned int i = 1; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || !var->scope()) continue; @@ -2232,9 +2229,9 @@ void CheckMemoryLeakInFunction::check() sz = 1; if (var->isArgument()) - checkScope(var->scope()->bodyStart->next(), var->name(), i, isInMemberFunc(var->scope()), sz); + checkScope(var->scope()->bodyStart->next(), var->name(), var->declarationId(), isInMemberFunc(var->scope()), sz); else - checkScope(var->nameToken(), var->name(), i, isInMemberFunc(var->scope()), sz); + checkScope(var->nameToken(), var->name(), var->declarationId(), isInMemberFunc(var->scope()), sz); } } //--------------------------------------------------------------------------- @@ -2434,8 +2431,7 @@ void CheckMemoryLeakInClass::publicAllocationError(const Token *tok, const std:: void CheckMemoryLeakStructMember::check() { const SymbolDatabase* symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int i = 1; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || !var->isLocal() || var->isStatic()) continue; if (var->typeEndToken()->isStandardType()) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 7483199d8..6dcf17bb5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1096,8 +1096,7 @@ void CheckOther::checkVariableScope() const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int i = 1; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || !var->isLocal() || (!var->isPointer() && !var->typeStartToken()->isStandardType())) continue; @@ -1416,8 +1415,7 @@ void CheckOther::checkPassByReference() const SymbolDatabase * const symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int i = 1; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || !var->isArgument() || !var->isClass() || var->isPointer() || var->isArray() || var->isReference() || var->isEnumType()) continue; @@ -2234,9 +2232,7 @@ void CheckOther::checkRedundantCopy() const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); - for (std::size_t i = 1; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); - + for (const Variable* var : symbolDatabase->variableList()) { if (!var || var->isReference() || !var->isConst() || var->isPointer() || (!var->type() && !var->isStlType())) // bailout if var is of standard type, if it is a pointer or non-const continue; diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index f9f004d14..085bf3629 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -130,13 +130,13 @@ void CheckStl::iterators() { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int iteratorId = 1; iteratorId < symbolDatabase->getVariableListSize(); iteratorId++) { - const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId); - + for (const Variable* var : symbolDatabase->variableList()) { bool inconclusiveType=false; if (!isIterator(var, inconclusiveType)) continue; + const unsigned int iteratorId = var->declarationId(); + // the validIterator flag says if the iterator has a valid value or not bool validIterator = Token::Match(var->nameToken()->next(), "[(=:{]"); const Scope* invalidationScope = nullptr; @@ -389,8 +389,7 @@ void CheckStl::mismatchingContainers() } } } - for (unsigned int varid = 0; varid < symbolDatabase->getVariableListSize(); varid++) { - const Variable* var = symbolDatabase->getVariableFromVarId(varid); + for (const Variable *var : symbolDatabase->variableList()) { if (var && var->isStlStringType() && Token::Match(var->nameToken(), "%var% (") && Token::Match(var->nameToken()->tokAt(2), pattern2.c_str())) { if (var->nameToken()->strAt(2) != var->nameToken()->strAt(8)) { mismatchingContainersError(var->nameToken()); @@ -605,13 +604,13 @@ void CheckStl::pushback() } // Iterator becomes invalid after reserve, resize, insert, push_back or push_front.. - for (unsigned int iteratorId = 1; iteratorId < symbolDatabase->getVariableListSize(); iteratorId++) { - const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId); - + for (const Variable* var : symbolDatabase->variableList()) { // Check that its an iterator if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) continue; + const unsigned int iteratorId = var->declarationId(); + // ... on std::vector if (!Token::Match(var->typeStartToken(), "std| ::| vector <")) continue; @@ -726,8 +725,7 @@ void CheckStl::invalidPointerError(const Token *tok, const std::string &func, co void CheckStl::stlBoundaries() { const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int iteratorId = 1; iteratorId < symbolDatabase->getVariableListSize(); iteratorId++) { - const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || !var->scope() || !var->scope()->isExecutable()) continue; diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 004a087a1..ad06852d0 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1342,8 +1342,7 @@ void CheckUnusedVar::checkStructMemberUsage() continue; // bail out for extern/global struct - for (size_t i = 0; i < symbolDatabase->getVariableListSize(); i++) { - const Variable* var = symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : symbolDatabase->variableList()) { if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic())) && var->typeEndToken()->str() == scope->className) { bailout = true; break; diff --git a/lib/checkvaarg.cpp b/lib/checkvaarg.cpp index 716f72526..aa0598118 100644 --- a/lib/checkvaarg.cpp +++ b/lib/checkvaarg.cpp @@ -96,8 +96,7 @@ void CheckVaarg::referenceAs_va_start_error(const Token *tok, const std::string& void CheckVaarg::va_list_usage() { const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); - for (unsigned int varid = 1; varid < symbolDatabase->getVariableListSize(); varid++) { - const Variable* var = symbolDatabase->getVariableFromVarId(varid); + for (const Variable* var : symbolDatabase->variableList()) { if (!var || var->isPointer() || var->isReference() || var->isArray() || !var->scope() || var->typeStartToken()->str() != "va_list") continue; if (!var->isLocal() && !var->isArgument()) // Check only local variables and arguments diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 4c6cff683..fde21c53e 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1164,8 +1164,8 @@ public: return _variableList.at(varId); } - std::size_t getVariableListSize() const { - return _variableList.size(); + const std::vector & variableList() const { + return _variableList; } /** diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d604a3bc5..cc201ae21 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1750,8 +1750,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration) createSymbolDatabase(); // Use symbol database to identify rvalue references. Split && to & &. This is safe, since it doesn't delete any tokens (which might be referenced by symbol database) - for (std::size_t i = 0; i < _symbolDatabase->getVariableListSize(); i++) { - const Variable* var = _symbolDatabase->getVariableFromVarId(i); + for (const Variable* var : _symbolDatabase->variableList()) { if (var && var->isRValueReference()) { Token* endTok = const_cast(var->typeEndToken()); endTok->str("&"); @@ -4067,8 +4066,7 @@ void Tokenizer::printDebugOutput(unsigned int simplification) const printUnknownTypes(); // the typeStartToken() should come before typeEndToken() - for (unsigned int varid = 1; varid < _symbolDatabase->getVariableListSize(); varid++) { - const Variable *var = _symbolDatabase->getVariableFromVarId(varid); + for (const Variable *var : _symbolDatabase->variableList()) { if (!var) continue; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 678b5aba9..19005cb53 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -377,7 +377,7 @@ private: ASSERT(db != nullptr); if (!db) return; - ASSERT(db->getVariableListSize() == 2); // the first one is not used + ASSERT(db->variableList().size() == 2); // the first one is not used const Variable * v = db->getVariableFromVarId(1); ASSERT(v != nullptr); if (!v) @@ -857,8 +857,8 @@ private: if (!db) return; ASSERT(db->scopeList.back().type == Scope::eFor); - ASSERT_EQUALS(2, db->getVariableListSize()); - if (db->getVariableListSize() < 2) + ASSERT_EQUALS(2, db->variableList().size()); + if (db->variableList().size() < 2) return; const Variable* e = db->getVariableFromVarId(1); ASSERT(e && e->isReference() && e->isLocal()); @@ -965,7 +965,7 @@ private: "const double Foo::d = 5.0;"); const Variable* v = db->getVariableFromVarId(1); - ASSERT(v && db->getVariableListSize() == 2); + ASSERT(v && db->variableList().size() == 2); ASSERT(v && v->isStatic() && v->isConst() && v->isPrivate()); } @@ -975,7 +975,7 @@ private: const Variable* v = db->getVariableFromVarId(2); // three elements: varId 0 also counts via a fake-entry - ASSERT(v && db->getVariableListSize() == 3); + ASSERT(v && db->variableList().size() == 3); ASSERT_THROW(db->getVariableFromVarId(3), std::out_of_range); } @@ -1304,7 +1304,7 @@ private: ASSERT(db != nullptr); if (db) { - ASSERT_EQUALS(10, db->getVariableListSize() - 1); + ASSERT_EQUALS(10, db->variableList().size() - 1); ASSERT_EQUALS(true, db->getVariableFromVarId(1) && db->getVariableFromVarId(1)->dimensions().size() == 1); ASSERT_EQUALS(true, db->getVariableFromVarId(2) != nullptr); ASSERT_EQUALS(true, db->getVariableFromVarId(3) && db->getVariableFromVarId(3)->dimensions().size() == 0); @@ -1325,7 +1325,7 @@ private: ASSERT(db != nullptr); if (db) { - ASSERT_EQUALS(1, db->getVariableListSize() - 1); + ASSERT_EQUALS(1, db->variableList().size() - 1); ASSERT_EQUALS(true, db->getVariableFromVarId(1) != nullptr); if (db->getVariableFromVarId(1)) ASSERT_EQUALS("pFun", db->getVariableFromVarId(1)->name()); @@ -1660,7 +1660,7 @@ private: "const char m[];\n" "void f(const char* const l;) {}"); - ASSERT(db && db->getVariableListSize() == 6 && db->getVariableFromVarId(1) && db->getVariableFromVarId(2) && db->getVariableFromVarId(3) && db->getVariableFromVarId(4) && db->getVariableFromVarId(5)); + ASSERT(db && db->variableList().size() == 6 && db->getVariableFromVarId(1) && db->getVariableFromVarId(2) && db->getVariableFromVarId(3) && db->getVariableFromVarId(4) && db->getVariableFromVarId(5)); if (db && db->getVariableFromVarId(1) && db->getVariableFromVarId(2) && db->getVariableFromVarId(3) && db->getVariableFromVarId(4) && db->getVariableFromVarId(5)) { ASSERT_EQUALS("std", db->getVariableFromVarId(1)->typeStartToken()->str()); ASSERT_EQUALS("std", db->getVariableFromVarId(2)->typeStartToken()->str()); @@ -1727,7 +1727,7 @@ private: void functionArgs1() { { GET_SYMBOL_DB("void f(std::vector, const std::vector & v) { }"); - ASSERT_EQUALS(1+1, db->getVariableListSize()); + ASSERT_EQUALS(1+1, db->variableList().size()); const Variable* v = db->getVariableFromVarId(1); ASSERT(v && v->isReference() && v->isConst() && v->isArgument()); const Scope* f = db->findScopeByName("f"); @@ -1738,7 +1738,7 @@ private: } { GET_SYMBOL_DB("void g(std::map > m) { }"); - ASSERT_EQUALS(1+1, db->getVariableListSize()); + ASSERT_EQUALS(1+1, db->variableList().size()); const Variable* m = db->getVariableFromVarId(1); ASSERT(m && !m->isReference() && !m->isConst() && m->isArgument() && m->isClass()); const Scope* g = db->findScopeByName("g"); @@ -2193,7 +2193,7 @@ private: "}"; GET_SYMBOL_DB(str); ASSERT_EQUALS("", errout.str()); - ASSERT(db && db->getVariableListSize() == 5); // index 0 + 4 variables + ASSERT(db && db->variableList().size() == 5); // index 0 + 4 variables ASSERT(db && db->scopeList.size() == 7); // global + function + try + 4 catch } @@ -2471,7 +2471,7 @@ private: "}"; GET_SYMBOL_DB(str); ASSERT_EQUALS("", errout.str()); - ASSERT(db && db->getVariableListSize() == 2); // index 0 + 1 variable + ASSERT(db && db->variableList().size() == 2); // index 0 + 1 variable } // ticket #3561 (throw C) @@ -2481,7 +2481,7 @@ private: "}"; GET_SYMBOL_DB_C(str); ASSERT_EQUALS("", errout.str()); - ASSERT(db && db->getVariableListSize() == 2); // index 0 + 1 variable + ASSERT(db && db->variableList().size() == 2); // index 0 + 1 variable } // ticket #3543 (segmentation fault) @@ -2617,8 +2617,8 @@ private: ASSERT(Fred && Fred->classDef && Fred->classScope && Fred->enclosingScope && Fred->name() == "Fred"); ASSERT(Wilma && Wilma->classDef && Wilma->classScope && Wilma->enclosingScope && Wilma->name() == "Wilma"); ASSERT(Barney && Barney->classDef && Barney->classScope && Barney->enclosingScope && Barney->name() == "Barney"); - ASSERT(db->getVariableListSize() == 5); - if (db->getVariableListSize() != 5) + ASSERT(db->variableList().size() == 5); + if (db->variableList().size() != 5) return; ASSERT(db->getVariableFromVarId(1) && db->getVariableFromVarId(1)->type() && db->getVariableFromVarId(1)->type()->name() == "Barney"); ASSERT(db->getVariableFromVarId(2) && db->getVariableFromVarId(2)->type() && db->getVariableFromVarId(2)->type()->name() == "Wilma"); @@ -2673,9 +2673,9 @@ private: " int l ( 1 );\n" "}"); ASSERT(db != nullptr); - ASSERT_EQUALS(4U, db->getVariableListSize() - 1); + ASSERT_EQUALS(4U, db->variableList().size() - 1); ASSERT_EQUALS(2U, db->scopeList.size()); - for (std::size_t i = 1U; i < db->getVariableListSize(); i++) + for (std::size_t i = 1U; i < db->variableList().size(); i++) ASSERT(db->getVariableFromVarId(i) != nullptr); } @@ -2695,8 +2695,8 @@ private: "}"); ASSERT(db != nullptr); - ASSERT_EQUALS(4U, db->getVariableListSize() - 1); - for (std::size_t i = 1U; i < db->getVariableListSize(); i++) + ASSERT_EQUALS(4U, db->variableList().size() - 1); + for (std::size_t i = 1U; i < db->variableList().size(); i++) ASSERT(db->getVariableFromVarId(i) != nullptr); ASSERT_EQUALS(4U, db->scopeList.size()); @@ -2826,7 +2826,7 @@ private: ASSERT(db != nullptr); if (db) { ASSERT_EQUALS(2, db->scopeList.size()); - ASSERT_EQUALS(2, db->getVariableListSize()-1); + ASSERT_EQUALS(2, db->variableList().size()-1); ASSERT(db->getVariableFromVarId(1) != nullptr); ASSERT(db->getVariableFromVarId(2) != nullptr); } @@ -3495,7 +3495,7 @@ private: ASSERT_EQUALS(true, scope->enumeratorList[1].value_known); ASSERT_EQUALS(2, scope->enumeratorList[1].value); - ASSERT(db->getVariableListSize() == 6); // the first one is not used + ASSERT(db->variableList().size() == 6); // the first one is not used const Variable * v = db->getVariableFromVarId(1); ASSERT(v != nullptr); if (!v) @@ -3587,7 +3587,7 @@ private: ASSERT(db != nullptr); if (!db) return; - ASSERT(db->getVariableListSize() == 13); // the first one is not used + ASSERT(db->variableList().size() == 13); // the first one is not used const Variable * v; unsigned int id = 1; TEST(settings1.sizeof_int);