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

Checking for uninitialized variables. More...

#include <checkuninitvar.h>

Inheritance diagram for CheckUninitVar:
Check

Public Types

enum  Alloc { NO_ALLOC , NO_CTOR_CALL , CTOR_CALL , ARRAY }
 

Public Member Functions

 CheckUninitVar ()
 This constructor is used when registering the CheckUninitVar. More...
 
const TokenisVariableUsage (const Token *vartok, bool pointer, Alloc alloc, int indirect=0) const
 
- 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...
 

Static Public Member Functions

static const TokenisVariableUsage (const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect=0)
 
- 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...
 

Private Member Functions

 CheckUninitVar (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...
 
bool diag (const Token *tok)
 
void check ()
 Check for uninitialized variables. More...
 
void checkScope (const Scope *scope, const std::set< std::string > &arrayTypeDefs)
 
void checkStruct (const Token *tok, const Variable &structvar)
 
bool checkScopeForVariable (const Token *tok, const Variable &var, bool *const possibleInit, bool *const noreturn, Alloc *const alloc, const std::string &membervar, std::map< nonneg int, VariableValue > &variableValue)
 
const TokencheckExpr (const Token *tok, const Variable &var, const Alloc alloc, bool known, bool *bailout=nullptr) const
 
bool checkIfForWhileHead (const Token *startparentheses, const Variable &var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar)
 
bool checkLoopBody (const Token *tok, const Variable &var, const Alloc alloc, const std::string &membervar, const bool suppressErrors)
 
const TokencheckLoopBodyRecursive (const Token *start, const Variable &var, const Alloc alloc, const std::string &membervar, bool &bailout) const
 recursively check loop, return error token More...
 
void checkRhs (const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar)
 
int isFunctionParUsage (const Token *vartok, bool pointer, Alloc alloc, int indirect=0) const
 
bool isMemberVariableAssignment (const Token *tok, const std::string &membervar) const
 
bool isMemberVariableUsage (const Token *tok, bool isPointer, Alloc alloc, const std::string &membervar) const
 
void valueFlowUninit ()
 ValueFlow-based checking for uninitialized variables. More...
 
Check::FileInfogetFileInfo (const Tokenizer &tokenizer, const Settings &settings) const override
 Parse current TU and extract file info. More...
 
Check::FileInfoloadFileInfoFromXml (const tinyxml2::XMLElement *xmlElement) const override
 
bool analyseWholeProgram (const CTU::FileInfo *ctu, const std::list< Check::FileInfo * > &fileInfo, const Settings &settings, ErrorLogger &errorLogger) override
 Analyse all file infos for all TU. More...
 
void uninitvarError (const Token *tok, const ValueFlow::Value &v)
 
void uninitdataError (const Token *tok, const std::string &varname)
 
void uninitvarError (const Token *tok, const std::string &varname, ErrorPath errorPath)
 
void uninitvarError (const Token *tok, const std::string &varname)
 
void uninitvarError (const Token *tok, const std::string &varname, Alloc alloc)
 
void uninitStructMemberError (const Token *tok, const std::string &membername)
 
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 int isFunctionParUsage (const Token *vartok, const Library &library, bool pointer, Alloc alloc, int indirect=0)
 
static std::string myName ()
 

Private Attributes

std::set< const Token * > mUninitDiags
 

Friends

class TestUninitVar
 

Additional Inherited Members

- 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

Checking for uninitialized variables.

Definition at line 65 of file checkuninitvar.h.

Member Enumeration Documentation

◆ Alloc

Enumerator
NO_ALLOC 
NO_CTOR_CALL 
CTOR_CALL 
ARRAY 

Definition at line 72 of file checkuninitvar.h.

Constructor & Destructor Documentation

◆ CheckUninitVar() [1/2]

CheckUninitVar::CheckUninitVar ( )
inline

This constructor is used when registering the CheckUninitVar.

Definition at line 70 of file checkuninitvar.h.

◆ CheckUninitVar() [2/2]

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

This constructor is used when running checks.

Definition at line 79 of file checkuninitvar.h.

Member Function Documentation

◆ analyseWholeProgram()

bool CheckUninitVar::analyseWholeProgram ( const CTU::FileInfo ctu,
const std::list< Check::FileInfo * > &  fileInfo,
const Settings settings,
ErrorLogger errorLogger 
)
overrideprivatevirtual

◆ check()

void CheckUninitVar::check ( )
private

◆ checkExpr()

const Token * CheckUninitVar::checkExpr ( const Token tok,
const Variable var,
const Alloc  alloc,
bool  known,
bool *  bailout = nullptr 
) const
private

◆ checkIfForWhileHead()

bool CheckUninitVar::checkIfForWhileHead ( const Token startparentheses,
const Variable var,
bool  suppressErrors,
bool  isuninit,
Alloc  alloc,
const std::string &  membervar 
)
private

◆ checkLoopBody()

bool CheckUninitVar::checkLoopBody ( const Token tok,
const Variable var,
const Alloc  alloc,
const std::string &  membervar,
const bool  suppressErrors 
)
private

◆ checkLoopBodyRecursive()

const Token * CheckUninitVar::checkLoopBodyRecursive ( const Token start,
const Variable var,
const Alloc  alloc,
const std::string &  membervar,
bool &  bailout 
) const
private

◆ checkRhs()

void CheckUninitVar::checkRhs ( const Token tok,
const Variable var,
Alloc  alloc,
nonneg int  number_of_if,
const std::string &  membervar 
)
private

◆ checkScope()

void CheckUninitVar::checkScope ( const Scope scope,
const std::set< std::string > &  arrayTypeDefs 
)
private

◆ checkScopeForVariable()

bool CheckUninitVar::checkScopeForVariable ( const Token tok,
const Variable var,
bool *const  possibleInit,
bool *const  noreturn,
Alloc *const  alloc,
const std::string &  membervar,
std::map< nonneg int, VariableValue > &  variableValue 
)
private

◆ checkStruct()

void CheckUninitVar::checkStruct ( const Token tok,
const Variable structvar 
)
private

◆ classInfo()

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

get information about this class, used to generate documentation

Implements Check.

Definition at line 147 of file checkuninitvar.h.

◆ diag()

bool CheckUninitVar::diag ( const Token tok)
private

Definition at line 106 of file checkuninitvar.cpp.

References Token::astParent(), Token::Match(), and mUninitDiags.

Referenced by uninitvarError().

◆ getErrorMessages()

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

get error messages

Implements Check.

Definition at line 132 of file checkuninitvar.h.

References uninitdataError(), uninitStructMemberError(), and uninitvarError().

◆ getFileInfo()

Check::FileInfo * CheckUninitVar::getFileInfo ( const Tokenizer tokenizer,
const Settings settings 
) const
overrideprivatevirtual

Parse current TU and extract file info.

Reimplemented from Check.

Definition at line 1701 of file checkuninitvar.cpp.

References CTU::getUnsafeUsage(), and isVariableUsage().

◆ isFunctionParUsage() [1/2]

int CheckUninitVar::isFunctionParUsage ( const Token vartok,
bool  pointer,
Alloc  alloc,
int  indirect = 0 
) const
private

Definition at line 1419 of file checkuninitvar.cpp.

References isFunctionParUsage(), Settings::library, and Check::mSettings.

◆ isFunctionParUsage() [2/2]

int CheckUninitVar::isFunctionParUsage ( const Token vartok,
const Library library,
bool  pointer,
Alloc  alloc,
int  indirect = 0 
)
staticprivate

◆ isMemberVariableAssignment()

bool CheckUninitVar::isMemberVariableAssignment ( const Token tok,
const std::string &  membervar 
) const
private

◆ isMemberVariableUsage()

bool CheckUninitVar::isMemberVariableUsage ( const Token tok,
bool  isPointer,
Alloc  alloc,
const std::string &  membervar 
) const
private

◆ isVariableUsage() [1/2]

const Token * CheckUninitVar::isVariableUsage ( const Token vartok,
bool  pointer,
Alloc  alloc,
int  indirect = 0 
) const

Definition at line 1340 of file checkuninitvar.cpp.

References isVariableUsage(), Settings::library, and Check::mSettings.

◆ isVariableUsage() [2/2]

const Token * CheckUninitVar::isVariableUsage ( const Token vartok,
const Library library,
bool  pointer,
Alloc  alloc,
int  indirect = 0 
)
static

◆ loadFileInfoFromXml()

Check::FileInfo * CheckUninitVar::loadFileInfoFromXml ( const tinyxml2::XMLElement *  xmlElement) const
overrideprivatevirtual

Reimplemented from Check.

Definition at line 1712 of file checkuninitvar.cpp.

References CTU::loadUnsafeUsageListFromXml().

◆ myName()

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

Definition at line 143 of file checkuninitvar.h.

◆ runChecks()

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

Run checks against the normal token list.

Implements Check.

Definition at line 83 of file checkuninitvar.h.

References check(), Tokenizer::getSettings(), and valueFlowUninit().

◆ uninitdataError()

void CheckUninitVar::uninitdataError ( const Token tok,
const std::string &  varname 
)
private

Definition at line 1535 of file checkuninitvar.cpp.

References CWE_USE_OF_UNINITIALIZED_VARIABLE, error, normal, and Check::reportError().

Referenced by getErrorMessages().

◆ uninitStructMemberError()

void CheckUninitVar::uninitStructMemberError ( const Token tok,
const std::string &  membername 
)
private

◆ uninitvarError() [1/4]

void CheckUninitVar::uninitvarError ( const Token tok,
const std::string &  varname 
)
inlineprivate

Definition at line 119 of file checkuninitvar.h.

◆ uninitvarError() [2/4]

void CheckUninitVar::uninitvarError ( const Token tok,
const std::string &  varname,
Alloc  alloc 
)
inlineprivate

Definition at line 122 of file checkuninitvar.h.

◆ uninitvarError() [3/4]

void CheckUninitVar::uninitvarError ( const Token tok,
const std::string &  varname,
ErrorPath  errorPath 
)
private

◆ uninitvarError() [4/4]

void CheckUninitVar::uninitvarError ( const Token tok,
const ValueFlow::Value v 
)
private

◆ valueFlowUninit()

void CheckUninitVar::valueFlowUninit ( )
private

Friends And Related Function Documentation

◆ TestUninitVar

friend class TestUninitVar
friend

Definition at line 66 of file checkuninitvar.h.

Member Data Documentation

◆ mUninitDiags

std::set<const Token*> CheckUninitVar::mUninitDiags
private

Definition at line 130 of file checkuninitvar.h.

Referenced by diag().


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