Refactor lib/checkclass.cpp

This commit is contained in:
jrp2014 2018-04-05 15:48:23 +02:00 committed by Daniel Marjamäki
parent 4836df9b1d
commit 043b64467b
1 changed files with 15 additions and 31 deletions

View File

@ -134,10 +134,9 @@ void CheckClass::constructors()
} }
std::list<Function>::const_iterator func;
std::vector<Usage> usage(scope->varlist.size()); std::vector<Usage> usage(scope->varlist.size());
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (!func->hasBody() || !(func->isConstructor() || if (!func->hasBody() || !(func->isConstructor() ||
func->type == Function::eOperatorEqual)) func->type == Function::eOperatorEqual))
continue; continue;
@ -389,12 +388,11 @@ void CheckClass::noCopyConstructorError(const Token *tok, const std::string &cla
bool CheckClass::canNotCopy(const Scope *scope) bool CheckClass::canNotCopy(const Scope *scope)
{ {
std::list<Function>::const_iterator func;
bool constructor = false; bool constructor = false;
bool publicAssign = false; bool publicAssign = false;
bool publicCopy = false; bool publicCopy = false;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->isConstructor()) if (func->isConstructor())
constructor = true; constructor = true;
if (func->access != Public) if (func->access != Public)
@ -413,13 +411,12 @@ bool CheckClass::canNotCopy(const Scope *scope)
bool CheckClass::canNotMove(const Scope *scope) bool CheckClass::canNotMove(const Scope *scope)
{ {
std::list<Function>::const_iterator func;
bool constructor = false; bool constructor = false;
bool publicAssign = false; bool publicAssign = false;
bool publicCopy = false; bool publicCopy = false;
bool publicMove = false; bool publicMove = false;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->isConstructor()) if (func->isConstructor())
constructor = true; constructor = true;
if (func->access != Public) if (func->access != Public)
@ -441,10 +438,9 @@ bool CheckClass::canNotMove(const Scope *scope)
void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage) void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage)
{ {
std::list<Variable>::const_iterator var;
unsigned int count = 0; unsigned int count = 0;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { for (std::list<Variable>::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) {
if (var->declarationId() == varid) { if (var->declarationId() == varid) {
usage[count].assign = true; usage[count].assign = true;
return; return;
@ -454,10 +450,9 @@ void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector<U
void CheckClass::initVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage) void CheckClass::initVar(unsigned int varid, const Scope *scope, std::vector<Usage> &usage)
{ {
std::list<Variable>::const_iterator var;
unsigned int count = 0; unsigned int count = 0;
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { for (std::list<Variable>::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) {
if (var->declarationId() == varid) { if (var->declarationId() == varid) {
usage[count].init = true; usage[count].init = true;
return; return;
@ -488,9 +483,8 @@ bool CheckClass::isBaseClassFunc(const Token *tok, const Scope *scope)
// Check if base class exists in database // Check if base class exists in database
if (derivedFrom && derivedFrom->classScope) { if (derivedFrom && derivedFrom->classScope) {
const std::list<Function>& functionList = derivedFrom->classScope->functionList; const std::list<Function>& functionList = derivedFrom->classScope->functionList;
std::list<Function>::const_iterator func;
for (func = functionList.begin(); func != functionList.end(); ++func) { for (std::list<Function>::const_iterator func = functionList.begin(); func != functionList.end(); ++func) {
if (func->tokenDef->str() == tok->str()) if (func->tokenDef->str() == tok->str())
return true; return true;
} }
@ -589,8 +583,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
// Calling member variable function? // Calling member variable function?
if (Token::Match(ftok->next(), "%var% . %name% (")) { if (Token::Match(ftok->next(), "%var% . %name% (")) {
std::list<Variable>::const_iterator var; for (std::list<Variable>::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
if (var->declarationId() == 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()->varId(), scope, usage); assignVar(ftok->next()->varId(), scope, usage);
@ -1223,9 +1216,8 @@ void CheckClass::operatorEq()
const std::size_t classes = symbolDatabase->classAndStructScopes.size(); const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) { for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i]; const Scope * scope = symbolDatabase->classAndStructScopes[i];
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->type == Function::eOperatorEqual && func->access == Public) { if (func->type == Function::eOperatorEqual && func->access == Public) {
// skip "deleted" functions - cannot be called anyway // skip "deleted" functions - cannot be called anyway
if (func->isDelete()) if (func->isDelete())
@ -1278,9 +1270,8 @@ void CheckClass::operatorEqRetRefThis()
const std::size_t classes = symbolDatabase->classAndStructScopes.size(); const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) { for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i]; const Scope * scope = symbolDatabase->classAndStructScopes[i];
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->type == Function::eOperatorEqual && func->hasBody()) { if (func->type == Function::eOperatorEqual && func->hasBody()) {
// make sure return signature is correct // make sure return signature is correct
if (Token::Match(func->retDef, "%type% &") && func->retDef->str() == scope->className) { if (Token::Match(func->retDef, "%type% &") && func->retDef->str() == scope->className) {
@ -1316,10 +1307,8 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
// check if a function is called // check if a function is called
if (tok->strAt(2) == "(" && if (tok->strAt(2) == "(" &&
tok->linkAt(2)->next()->str() == ";") { tok->linkAt(2)->next()->str() == ";") {
std::list<Function>::const_iterator it;
// check if it is a member function // check if it is a member function
for (it = scope->functionList.begin(); it != scope->functionList.end(); ++it) { for (std::list<Function>::const_iterator it = scope->functionList.begin(); it != scope->functionList.end(); ++it) {
// check for a regular function with the same name and a body // check for a regular function with the same name and a body
if (it->type == Function::eFunction && it->hasBody() && if (it->type == Function::eFunction && it->hasBody() &&
it->token->str() == tok->next()->str()) { it->token->str() == tok->next()->str()) {
@ -1419,8 +1408,7 @@ void CheckClass::operatorEqToSelf()
if (scope->definedType->derivedFrom.size() > 1) if (scope->definedType->derivedFrom.size() > 1)
continue; continue;
std::list<Function>::const_iterator func; for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->type == Function::eOperatorEqual && func->hasBody()) { if (func->type == Function::eOperatorEqual && func->hasBody()) {
// make sure that the operator takes an object of the same type as *this, otherwise we can't detect self-assignment checks // make sure that the operator takes an object of the same type as *this, otherwise we can't detect self-assignment checks
if (func->argumentList.empty()) if (func->argumentList.empty())
@ -1548,8 +1536,7 @@ void CheckClass::virtualDestructor()
if (printInconclusive) { if (printInconclusive) {
const Function *destructor = scope->getDestructor(); const Function *destructor = scope->getDestructor();
if (destructor && !destructor->isVirtual()) { if (destructor && !destructor->isVirtual()) {
std::list<Function>::const_iterator func; for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->isVirtual()) { if (func->isVirtual()) {
inconclusiveErrors.push_back(destructor); inconclusiveErrors.push_back(destructor);
break; break;
@ -1719,9 +1706,8 @@ void CheckClass::checkConst()
const std::size_t classes = symbolDatabase->classAndStructScopes.size(); const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) { for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i]; const Scope * scope = symbolDatabase->classAndStructScopes[i];
std::list<Function>::const_iterator func;
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
// does the function have a body? // does the function have a body?
if (func->type != Function::eFunction || !func->hasBody()) if (func->type != Function::eFunction || !func->hasBody())
continue; continue;
@ -1819,8 +1805,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const
} }
} while (again); } while (again);
std::list<Variable>::const_iterator var; for (std::list<Variable>::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) {
if (var->name() == tok->str()) { if (var->name() == tok->str()) {
if (tok->varId() == 0) if (tok->varId() == 0)
symbolDatabase->debugMessage(tok, "CheckClass::isMemberVar found used member variable \'" + tok->str() + "\' with varid 0"); symbolDatabase->debugMessage(tok, "CheckClass::isMemberVar found used member variable \'" + tok->str() + "\' with varid 0");
@ -2105,10 +2090,9 @@ void CheckClass::initializerListOrder()
const std::size_t classes = symbolDatabase->classAndStructScopes.size(); const std::size_t classes = symbolDatabase->classAndStructScopes.size();
for (std::size_t i = 0; i < classes; ++i) { for (std::size_t i = 0; i < classes; ++i) {
const Scope * scope = symbolDatabase->classAndStructScopes[i]; const Scope * scope = symbolDatabase->classAndStructScopes[i];
std::list<Function>::const_iterator func;
// iterate through all member functions looking for constructors // iterate through all member functions looking for constructors
for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->isConstructor() && func->hasBody()) { if (func->isConstructor() && func->hasBody()) {
// check for initializer list // check for initializer list
const Token *tok = func->arg->link()->next(); const Token *tok = func->arg->link()->next();