Cppcheck
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
CheckUnusedVar Class Reference

Various small checks. More...

#include <checkunusedvar.h>

Inheritance diagram for CheckUnusedVar:
Check

Public Member Functions

 CheckUnusedVar ()
 This constructor is used when registering the CheckClass. More...
 
- Public Member Functions inherited from Check
 Check (const std::string &aname)
 This constructor is used when registering the CheckClass. More...
 
virtual ~Check ()
 
 Check (const Check &)=delete
 
Checkoperator= (const Check &)=delete
 
const std::string & name () const
 class name, used to generate documentation More...
 
virtual FileInfogetFileInfo (const Tokenizer &, const Settings &) const
 
virtual FileInfoloadFileInfoFromXml (const tinyxml2::XMLElement *xmlElement) const
 
virtual bool analyseWholeProgram (const CTU::FileInfo *ctu, const std::list< FileInfo * > &fileInfo, const Settings &, ErrorLogger &)
 

Private Member Functions

 CheckUnusedVar (const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void runChecks (const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
 Run checks against the normal token list. More...
 
void checkFunctionVariableUsage_iterateScopes (const Scope *const scope, Variables &variables)
 Check for unused function variables More...
 
void checkFunctionVariableUsage ()
 
void checkStructMemberUsage ()
 Check that all struct members are used More...
 
bool isRecordTypeWithoutSideEffects (const Type *type)
 
bool isVariableWithoutSideEffects (const Variable &var, const Type *type=nullptr)
 
bool isEmptyType (const Type *type)
 
bool isFunctionWithoutSideEffects (const Function &func, const Token *functionUsageToken, std::list< const Function * > checkedFuncs)
 
void unusedStructMemberError (const Token *tok, const std::string &structname, const std::string &varname, const std::string &prefix="struct")
 
void unusedVariableError (const Token *tok, const std::string &varname)
 
void allocatedButUnusedVariableError (const Token *tok, const std::string &varname)
 
void unreadVariableError (const Token *tok, const std::string &varname, bool modified)
 
void unassignedVariableError (const Token *tok, const std::string &varname)
 
void getErrorMessages (ErrorLogger *errorLogger, const Settings *settings) const override
 get error messages More...
 
std::string classInfo () const override
 get information about this class, used to generate documentation More...
 

Static Private Member Functions

static std::string myName ()
 

Private Attributes

std::map< const Type *, bool > mIsRecordTypeWithoutSideEffectsMap
 
std::map< const Type *, bool > mIsEmptyTypeMap
 

Friends

class TestUnusedVar
 

Additional Inherited Members

- Static Public Member Functions inherited from Check
static std::list< Check * > & instances ()
 List of registered check classes. More...
 
static void writeToErrorList (const ErrorMessage &errmsg)
 Write given error to stdout in xml format. More...
 
- Protected Member Functions inherited from Check
 Check (std::string aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
 This constructor is used when running checks. More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const Token *tok, const Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg)
 report an error More...
 
void reportError (const std::list< const Token * > &callstack, Severity severity, const std::string &id, const std::string &msg, const CWE &cwe, Certainty certainty)
 report an error More...
 
void reportError (const ErrorPath &errorPath, Severity severity, const char id[], const std::string &msg, const CWE &cwe, Certainty certainty)
 
void logChecker (const char id[])
 log checker More...
 
ErrorPath getErrorPath (const Token *errtok, const ValueFlow::Value *value, std::string bug) const
 
bool wrongData (const Token *tok, const char *str)
 Use WRONG_DATA in checkers when you check for wrong data. More...
 
- Static Protected Member Functions inherited from Check
static std::string getMessageId (const ValueFlow::Value &value, const char id[])
 
- Protected Attributes inherited from Check
const Tokenizer *const mTokenizer {}
 
const Settings *const mSettings {}
 
ErrorLogger *const mErrorLogger {}
 

Detailed Description

Various small checks.

Definition at line 47 of file checkunusedvar.h.

Constructor & Destructor Documentation

◆ CheckUnusedVar() [1/2]

CheckUnusedVar::CheckUnusedVar ( )
inline

This constructor is used when registering the CheckClass.

Definition at line 52 of file checkunusedvar.h.

◆ CheckUnusedVar() [2/2]

CheckUnusedVar::CheckUnusedVar ( const Tokenizer tokenizer,
const Settings settings,
ErrorLogger errorLogger 
)
inlineprivate

This constructor is used when running checks.

Definition at line 56 of file checkunusedvar.h.

Member Function Documentation

◆ allocatedButUnusedVariableError()

void CheckUnusedVar::allocatedButUnusedVariableError ( const Token tok,
const std::string &  varname 
)
private

◆ checkFunctionVariableUsage()

void CheckUnusedVar::checkFunctionVariableUsage ( )
private

Definition at line 1152 of file checkunusedvar.cpp.

References Variables::VariableUsage::_allocateMemory, Variables::VariableUsage::_lastAccess, Variables::VariableUsage::_modified, Variables::VariableUsage::_read, Variables::VariableUsage::_var, Variables::VariableUsage::_write, allocatedButUnusedVariableError(), Token::astOperand1(), Token::astOperand2(), Token::astParent(), Scope::bodyEnd, Scope::bodyStart, Library::check, Library::checkFiniteLifetime, checkFunctionVariableUsage_iterateScopes(), Settings::checkLibrary, Library::def, Scope::definedType, error, Scope::eUnion, Token::expressionString(), Type::False, findExpressionChanged(), Token::findExpressionStartEndTokens(), findLambdaEndToken(), SymbolDatabase::functionScopes, ValueFlow::getEndOfExprScope(), Tokenizer::getSymbolDatabase(), Library::getTypeCheck(), Variable::getTypeName(), SymbolDatabase::getVariableFromVarId(), Scope::hasInlineOrLambdaFunction(), information, Token::isAssignmentOp(), Token::isAttributeUnused(), Token::isAttributeUsed(), Variable::isClass(), Token::isCpp(), Tokenizer::isCPP(), isEmptyType(), SimpleEnableGroup< T >::isEnabled(), Token::isExpandedMacro(), Variable::isMaybeUnused(), Token::isName(), isNullOperand(), Settings::isPremiumEnabled(), isRaiiClass(), isRecordTypeWithoutSideEffects(), Variable::isReference(), Token::isSplittedVarDeclEq(), Variable::isStatic(), Variable::isStlType(), Type::isUnionType(), Settings::library, Token::link(), Token::linkAt(), Check::logChecker(), Token::Match(), Check::mSettings, Check::mTokenizer, Variables::VariableUsage::mType, Variable::name(), Variable::nameToken(), Type::needInitialization, Token::next(), ValueType::pointer, Variables::pointerArray, Variables::pointerPointer, precedes(), Token::previous(), ValueType::RECORD, Variables::referenceArray, Check::reportError(), Settings::severity, Token::simpleMatch(), ValueType::SMART_POINTER, startsWith(), Token::str(), style, Library::suppress, Token::tokAt(), Token::tokType(), Variable::type(), ValueType::type, ValueType::typeScope, unassignedVariableError(), unreadVariableError(), Variables::VariableUsage::unused(), FwdAnalysis::unusedValue(), unusedVariableError(), Variable::valueType(), Token::valueType(), Token::variable(), and Variables::varUsage().

Referenced by runChecks().

◆ checkFunctionVariableUsage_iterateScopes()

void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes ( const Scope *const  scope,
Variables variables 
)
private

Check for unused function variables

Definition at line 691 of file checkunusedvar.cpp.

References Variables::VariableUsage::_aliases, Variables::VariableUsage::_allocateMemory, Variables::addVar(), Variables::allocateMemory(), Variables::array, astIsLHS(), Token::astOperand1(), Token::astOperand2(), Token::astParent(), Scope::bodyEnd, Scope::bodyStart, Library::check, Scope::classDef, Variables::clear(), Library::detectContainer(), doAssignment(), Scope::eFunction, Token::eIncDecOp, endsWith(), Variables::erase(), Variables::eraseAll(), Type::False, Variables::find(), findAllocFuncCallToken(), Token::function(), Library::getAllocFuncInfo(), Library::getDeallocFuncInfo(), Library::getTypeCheck(), Library::AllocFunc::groupId, Token::isAssignmentOp(), Tokenizer::isC(), Token::isCpp(), Scope::isExecutable(), Token::isExtendedOp(), isLikelyStreamRead(), Library::ismemory(), Token::isName(), isPartOfClassStructUnion(), isRecordTypeWithoutSideEffects(), Variable::isReference(), Token::isStandardType(), Token::isUnaryOp(), isVarDecl(), Settings::library, Token::link(), Token::linkAt(), Token::Match(), Variables::modified(), Check::mSettings, Check::mTokenizer, Variables::VariableUsage::mType, Variable::nameToken(), Scope::nestedList, Token::next(), Variables::none, nonneg, Variables::pointer, ValueType::pointer, Variables::pointerArray, Variables::pointerPointer, Token::previous(), Variables::read(), Variables::readAll(), Variables::reference, Variables::referenceArray, Token::scope(), Token::simpleMatch(), skipBrackets(), skipBracketsAndMembers(), Variables::standard, Token::str(), Token::strAt(), Token::tokAt(), Token::tokType(), Variable::type(), Scope::type, Variable::typeEndToken(), ValueType::UNKNOWN_TYPE, Variables::use(), useFunctionArgs(), Token::valueType(), Token::variable(), Token::varId(), Scope::varlist, Variables::write(), Variables::writeAliases(), and Variables::writeAll().

Referenced by checkFunctionVariableUsage().

◆ checkStructMemberUsage()

void CheckUnusedVar::checkStructMemberUsage ( )
private

◆ classInfo()

std::string CheckUnusedVar::classInfo ( ) const
inlineoverrideprivatevirtual

get information about this class, used to generate documentation

Implements Check.

Definition at line 101 of file checkunusedvar.h.

◆ getErrorMessages()

void CheckUnusedVar::getErrorMessages ( ErrorLogger errorLogger,
const Settings settings 
) const
inlineoverrideprivatevirtual

◆ isEmptyType()

bool CheckUnusedVar::isEmptyType ( const Type type)
private

◆ isFunctionWithoutSideEffects()

bool CheckUnusedVar::isFunctionWithoutSideEffects ( const Function func,
const Token functionUsageToken,
std::list< const Function * >  checkedFuncs 
)
private

◆ isRecordTypeWithoutSideEffects()

bool CheckUnusedVar::isRecordTypeWithoutSideEffects ( const Type type)
private

◆ isVariableWithoutSideEffects()

bool CheckUnusedVar::isVariableWithoutSideEffects ( const Variable var,
const Type type = nullptr 
)
private

◆ myName()

static std::string CheckUnusedVar::myName ( )
inlinestaticprivate

Definition at line 97 of file checkunusedvar.h.

◆ runChecks()

void CheckUnusedVar::runChecks ( const Tokenizer tokenizer,
ErrorLogger errorLogger 
)
inlineoverrideprivatevirtual

Run checks against the normal token list.

Implements Check.

Definition at line 60 of file checkunusedvar.h.

References checkFunctionVariableUsage(), checkStructMemberUsage(), and Tokenizer::getSettings().

◆ unassignedVariableError()

void CheckUnusedVar::unassignedVariableError ( const Token tok,
const std::string &  varname 
)
private

◆ unreadVariableError()

void CheckUnusedVar::unreadVariableError ( const Token tok,
const std::string &  varname,
bool  modified 
)
private

◆ unusedStructMemberError()

void CheckUnusedVar::unusedStructMemberError ( const Token tok,
const std::string &  structname,
const std::string &  varname,
const std::string &  prefix = "struct" 
)
private

Definition at line 1551 of file checkunusedvar.cpp.

References CWE563, normal, Check::reportError(), and style.

Referenced by checkStructMemberUsage(), and getErrorMessages().

◆ unusedVariableError()

void CheckUnusedVar::unusedVariableError ( const Token tok,
const std::string &  varname 
)
private

Friends And Related Function Documentation

◆ TestUnusedVar

friend class TestUnusedVar
friend

Definition at line 48 of file checkunusedvar.h.

Member Data Documentation

◆ mIsEmptyTypeMap

std::map<const Type *,bool> CheckUnusedVar::mIsEmptyTypeMap
private

Definition at line 114 of file checkunusedvar.h.

Referenced by isEmptyType().

◆ mIsRecordTypeWithoutSideEffectsMap

std::map<const Type *,bool> CheckUnusedVar::mIsRecordTypeWithoutSideEffectsMap
private

Definition at line 112 of file checkunusedvar.h.

Referenced by isRecordTypeWithoutSideEffects().


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