2011-07-30 21:43:21 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 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;
|
|
|
|
|
2012-09-29 10:54:09 +02:00
|
|
|
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
|
|
|
|
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% =")) {
|
2011-07-30 21:43:21 +02:00
|
|
|
const unsigned int varid(tok->previous()->varId());
|
|
|
|
if (varid == 0)
|
|
|
|
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 {
|
2012-12-01 03:17:55 +01:00
|
|
|
const Token * const endToken = Token::findsimplematch(tok, ";");
|
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;
|
|
|
|
|
2012-09-29 10:54:09 +02:00
|
|
|
bool islocal = false;
|
|
|
|
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
|
|
|
|
if (var && var->isLocal())
|
|
|
|
islocal = true;
|
|
|
|
|
|
|
|
assignIfParseScope(tok, tok->tokAt(4), varid, 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)
|
|
|
|
{
|
|
|
|
for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) {
|
2012-12-27 15:40:00 +01:00
|
|
|
if (Token::Match(tok2, "%varid% =", varid))
|
|
|
|
return true;
|
2012-09-29 10:41:34 +02:00
|
|
|
if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid))
|
2012-09-29 10:33:54 +02:00
|
|
|
return true;
|
|
|
|
if (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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckAssignIf::comparison()
|
|
|
|
{
|
2011-08-07 09:28:08 +02:00
|
|
|
if (!_settings->isEnabled("style"))
|
2011-07-31 11:23:38 +02:00
|
|
|
return;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
2012-09-14 19:13:44 +02:00
|
|
|
if (Token::Match(tok, "&|%or% %num% )| ==|!= %num% &&|%oror%|)")) {
|
2011-07-31 11:23:38 +02:00
|
|
|
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1));
|
|
|
|
if (num1 < 0)
|
|
|
|
continue;
|
|
|
|
|
2011-07-31 17:09:53 +02:00
|
|
|
const Token *compareToken = tok->tokAt(2);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (compareToken->str() == ")") {
|
2011-07-31 17:09:53 +02:00
|
|
|
if (!Token::Match(compareToken->link()->previous(), "(|%oror%|&&"))
|
|
|
|
continue;
|
|
|
|
compareToken = compareToken->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(compareToken->strAt(1));
|
2011-07-31 11:23:38 +02:00
|
|
|
if (num2 < 0)
|
|
|
|
continue;
|
|
|
|
|
2012-09-14 19:13:44 +02:00
|
|
|
if ((tok->str() == "&" && (num1 & num2) != num2) ||
|
|
|
|
(tok->str() == "|" && (num1 | num2) != num2)) {
|
2012-05-25 12:09:41 +02:00
|
|
|
const std::string& op(compareToken->str());
|
2012-09-14 19:13:44 +02:00
|
|
|
comparisonError(tok, tok->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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (i->type == Scope::eIf && Token::Match(i->classDef, "if ( %var% & %num% ) {")) {
|
|
|
|
const Token* const tok = i->classDef;
|
2011-08-01 09:35:48 +02:00
|
|
|
const unsigned int varid(tok->tokAt(2)->varId());
|
|
|
|
if (varid == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(4));
|
|
|
|
if (num1 < 0)
|
|
|
|
continue;
|
|
|
|
|
2011-11-20 14:22:39 +01:00
|
|
|
const Token *tok2 = tok->linkAt(6);
|
2011-10-13 20:53:06 +02:00
|
|
|
while (Token::simpleMatch(tok2, "} else { if (")) {
|
2011-08-01 09:35:48 +02:00
|
|
|
// Goto '('
|
|
|
|
const Token * const opar = tok2->tokAt(4);
|
|
|
|
|
|
|
|
// tok2: skip if-block
|
|
|
|
tok2 = opar->link();
|
|
|
|
if (Token::simpleMatch(tok2, ") {"))
|
|
|
|
tok2 = tok2->next()->link();
|
|
|
|
|
|
|
|
// check condition..
|
2011-10-13 20:53:06 +02:00
|
|
|
if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid)) {
|
2011-08-01 09:35:48 +02:00
|
|
|
const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3));
|
|
|
|
if (num2 < 0)
|
|
|
|
continue;
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if ((num1 & num2) == num2) {
|
2011-08-01 11:33:09 +02:00
|
|
|
multiConditionError(opar, tok->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
|
|
|
}
|