2011-07-30 21:43:21 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2014-02-15 07:45:39 +01:00
|
|
|
* Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team.
|
2011-07-30 21:43:21 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Check for assignment / condition mismatches
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "checkassignif.h"
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Register this check class (by creating a static instance of it)
|
2011-10-13 20:53:06 +02:00
|
|
|
namespace {
|
|
|
|
CheckAssignIf instance;
|
2011-07-30 21:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-31 11:23:38 +02:00
|
|
|
void CheckAssignIf::assignIf()
|
2011-07-30 21:43:21 +02:00
|
|
|
{
|
2011-08-07 09:28:08 +02:00
|
|
|
if (!_settings->isEnabled("style"))
|
2011-07-30 21:43:21 +02:00
|
|
|
return;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
2011-07-30 21:43:21 +02:00
|
|
|
if (tok->str() != "=")
|
|
|
|
continue;
|
|
|
|
|
2012-11-29 10:19:52 +01:00
|
|
|
if (Token::Match(tok->tokAt(-2), "[;{}] %var% =")) {
|
2013-01-31 20:08:48 +01:00
|
|
|
const Variable *var = tok->previous()->variable();
|
|
|
|
if (var == 0)
|
2011-07-30 21:43:21 +02:00
|
|
|
continue;
|
|
|
|
|
2012-11-29 10:19:52 +01:00
|
|
|
char bitop = '\0';
|
|
|
|
MathLib::bigint num = 0;
|
2011-08-19 16:10:09 +02:00
|
|
|
|
2012-11-29 10:19:52 +01:00
|
|
|
if (Token::Match(tok->next(), "%num% [&|]")) {
|
|
|
|
bitop = tok->strAt(2).at(0);
|
|
|
|
num = MathLib::toLongNumber(tok->next()->str());
|
|
|
|
} else {
|
2013-04-23 06:47:15 +02:00
|
|
|
const Token *endToken = Token::findsimplematch(tok, ";");
|
|
|
|
|
|
|
|
// Casting address
|
|
|
|
if (Token::Match(endToken->tokAt(-4), "* ) & %any% ;"))
|
2014-02-16 10:32:10 +01:00
|
|
|
endToken = nullptr;
|
2013-04-23 06:47:15 +02:00
|
|
|
|
2012-11-29 10:19:52 +01:00
|
|
|
if (endToken && Token::Match(endToken->tokAt(-2), "[&|] %num% ;")) {
|
|
|
|
bitop = endToken->strAt(-2).at(0);
|
|
|
|
num = MathLib::toLongNumber(endToken->previous()->str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitop == '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (num < 0 && bitop == '|')
|
2011-07-30 21:43:21 +02:00
|
|
|
continue;
|
|
|
|
|
2013-07-20 12:31:04 +02:00
|
|
|
assignIfParseScope(tok, tok->tokAt(4), var->declarationId(), var->isLocal(), bitop, num);
|
2012-09-29 10:33:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** parse scopes recursively */
|
|
|
|
bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
|
|
|
|
const Token * const startTok,
|
|
|
|
const unsigned int varid,
|
2012-09-29 10:54:09 +02:00
|
|
|
const bool islocal,
|
2012-09-29 10:33:54 +02:00
|
|
|
const char bitop,
|
|
|
|
const MathLib::bigint num)
|
|
|
|
{
|
2013-04-07 04:25:10 +02:00
|
|
|
bool ret = false;
|
|
|
|
|
2012-09-29 10:33:54 +02:00
|
|
|
for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) {
|
2013-03-01 11:43:59 +01:00
|
|
|
if (Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) {
|
2013-01-21 19:59:34 +01:00
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
|
|
|
|
if ((bitop == '&') && (0 == (num & num2)))
|
|
|
|
mismatchingBitAndError(assignTok, num, tok2, num2);
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2, "%varid% =", varid)) {
|
2013-03-01 11:43:59 +01:00
|
|
|
if (Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) {
|
2013-01-21 19:59:34 +01:00
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
|
|
|
|
if ((bitop == '&') && (0 == (num & num2)))
|
|
|
|
mismatchingBitAndError(assignTok, num, tok2, num2);
|
|
|
|
}
|
2012-12-27 15:40:00 +01:00
|
|
|
return true;
|
2013-01-21 19:59:34 +01:00
|
|
|
}
|
2013-08-13 14:08:54 +02:00
|
|
|
if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid)) {
|
|
|
|
unsigned int argumentNumber = 0;
|
|
|
|
const Token *ftok;
|
|
|
|
for (ftok = tok2; ftok && ftok->str() != "("; ftok = ftok->previous()) {
|
|
|
|
if (ftok->str() == ")")
|
|
|
|
ftok = ftok->link();
|
|
|
|
else if (ftok->str() == ",")
|
|
|
|
argumentNumber++;
|
|
|
|
}
|
2014-02-16 10:32:10 +01:00
|
|
|
ftok = ftok ? ftok->previous() : nullptr;
|
2013-08-13 14:08:54 +02:00
|
|
|
if (!(ftok && ftok->function()))
|
|
|
|
return true;
|
|
|
|
const Variable *par = ftok->function()->getArgumentVar(argumentNumber);
|
2014-02-16 10:32:10 +01:00
|
|
|
if (par == nullptr || par->isReference() || par->isPointer())
|
2013-08-13 14:08:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-09-29 10:33:54 +02:00
|
|
|
if (tok2->str() == "}")
|
|
|
|
return false;
|
2013-04-07 04:25:10 +02:00
|
|
|
if (Token::Match(tok2, "break|continue|return"))
|
|
|
|
ret = true;
|
|
|
|
if (ret && tok2->str() == ";")
|
|
|
|
return false;
|
2012-09-29 19:22:34 +02:00
|
|
|
if (!islocal && Token::Match(tok2, "%var% (") && !Token::simpleMatch(tok2->next()->link(), ") {"))
|
|
|
|
return true;
|
2012-09-29 10:54:09 +02:00
|
|
|
if (Token::Match(tok2, "if|while (")) {
|
|
|
|
if (!islocal && tok2->str() == "while")
|
|
|
|
continue;
|
|
|
|
|
2012-09-29 10:33:54 +02:00
|
|
|
// parse condition
|
|
|
|
const Token * const end = tok2->next()->link();
|
|
|
|
for (; tok2 != end; tok2 = tok2->next()) {
|
|
|
|
if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok2,"&&|%oror%|( %varid% %any% %num% &&|%oror%|)", varid)) {
|
|
|
|
const Token *vartok = tok2->next();
|
|
|
|
const std::string& op(vartok->strAt(1));
|
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(vartok->strAt(2));
|
|
|
|
const std::string condition(vartok->str() + op + vartok->strAt(2));
|
|
|
|
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
|
|
|
|
assignIfError(assignTok, tok2, condition, false);
|
|
|
|
else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num))
|
|
|
|
assignIfError(assignTok, tok2, condition, true);
|
2011-07-30 21:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-29 10:33:54 +02:00
|
|
|
|
2012-09-29 10:54:09 +02:00
|
|
|
bool ret1 = assignIfParseScope(assignTok, end->tokAt(2), varid, islocal, bitop, num);
|
2012-09-29 10:33:54 +02:00
|
|
|
bool ret2 = false;
|
|
|
|
if (Token::simpleMatch(end->next()->link(), "} else {"))
|
2012-09-29 10:54:09 +02:00
|
|
|
ret2 = assignIfParseScope(assignTok, end->next()->link()->tokAt(3), varid, islocal, bitop, num);
|
2012-09-29 10:33:54 +02:00
|
|
|
if (ret1 || ret2)
|
|
|
|
return true;
|
2011-07-30 21:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-29 10:33:54 +02:00
|
|
|
return false;
|
2011-07-30 21:43:21 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 17:03:16 +02:00
|
|
|
void CheckAssignIf::assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result)
|
2011-07-30 21:43:21 +02:00
|
|
|
{
|
2012-09-28 17:03:16 +02:00
|
|
|
std::list<const Token *> locations;
|
|
|
|
locations.push_back(tok1);
|
|
|
|
locations.push_back(tok2);
|
|
|
|
|
|
|
|
reportError(locations,
|
|
|
|
Severity::style,
|
2011-07-31 11:23:38 +02:00
|
|
|
"assignIfError",
|
2012-09-28 17:03:16 +02:00
|
|
|
"Mismatching assignment and comparison, comparison '" + condition + "' is always " + std::string(result ? "true" : "false") + ".");
|
2011-07-30 21:43:21 +02:00
|
|
|
}
|
2011-07-31 11:23:38 +02:00
|
|
|
|
|
|
|
|
2013-01-21 19:59:34 +01:00
|
|
|
void CheckAssignIf::mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2)
|
|
|
|
{
|
|
|
|
std::list<const Token *> locations;
|
|
|
|
locations.push_back(tok1);
|
|
|
|
locations.push_back(tok2);
|
|
|
|
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Mismatching bitmasks. Result is always 0 ("
|
|
|
|
<< "X = Y & 0x" << std::hex << num1 << "; Z = X & 0x" << std::hex << num2 << "; => Z=0).";
|
|
|
|
|
|
|
|
reportError(locations,
|
|
|
|
Severity::style,
|
|
|
|
"mismatchingBitAnd",
|
|
|
|
msg.str());
|
|
|
|
}
|
2011-07-31 11:23:38 +02:00
|
|
|
|
|
|
|
|
2013-11-07 14:38:08 +01:00
|
|
|
static void getnumchildren(const Token *tok, std::list<MathLib::bigint> &numchildren)
|
|
|
|
{
|
|
|
|
if (tok->astOperand1() && tok->astOperand1()->isNumber())
|
|
|
|
numchildren.push_back(MathLib::toLongNumber(tok->astOperand1()->str()));
|
|
|
|
else if (tok->astOperand1() && tok->str() == tok->astOperand1()->str())
|
|
|
|
getnumchildren(tok->astOperand1(), numchildren);
|
|
|
|
if (tok->astOperand2() && tok->astOperand2()->isNumber())
|
|
|
|
numchildren.push_back(MathLib::toLongNumber(tok->astOperand2()->str()));
|
|
|
|
else if (tok->astOperand2() && tok->str() == tok->astOperand2()->str())
|
|
|
|
getnumchildren(tok->astOperand2(), numchildren);
|
|
|
|
}
|
2011-07-31 11:23:38 +02:00
|
|
|
|
|
|
|
void CheckAssignIf::comparison()
|
|
|
|
{
|
2011-08-07 09:28:08 +02:00
|
|
|
if (!_settings->isEnabled("style"))
|
2011-07-31 11:23:38 +02:00
|
|
|
return;
|
|
|
|
|
2013-12-09 18:06:19 +01:00
|
|
|
// Experimental code based on AST
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
2013-12-09 18:06:19 +01:00
|
|
|
if (Token::Match(tok, "==|!=")) {
|
|
|
|
const Token *expr1 = tok->astOperand1();
|
|
|
|
const Token *expr2 = tok->astOperand2();
|
|
|
|
if (!expr1 || !expr2)
|
2011-07-31 11:23:38 +02:00
|
|
|
continue;
|
2013-12-09 18:06:19 +01:00
|
|
|
if (expr1->isNumber())
|
|
|
|
std::swap(expr1,expr2);
|
|
|
|
if (!expr2->isNumber())
|
|
|
|
continue;
|
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(expr2->str());
|
2011-07-31 11:23:38 +02:00
|
|
|
if (num2 < 0)
|
|
|
|
continue;
|
2013-12-09 18:06:19 +01:00
|
|
|
if (!Token::Match(expr1,"[&|]"))
|
|
|
|
continue;
|
|
|
|
std::list<MathLib::bigint> numbers;
|
|
|
|
getnumchildren(expr1, numbers);
|
|
|
|
for (std::list<MathLib::bigint>::const_iterator num = numbers.begin(); num != numbers.end(); ++num) {
|
|
|
|
const MathLib::bigint num1 = *num;
|
|
|
|
if (num1 < 0)
|
|
|
|
continue;
|
|
|
|
if ((expr1->str() == "&" && (num1 & num2) != num2) ||
|
|
|
|
(expr1->str() == "|" && (num1 | num2) != num2)) {
|
|
|
|
const std::string& op(tok->str());
|
|
|
|
comparisonError(expr1, expr1->str(), num1, op, num2, op=="==" ? false : true);
|
|
|
|
}
|
2011-07-31 11:23:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-14 19:13:44 +02:00
|
|
|
void CheckAssignIf::comparisonError(const Token *tok, const std::string &bitop, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result)
|
2011-07-31 11:23:38 +02:00
|
|
|
{
|
2011-11-18 20:07:42 +01:00
|
|
|
std::ostringstream expression;
|
2012-09-14 19:13:44 +02:00
|
|
|
expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2;
|
2011-08-01 07:48:18 +02:00
|
|
|
|
2012-07-07 20:31:18 +02:00
|
|
|
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n"
|
2011-11-19 08:38:54 +01:00
|
|
|
"The expression '" + expression.str() + "' is always " + (result?"true":"false") +
|
|
|
|
". Check carefully constants and operators used, these errors might be hard to "
|
|
|
|
"spot sometimes. In case of complex expression it might help to split it to "
|
|
|
|
"separate expressions.");
|
2011-08-01 07:13:47 +02:00
|
|
|
|
|
|
|
reportError(tok, Severity::style, "comparisonError", errmsg);
|
2011-07-31 11:23:38 +02:00
|
|
|
}
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
extern bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::string> &constFunctions);
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
static bool isOverlappingCond(const Token * const cond1, const Token * const cond2, const std::set<std::string> &constFunctions)
|
|
|
|
{
|
|
|
|
if (!cond1 || !cond2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// same expressions
|
|
|
|
if (isSameExpression(cond1,cond2,constFunctions))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// bitwise overlap for example 'x&7' and 'x==1'
|
|
|
|
if (cond1->str() == "&" && cond1->astOperand1() && cond2->astOperand2()) {
|
|
|
|
const Token *expr1 = cond1->astOperand1();
|
|
|
|
const Token *num1 = cond1->astOperand2();
|
|
|
|
if (!num1->isNumber())
|
|
|
|
std::swap(expr1,num1);
|
|
|
|
if (!num1->isNumber() || MathLib::isNegative(num1->str()))
|
|
|
|
return false;
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
if (!Token::Match(cond2, "&|==") || !cond2->astOperand1() || !cond2->astOperand2())
|
|
|
|
return false;
|
|
|
|
const Token *expr2 = cond2->astOperand1();
|
|
|
|
const Token *num2 = cond2->astOperand2();
|
|
|
|
if (!num2->isNumber())
|
|
|
|
std::swap(expr2,num2);
|
|
|
|
if (!num2->isNumber() || MathLib::isNegative(num2->str()))
|
|
|
|
return false;
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
if (!isSameExpression(expr1,expr2,constFunctions))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const MathLib::bigint value1 = MathLib::toLongNumber(num1->str());
|
|
|
|
const MathLib::bigint value2 = MathLib::toLongNumber(num2->str());
|
|
|
|
return ((value1 & value2) == value2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-01 09:35:48 +02:00
|
|
|
|
|
|
|
|
2011-08-01 11:33:09 +02:00
|
|
|
void CheckAssignIf::multiCondition()
|
2011-08-01 09:35:48 +02:00
|
|
|
{
|
2011-08-07 09:28:08 +02:00
|
|
|
if (!_settings->isEnabled("style"))
|
2011-08-01 09:35:48 +02:00
|
|
|
return;
|
|
|
|
|
2011-12-09 21:00:57 +01:00
|
|
|
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
|
|
|
|
|
|
|
|
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
|
2014-04-03 19:35:50 +02:00
|
|
|
if (i->type != Scope::eIf || !Token::simpleMatch(i->classDef, "if ("))
|
|
|
|
continue;
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
const Token * const cond1 = i->classDef->next()->astOperand2();
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2014-04-03 19:35:50 +02:00
|
|
|
const Token * tok2 = i->classDef->next();
|
|
|
|
while (tok2) {
|
|
|
|
tok2 = tok2->link();
|
|
|
|
if (!Token::simpleMatch(tok2, ") {"))
|
|
|
|
break;
|
|
|
|
tok2 = tok2->linkAt(1);
|
|
|
|
if (!Token::simpleMatch(tok2, "} else { if ("))
|
|
|
|
break;
|
|
|
|
tok2 = tok2->tokAt(4);
|
|
|
|
|
|
|
|
if (isOverlappingCond(cond1, tok2->astOperand2(), _settings->library.functionpure))
|
|
|
|
multiConditionError(tok2, cond1->linenr());
|
2011-08-01 09:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 11:33:09 +02:00
|
|
|
void CheckAssignIf::multiConditionError(const Token *tok, unsigned int line1)
|
2011-08-01 09:35:48 +02:00
|
|
|
{
|
|
|
|
std::ostringstream errmsg;
|
2012-07-07 20:31:18 +02:00
|
|
|
errmsg << "Expression is always false because 'else if' condition matches previous condition at line "
|
|
|
|
<< line1 << ".";
|
2011-08-01 09:35:48 +02:00
|
|
|
|
2011-10-05 19:44:00 +02:00
|
|
|
reportError(tok, Severity::style, "multiCondition", errmsg.str());
|
2011-08-01 09:35:48 +02:00
|
|
|
}
|