Merge pull request #374 from Dmitry-Me/useTempVariablesToAvoidRepeatedActions

Use temp variables to avoid repeated actions in code.
This commit is contained in:
Daniel Marjamäki 2014-07-23 18:30:59 +02:00
commit 1cb37e9870
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()
{
bool style = _settings->isEnabled("style");
bool warnings = _settings->isEnabled("warning");
const bool style = _settings->isEnabled("style");
const bool warnings = _settings->isEnabled("warning");
if (!style && !warnings)
return;
@ -419,9 +419,10 @@ bool CheckClass::isBaseClassFunc(const Token *tok, const Scope *scope)
// Check if base class exists in database
if (derivedFrom && derivedFrom->classScope) {
const std::list<Function>& functionList = derivedFrom->classScope->functionList;
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())
return true;
}
@ -880,30 +881,31 @@ void CheckClass::privateFunctions()
if (Token::findsimplematch(scope->classStart, "; __property ;", scope->classEnd))
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) {
// Get private functions..
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
if (!scope->definedType->derivedFrom.empty()) {
// 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
FuncList.erase(it++);
privateFunctions.erase(it++);
else
++it;
}
}
while (!FuncList.empty()) {
const std::string& funcName = FuncList.front()->tokenDef->str();
while (!privateFunctions.empty()) {
const std::string& funcName = privateFunctions.front()->tokenDef->str();
// Check that all private functions are used
bool used = checkFunctionUsage(funcName, &*scope); // Usage in this class
// 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)
used = checkFunctionUsage(funcName, it->type->classScope);
else
@ -911,9 +913,9 @@ void CheckClass::privateFunctions()
}
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()) {
if (Token::Match(tok, "memset|memcpy|memmove ( %any%")) {
const Token* arg1 = tok->tokAt(2);
const Token* arg3 = arg1;
arg3 = arg3->nextArgument();
const Token* arg3 = arg1->nextArgument();
if (arg3)
arg3 = arg3->nextArgument();
if (!arg3)
@ -1043,8 +1044,9 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
// recursively check all parent classes
for (std::size_t i = 0; i < type->definedType->derivedFrom.size(); i++) {
if (type->definedType->derivedFrom[i].type && type->definedType->derivedFrom[i].type->classScope)
checkMemsetType(start, tok, type->definedType->derivedFrom[i].type->classScope, allocation, parsedTypes);
const Type* derivedFrom = type->definedType->derivedFrom[i].type;
if (derivedFrom && derivedFrom->classScope)
checkMemsetType(start, tok, derivedFrom->classScope, allocation, parsedTypes);
}
// 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
if (_variableList[i] && _variableList[i]->isArray()) {
// check each array dimension
for (std::size_t j = 0; j < _variableList[i]->dimensions().size(); j++) {
Dimension &dimension = const_cast<Dimension &>(_variableList[i]->dimensions()[j]);
const std::vector<Dimension>& dimensions = _variableList[i]->dimensions();
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
if (dimension.num == 0) {
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)) {
function = &*i;
// copy attributes from function prototype to function
const_cast<Token *>(tok)->isAttributeConstructor(i->tokenDef->isAttributeConstructor());
const_cast<Token *>(tok)->isAttributeDestructor(i->tokenDef->isAttributeDestructor());
const_cast<Token *>(tok)->isAttributePure(i->tokenDef->isAttributePure());
const_cast<Token *>(tok)->isAttributeConst(i->tokenDef->isAttributeConst());
const_cast<Token *>(tok)->isAttributeNothrow(i->tokenDef->isAttributeNothrow());
const_cast<Token *>(tok)->isDeclspecNothrow(i->tokenDef->isDeclspecNothrow());
Token* to = const_cast<Token *>(tok);
const Token* from = i->tokenDef;
to->isAttributeConstructor(from->isAttributeConstructor());
to->isAttributeDestructor(from->isAttributeDestructor());
to->isAttributePure(from->isAttributePure());
to->isAttributeConst(from->isAttributeConst());
to->isAttributeNothrow(from->isAttributeNothrow());
to->isDeclspecNothrow(from->isDeclspecNothrow());
break;
}
}
@ -2293,9 +2296,10 @@ bool Function::isImplicitlyVirtual_rec(const ::Type* baseType, bool& safe) const
{
// check each base class
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
if (baseType->derivedFrom[i].type && baseType->derivedFrom[i].type->classScope) {
const Scope *parent = baseType->derivedFrom[i].type->classScope;
if (derivedFromType && derivedFromType->classScope) {
const Scope *parent = derivedFromType->classScope;
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
// #5590 with a loop within the class hierarchie.
if (isImplicitlyVirtual_rec(baseType->derivedFrom[i].type, safe)) {
if (isImplicitlyVirtual_rec(derivedFromType, safe)) {
return true;
}
}