Cppcheck
Classes | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
CheckClass Class Reference

Check classes. More...

#include <checkclass.h>

Inheritance diagram for CheckClass:
Check

List of all members.

Classes

struct  Usage
 Information about a member variable. More...

Public Member Functions

 CheckClass ()
 This constructor is used when registering the CheckClass.
 CheckClass (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks.
void runChecks (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 Run checks on the normal token list.
void runSimplifiedChecks (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 Run checks on the simplified token list.
void constructors ()
 Check that all class constructors are ok
void privateFunctions ()
 Check that all private functions are called
void noMemset ()
 Check that the memsets are valid.
void checkMemsetType (const Scope *start, const Token *tok, const Scope *type, bool allocation)
void operatorEq ()
 'operator=' should return something and it should not be const.
void operatorEqRetRefThis ()
 'operator=' should return reference to *this
void operatorEqToSelf ()
 'operator=' should check for assignment to self
void virtualDestructor ()
 The destructor in a base class should be virtual.
void thisSubtraction ()
 warn for "this-x".
void checkConst ()
 can member function be const?
void initializerListOrder ()
 Check initializer list order.
void initializationListUsage ()
void copyconstructors ()

Private Member Functions

void noConstructorError (const Token *tok, const std::string &classname, bool isStruct)
void copyConstructorShallowCopyError (const Token *tok, const std::string &varname)
void noCopyConstructorError (const Token *tok, const std::string &classname, bool isStruct)
void uninitVarError (const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
void operatorEqVarError (const Token *tok, const std::string &classname, const std::string &varname, bool inconclusive)
void unusedPrivateFunctionError (const Token *tok, const std::string &classname, const std::string &funcname)
void memsetError (const Token *tok, const std::string &memfunc, const std::string &classname, const std::string &type)
void mallocOnClassError (const Token *tok, const std::string &memfunc, const Token *classTok, const std::string &classname)
void mallocOnClassWarning (const Token *tok, const std::string &memfunc, const Token *classTok)
void operatorEqReturnError (const Token *tok, const std::string &className)
void virtualDestructorError (const Token *tok, const std::string &Base, const std::string &Derived)
void thisSubtractionError (const Token *tok)
void operatorEqRetRefThisError (const Token *tok)
void operatorEqToSelfError (const Token *tok)
void checkConstError (const Token *tok, const std::string &classname, const std::string &funcname, bool suggestStatic)
void checkConstError2 (const Token *tok1, const Token *tok2, const std::string &classname, const std::string &funcname, bool suggestStatic)
void initializerListError (const Token *tok1, const Token *tok2, const std::string &classname, const std::string &varname)
void suggestInitializationList (const Token *tok, const std::string &varname)
void getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const
 get error messages
std::string classInfo () const
 get information about this class, used to generate documentation
void checkReturnPtrThis (const Scope *scope, const Function *func, const Token *tok, const Token *last)
bool hasAllocation (const Function *func, const Scope *scope)
bool isMemberVar (const Scope *scope, const Token *tok)
bool isMemberFunc (const Scope *scope, const Token *tok)
bool isConstMemberFunc (const Scope *scope, const Token *tok)
bool checkConstFunc (const Scope *scope, const Function *func, bool &memberAccessed)
void initializeVarList (const Function &func, std::list< const Function * > &callstack, const Scope *scope, std::vector< Usage > &usage)
 parse a scope for a constructor or member function and set the "init" flags in the provided varlist

Static Private Member Functions

static std::string myName ()
static bool hasAssignSelf (const Function *func, const Token *rhs)
static bool isBaseClassFunc (const Token *tok, const Scope *scope)
static void assignVar (const std::string &varname, const Scope *scope, std::vector< Usage > &usage)
 assign a variable in the varlist
static void initVar (const std::string &varname, const Scope *scope, std::vector< Usage > &usage)
 initialize a variable in the varlist
static void assignAllVar (std::vector< Usage > &usage)
 set all variables in list assigned
static void clearAllVar (std::vector< Usage > &usage)
 set all variables in list not assigned and not initialized
static bool canNotCopy (const Scope *scope)

Private Attributes

const SymbolDatabasesymbolDatabase

Detailed Description

Check classes.

Uninitialized member variables, non-conforming operators, missing virtual destructor, etc

Definition at line 36 of file checkclass.h.


Constructor & Destructor Documentation

CheckClass::CheckClass ( ) [inline]

This constructor is used when registering the CheckClass.

Definition at line 39 of file checkclass.h.

CheckClass::CheckClass ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
)

This constructor is used when running checks.

Definition at line 40 of file checkclass.cpp.


Member Function Documentation

void CheckClass::assignAllVar ( std::vector< Usage > &  usage) [static, private]

set all variables in list assigned

Parameters:
usagereference to usage vector

Definition at line 339 of file checkclass.cpp.

Referenced by initializeVarList().

void CheckClass::assignVar ( const std::string &  varname,
const Scope scope,
std::vector< Usage > &  usage 
) [static, private]

assign a variable in the varlist

Parameters:
varnamename of variable to mark assigned
scopepointer to variable Scope
usagereference to usage vector

Definition at line 313 of file checkclass.cpp.

References Scope::varlist.

Referenced by initializeVarList().

bool CheckClass::canNotCopy ( const Scope scope) [static, private]
void CheckClass::checkConstError ( const Token tok,
const std::string &  classname,
const std::string &  funcname,
bool  suggestStatic 
) [private]

Definition at line 1734 of file checkclass.cpp.

References checkConstError2().

Referenced by checkConst(), and getErrorMessages().

void CheckClass::checkConstError2 ( const Token tok1,
const Token tok2,
const std::string &  classname,
const std::string &  funcname,
bool  suggestStatic 
) [private]

Definition at line 1739 of file checkclass.cpp.

References Severity::performance, Check::reportError(), and Severity::style.

Referenced by checkConst(), and checkConstError().

bool CheckClass::checkConstFunc ( const Scope scope,
const Function func,
bool &  memberAccessed 
) [private]
void CheckClass::checkMemsetType ( const Scope start,
const Token tok,
const Scope type,
bool  allocation 
)
void CheckClass::checkReturnPtrThis ( const Scope scope,
const Function func,
const Token tok,
const Token last 
) [private]
std::string CheckClass::classInfo ( ) const [inline, private, virtual]

get information about this class, used to generate documentation

Implements Check.

Definition at line 172 of file checkclass.h.

void CheckClass::clearAllVar ( std::vector< Usage > &  usage) [static, private]

set all variables in list not assigned and not initialized

Parameters:
usagereference to usage vector

Definition at line 345 of file checkclass.cpp.

Referenced by constructors().

void CheckClass::copyConstructorShallowCopyError ( const Token tok,
const std::string &  varname 
) [private]

Definition at line 281 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by copyconstructors(), and getErrorMessages().

void CheckClass::getErrorMessages ( ErrorLogger errorLogger,
const Settings settings 
) const [inline, private, virtual]
bool CheckClass::hasAllocation ( const Function func,
const Scope scope 
) [private]
bool CheckClass::hasAssignSelf ( const Function func,
const Token rhs 
) [static, private]
void CheckClass::initializerListError ( const Token tok1,
const Token tok2,
const std::string &  classname,
const std::string &  varname 
) [private]

Definition at line 1833 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by getErrorMessages(), and initializerListOrder().

void CheckClass::initializeVarList ( const Function func,
std::list< const Function * > &  callstack,
const Scope scope,
std::vector< Usage > &  usage 
) [private]

parse a scope for a constructor or member function and set the "init" flags in the provided varlist

Parameters:
funcreference to the function that should be checked
callstackthe function doesn't look into recursive function calls.
scopepointer to variable Scope
usagereference to usage vector
Todo:
false negative: just bail
Todo:
false negative: we assume function changes variable state
Todo:
false negative: just bail
Todo:
False Negative: we should look at the base class functions to see if they call any derived class virtual functions that change the derived class state

Definition at line 377 of file checkclass.cpp.

References Function::arg, assignAllVar(), assignVar(), Scope::classEnd, Scope::definedType, Function::eConstructor, Function::eCopyConstructor, Function::eOperatorEqual, Scope::findFunction(), Type::friendList, Token::function(), Function::functionScope, Function::hasBody, initVar(), isBaseClassFunc(), Token::link(), Token::linkAt(), Token::Match(), Function::name(), Token::next(), Token::previous(), Token::simpleMatch(), Token::str(), Token::strAt(), Token::tokAt(), Function::type, Token::varId(), and Scope::varlist.

Referenced by constructors().

void CheckClass::initVar ( const std::string &  varname,
const Scope scope,
std::vector< Usage > &  usage 
) [static, private]

initialize a variable in the varlist

Parameters:
varnamename of variable to mark initialized
scopepointer to variable Scope
usagereference to usage vector

Definition at line 326 of file checkclass.cpp.

References Scope::varlist.

Referenced by initializeVarList().

bool CheckClass::isBaseClassFunc ( const Token tok,
const Scope scope 
) [static, private]
bool CheckClass::isConstMemberFunc ( const Scope scope,
const Token tok 
) [private]
Todo:
we need to look at the argument types when there are overloaded functions with the same number of arguments

Definition at line 1585 of file checkclass.cpp.

References Type::classScope, countMinArgs(), countParameters(), Scope::definedType, Type::derivedFrom, Scope::functionList, and Token::str().

Referenced by checkConstFunc().

bool CheckClass::isMemberFunc ( const Scope scope,
const Token tok 
) [private]
Todo:
we need to look at the argument types when there are overloaded functions with the same number of arguments

Definition at line 1555 of file checkclass.cpp.

References Type::classScope, countMinArgs(), countParameters(), Scope::definedType, Type::derivedFrom, Scope::functionList, and Token::str().

Referenced by checkConstFunc(), and initializationListUsage().

bool CheckClass::isMemberVar ( const Scope scope,
const Token tok 
) [private]
void CheckClass::mallocOnClassError ( const Token tok,
const std::string &  memfunc,
const Token classTok,
const std::string &  classname 
) [private]

Definition at line 959 of file checkclass.cpp.

References Severity::error, and Check::reportError().

Referenced by checkMemsetType(), and getErrorMessages().

void CheckClass::mallocOnClassWarning ( const Token tok,
const std::string &  memfunc,
const Token classTok 
) [private]

Definition at line 948 of file checkclass.cpp.

References Check::reportError(), and Severity::warning.

Referenced by getErrorMessages(), and noMemset().

void CheckClass::memsetError ( const Token tok,
const std::string &  memfunc,
const std::string &  classname,
const std::string &  type 
) [private]

Definition at line 970 of file checkclass.cpp.

References Severity::error, and Check::reportError().

Referenced by checkMemsetType(), and getErrorMessages().

static std::string CheckClass::myName ( ) [inline, static, private]

Definition at line 168 of file checkclass.h.

void CheckClass::noConstructorError ( const Token tok,
const std::string &  classname,
bool  isStruct 
) [private]

Definition at line 654 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by constructors(), and getErrorMessages().

void CheckClass::noCopyConstructorError ( const Token tok,
const std::string &  classname,
bool  isStruct 
) [private]

Definition at line 286 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by copyconstructors(), and getErrorMessages().

Check that the memsets are valid.

The 'memset' function can do dangerous things if used wrong. If it is used on STL containers for instance it will clear all its data and then the STL container may leak memory or worse have an invalid state. It can also overwrite the virtual table. Important: The checking doesn't work on simplified tokens list.

Definition at line 835 of file checkclass.cpp.

References Check::_settings, checkMemsetType(), Scope::classEnd, Type::classScope, Scope::classStart, Variable::dimensions(), findFunctionOf(), SymbolDatabase::findVariableType(), SymbolDatabase::functionScopes, Variable::isArray(), Settings::isEnabled(), Variable::isPointer(), mallocOnClassWarning(), Token::Match(), Token::next(), Token::nextArgument(), Token::scope(), Token::simpleMatch(), Token::str(), Token::strAt(), symbolDatabase, Token::tokAt(), Variable::typeScope(), and Token::variable().

Referenced by runChecks().

void CheckClass::operatorEqRetRefThisError ( const Token tok) [private]

Definition at line 1095 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by checkReturnPtrThis(), and getErrorMessages().

void CheckClass::operatorEqReturnError ( const Token tok,
const std::string &  className 
) [private]

Definition at line 1008 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by getErrorMessages(), and operatorEq().

void CheckClass::operatorEqToSelfError ( const Token tok) [private]

Definition at line 1215 of file checkclass.cpp.

References Check::reportError(), and Severity::warning.

Referenced by getErrorMessages(), and operatorEqToSelf().

void CheckClass::operatorEqVarError ( const Token tok,
const std::string &  classname,
const std::string &  varname,
bool  inconclusive 
) [private]

Definition at line 671 of file checkclass.cpp.

References Check::reportError(), and Severity::warning.

Referenced by constructors(), and getErrorMessages().

void CheckClass::runChecks ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
) [inline, virtual]

Run checks on the normal token list.

Reimplemented from Check.

Definition at line 46 of file checkclass.h.

References Tokenizer::isC(), and noMemset().

void CheckClass::runSimplifiedChecks ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
) [inline, virtual]
void CheckClass::suggestInitializationList ( const Token tok,
const std::string &  varname 
) [private]

Definition at line 728 of file checkclass.cpp.

References Severity::performance, and Check::reportError().

Referenced by getErrorMessages(), and initializationListUsage().

warn for "this-x".

The indented code may be "this->x"

Definition at line 1353 of file checkclass.cpp.

References Check::_settings, Check::_tokenizer, Token::findmatch(), Settings::isEnabled(), Token::next(), Token::strAt(), thisSubtractionError(), and Tokenizer::tokens().

Referenced by runSimplifiedChecks().

void CheckClass::thisSubtractionError ( const Token tok) [private]

Definition at line 1371 of file checkclass.cpp.

References Check::reportError(), and Severity::warning.

Referenced by getErrorMessages(), and thisSubtraction().

void CheckClass::uninitVarError ( const Token tok,
const std::string &  classname,
const std::string &  varname,
bool  inconclusive 
) [private]

Definition at line 666 of file checkclass.cpp.

References Check::reportError(), and Severity::warning.

Referenced by constructors(), and getErrorMessages().

void CheckClass::unusedPrivateFunctionError ( const Token tok,
const std::string &  classname,
const std::string &  funcname 
) [private]

Definition at line 816 of file checkclass.cpp.

References Check::reportError(), and Severity::style.

Referenced by getErrorMessages(), and privateFunctions().

void CheckClass::virtualDestructorError ( const Token tok,
const std::string &  Base,
const std::string &  Derived 
) [private]

Definition at line 1340 of file checkclass.cpp.

References Severity::error, and Check::reportError().

Referenced by getErrorMessages(), and virtualDestructor().


Member Data Documentation


The documentation for this class was generated from the following files: