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

Check for condition mismatches. More...

#include <checkcondition.h>

Inheritance diagram for CheckCondition:
Check

Public Member Functions

 CheckCondition ()
 This constructor is used when registering the CheckAssignIf. 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

 CheckCondition (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, the token list is not simplified More...
 
void assignIf ()
 mismatching assignment / comparison More...
 
bool assignIfParseScope (const Token *const assignTok, const Token *const startTok, const nonneg int varid, const bool islocal, const char bitop, const MathLib::bigint num)
 parse scopes recursively More...
 
void checkBadBitmaskCheck ()
 check bitmask using | instead of & More...
 
void comparison ()
 mismatching lhs and rhs in comparison More...
 
void duplicateCondition ()
 
void multiCondition ()
 match 'if' and 'else if' conditions More...
 
void multiCondition2 ()
 multiconditions #2 More...
 
void checkIncorrectLogicOperator ()
 Check for testing for mutual exclusion over || More...
 
void checkModuloAlwaysTrueFalse ()
 Check for suspicious usage of modulo (e.g. More...
 
void clarifyCondition ()
 Suspicious condition (assignment+comparison) More...
 
void alwaysTrueFalse ()
 Condition is always true/false. More...
 
void checkInvalidTestForOverflow ()
 Check for invalid test for overflow 'x+100 < x' More...
 
void checkPointerAdditionResultNotNull ()
 Check if pointer addition result is NULL '(ptr + 1) == NULL'. More...
 
void checkDuplicateConditionalAssign ()
 
void checkAssignmentInCondition ()
 Assignment in condition. More...
 
bool diag (const Token *tok, bool insert=true)
 
bool isAliased (const std::set< int > &vars) const
 
bool isOverlappingCond (const Token *const cond1, const Token *const cond2, bool pure) const
 
void assignIfError (const Token *tok1, const Token *tok2, const std::string &condition, bool result)
 
void mismatchingBitAndError (const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2)
 
void badBitmaskCheckError (const Token *tok, bool isNoOp=false)
 
void comparisonError (const Token *tok, const std::string &bitop, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result)
 
void duplicateConditionError (const Token *tok1, const Token *tok2, ErrorPath errorPath)
 
void overlappingElseIfConditionError (const Token *tok, nonneg int line1)
 
void oppositeElseIfConditionError (const Token *ifCond, const Token *elseIfCond, ErrorPath errorPath)
 
void oppositeInnerConditionError (const Token *tok1, const Token *tok2, ErrorPath errorPath)
 
void identicalInnerConditionError (const Token *tok1, const Token *tok2, ErrorPath errorPath)
 
void identicalConditionAfterEarlyExitError (const Token *cond1, const Token *cond2, ErrorPath errorPath)
 
void incorrectLogicOperatorError (const Token *tok, const std::string &condition, bool always, bool inconclusive, ErrorPath errors)
 
void redundantConditionError (const Token *tok, const std::string &text, bool inconclusive)
 
void moduloAlwaysTrueFalseError (const Token *tok, const std::string &maxVal)
 
void clarifyConditionError (const Token *tok, bool assign, bool boolop)
 
void alwaysTrueFalseError (const Token *tok, const Token *condition, const ValueFlow::Value *value)
 
void invalidTestForOverflow (const Token *tok, const ValueType *valueType, const std::string &replace)
 
void pointerAdditionResultNotNullError (const Token *tok, const Token *calc)
 
void duplicateConditionalAssignError (const Token *condTok, const Token *assignTok, bool isRedundant=false)
 
void assignmentInCondition (const Token *eq)
 
void checkCompareValueOutOfTypeRange ()
 
void compareValueOutOfTypeRangeError (const Token *comparison, const std::string &type, long long value, bool result)
 
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::set< const Token * > mCondDiags
 

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

Check for condition mismatches.

Definition at line 50 of file checkcondition.h.

Constructor & Destructor Documentation

◆ CheckCondition() [1/2]

CheckCondition::CheckCondition ( )
inline

This constructor is used when registering the CheckAssignIf.

Definition at line 53 of file checkcondition.h.

◆ CheckCondition() [2/2]

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

This constructor is used when running checks.

Definition at line 57 of file checkcondition.h.

Member Function Documentation

◆ alwaysTrueFalse()

void CheckCondition::alwaysTrueFalse ( )
private

◆ alwaysTrueFalseError()

void CheckCondition::alwaysTrueFalseError ( const Token tok,
const Token condition,
const ValueFlow::Value value 
)
private

◆ assignIf()

void CheckCondition::assignIf ( )
private

◆ assignIfError()

void CheckCondition::assignIfError ( const Token tok1,
const Token tok2,
const std::string &  condition,
bool  result 
)
private

◆ assignIfParseScope()

bool CheckCondition::assignIfParseScope ( const Token *const  assignTok,
const Token *const  startTok,
const nonneg int  varid,
const bool  islocal,
const char  bitop,
const MathLib::bigint  num 
)
private

◆ assignmentInCondition()

void CheckCondition::assignmentInCondition ( const Token eq)
private

◆ badBitmaskCheckError()

void CheckCondition::badBitmaskCheckError ( const Token tok,
bool  isNoOp = false 
)
private

Definition at line 347 of file checkcondition.cpp.

References CWE571, normal, Check::reportError(), style, and warning.

Referenced by checkBadBitmaskCheck(), and getErrorMessages().

◆ checkAssignmentInCondition()

void CheckCondition::checkAssignmentInCondition ( )
private

◆ checkBadBitmaskCheck()

void CheckCondition::checkBadBitmaskCheck ( )
private

◆ checkCompareValueOutOfTypeRange()

void CheckCondition::checkCompareValueOutOfTypeRange ( )
private

◆ checkDuplicateConditionalAssign()

void CheckCondition::checkDuplicateConditionalAssign ( )
private

◆ checkIncorrectLogicOperator()

void CheckCondition::checkIncorrectLogicOperator ( )
private

◆ checkInvalidTestForOverflow()

void CheckCondition::checkInvalidTestForOverflow ( )
private

◆ checkModuloAlwaysTrueFalse()

void CheckCondition::checkModuloAlwaysTrueFalse ( )
private

◆ checkPointerAdditionResultNotNull()

void CheckCondition::checkPointerAdditionResultNotNull ( )
private

◆ clarifyCondition()

void CheckCondition::clarifyCondition ( )
private

◆ clarifyConditionError()

void CheckCondition::clarifyConditionError ( const Token tok,
bool  assign,
bool  boolop 
)
private

Definition at line 1460 of file checkcondition.cpp.

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

Referenced by clarifyCondition(), and getErrorMessages().

◆ classInfo()

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

get information about this class, used to generate documentation

Implements Check.

Definition at line 203 of file checkcondition.h.

◆ compareValueOutOfTypeRangeError()

void CheckCondition::compareValueOutOfTypeRangeError ( const Token comparison,
const std::string &  type,
long long  value,
bool  result 
)
private

◆ comparison()

void CheckCondition::comparison ( )
private

◆ comparisonError()

void CheckCondition::comparisonError ( const Token tok,
const std::string &  bitop,
MathLib::bigint  value1,
const std::string &  op,
MathLib::bigint  value2,
bool  result 
)
private

Definition at line 418 of file checkcondition.cpp.

References bool_to_string(), CWE398, normal, Check::reportError(), and style.

Referenced by comparison(), and getErrorMessages().

◆ diag()

bool CheckCondition::diag ( const Token tok,
bool  insert = true 
)
private

◆ duplicateCondition()

void CheckCondition::duplicateCondition ( )
private

◆ duplicateConditionalAssignError()

void CheckCondition::duplicateConditionalAssignError ( const Token condTok,
const Token assignTok,
bool  isRedundant = false 
)
private

◆ duplicateConditionError()

void CheckCondition::duplicateConditionError ( const Token tok1,
const Token tok2,
ErrorPath  errorPath 
)
private

Definition at line 512 of file checkcondition.cpp.

References CWE398, diag(), normal, Check::reportError(), and style.

Referenced by duplicateCondition(), and getErrorMessages().

◆ getErrorMessages()

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

◆ identicalConditionAfterEarlyExitError()

void CheckCondition::identicalConditionAfterEarlyExitError ( const Token cond1,
const Token cond2,
ErrorPath  errorPath 
)
private

◆ identicalInnerConditionError()

void CheckCondition::identicalInnerConditionError ( const Token tok1,
const Token tok2,
ErrorPath  errorPath 
)
private

◆ incorrectLogicOperatorError()

void CheckCondition::incorrectLogicOperatorError ( const Token tok,
const std::string &  condition,
bool  always,
bool  inconclusive,
ErrorPath  errors 
)
private

◆ invalidTestForOverflow()

void CheckCondition::invalidTestForOverflow ( const Token tok,
const ValueType valueType,
const std::string &  replace 
)
private

◆ isAliased()

bool CheckCondition::isAliased ( const std::set< int > &  vars) const
private

Definition at line 79 of file checkcondition.cpp.

References Token::Match(), Check::mTokenizer, Token::next(), and Tokenizer::tokens().

Referenced by multiCondition2().

◆ isOverlappingCond()

bool CheckCondition::isOverlappingCond ( const Token *const  cond1,
const Token *const  cond2,
bool  pure 
) const
private

◆ mismatchingBitAndError()

void CheckCondition::mismatchingBitAndError ( const Token tok1,
const MathLib::bigint  num1,
const Token tok2,
const MathLib::bigint  num2 
)
private

Definition at line 251 of file checkcondition.cpp.

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

Referenced by assignIfParseScope(), and getErrorMessages().

◆ moduloAlwaysTrueFalseError()

void CheckCondition::moduloAlwaysTrueFalseError ( const Token tok,
const std::string &  maxVal 
)
private

Definition at line 1393 of file checkcondition.cpp.

References CWE398, diag(), normal, Check::reportError(), and warning.

Referenced by checkModuloAlwaysTrueFalse(), and getErrorMessages().

◆ multiCondition()

void CheckCondition::multiCondition ( )
private

◆ multiCondition2()

void CheckCondition::multiCondition2 ( )
private

◆ myName()

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

Definition at line 199 of file checkcondition.h.

◆ oppositeElseIfConditionError()

void CheckCondition::oppositeElseIfConditionError ( const Token ifCond,
const Token elseIfCond,
ErrorPath  errorPath 
)
private

Definition at line 578 of file checkcondition.cpp.

References CWE398, diag(), Token::linenr(), normal, Check::reportError(), and style.

Referenced by multiCondition().

◆ oppositeInnerConditionError()

void CheckCondition::oppositeInnerConditionError ( const Token tok1,
const Token tok2,
ErrorPath  errorPath 
)
private

◆ overlappingElseIfConditionError()

void CheckCondition::overlappingElseIfConditionError ( const Token tok,
nonneg int  line1 
)
private

Definition at line 567 of file checkcondition.cpp.

References CWE398, diag(), normal, Check::reportError(), and style.

Referenced by getErrorMessages(), and multiCondition().

◆ pointerAdditionResultNotNullError()

void CheckCondition::pointerAdditionResultNotNullError ( const Token tok,
const Token calc 
)
private

◆ redundantConditionError()

void CheckCondition::redundantConditionError ( const Token tok,
const std::string &  text,
bool  inconclusive 
)
private

◆ runChecks()

void CheckCondition::runChecks ( const Tokenizer ,
ErrorLogger  
)
inlineoverrideprivatevirtual

Member Data Documentation

◆ mCondDiags

std::set<const Token*> CheckCondition::mCondDiags
private

Definition at line 133 of file checkcondition.h.

Referenced by diag().


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