Cppcheck
|
Various small checks. More...
#include <checkunusedvar.h>
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 | |
Check & | operator= (const Check &)=delete |
const std::string & | name () const |
class name, used to generate documentation More... | |
virtual FileInfo * | getFileInfo (const Tokenizer &, const Settings &) const |
virtual FileInfo * | loadFileInfoFromXml (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 {} |
Various small checks.
Definition at line 47 of file checkunusedvar.h.
|
inline |
This constructor is used when registering the CheckClass.
Definition at line 52 of file checkunusedvar.h.
|
inlineprivate |
This constructor is used when running checks.
Definition at line 56 of file checkunusedvar.h.
|
private |
Definition at line 1416 of file checkunusedvar.cpp.
References CWE563, SimpleEnableGroup< T >::isEnabled(), Settings::isPremiumEnabled(), Check::mSettings, normal, Check::reportError(), Settings::severity, and style.
Referenced by checkFunctionVariableUsage(), and getErrorMessages().
|
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().
|
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().
|
private |
Check that all struct members are used
Definition at line 1446 of file checkunusedvar.cpp.
References astIsContainer(), bailout, Scope::bodyEnd, Scope::bodyStart, Scope::classDef, Scope::className, Scope::eClass, Scope::eStruct, Scope::eUnion, Token::fileIndex(), Token::findmatch(), Token::findsimplematch(), Tokenizer::getSymbolDatabase(), Token::isAttributePacked(), SimpleEnableGroup< T >::isEnabled(), Token::isExpandedMacro(), Tokenizer::isPacked(), Variable::isPointer(), Settings::isPremiumEnabled(), Variable::isPrivate(), isRecordTypeWithoutSideEffects(), Token::isStandardType(), Check::logChecker(), Token::Match(), Check::mSettings, Check::mTokenizer, Variable::name(), Variable::nameToken(), Token::next(), SymbolDatabase::scopeList, Settings::severity, style, Tokenizer::tokens(), Variable::type(), Scope::type, Variable::typeStartToken(), unusedStructMemberError(), SymbolDatabase::variableList(), and Scope::varlist.
Referenced by runChecks().
|
inlineoverrideprivatevirtual |
get information about this class, used to generate documentation
Implements Check.
Definition at line 101 of file checkunusedvar.h.
|
inlineoverrideprivatevirtual |
get error messages
Implements Check.
Definition at line 88 of file checkunusedvar.h.
References allocatedButUnusedVariableError(), unassignedVariableError(), unreadVariableError(), unusedStructMemberError(), and unusedVariableError().
|
private |
Definition at line 1649 of file checkunusedvar.cpp.
References Type::classScope, Type::derivedFrom, mIsEmptyTypeMap, Scope::numConstructors, and Scope::varlist.
Referenced by checkFunctionVariableUsage().
|
private |
Definition at line 1670 of file checkunusedvar.cpp.
References Scope::bodyEnd, Scope::bodyStart, Variable::dimensions(), Function::functionScope, Function::hasBody(), Variable::isArray(), Variable::isGlobal(), Token::isLiteral(), Variable::isPointer(), isVariableChanged(), isVariableWithoutSideEffects(), Check::mSettings, Token::next(), Token::simpleMatch(), Token::tokAt(), and Token::variable().
Referenced by isRecordTypeWithoutSideEffects().
|
private |
Definition at line 1556 of file checkunusedvar.cpp.
References Function::argDef, Scope::bodyStart, Type::classScope, Type::derivedFrom, Scope::functionList, Function::functionScope, Function::isConstructor(), Function::isDestructor(), isFunctionWithoutSideEffects(), isVariableWithoutSideEffects(), Token::link(), Token::linkAt(), Token::Match(), mIsRecordTypeWithoutSideEffectsMap, Token::next(), Token::simpleMatch(), Token::tokAt(), Token::variable(), and Scope::varlist.
Referenced by checkFunctionVariableUsage(), checkFunctionVariableUsage_iterateScopes(), checkStructMemberUsage(), and isVariableWithoutSideEffects().
|
private |
Definition at line 1632 of file checkunusedvar.cpp.
References isRecordTypeWithoutSideEffects(), Variable::type(), ValueType::type, Variable::typeStartToken(), Variable::valueType(), and WRONG_DATA.
Referenced by isFunctionWithoutSideEffects(), and isRecordTypeWithoutSideEffects().
|
inlinestaticprivate |
Definition at line 97 of file checkunusedvar.h.
|
inlineoverrideprivatevirtual |
Run checks against the normal token list.
Implements Check.
Definition at line 60 of file checkunusedvar.h.
References checkFunctionVariableUsage(), checkStructMemberUsage(), and Tokenizer::getSettings().
|
private |
Definition at line 1435 of file checkunusedvar.cpp.
References CWE665, SimpleEnableGroup< T >::isEnabled(), Settings::isPremiumEnabled(), Check::mSettings, normal, Check::reportError(), Settings::severity, and style.
Referenced by checkFunctionVariableUsage(), and getErrorMessages().
|
private |
Definition at line 1424 of file checkunusedvar.cpp.
References CWE563, SimpleEnableGroup< T >::isEnabled(), Settings::isPremiumEnabled(), Check::mSettings, normal, Check::reportError(), Settings::severity, and style.
Referenced by checkFunctionVariableUsage(), and getErrorMessages().
|
private |
Definition at line 1551 of file checkunusedvar.cpp.
References CWE563, normal, Check::reportError(), and style.
Referenced by checkStructMemberUsage(), and getErrorMessages().
|
private |
Definition at line 1408 of file checkunusedvar.cpp.
References CWE563, SimpleEnableGroup< T >::isEnabled(), Settings::isPremiumEnabled(), Check::mSettings, normal, Check::reportError(), Settings::severity, and style.
Referenced by checkFunctionVariableUsage(), and getErrorMessages().
|
friend |
Definition at line 48 of file checkunusedvar.h.
|
private |
Definition at line 114 of file checkunusedvar.h.
Referenced by isEmptyType().
|
private |
Definition at line 112 of file checkunusedvar.h.
Referenced by isRecordTypeWithoutSideEffects().