Use temp variables to avoid repeated actions in code.

This commit is contained in:
Dmitry-Me 2014-07-23 18:51:23 +04:00
parent 8609e84737
commit 615eb32fdc
2 changed files with 34 additions and 28 deletions

View File

@ -75,8 +75,8 @@ CheckClass::CheckClass(const Tokenizer *tokenizer, const Settings *settings, Err
void CheckClass::constructors() void CheckClass::constructors()
{ {
bool style = _settings->isEnabled("style"); const bool style = _settings->isEnabled("style");
bool warnings = _settings->isEnabled("warning"); const bool warnings = _settings->isEnabled("warning");
if (!style && !warnings) if (!style && !warnings)
return; return;
@ -419,9 +419,10 @@ 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;
std::list<Function>::const_iterator func; std::list<Function>::const_iterator func;
for (func = derivedFrom->classScope->functionList.begin(); func != derivedFrom->classScope->functionList.end(); ++func) { for (func = functionList.begin(); func != functionList.end(); ++func) {
if (func->tokenDef->str() == tok->str()) if (func->tokenDef->str() == tok->str())
return true; return true;
} }
@ -880,30 +881,31 @@ void CheckClass::privateFunctions()
if (Token::findsimplematch(scope->classStart, "; __property ;", scope->classEnd)) if (Token::findsimplematch(scope->classStart, "; __property ;", scope->classEnd))
continue; continue;
std::list<const Function*> FuncList; std::list<const Function*> privateFunctions;
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
// Get private functions.. // Get private functions..
if (func->type == Function::eFunction && func->access == Private && !func->isOperator) // TODO: There are smarter ways to check private operator usage if (func->type == Function::eFunction && func->access == Private && !func->isOperator) // TODO: There are smarter ways to check private operator usage
FuncList.push_back(&*func); privateFunctions.push_back(&*func);
} }
// Bailout for overridden virtual functions of base classes // Bailout for overridden virtual functions of base classes
if (!scope->definedType->derivedFrom.empty()) { if (!scope->definedType->derivedFrom.empty()) {
// Check virtual functions // Check virtual functions
for (std::list<const Function*>::iterator it = FuncList.begin(); it != FuncList.end();) { for (std::list<const Function*>::iterator it = privateFunctions.begin(); it != privateFunctions.end();) {
if ((*it)->isImplicitlyVirtual(true)) // Give true as default value to be returned if we don't see all base classes if ((*it)->isImplicitlyVirtual(true)) // Give true as default value to be returned if we don't see all base classes
FuncList.erase(it++); privateFunctions.erase(it++);
else else
++it; ++it;
} }
} }
while (!FuncList.empty()) { while (!privateFunctions.empty()) {
const std::string& funcName = FuncList.front()->tokenDef->str(); const std::string& funcName = privateFunctions.front()->tokenDef->str();
// Check that all private functions are used // Check that all private functions are used
bool used = checkFunctionUsage(funcName, &*scope); // Usage in this class bool used = checkFunctionUsage(funcName, &*scope); // Usage in this class
// Check in friend classes // Check in friend classes
for (std::list<Type::FriendInfo>::const_iterator it = scope->definedType->friendList.begin(); !used && it != scope->definedType->friendList.end(); ++it) { const std::list<Type::FriendInfo>& friendList = scope->definedType->friendList;
for (std::list<Type::FriendInfo>::const_iterator it = friendList.begin(); !used && it != friendList.end(); ++it) {
if (it->type) if (it->type)
used = checkFunctionUsage(funcName, it->type->classScope); used = checkFunctionUsage(funcName, it->type->classScope);
else else
@ -911,9 +913,9 @@ void CheckClass::privateFunctions()
} }
if (!used) if (!used)
unusedPrivateFunctionError(FuncList.front()->tokenDef, scope->className, funcName); unusedPrivateFunctionError(privateFunctions.front()->tokenDef, scope->className, funcName);
FuncList.pop_front(); privateFunctions.pop_front();
} }
} }
} }
@ -945,8 +947,7 @@ void CheckClass::checkMemset()
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) { for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "memset|memcpy|memmove ( %any%")) { if (Token::Match(tok, "memset|memcpy|memmove ( %any%")) {
const Token* arg1 = tok->tokAt(2); const Token* arg1 = tok->tokAt(2);
const Token* arg3 = arg1; const Token* arg3 = arg1->nextArgument();
arg3 = arg3->nextArgument();
if (arg3) if (arg3)
arg3 = arg3->nextArgument(); arg3 = arg3->nextArgument();
if (!arg3) if (!arg3)
@ -1043,8 +1044,9 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
// recursively check all parent classes // recursively check all parent classes
for (std::size_t i = 0; i < type->definedType->derivedFrom.size(); i++) { for (std::size_t i = 0; i < type->definedType->derivedFrom.size(); i++) {
if (type->definedType->derivedFrom[i].type && type->definedType->derivedFrom[i].type->classScope) const Type* derivedFrom = type->definedType->derivedFrom[i].type;
checkMemsetType(start, tok, type->definedType->derivedFrom[i].type->classScope, allocation, parsedTypes); if (derivedFrom && derivedFrom->classScope)
checkMemsetType(start, tok, derivedFrom->classScope, allocation, parsedTypes);
} }
// Warn if type is a class that contains any virtual functions // Warn if type is a class that contains any virtual functions

View File

@ -1015,8 +1015,9 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
// check each array variable // check each array variable
if (_variableList[i] && _variableList[i]->isArray()) { if (_variableList[i] && _variableList[i]->isArray()) {
// check each array dimension // check each array dimension
for (std::size_t j = 0; j < _variableList[i]->dimensions().size(); j++) { const std::vector<Dimension>& dimensions = _variableList[i]->dimensions();
Dimension &dimension = const_cast<Dimension &>(_variableList[i]->dimensions()[j]); for (std::size_t j = 0; j < dimensions.size(); j++) {
Dimension &dimension = const_cast<Dimension &>(dimensions[j]);
// check for a single token dimension that is a variable // check for a single token dimension that is a variable
if (dimension.num == 0) { if (dimension.num == 0) {
dimension.known = false; dimension.known = false;
@ -1361,12 +1362,14 @@ Function* SymbolDatabase::addGlobalFunction(Scope*& scope, const Token*& tok, co
if (i->tokenDef->str() == tok->str() && Function::argsMatch(scope, i->argDef->next(), argStart->next(), "", 0)) { if (i->tokenDef->str() == tok->str() && Function::argsMatch(scope, i->argDef->next(), argStart->next(), "", 0)) {
function = &*i; function = &*i;
// copy attributes from function prototype to function // copy attributes from function prototype to function
const_cast<Token *>(tok)->isAttributeConstructor(i->tokenDef->isAttributeConstructor()); Token* to = const_cast<Token *>(tok);
const_cast<Token *>(tok)->isAttributeDestructor(i->tokenDef->isAttributeDestructor()); const Token* from = i->tokenDef;
const_cast<Token *>(tok)->isAttributePure(i->tokenDef->isAttributePure()); to->isAttributeConstructor(from->isAttributeConstructor());
const_cast<Token *>(tok)->isAttributeConst(i->tokenDef->isAttributeConst()); to->isAttributeDestructor(from->isAttributeDestructor());
const_cast<Token *>(tok)->isAttributeNothrow(i->tokenDef->isAttributeNothrow()); to->isAttributePure(from->isAttributePure());
const_cast<Token *>(tok)->isDeclspecNothrow(i->tokenDef->isDeclspecNothrow()); to->isAttributeConst(from->isAttributeConst());
to->isAttributeNothrow(from->isAttributeNothrow());
to->isDeclspecNothrow(from->isDeclspecNothrow());
break; break;
} }
} }
@ -2293,9 +2296,10 @@ bool Function::isImplicitlyVirtual_rec(const ::Type* baseType, bool& safe) const
{ {
// check each base class // check each base class
for (std::size_t i = 0; i < baseType->derivedFrom.size(); ++i) { for (std::size_t i = 0; i < baseType->derivedFrom.size(); ++i) {
const ::Type* derivedFromType = baseType->derivedFrom[i].type;
// check if base class exists in database // check if base class exists in database
if (baseType->derivedFrom[i].type && baseType->derivedFrom[i].type->classScope) { if (derivedFromType && derivedFromType->classScope) {
const Scope *parent = baseType->derivedFrom[i].type->classScope; const Scope *parent = derivedFromType->classScope;
std::list<Function>::const_iterator func; std::list<Function>::const_iterator func;
@ -2324,10 +2328,10 @@ bool Function::isImplicitlyVirtual_rec(const ::Type* baseType, bool& safe) const
} }
} }
if (!baseType->derivedFrom[i].type->derivedFrom.empty() && !baseType->derivedFrom[i].type->hasCircularDependencies()) { if (!derivedFromType->derivedFrom.empty() && !derivedFromType->hasCircularDependencies()) {
// avoid endless recursion, see #5289 Crash: Stack overflow in isImplicitlyVirtual_rec when checking SVN and // avoid endless recursion, see #5289 Crash: Stack overflow in isImplicitlyVirtual_rec when checking SVN and
// #5590 with a loop within the class hierarchie. // #5590 with a loop within the class hierarchie.
if (isImplicitlyVirtual_rec(baseType->derivedFrom[i].type, safe)) { if (isImplicitlyVirtual_rec(derivedFromType, safe)) {
return true; return true;
} }
} }