From ca50bc7850a1c4d3942e289538f8e253bf1f8357 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 25 Mar 2011 22:21:40 -0400 Subject: [PATCH] move 2 CheckClass helper functions to follow externally called functions so code matches comments --- lib/checkclass.cpp | 158 ++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 80 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 0805242f5..77a21e8f7 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -689,49 +689,6 @@ void CheckClass::unusedPrivateFunctionError(const Token *tok, const std::string // ClassCheck: Check that memset is not used on classes //--------------------------------------------------------------------------- -void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Scope *type) -{ - // recursively check all parent classes - for (size_t i = 0; i < type->derivedFrom.size(); i++) - { - if (type->derivedFrom[i].scope) - checkMemsetType(start, tok, type->derivedFrom[i].scope); - } - - // Warn if type is a class that contains any virtual functions - std::list::const_iterator func; - - for (func = type->functionList.begin(); func != type->functionList.end(); ++func) - { - if (func->isVirtual) - memsetError(tok, tok->str(), "virtual method", type->classDef->str()); - } - - // Warn if type is a class or struct that contains any std::* variables - std::list::const_iterator var; - - for (var = type->varlist.begin(); var != type->varlist.end(); ++var) - { - // don't warn if variable static or const - if (!var->isStatic() && !var->isConst()) - { - const Token *tok1 = var->typeStartToken(); - - // skip mutable token - if (var->isMutable()) - tok1 = tok1->next(); - - // check for std:: type that is not a pointer or reference - if (Token::simpleMatch(tok1, "std ::") && !Token::Match(var->nameToken()->previous(), "*|&")) - memsetError(tok, tok->str(), "'std::" + tok1->strAt(2) + "'", type->classDef->str()); - - // check for known type that is not a pointer or reference - else if (var->type() && !Token::Match(var->nameToken()->previous(), "*|&")) - checkMemsetType(start, tok, var->type()); - } - } -} - void CheckClass::noMemset() { createSymbolDatabase(); @@ -790,6 +747,49 @@ void CheckClass::noMemset() } } +void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Scope *type) +{ + // recursively check all parent classes + for (size_t i = 0; i < type->derivedFrom.size(); i++) + { + if (type->derivedFrom[i].scope) + checkMemsetType(start, tok, type->derivedFrom[i].scope); + } + + // Warn if type is a class that contains any virtual functions + std::list::const_iterator func; + + for (func = type->functionList.begin(); func != type->functionList.end(); ++func) + { + if (func->isVirtual) + memsetError(tok, tok->str(), "virtual method", type->classDef->str()); + } + + // Warn if type is a class or struct that contains any std::* variables + std::list::const_iterator var; + + for (var = type->varlist.begin(); var != type->varlist.end(); ++var) + { + // don't warn if variable static or const + if (!var->isStatic() && !var->isConst()) + { + const Token *tok1 = var->typeStartToken(); + + // skip mutable token + if (var->isMutable()) + tok1 = tok1->next(); + + // check for std:: type that is not a pointer or reference + if (Token::simpleMatch(tok1, "std ::") && !Token::Match(var->nameToken()->previous(), "*|&")) + memsetError(tok, tok->str(), "'std::" + tok1->strAt(2) + "'", type->classDef->str()); + + // check for known type that is not a pointer or reference + else if (var->type() && !Token::Match(var->nameToken()->previous(), "*|&")) + checkMemsetType(start, tok, var->type()); + } + } +} + void CheckClass::memsetError(const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type) { reportError(tok, Severity::error, "memsetClass", "Using '" + memfunc + "' on " + type + " that contains a " + classname); @@ -833,6 +833,41 @@ void CheckClass::operatorEqReturnError(const Token *tok) // operator= should return a reference to *this //--------------------------------------------------------------------------- +void CheckClass::operatorEqRetRefThis() +{ + if (!_settings->_checkCodingStyle) + return; + + createSymbolDatabase(); + + std::list::const_iterator scope; + + for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) + { + // only check classes and structures + if (scope->isClassOrStruct()) + { + std::list::const_iterator func; + + for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) + { + if (func->type == Function::eOperatorEqual && func->hasBody) + { + // make sure return signature is correct + if (Token::Match(func->tokenDef->tokAt(-3), ";|}|{|public:|protected:|private: %type% &") && + func->tokenDef->strAt(-2) == scope->className) + { + // find the ')' + const Token *tok = func->token->next()->link(); + + checkReturnPtrThis(&(*scope), &(*func), tok->tokAt(2), tok->next()->link()); + } + } + } + } + } +} + void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, const Token *tok, const Token *last) { bool foundReturn = false; @@ -895,41 +930,6 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co operatorEqRetRefThisError(func->token); } -void CheckClass::operatorEqRetRefThis() -{ - if (!_settings->_checkCodingStyle) - return; - - createSymbolDatabase(); - - std::list::const_iterator scope; - - for (scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) - { - // only check classes and structures - if (scope->isClassOrStruct()) - { - std::list::const_iterator func; - - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) - { - if (func->type == Function::eOperatorEqual && func->hasBody) - { - // make sure return signature is correct - if (Token::Match(func->tokenDef->tokAt(-3), ";|}|{|public:|protected:|private: %type% &") && - func->tokenDef->strAt(-2) == scope->className) - { - // find the ')' - const Token *tok = func->token->next()->link(); - - checkReturnPtrThis(&(*scope), &(*func), tok->tokAt(2), tok->next()->link()); - } - } - } - } - } -} - void CheckClass::operatorEqRetRefThisError(const Token *tok) { reportError(tok, Severity::style, "operatorEqRetRefThis", "'operator=' should return reference to self"); @@ -1580,8 +1580,6 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Token *tok) return isconst; } -//--------------------------------------------------------------------------- - // check if this function is defined virtual in the base classes bool CheckClass::isVirtualFunc(const Scope *scope, const Token *functionToken) const {