21 #ifndef checkconditionH
22 #define checkconditionH
58 :
Check(myName(), tokenizer, settings, errorLogger) {}
83 bool assignIfParseScope(
const Token *
const assignTok,
84 const Token *
const startTok,
91 void checkBadBitmaskCheck();
96 void duplicateCondition();
99 void multiCondition();
107 void multiCondition2();
110 void checkIncorrectLogicOperator();
113 void checkModuloAlwaysTrueFalse();
116 void clarifyCondition();
119 void alwaysTrueFalse();
122 void checkInvalidTestForOverflow();
125 void checkPointerAdditionResultNotNull();
127 void checkDuplicateConditionalAssign();
130 void checkAssignmentInCondition();
134 bool diag(
const Token* tok,
bool insert=
true);
135 bool isAliased(
const std::set<int> &vars)
const;
136 bool isOverlappingCond(
const Token *
const cond1,
const Token *
const cond2,
bool pure)
const;
137 void assignIfError(
const Token *tok1,
const Token *tok2,
const std::string &condition,
bool result);
139 void badBitmaskCheckError(
const Token *tok,
bool isNoOp =
false);
140 void comparisonError(
const Token *tok,
141 const std::string &bitop,
143 const std::string &op,
147 void overlappingElseIfConditionError(
const Token *tok,
nonneg int line1);
148 void oppositeElseIfConditionError(
const Token *ifCond,
const Token *elseIfCond,
ErrorPath errorPath);
150 void oppositeInnerConditionError(
const Token *tok1,
const Token* tok2,
ErrorPath errorPath);
152 void identicalInnerConditionError(
const Token *tok1,
const Token* tok2,
ErrorPath errorPath);
154 void identicalConditionAfterEarlyExitError(
const Token *cond1,
const Token *cond2,
ErrorPath errorPath);
156 void incorrectLogicOperatorError(
const Token *tok,
const std::string &condition,
bool always,
bool inconclusive,
ErrorPath errors);
157 void redundantConditionError(
const Token *tok,
const std::string &text,
bool inconclusive);
159 void moduloAlwaysTrueFalseError(
const Token* tok,
const std::string& maxVal);
161 void clarifyConditionError(
const Token *tok,
bool assign,
bool boolop);
165 void invalidTestForOverflow(
const Token* tok,
const ValueType *valueType,
const std::string &
replace);
166 void pointerAdditionResultNotNullError(
const Token *tok,
const Token *calc);
168 void duplicateConditionalAssignError(
const Token *condTok,
const Token* assignTok,
bool isRedundant =
false);
170 void assignmentInCondition(
const Token *eq);
172 void checkCompareValueOutOfTypeRange();
173 void compareValueOutOfTypeRangeError(
const Token *comparison,
const std::string &type,
long long value,
bool result);
204 return "Match conditions with assignments and other conditions:\n"
205 "- Mismatching assignment and comparison => comparison is always true/false\n"
206 "- Mismatching lhs and rhs in comparison => comparison is always true/false\n"
207 "- Detect usage of | where & should be used\n"
208 "- Duplicate condition and assignment\n"
209 "- Detect matching 'if' and 'else if' conditions\n"
210 "- Mismatching bitand (a &= 0xf0; a &= 1; => a = 0)\n"
211 "- Opposite inner condition is always false\n"
212 "- Identical condition after early exit is always false\n"
213 "- Condition that is always true/false\n"
214 "- Mutual exclusion over || always evaluating to true\n"
215 "- Comparisons of modulo results that are always true/false.\n"
216 "- Known variable values => condition is always true/false\n"
217 "- Invalid test for overflow. Some mainstream compilers remove such overflow tests when optimising code.\n"
218 "- Suspicious assignment of container/iterator in condition => condition is always true.\n";
static bool isAliased(const Token *startTok, const Token *endTok, nonneg int varid)
Check for condition mismatches.
void oppositeInnerConditionError(const Token *tok1, const Token *tok2, ErrorPath errorPath)
void checkInvalidTestForOverflow()
Check for invalid test for overflow 'x+100 < x'
void checkIncorrectLogicOperator()
Check for testing for mutual exclusion over ||
void comparison()
mismatching lhs and rhs in comparison
std::set< const Token * > mCondDiags
void multiCondition()
match 'if' and 'else if' conditions
void assignIf()
mismatching assignment / comparison
void invalidTestForOverflow(const Token *tok, const ValueType *valueType, const std::string &replace)
void checkPointerAdditionResultNotNull()
Check if pointer addition result is NULL '(ptr + 1) == NULL'.
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result)
void redundantConditionError(const Token *tok, const std::string &text, bool inconclusive)
void alwaysTrueFalseError(const Token *tok, const Token *condition, const ValueFlow::Value *value)
void identicalConditionAfterEarlyExitError(const Token *cond1, const Token *cond2, ErrorPath errorPath)
void assignmentInCondition(const Token *eq)
void multiCondition2()
multiconditions #2
void badBitmaskCheckError(const Token *tok, bool isNoOp=false)
void identicalInnerConditionError(const Token *tok1, const Token *tok2, ErrorPath errorPath)
void duplicateConditionError(const Token *tok1, const Token *tok2, ErrorPath errorPath)
CheckCondition()
This constructor is used when registering the CheckAssignIf.
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
void moduloAlwaysTrueFalseError(const Token *tok, const std::string &maxVal)
void checkModuloAlwaysTrueFalse()
Check for suspicious usage of modulo (e.g.
void clarifyConditionError(const Token *tok, bool assign, bool boolop)
void mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2)
void clarifyCondition()
Suspicious condition (assignment+comparison)
void duplicateConditionalAssignError(const Token *condTok, const Token *assignTok, bool isRedundant=false)
void incorrectLogicOperatorError(const Token *tok, const std::string &condition, bool always, bool inconclusive, ErrorPath errors)
void compareValueOutOfTypeRangeError(const Token *comparison, const std::string &type, long long value, bool result)
void checkDuplicateConditionalAssign()
std::string classInfo() const override
get information about this class, used to generate documentation
void alwaysTrueFalse()
Condition is always true/false.
void overlappingElseIfConditionError(const Token *tok, nonneg int line1)
void comparisonError(const Token *tok, const std::string &bitop, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result)
CheckCondition(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
void pointerAdditionResultNotNullError(const Token *tok, const Token *calc)
static std::string myName()
void checkCompareValueOutOfTypeRange()
void checkAssignmentInCondition()
Assignment in condition.
void duplicateCondition()
void checkBadBitmaskCheck()
check bitmask using | instead of &
Interface class that cppcheck uses to communicate with the checks.
This is an interface, which the class responsible of error logging should implement.
This is just a container for general settings so that we don't need to pass individual values to func...
The token list that the TokenList generates is a linked-list of this class.
The main purpose is to tokenize the source code.
const Settings & getSettings() const
static const std::string emptyString
static void replace(std::string &source, const std::unordered_map< std::string, std::string > &substitutionMap)
std::list< ErrorPathItem > ErrorPath