2015-08-02 21:57:32 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2019-02-09 07:24:06 +01:00
|
|
|
* Copyright (C) 2007-2019 Cppcheck team.
|
2015-08-02 21:57:32 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#include "astutils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
|
|
|
#include "library.h"
|
|
|
|
#include "mathlib.h"
|
2016-10-23 13:54:44 +02:00
|
|
|
#include "settings.h"
|
2015-08-03 09:20:50 +02:00
|
|
|
#include "symboldatabase.h"
|
|
|
|
#include "token.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "valueflow.h"
|
|
|
|
|
2019-09-28 06:24:32 +02:00
|
|
|
#include <iterator>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
2018-11-23 06:53:43 +01:00
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
|
|
|
|
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor)
|
|
|
|
{
|
|
|
|
std::stack<const Token *> tokens;
|
|
|
|
tokens.push(ast);
|
|
|
|
while (!tokens.empty()) {
|
|
|
|
const Token *tok = tokens.top();
|
|
|
|
tokens.pop();
|
|
|
|
if (!tok)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ChildrenToVisit c = visitor(tok);
|
|
|
|
|
|
|
|
if (c == ChildrenToVisit::done)
|
|
|
|
break;
|
|
|
|
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2)
|
|
|
|
tokens.push(tok->astOperand1());
|
2018-12-17 15:40:15 +01:00
|
|
|
if (c == ChildrenToVisit::op2 || c == ChildrenToVisit::op1_and_op2)
|
2018-11-23 06:53:43 +01:00
|
|
|
tokens.push(tok->astOperand2());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 06:23:21 +02:00
|
|
|
static void astFlattenRecursive(const Token *tok, std::vector<const Token *> *result, const char* op, nonneg int depth = 0)
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
if (!tok || depth >= 100)
|
|
|
|
return;
|
|
|
|
if (tok->str() == op) {
|
|
|
|
astFlattenRecursive(tok->astOperand1(), result, op, depth);
|
|
|
|
astFlattenRecursive(tok->astOperand2(), result, op, depth);
|
|
|
|
} else {
|
|
|
|
result->push_back(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Token*> astFlatten(const Token* tok, const char* op)
|
|
|
|
{
|
|
|
|
std::vector<const Token*> result;
|
|
|
|
astFlattenRecursive(tok, &result, op);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2019-08-29 08:38:50 +02:00
|
|
|
bool astHasToken(const Token* root, const Token * tok)
|
|
|
|
{
|
|
|
|
if (!root)
|
|
|
|
return false;
|
|
|
|
if (root == tok)
|
|
|
|
return true;
|
|
|
|
return astHasToken(root->astOperand1(), tok) || astHasToken(root->astOperand2(), tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool astHasVar(const Token * tok, nonneg int varid)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->varId() == varid)
|
|
|
|
return true;
|
|
|
|
return astHasVar(tok->astOperand1(), varid) || astHasVar(tok->astOperand2(), varid);
|
|
|
|
}
|
|
|
|
|
2016-02-08 08:08:35 +01:00
|
|
|
static bool astIsCharWithSign(const Token *tok, ValueType::Sign sign)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const ValueType *valueType = tok->valueType();
|
|
|
|
if (!valueType)
|
|
|
|
return false;
|
|
|
|
return valueType->type == ValueType::Type::CHAR && valueType->pointer == 0U && valueType->sign == sign;
|
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
bool astIsSignedChar(const Token *tok)
|
|
|
|
{
|
2016-02-08 08:08:35 +01:00
|
|
|
return astIsCharWithSign(tok, ValueType::Sign::SIGNED);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool astIsUnknownSignChar(const Token *tok)
|
|
|
|
{
|
|
|
|
return astIsCharWithSign(tok, ValueType::Sign::UNKNOWN_SIGN);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
2015-08-02 21:57:32 +02:00
|
|
|
|
|
|
|
bool astIsIntegral(const Token *tok, bool unknown)
|
|
|
|
{
|
2015-10-11 14:11:11 +02:00
|
|
|
const ValueType *vt = tok ? tok->valueType() : nullptr;
|
|
|
|
if (!vt)
|
2015-08-02 21:57:32 +02:00
|
|
|
return unknown;
|
2015-10-11 14:11:11 +02:00
|
|
|
return vt->isIntegral() && vt->pointer == 0U;
|
2015-08-02 21:57:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool astIsFloat(const Token *tok, bool unknown)
|
|
|
|
{
|
2015-10-11 12:20:40 +02:00
|
|
|
const ValueType *vt = tok ? tok->valueType() : nullptr;
|
|
|
|
if (!vt)
|
2015-08-02 21:57:32 +02:00
|
|
|
return unknown;
|
2015-10-11 12:20:40 +02:00
|
|
|
return vt->type >= ValueType::Type::FLOAT && vt->pointer == 0U;
|
2015-08-02 21:57:32 +02:00
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2016-02-05 15:48:51 +01:00
|
|
|
bool astIsBool(const Token *tok)
|
|
|
|
{
|
2016-02-05 20:22:30 +01:00
|
|
|
return tok && (tok->isBoolean() || (tok->valueType() && tok->valueType()->type == ValueType::Type::BOOL && !tok->valueType()->pointer));
|
2016-02-05 15:48:51 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 10:08:17 +01:00
|
|
|
bool astIsPointer(const Token *tok)
|
|
|
|
{
|
|
|
|
return tok && tok->valueType() && tok->valueType()->pointer;
|
|
|
|
}
|
|
|
|
|
2019-10-01 08:39:08 +02:00
|
|
|
bool astIsSmartPointer(const Token* tok)
|
|
|
|
{
|
|
|
|
return tok && tok->valueType() && tok->valueType()->smartPointerTypeToken;
|
|
|
|
}
|
2019-09-30 21:04:43 +02:00
|
|
|
|
2018-11-17 09:41:59 +01:00
|
|
|
bool astIsIterator(const Token *tok)
|
|
|
|
{
|
|
|
|
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::ITERATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool astIsContainer(const Token *tok)
|
|
|
|
{
|
2019-08-15 21:14:27 +02:00
|
|
|
return getLibraryContainer(tok) != nullptr && tok->valueType()->type != ValueType::Type::ITERATOR;
|
2018-11-17 09:41:59 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 09:41:06 +02:00
|
|
|
std::string astCanonicalType(const Token *expr)
|
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return "";
|
|
|
|
if (expr->variable()) {
|
|
|
|
const Variable *var = expr->variable();
|
|
|
|
std::string ret;
|
|
|
|
for (const Token *type = var->typeStartToken(); Token::Match(type,"%name%|::") && type != var->nameToken(); type = type->next()) {
|
|
|
|
if (!Token::Match(type, "const|static"))
|
|
|
|
ret += type->str();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
// TODO: handle expressions
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:44:02 +02:00
|
|
|
static bool match(const Token *tok, const std::string &rhs)
|
|
|
|
{
|
|
|
|
if (tok->str() == rhs)
|
|
|
|
return true;
|
2019-05-18 06:22:25 +02:00
|
|
|
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs)
|
2016-10-18 21:44:02 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
const Token * astIsVariableComparison(const Token *tok, const std::string &comp, const std::string &rhs, const Token **vartok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Token *ret = nullptr;
|
|
|
|
if (tok->isComparisonOp()) {
|
2016-10-18 21:44:02 +02:00
|
|
|
if (tok->astOperand1() && match(tok->astOperand1(), rhs)) {
|
2015-08-03 09:20:50 +02:00
|
|
|
// Invert comparator
|
|
|
|
std::string s = tok->str();
|
|
|
|
if (s[0] == '>')
|
|
|
|
s[0] = '<';
|
|
|
|
else if (s[0] == '<')
|
|
|
|
s[0] = '>';
|
|
|
|
if (s == comp) {
|
|
|
|
ret = tok->astOperand2();
|
|
|
|
}
|
2016-10-18 21:44:02 +02:00
|
|
|
} else if (tok->str() == comp && tok->astOperand2() && match(tok->astOperand2(), rhs)) {
|
2015-08-03 09:20:50 +02:00
|
|
|
ret = tok->astOperand1();
|
|
|
|
}
|
|
|
|
} else if (comp == "!=" && rhs == std::string("0")) {
|
|
|
|
ret = tok;
|
|
|
|
} else if (comp == "==" && rhs == std::string("0")) {
|
2018-10-26 06:21:45 +02:00
|
|
|
if (tok->str() == "!") {
|
2015-08-03 09:20:50 +02:00
|
|
|
ret = tok->astOperand1();
|
2018-10-26 06:21:45 +02:00
|
|
|
// handle (!(x!=0)) as (x==0)
|
|
|
|
astIsVariableComparison(ret, "!=", "0", &ret);
|
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
while (ret && ret->str() == ".")
|
|
|
|
ret = ret->astOperand2();
|
2019-01-08 20:53:53 +01:00
|
|
|
if (ret && ret->str() == "=" && ret->astOperand1() && ret->astOperand1()->varId())
|
|
|
|
ret = ret->astOperand1();
|
|
|
|
else if (ret && ret->varId() == 0U)
|
2015-08-03 09:20:50 +02:00
|
|
|
ret = nullptr;
|
|
|
|
if (vartok)
|
|
|
|
*vartok = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:28:39 +02:00
|
|
|
bool isTemporary(bool cpp, const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (Token::simpleMatch(tok, "."))
|
2019-10-13 19:12:46 +02:00
|
|
|
return (tok->originalName() != "->" && isTemporary(cpp, tok->astOperand1())) ||
|
|
|
|
isTemporary(cpp, tok->astOperand2());
|
2019-10-08 09:28:39 +02:00
|
|
|
if (Token::Match(tok, ",|::"))
|
|
|
|
return isTemporary(cpp, tok->astOperand2());
|
2019-10-10 06:52:11 +02:00
|
|
|
if (tok->isCast() || (cpp && isCPPCast(tok)))
|
|
|
|
return isTemporary(cpp, tok->astOperand2());
|
2019-10-08 09:28:39 +02:00
|
|
|
if (Token::Match(tok, "?|.|[|++|--|%name%|%assign%"))
|
|
|
|
return false;
|
|
|
|
if (tok->isUnaryOp("*"))
|
|
|
|
return false;
|
|
|
|
if (Token::Match(tok, "&|<<|>>") && isLikelyStream(cpp, tok->astOperand1()))
|
|
|
|
return false;
|
|
|
|
if (Token::Match(tok->previous(), "%name% ("))
|
|
|
|
return tok->previous()->function() && !Function::returnsReference(tok->previous()->function(), true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:01:21 +02:00
|
|
|
static bool isFunctionCall(const Token* tok)
|
2019-07-24 09:59:01 +02:00
|
|
|
{
|
|
|
|
if (Token::Match(tok, "%name% ("))
|
|
|
|
return true;
|
|
|
|
if (Token::Match(tok, "%name% <") && Token::simpleMatch(tok->next()->link(), "> ("))
|
|
|
|
return true;
|
|
|
|
if (Token::Match(tok, "%name% ::"))
|
|
|
|
return isFunctionCall(tok->tokAt(2));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-10 16:40:40 +01:00
|
|
|
static bool hasToken(const Token * startTok, const Token * stopTok, const Token * tok)
|
|
|
|
{
|
2018-11-10 21:30:01 +01:00
|
|
|
for (const Token * tok2 = startTok; tok2 != stopTok; tok2 = tok2->next()) {
|
|
|
|
if (tok2 == tok)
|
2018-11-10 16:40:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-18 11:56:23 +02:00
|
|
|
const Token * nextAfterAstRightmostLeaf(const Token * tok)
|
|
|
|
{
|
2018-11-10 16:40:40 +01:00
|
|
|
const Token * rightmostLeaf = tok;
|
|
|
|
if (!rightmostLeaf || !rightmostLeaf->astOperand1())
|
2018-10-18 11:56:23 +02:00
|
|
|
return nullptr;
|
2018-11-10 16:40:40 +01:00
|
|
|
do {
|
|
|
|
if (rightmostLeaf->astOperand2())
|
|
|
|
rightmostLeaf = rightmostLeaf->astOperand2();
|
|
|
|
else
|
|
|
|
rightmostLeaf = rightmostLeaf->astOperand1();
|
|
|
|
} while (rightmostLeaf->astOperand1());
|
2018-11-10 21:30:01 +01:00
|
|
|
while (Token::Match(rightmostLeaf->next(), "]|)") && !hasToken(rightmostLeaf->next()->link(), rightmostLeaf->next(), tok))
|
2018-11-10 16:40:40 +01:00
|
|
|
rightmostLeaf = rightmostLeaf->next();
|
2018-11-10 21:30:01 +01:00
|
|
|
if (rightmostLeaf->str() == "{" && rightmostLeaf->link())
|
2018-11-10 16:40:40 +01:00
|
|
|
rightmostLeaf = rightmostLeaf->link();
|
|
|
|
return rightmostLeaf->next();
|
2018-10-18 11:56:23 +02:00
|
|
|
}
|
|
|
|
|
2019-08-15 21:14:27 +02:00
|
|
|
const Token* astParentSkipParens(const Token* tok)
|
|
|
|
{
|
|
|
|
return astParentSkipParens(const_cast<Token*>(tok));
|
|
|
|
}
|
|
|
|
Token* astParentSkipParens(Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
Token * parent = tok->astParent();
|
|
|
|
if (!Token::simpleMatch(parent, "("))
|
|
|
|
return parent;
|
|
|
|
if (parent->link() != nextAfterAstRightmostLeaf(tok))
|
|
|
|
return parent;
|
|
|
|
return astParentSkipParens(parent);
|
|
|
|
}
|
|
|
|
|
2018-08-07 09:32:16 +02:00
|
|
|
static const Token * getVariableInitExpression(const Variable * var)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!var || !var->declEndToken())
|
2018-08-07 09:32:16 +02:00
|
|
|
return nullptr;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (Token::Match(var->declEndToken(), "; %varid% =", var->declarationId()))
|
2018-08-07 09:32:16 +02:00
|
|
|
return var->declEndToken()->tokAt(2)->astOperand2();
|
|
|
|
return var->declEndToken()->astOperand2();
|
|
|
|
}
|
|
|
|
|
2018-08-07 18:06:51 +02:00
|
|
|
static bool isInLoopCondition(const Token * tok)
|
|
|
|
{
|
2018-08-07 09:32:16 +02:00
|
|
|
return Token::Match(tok->astTop()->previous(), "for|while (");
|
|
|
|
}
|
|
|
|
|
2018-09-07 20:16:38 +02:00
|
|
|
/// If tok2 comes after tok1
|
2018-11-11 16:43:54 +01:00
|
|
|
bool precedes(const Token * tok1, const Token * tok2)
|
2018-09-07 20:16:38 +02:00
|
|
|
{
|
2018-09-08 09:07:59 +02:00
|
|
|
if (!tok1)
|
2018-09-07 20:16:38 +02:00
|
|
|
return false;
|
2018-09-08 09:07:59 +02:00
|
|
|
if (!tok2)
|
2018-09-07 20:16:38 +02:00
|
|
|
return false;
|
2019-06-21 22:16:23 +02:00
|
|
|
return tok1->index() < tok2->index();
|
2018-09-07 20:16:38 +02:00
|
|
|
}
|
2018-08-07 09:32:16 +02:00
|
|
|
|
2019-09-11 19:25:09 +02:00
|
|
|
bool isAliasOf(const Token *tok, nonneg int varid)
|
|
|
|
{
|
|
|
|
if (tok->varId() == varid)
|
|
|
|
return false;
|
|
|
|
if (tok->varId() == 0)
|
|
|
|
return false;
|
|
|
|
for (const ValueFlow::Value &val : tok->values()) {
|
|
|
|
if (!val.isLocalLifetimeValue())
|
|
|
|
continue;
|
|
|
|
if (val.isInconclusive())
|
|
|
|
continue;
|
|
|
|
if (val.tokvalue->varId() == varid)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:01:21 +02:00
|
|
|
static bool isAliased(const Token *startTok, const Token *endTok, nonneg int varid)
|
2018-09-24 06:37:47 +02:00
|
|
|
{
|
2019-07-24 09:59:01 +02:00
|
|
|
if (!precedes(startTok, endTok))
|
|
|
|
return false;
|
2018-09-24 06:37:47 +02:00
|
|
|
for (const Token *tok = startTok; tok != endTok; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "= & %varid% ;", varid))
|
|
|
|
return true;
|
2019-09-11 19:25:09 +02:00
|
|
|
if (isAliasOf(tok, varid))
|
|
|
|
return true;
|
2018-09-24 06:37:47 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 09:59:01 +02:00
|
|
|
bool isAliased(const Variable *var)
|
|
|
|
{
|
|
|
|
if (!var)
|
|
|
|
return false;
|
|
|
|
if (!var->scope())
|
|
|
|
return false;
|
|
|
|
const Token *start = var->declEndToken();
|
|
|
|
if (!start)
|
|
|
|
return false;
|
|
|
|
return isAliased(start, var->scope()->bodyEnd, var->declarationId());
|
|
|
|
}
|
|
|
|
|
2019-09-30 21:04:43 +02:00
|
|
|
bool exprDependsOnThis(const Token* expr, nonneg int depth)
|
2018-10-07 18:30:29 +02:00
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return false;
|
2019-03-26 18:57:01 +01:00
|
|
|
if (depth >= 1000)
|
|
|
|
// Abort recursion to avoid stack overflow
|
|
|
|
return true;
|
|
|
|
++depth;
|
2018-10-07 18:30:29 +02:00
|
|
|
// calling nonstatic method?
|
|
|
|
if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct()) {
|
|
|
|
// is it a method of this?
|
2018-10-24 20:17:00 +02:00
|
|
|
const Scope *nestedIn = expr->scope()->functionOf;
|
|
|
|
if (nestedIn && nestedIn->function)
|
|
|
|
nestedIn = nestedIn->function->token->scope();
|
|
|
|
while (nestedIn && nestedIn != expr->function()->nestedIn) {
|
2018-10-07 18:30:29 +02:00
|
|
|
nestedIn = nestedIn->nestedIn;
|
2018-10-24 20:17:00 +02:00
|
|
|
}
|
2018-10-07 18:30:29 +02:00
|
|
|
return nestedIn == expr->function()->nestedIn;
|
2019-09-30 21:04:43 +02:00
|
|
|
} else if (Token::Match(expr, "%var%") && expr->variable()) {
|
|
|
|
const Variable* var = expr->variable();
|
|
|
|
return (var->isPrivate() || var->isPublic() || var->isProtected());
|
2018-10-07 18:30:29 +02:00
|
|
|
}
|
2019-09-30 21:04:43 +02:00
|
|
|
if (Token::simpleMatch(expr, "."))
|
|
|
|
return exprDependsOnThis(expr->astOperand1(), depth);
|
2019-03-26 18:57:01 +01:00
|
|
|
return exprDependsOnThis(expr->astOperand1(), depth) || exprDependsOnThis(expr->astOperand2(), depth);
|
2018-10-07 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 09:32:16 +02:00
|
|
|
/// This takes a token that refers to a variable and it will return the token
|
|
|
|
/// to the expression that the variable is assigned to. If its not valid to
|
|
|
|
/// make such substitution then it will return the original token.
|
2018-09-07 20:16:38 +02:00
|
|
|
static const Token * followVariableExpression(const Token * tok, bool cpp, const Token * end = nullptr)
|
2018-08-07 09:32:16 +02:00
|
|
|
{
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!tok)
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-07 20:16:38 +02:00
|
|
|
// Skip following variables that is across multiple files
|
2018-09-08 09:07:59 +02:00
|
|
|
if (end && end->fileIndex() != tok->fileIndex())
|
2018-09-07 20:16:38 +02:00
|
|
|
return tok;
|
2018-08-07 09:32:16 +02:00
|
|
|
// Skip array access
|
2018-08-07 18:06:51 +02:00
|
|
|
if (Token::Match(tok, "%var% ["))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
|
|
|
// Skip pointer indirection
|
2018-08-07 18:06:51 +02:00
|
|
|
if (tok->astParent() && tok->isUnaryOp("*"))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
|
|
|
// Skip following variables if it is used in an assignment
|
2018-09-13 09:19:15 +02:00
|
|
|
if (Token::Match(tok->next(), "%assign%"))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
|
|
|
const Variable * var = tok->variable();
|
|
|
|
const Token * varTok = getVariableInitExpression(var);
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!varTok)
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-10-07 18:30:29 +02:00
|
|
|
// Bailout. If variable value depends on value of "this".
|
2019-09-30 21:04:43 +02:00
|
|
|
if (exprDependsOnThis(varTok))
|
2018-10-07 18:30:29 +02:00
|
|
|
return tok;
|
2018-08-07 09:32:16 +02:00
|
|
|
// Skip array access
|
2018-08-07 18:06:51 +02:00
|
|
|
if (Token::simpleMatch(varTok, "["))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-03 19:51:48 +02:00
|
|
|
if (var->isVolatile())
|
|
|
|
return tok;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!var->isLocal() && !var->isConst())
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (var->isStatic() && !var->isConst())
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (var->isArgument())
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-07 20:16:38 +02:00
|
|
|
const Token * lastTok = precedes(tok, end) ? end : tok;
|
2018-08-07 09:32:16 +02:00
|
|
|
// If this is in a loop then check if variables are modified in the entire scope
|
2018-09-07 20:16:38 +02:00
|
|
|
const Token * endToken = (isInLoopCondition(tok) || isInLoopCondition(varTok) || var->scope() != tok->scope()) ? var->scope()->bodyEnd : lastTok;
|
2018-09-13 09:19:15 +02:00
|
|
|
if (!var->isConst() && (!precedes(varTok, endToken) || isVariableChanged(varTok, endToken, tok->varId(), false, nullptr, cpp)))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-24 06:37:47 +02:00
|
|
|
if (precedes(varTok, endToken) && isAliased(varTok, endToken, tok->varId()))
|
|
|
|
return tok;
|
2018-08-30 19:51:39 +02:00
|
|
|
// Start at beginning of initialization
|
2018-08-07 09:32:16 +02:00
|
|
|
const Token * startToken = varTok;
|
2018-08-07 18:06:51 +02:00
|
|
|
while (Token::Match(startToken, "%op%|.|(|{") && startToken->astOperand1())
|
2018-08-07 09:32:16 +02:00
|
|
|
startToken = startToken->astOperand1();
|
|
|
|
// Skip if the variable its referring to is modified
|
2018-08-07 18:06:51 +02:00
|
|
|
for (const Token * tok2 = startToken; tok2 != endToken; tok2 = tok2->next()) {
|
|
|
|
if (Token::simpleMatch(tok2, ";"))
|
2018-08-07 09:32:16 +02:00
|
|
|
break;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (tok2->astParent() && tok2->isUnaryOp("*"))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (tok2->tokType() == Token::eIncDecOp ||
|
|
|
|
tok2->isAssignmentOp() ||
|
2018-08-07 09:32:16 +02:00
|
|
|
Token::Match(tok2, "%name% .|[|++|--|%assign%")) {
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2018-08-07 18:06:51 +02:00
|
|
|
if (const Variable * var2 = tok2->variable()) {
|
2018-09-08 09:07:59 +02:00
|
|
|
if (!var2->scope())
|
2018-09-02 08:28:53 +02:00
|
|
|
return tok;
|
2018-08-07 09:32:16 +02:00
|
|
|
const Token * endToken2 = var2->scope() != tok->scope() ? var2->scope()->bodyEnd : endToken;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!var2->isLocal() && !var2->isConst() && !var2->isArgument())
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (var2->isStatic() && !var2->isConst())
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-13 09:19:15 +02:00
|
|
|
if (!var2->isConst() && (!precedes(tok2, endToken2) || isVariableChanged(tok2, endToken2, tok2->varId(), false, nullptr, cpp)))
|
2018-08-07 09:32:16 +02:00
|
|
|
return tok;
|
2018-09-24 06:37:47 +02:00
|
|
|
if (precedes(tok2, endToken2) && isAliased(tok2, endToken2, tok2->varId()))
|
|
|
|
return tok;
|
2018-09-08 09:07:59 +02:00
|
|
|
// Recognized as a variable but the declaration is unknown
|
|
|
|
} else if (tok2->varId() > 0) {
|
2018-09-05 18:07:01 +02:00
|
|
|
return tok;
|
2018-09-17 17:16:32 +02:00
|
|
|
} else if (tok2->tokType() == Token::eName && !Token::Match(tok2, "sizeof|decltype|typeof") && !tok2->function()) {
|
2018-09-12 17:30:18 +02:00
|
|
|
return tok;
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return varTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void followVariableExpressionError(const Token *tok1, const Token *tok2, ErrorPath* errors)
|
|
|
|
{
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!errors)
|
2018-08-07 09:32:16 +02:00
|
|
|
return;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!tok1)
|
2018-08-07 09:32:16 +02:00
|
|
|
return;
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!tok2)
|
2018-08-07 09:32:16 +02:00
|
|
|
return;
|
2018-08-18 07:32:30 +02:00
|
|
|
ErrorPathItem item = std::make_pair(tok2, "'" + tok1->str() + "' is assigned value '" + tok2->expressionString() + "' here.");
|
2018-08-18 15:45:50 +02:00
|
|
|
if (std::find(errors->begin(), errors->end(), item) != errors->end())
|
2018-08-18 07:32:30 +02:00
|
|
|
return;
|
|
|
|
errors->push_back(item);
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2018-09-28 08:38:24 +02:00
|
|
|
|
2019-09-05 15:15:58 +02:00
|
|
|
template<class Predicate, class F>
|
|
|
|
static void findTokenValue(const Token* const tok, Predicate pred, F f)
|
|
|
|
{
|
|
|
|
auto x = std::find_if(tok->values().begin(), tok->values().end(), pred);
|
|
|
|
if (x != tok->values().end())
|
|
|
|
f(*x);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
findTokenValue(tok1, std::mem_fn(&ValueFlow::Value::isKnown), [&](const ValueFlow::Value& v1) {
|
|
|
|
findTokenValue(tok2, std::mem_fn(&ValueFlow::Value::isKnown), [&](const ValueFlow::Value& v2) {
|
|
|
|
result = v1.equalValue(v2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
findTokenValue(tok1, std::mem_fn(&ValueFlow::Value::isKnown), [&](const ValueFlow::Value& v1) {
|
|
|
|
findTokenValue(tok2, std::mem_fn(&ValueFlow::Value::isKnown), [&](const ValueFlow::Value& v2) {
|
|
|
|
result = !v1.equalValue(v2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
|
2018-08-07 09:32:16 +02:00
|
|
|
{
|
2018-08-07 18:06:51 +02:00
|
|
|
if (tok1 == nullptr && tok2 == nullptr)
|
2015-08-03 09:20:50 +02:00
|
|
|
return true;
|
|
|
|
if (tok1 == nullptr || tok2 == nullptr)
|
|
|
|
return false;
|
|
|
|
if (cpp) {
|
|
|
|
if (tok1->str() == "." && tok1->astOperand1() && tok1->astOperand1()->str() == "this")
|
|
|
|
tok1 = tok1->astOperand2();
|
|
|
|
if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this")
|
|
|
|
tok2 = tok2->astOperand2();
|
|
|
|
}
|
2018-08-07 09:32:16 +02:00
|
|
|
// Skip double not
|
2018-07-15 14:45:33 +02:00
|
|
|
if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=")) {
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, macro, tok1->astOperand1()->astOperand1(), tok2, library, pure, followVar, errors);
|
2018-07-15 11:30:02 +02:00
|
|
|
}
|
2018-07-15 14:45:33 +02:00
|
|
|
if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=")) {
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, macro, tok1, tok2->astOperand1()->astOperand1(), library, pure, followVar, errors);
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2019-09-05 15:15:58 +02:00
|
|
|
if (tok1->str() != tok2->str() && isDifferentKnownValues(tok1, tok2))
|
|
|
|
return false;
|
2018-09-28 08:38:24 +02:00
|
|
|
// Follow variable
|
|
|
|
if (followVar && tok1->str() != tok2->str() && (Token::Match(tok1, "%var%") || Token::Match(tok2, "%var%"))) {
|
2018-09-07 20:16:38 +02:00
|
|
|
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
|
2018-08-07 09:32:16 +02:00
|
|
|
if (varTok1->str() == tok2->str()) {
|
|
|
|
followVariableExpressionError(tok1, varTok1, errors);
|
2018-12-28 12:59:05 +01:00
|
|
|
return isSameExpression(cpp, macro, varTok1, tok2, library, true, followVar, errors);
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2018-09-07 20:16:38 +02:00
|
|
|
const Token * varTok2 = followVariableExpression(tok2, cpp, tok1);
|
2018-08-07 18:06:51 +02:00
|
|
|
if (tok1->str() == varTok2->str()) {
|
2018-08-07 09:32:16 +02:00
|
|
|
followVariableExpressionError(tok2, varTok2, errors);
|
2018-12-28 12:59:05 +01:00
|
|
|
return isSameExpression(cpp, macro, tok1, varTok2, library, true, followVar, errors);
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2018-08-07 18:06:51 +02:00
|
|
|
if (varTok1->str() == varTok2->str()) {
|
2018-08-07 09:32:16 +02:00
|
|
|
followVariableExpressionError(tok1, varTok1, errors);
|
|
|
|
followVariableExpressionError(tok2, varTok2, errors);
|
2018-12-28 12:59:05 +01:00
|
|
|
return isSameExpression(cpp, macro, varTok1, varTok2, library, true, followVar, errors);
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2018-07-15 11:30:02 +02:00
|
|
|
}
|
2016-01-10 11:21:43 +01:00
|
|
|
if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str() || tok1->originalName() != tok2->originalName()) {
|
2015-08-03 09:20:50 +02:00
|
|
|
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||
|
|
|
|
(Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) {
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure, followVar, errors) &&
|
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-11 09:41:22 +01:00
|
|
|
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
2017-02-22 21:13:36 +01:00
|
|
|
if (tok1->isComplex() != tok2->isComplex())
|
|
|
|
return false;
|
|
|
|
if (tok1->isLong() != tok2->isLong())
|
|
|
|
return false;
|
|
|
|
if (tok1->isUnsigned() != tok2->isUnsigned())
|
|
|
|
return false;
|
|
|
|
if (tok1->isSigned() != tok2->isSigned())
|
|
|
|
return false;
|
2018-04-08 20:11:44 +02:00
|
|
|
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {
|
2018-07-08 15:58:04 +02:00
|
|
|
if (!tok1->function()) {
|
|
|
|
if (Token::simpleMatch(tok1->previous(), ".")) {
|
|
|
|
const Token *lhs = tok1->previous();
|
|
|
|
while (Token::Match(lhs, "(|.|["))
|
|
|
|
lhs = lhs->astOperand1();
|
2018-09-24 15:08:16 +02:00
|
|
|
const bool lhsIsConst = (lhs->variable() && lhs->variable()->isConst()) ||
|
|
|
|
(lhs->valueType() && lhs->valueType()->constness > 0) ||
|
|
|
|
(Token::Match(lhs, "%var% . %name% (") && library.isFunctionConst(lhs->tokAt(2)));
|
2018-07-08 15:58:04 +02:00
|
|
|
if (!lhsIsConst)
|
|
|
|
return false;
|
2018-11-07 06:49:07 +01:00
|
|
|
} else {
|
|
|
|
const Token * ftok = tok1;
|
|
|
|
if (Token::simpleMatch(tok1->previous(), "::"))
|
|
|
|
ftok = tok1->previous();
|
|
|
|
if (!library.isFunctionConst(ftok) && !ftok->isAttributeConst() && !ftok->isAttributePure())
|
|
|
|
return false;
|
2018-07-08 15:58:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tok1->function() && !tok1->function()->isConst() && !tok1->function()->isAttributeConst() && !tok1->function()->isAttributePure())
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
// templates/casts
|
|
|
|
if ((Token::Match(tok1, "%name% <") && tok1->next()->link()) ||
|
|
|
|
(Token::Match(tok2, "%name% <") && tok2->next()->link())) {
|
|
|
|
|
|
|
|
// non-const template function that is not a dynamic_cast => return false
|
2018-09-30 14:49:58 +02:00
|
|
|
if (pure && Token::simpleMatch(tok1->next()->link(), "> (") &&
|
2015-08-03 09:20:50 +02:00
|
|
|
!(tok1->function() && tok1->function()->isConst()) &&
|
|
|
|
tok1->str() != "dynamic_cast")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// some template/cast stuff.. check that the template arguments are same
|
|
|
|
const Token *t1 = tok1->next();
|
|
|
|
const Token *t2 = tok2->next();
|
|
|
|
const Token *end1 = t1->link();
|
|
|
|
const Token *end2 = t2->link();
|
|
|
|
while (t1 && t2 && t1 != end1 && t2 != end2) {
|
|
|
|
if (t1->str() != t2->str())
|
|
|
|
return false;
|
|
|
|
t1 = t1->next();
|
|
|
|
t2 = t2->next();
|
|
|
|
}
|
|
|
|
if (t1 != end1 || t2 != end2)
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok1->tokType() == Token::eIncDecOp || tok1->isAssignmentOp())
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
|
|
|
// bailout when we see ({..})
|
|
|
|
if (tok1->str() == "{")
|
|
|
|
return false;
|
2018-09-30 14:49:58 +02:00
|
|
|
// cast => assert that the casts are equal
|
2018-10-01 11:53:32 +02:00
|
|
|
if (tok1->str() == "(" && tok1->previous() &&
|
|
|
|
!tok1->previous()->isName() &&
|
2018-09-30 14:49:58 +02:00
|
|
|
!(tok1->previous()->str() == ">" && tok1->previous()->link())) {
|
2015-08-03 09:20:50 +02:00
|
|
|
const Token *t1 = tok1->next();
|
|
|
|
const Token *t2 = tok2->next();
|
2016-08-01 21:53:43 +02:00
|
|
|
while (t1 && t2 &&
|
|
|
|
t1->str() == t2->str() &&
|
|
|
|
t1->isLong() == t2->isLong() &&
|
|
|
|
t1->isUnsigned() == t2->isUnsigned() &&
|
|
|
|
t1->isSigned() == t2->isSigned() &&
|
|
|
|
(t1->isName() || t1->str() == "*")) {
|
2015-08-03 09:20:50 +02:00
|
|
|
t1 = t1->next();
|
|
|
|
t2 = t2->next();
|
|
|
|
}
|
|
|
|
if (!t1 || !t2 || t1->str() != ")" || t2->str() != ")")
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-01 13:33:56 +02:00
|
|
|
bool noncommutativeEquals =
|
2018-09-28 08:38:24 +02:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand1(), library, pure, followVar, errors);
|
2016-08-01 13:33:56 +02:00
|
|
|
noncommutativeEquals = noncommutativeEquals &&
|
2018-09-28 08:38:24 +02:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand2(), library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2016-08-01 13:33:56 +02:00
|
|
|
if (noncommutativeEquals)
|
2015-08-03 09:20:50 +02:00
|
|
|
return true;
|
|
|
|
|
2017-03-15 19:15:05 +01:00
|
|
|
// in c++, a+b might be different to b+a, depending on the type of a and b
|
2018-07-13 18:52:03 +02:00
|
|
|
if (cpp && tok1->str() == "+" && tok1->isBinaryOp()) {
|
2017-03-15 19:15:05 +01:00
|
|
|
const ValueType* vt1 = tok1->astOperand1()->valueType();
|
2017-08-30 21:43:54 +02:00
|
|
|
const ValueType* vt2 = tok1->astOperand2()->valueType();
|
2017-03-15 19:15:05 +01:00
|
|
|
if (!(vt1 && (vt1->type >= ValueType::VOID || vt1->pointer) && vt2 && (vt2->type >= ValueType::VOID || vt2->pointer)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:52:03 +02:00
|
|
|
const bool commutative = tok1->isBinaryOp() && Token::Match(tok1, "%or%|%oror%|+|*|&|&&|^|==|!=");
|
2016-08-01 13:33:56 +02:00
|
|
|
bool commutativeEquals = commutative &&
|
2018-09-28 08:38:24 +02:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure, followVar, errors);
|
2016-08-01 13:33:56 +02:00
|
|
|
commutativeEquals = commutativeEquals &&
|
2018-09-28 08:38:24 +02:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2015-10-03 14:56:24 +02:00
|
|
|
|
2016-08-01 13:33:56 +02:00
|
|
|
return commutativeEquals;
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-11 10:22:06 +02:00
|
|
|
static bool isZeroBoundCond(const Token * const cond)
|
|
|
|
{
|
2018-05-11 14:49:31 +02:00
|
|
|
if (cond == nullptr)
|
2018-05-11 10:22:06 +02:00
|
|
|
return false;
|
|
|
|
// Assume unsigned
|
|
|
|
// TODO: Handle reverse conditions
|
|
|
|
const bool isZero = cond->astOperand2()->getValue(0);
|
|
|
|
if (cond->str() == "==" || cond->str() == ">=")
|
|
|
|
return isZero;
|
|
|
|
if (cond->str() == "<=")
|
|
|
|
return true;
|
|
|
|
if (cond->str() == "<")
|
|
|
|
return !isZero;
|
|
|
|
if (cond->str() == ">")
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
|
|
|
if (!cond1 || !cond2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (cond1->str() == "!") {
|
|
|
|
if (cond2->str() == "!=") {
|
|
|
|
if (cond2->astOperand1() && cond2->astOperand1()->str() == "0")
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand2(), library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
if (cond2->astOperand2() && cond2->astOperand2()->str() == "0")
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2, library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cond2->str() == "!")
|
2018-10-04 21:17:47 +02:00
|
|
|
return isOppositeCond(isNot, cpp, cond2, cond1, library, pure, followVar, errors);
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2018-03-24 12:30:11 +01:00
|
|
|
if (!isNot) {
|
2018-03-24 07:58:37 +01:00
|
|
|
if (cond1->str() == "==" && cond2->str() == "==") {
|
2018-09-28 08:38:24 +02:00
|
|
|
if (isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors))
|
2018-03-24 07:58:37 +01:00
|
|
|
return isDifferentKnownValues(cond1->astOperand2(), cond2->astOperand2());
|
2018-09-28 08:38:24 +02:00
|
|
|
if (isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand2(), library, pure, followVar, errors))
|
2018-03-24 07:58:37 +01:00
|
|
|
return isDifferentKnownValues(cond1->astOperand1(), cond2->astOperand1());
|
|
|
|
}
|
2018-05-11 10:22:06 +02:00
|
|
|
// TODO: Handle reverse conditions
|
2019-07-17 11:39:30 +02:00
|
|
|
if (Library::isContainerYield(cond1, Library::Container::Yield::EMPTY, "empty") &&
|
|
|
|
Library::isContainerYield(cond2->astOperand1(), Library::Container::Yield::SIZE, "size") &&
|
2018-04-05 06:43:13 +02:00
|
|
|
cond1->astOperand1()->astOperand1()->varId() == cond2->astOperand1()->astOperand1()->astOperand1()->varId()) {
|
2018-05-11 10:22:06 +02:00
|
|
|
return !isZeroBoundCond(cond2);
|
2018-03-24 07:58:37 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 11:39:30 +02:00
|
|
|
if (Library::isContainerYield(cond2, Library::Container::Yield::EMPTY, "empty") &&
|
|
|
|
Library::isContainerYield(cond1->astOperand1(), Library::Container::Yield::SIZE, "size") &&
|
2018-04-05 06:43:13 +02:00
|
|
|
cond2->astOperand1()->astOperand1()->varId() == cond1->astOperand1()->astOperand1()->astOperand1()->varId()) {
|
2018-05-11 10:22:06 +02:00
|
|
|
return !isZeroBoundCond(cond1);
|
2018-03-24 07:58:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-04 22:54:06 +02:00
|
|
|
if (!cond1->isComparisonOp() || !cond2->isComparisonOp())
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string &comp1 = cond1->str();
|
|
|
|
|
|
|
|
// condition found .. get comparator
|
|
|
|
std::string comp2;
|
2018-10-01 14:40:03 +02:00
|
|
|
if (isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors) &&
|
|
|
|
isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand2(), library, pure, followVar, errors)) {
|
2015-08-03 09:20:50 +02:00
|
|
|
comp2 = cond2->str();
|
2018-10-01 14:40:03 +02:00
|
|
|
} else if (isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand2(), library, pure, followVar, errors) &&
|
|
|
|
isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand1(), library, pure, followVar, errors)) {
|
2015-08-03 09:20:50 +02:00
|
|
|
comp2 = cond2->str();
|
|
|
|
if (comp2[0] == '>')
|
|
|
|
comp2[0] = '<';
|
|
|
|
else if (comp2[0] == '<')
|
|
|
|
comp2[0] = '>';
|
|
|
|
}
|
|
|
|
|
2017-08-31 16:00:12 +02:00
|
|
|
if (!isNot && comp2.empty()) {
|
2017-09-22 14:01:20 +02:00
|
|
|
const Token *expr1 = nullptr, *value1 = nullptr, *expr2 = nullptr, *value2 = nullptr;
|
|
|
|
std::string op1 = cond1->str(), op2 = cond2->str();
|
|
|
|
if (cond1->astOperand2()->hasKnownIntValue()) {
|
|
|
|
expr1 = cond1->astOperand1();
|
|
|
|
value1 = cond1->astOperand2();
|
|
|
|
} else if (cond1->astOperand1()->hasKnownIntValue()) {
|
|
|
|
expr1 = cond1->astOperand2();
|
|
|
|
value1 = cond1->astOperand1();
|
|
|
|
if (op1[0] == '>')
|
|
|
|
op1[0] = '<';
|
|
|
|
else if (op1[0] == '<')
|
|
|
|
op1[0] = '>';
|
2017-08-31 16:00:12 +02:00
|
|
|
}
|
2017-09-22 14:01:20 +02:00
|
|
|
if (cond2->astOperand2()->hasKnownIntValue()) {
|
|
|
|
expr2 = cond2->astOperand1();
|
|
|
|
value2 = cond2->astOperand2();
|
|
|
|
} else if (cond2->astOperand1()->hasKnownIntValue()) {
|
|
|
|
expr2 = cond2->astOperand2();
|
|
|
|
value2 = cond2->astOperand1();
|
|
|
|
if (op2[0] == '>')
|
|
|
|
op2[0] = '<';
|
|
|
|
else if (op2[0] == '<')
|
|
|
|
op2[0] = '>';
|
|
|
|
}
|
|
|
|
if (!expr1 || !value1 || !expr2 || !value2) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-28 08:38:24 +02:00
|
|
|
if (!isSameExpression(cpp, true, expr1, expr2, library, pure, followVar, errors))
|
2017-09-22 14:01:20 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const ValueFlow::Value &rhsValue1 = value1->values().front();
|
|
|
|
const ValueFlow::Value &rhsValue2 = value2->values().front();
|
|
|
|
|
|
|
|
if (op1 == "<" || op1 == "<=")
|
|
|
|
return (op2 == "==" || op2 == ">" || op2 == ">=") && (rhsValue1.intvalue < rhsValue2.intvalue);
|
|
|
|
else if (op1 == ">=" || op1 == ">")
|
|
|
|
return (op2 == "==" || op2 == "<" || op2 == "<=") && (rhsValue1.intvalue > rhsValue2.intvalue);
|
|
|
|
|
|
|
|
return false;
|
2017-08-31 16:00:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
// is condition opposite?
|
|
|
|
return ((comp1 == "==" && comp2 == "!=") ||
|
|
|
|
(comp1 == "!=" && comp2 == "==") ||
|
|
|
|
(comp1 == "<" && comp2 == ">=") ||
|
|
|
|
(comp1 == "<=" && comp2 == ">") ||
|
|
|
|
(comp1 == ">" && comp2 == "<=") ||
|
|
|
|
(comp1 == ">=" && comp2 == "<") ||
|
|
|
|
(!isNot && ((comp1 == "<" && comp2 == ">") ||
|
2018-08-05 22:39:40 +02:00
|
|
|
(comp1 == ">" && comp2 == "<") ||
|
|
|
|
(comp1 == "==" && (comp2 == "!=" || comp2 == ">" || comp2 == "<")) ||
|
2018-08-05 22:40:21 +02:00
|
|
|
((comp1 == "!=" || comp1 == ">" || comp1 == "<") && comp2 == "==")
|
|
|
|
)));
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
|
2018-05-02 06:32:33 +02:00
|
|
|
{
|
|
|
|
if (!tok1 || !tok2)
|
|
|
|
return false;
|
2018-10-04 21:17:47 +02:00
|
|
|
if (isOppositeCond(true, cpp, tok1, tok2, library, pure, followVar, errors))
|
2018-05-02 06:32:33 +02:00
|
|
|
return true;
|
2018-07-13 18:52:03 +02:00
|
|
|
if (tok1->isUnaryOp("-"))
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, tok1->astOperand1(), tok2, library, pure, followVar, errors);
|
2018-07-13 18:52:03 +02:00
|
|
|
if (tok2->isUnaryOp("-"))
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, tok2->astOperand1(), tok1, library, pure, followVar, errors);
|
2018-05-02 06:32:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-21 07:09:20 +02:00
|
|
|
bool isConstExpression(const Token *tok, const Library& library, bool pure, bool cpp)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return true;
|
|
|
|
if (tok->isName() && tok->next()->str() == "(") {
|
2016-12-06 12:31:16 +01:00
|
|
|
if (!tok->function() && !Token::Match(tok->previous(), ".|::") && !library.isFunctionConst(tok->str(), pure))
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
|
|
|
else if (tok->function() && !tok->function()->isConst())
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 20:46:13 +02:00
|
|
|
if (tok->tokType() == Token::eIncDecOp)
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
2018-10-21 20:28:46 +02:00
|
|
|
if (tok->isAssignmentOp())
|
2018-10-21 07:09:20 +02:00
|
|
|
return false;
|
2018-10-21 20:28:46 +02:00
|
|
|
if (isLikelyStreamRead(cpp, tok))
|
2018-10-21 07:09:20 +02:00
|
|
|
return false;
|
2015-08-03 09:20:50 +02:00
|
|
|
// bailout when we see ({..})
|
|
|
|
if (tok->str() == "{")
|
|
|
|
return false;
|
2018-10-21 07:09:20 +02:00
|
|
|
return isConstExpression(tok->astOperand1(), library, pure, cpp) && isConstExpression(tok->astOperand2(), library, pure, cpp);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isWithoutSideEffects(bool cpp, const Token* tok)
|
|
|
|
{
|
|
|
|
if (!cpp)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (tok && tok->astOperand2() && tok->astOperand2()->str() != "(")
|
|
|
|
tok = tok->astOperand2();
|
|
|
|
if (tok && tok->varId()) {
|
|
|
|
const Variable* var = tok->variable();
|
|
|
|
return var && (!var->isClass() || var->isPointer() || var->isStlType());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-13 20:20:55 +02:00
|
|
|
bool isUniqueExpression(const Token* tok)
|
|
|
|
{
|
2018-05-14 10:15:50 +02:00
|
|
|
if (!tok)
|
2018-05-13 20:20:55 +02:00
|
|
|
return true;
|
2018-05-14 10:15:50 +02:00
|
|
|
if (tok->function()) {
|
2018-05-13 20:20:55 +02:00
|
|
|
const Function * fun = tok->function();
|
|
|
|
const Scope * scope = fun->nestedIn;
|
2018-05-14 10:15:50 +02:00
|
|
|
if (!scope)
|
2018-05-13 20:20:55 +02:00
|
|
|
return true;
|
2018-06-14 22:39:26 +02:00
|
|
|
const std::string returnType = fun->retType ? fun->retType->name() : fun->retDef->stringifyList(fun->tokenDef);
|
2018-05-14 10:15:50 +02:00
|
|
|
for (const Function& f:scope->functionList) {
|
2018-06-14 22:39:26 +02:00
|
|
|
if (f.type != Function::eFunction)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::string freturnType = f.retType ? f.retType->name() : f.retDef->stringifyList(f.tokenDef);
|
2018-06-09 08:05:19 +02:00
|
|
|
if (f.argumentList.size() == fun->argumentList.size() &&
|
|
|
|
returnType == freturnType &&
|
|
|
|
f.name() != fun->name()) {
|
2018-05-13 20:20:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 10:15:50 +02:00
|
|
|
} else if (tok->variable()) {
|
2018-05-13 20:20:55 +02:00
|
|
|
const Variable * var = tok->variable();
|
|
|
|
const Scope * scope = var->scope();
|
2018-05-14 10:15:50 +02:00
|
|
|
if (!scope)
|
2018-05-13 20:20:55 +02:00
|
|
|
return true;
|
|
|
|
const Type * varType = var->type();
|
|
|
|
// Iterate over the variables in scope and the parameters of the function if possible
|
|
|
|
const Function * fun = scope->function;
|
|
|
|
const std::list<Variable>* setOfVars[] = {&scope->varlist, fun ? &fun->argumentList : nullptr};
|
2019-04-28 07:40:00 +02:00
|
|
|
|
|
|
|
for (const std::list<Variable>* vars:setOfVars) {
|
|
|
|
if (!vars)
|
|
|
|
continue;
|
|
|
|
bool other = std::any_of(vars->cbegin(), vars->cend(), [=](const Variable &v) {
|
|
|
|
if (varType)
|
|
|
|
return v.type() && v.type()->name() == varType->name() && v.name() != var->name();
|
|
|
|
return v.isFloatingType() == var->isFloatingType() &&
|
|
|
|
v.isEnumType() == var->isEnumType() &&
|
|
|
|
v.isClass() == var->isClass() &&
|
|
|
|
v.isArray() == var->isArray() &&
|
|
|
|
v.isPointer() == var->isPointer() &&
|
|
|
|
v.name() != var->name();
|
|
|
|
});
|
|
|
|
if (other)
|
|
|
|
return false;
|
2018-05-13 20:20:55 +02:00
|
|
|
}
|
2018-05-14 10:15:50 +02:00
|
|
|
} else if (!isUniqueExpression(tok->astOperand1())) {
|
2018-05-13 20:20:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isUniqueExpression(tok->astOperand2());
|
|
|
|
}
|
|
|
|
|
2019-08-08 07:46:47 +02:00
|
|
|
static bool isEscaped(const Token* tok, bool functionsScope)
|
|
|
|
{
|
|
|
|
if (functionsScope)
|
|
|
|
return Token::simpleMatch(tok, "throw");
|
|
|
|
else
|
|
|
|
return Token::Match(tok, "return|throw");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEscapedOrJump(const Token* tok, bool functionsScope)
|
|
|
|
{
|
|
|
|
if (functionsScope)
|
|
|
|
return Token::simpleMatch(tok, "throw");
|
|
|
|
else
|
|
|
|
return Token::Match(tok, "return|goto|throw|continue|break");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReturnScope(const Token * const endToken, const Settings * settings, bool functionScope)
|
2016-01-16 18:52:34 +01:00
|
|
|
{
|
|
|
|
if (!endToken || endToken->str() != "}")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Token *prev = endToken->previous();
|
2016-01-17 12:38:49 +01:00
|
|
|
while (prev && Token::simpleMatch(prev->previous(), "; ;"))
|
|
|
|
prev = prev->previous();
|
2016-01-16 18:52:34 +01:00
|
|
|
if (prev && Token::simpleMatch(prev->previous(), "} ;"))
|
|
|
|
prev = prev->previous();
|
|
|
|
|
|
|
|
if (Token::simpleMatch(prev, "}")) {
|
|
|
|
if (Token::simpleMatch(prev->link()->tokAt(-2), "} else {"))
|
2019-08-08 07:46:47 +02:00
|
|
|
return isReturnScope(prev, settings, functionScope) && isReturnScope(prev->link()->tokAt(-2), settings, functionScope);
|
2016-01-16 18:52:34 +01:00
|
|
|
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
|
|
|
|
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
|
|
|
|
!Token::findsimplematch(prev->link(), "break", prev)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-08 07:46:47 +02:00
|
|
|
if (isEscaped(prev->link()->astTop(), functionScope))
|
2016-01-17 12:38:49 +01:00
|
|
|
return true;
|
2016-01-18 15:39:20 +01:00
|
|
|
if (Token::Match(prev->link()->previous(), "[;{}] {"))
|
2019-08-08 07:46:47 +02:00
|
|
|
return isReturnScope(prev, settings, functionScope);
|
2016-01-16 18:52:34 +01:00
|
|
|
} else if (Token::simpleMatch(prev, ";")) {
|
2019-08-08 07:46:47 +02:00
|
|
|
if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% (")) {
|
|
|
|
const Token * ftok = prev->linkAt(-1)->previous();
|
|
|
|
const Function * function = ftok->function();
|
|
|
|
if (function) {
|
|
|
|
if (function->isEscapeFunction())
|
|
|
|
return true;
|
|
|
|
if (function->isAttributeNoreturn())
|
|
|
|
return true;
|
|
|
|
} else if (settings) {
|
|
|
|
if (settings->library.isnoreturn(ftok))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-14 18:31:10 +01:00
|
|
|
if (Token::simpleMatch(prev->previous(), ") ;") && prev->previous()->link() &&
|
2019-08-08 07:46:47 +02:00
|
|
|
isEscaped(prev->previous()->link()->astTop(), functionScope))
|
2018-12-14 18:31:10 +01:00
|
|
|
return true;
|
2019-08-08 07:46:47 +02:00
|
|
|
if (isEscaped(prev->previous()->astTop(), functionScope))
|
2018-12-14 18:31:10 +01:00
|
|
|
return true;
|
2016-01-16 18:52:34 +01:00
|
|
|
// return/goto statement
|
|
|
|
prev = prev->previous();
|
2019-08-08 07:46:47 +02:00
|
|
|
while (prev && !Token::Match(prev, ";|{|}") && !isEscapedOrJump(prev, functionScope))
|
2016-01-16 18:52:34 +01:00
|
|
|
prev = prev->previous();
|
|
|
|
return prev && prev->isName();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:32:45 +02:00
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, int indirect, nonneg int varid, const Settings *settings, bool *inconclusive)
|
2017-11-18 11:02:52 +01:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->varId() == varid)
|
2019-08-30 18:32:45 +02:00
|
|
|
return isVariableChangedByFunctionCall(tok, indirect, settings, inconclusive);
|
|
|
|
return isVariableChangedByFunctionCall(tok->astOperand1(), indirect, varid, settings, inconclusive) ||
|
|
|
|
isVariableChangedByFunctionCall(tok->astOperand2(), indirect, varid, settings, inconclusive);
|
2017-11-18 11:02:52 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 19:41:35 +02:00
|
|
|
bool isScopeBracket(const Token* tok)
|
2019-07-24 09:59:01 +02:00
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "{|}"))
|
|
|
|
return false;
|
|
|
|
if (!tok->scope())
|
|
|
|
return false;
|
|
|
|
if (tok->str() == "{")
|
|
|
|
return tok->scope()->bodyStart == tok;
|
|
|
|
if (tok->str() == "}")
|
|
|
|
return tok->scope()->bodyEnd == tok;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:18:45 +02:00
|
|
|
const Token * getTokenArgumentFunction(const Token * tok, int& argn)
|
2016-10-23 13:54:44 +02:00
|
|
|
{
|
2019-09-06 21:18:45 +02:00
|
|
|
argn = -1;
|
2019-02-28 09:52:52 +01:00
|
|
|
{
|
2017-11-18 11:02:52 +01:00
|
|
|
const Token *parent = tok->astParent();
|
2019-02-28 09:52:52 +01:00
|
|
|
if (parent && parent->isUnaryOp("&"))
|
2017-11-18 11:02:52 +01:00
|
|
|
parent = parent->astParent();
|
2019-02-28 09:52:52 +01:00
|
|
|
while (parent && parent->isCast())
|
2017-11-18 11:02:52 +01:00
|
|
|
parent = parent->astParent();
|
2016-10-23 13:54:44 +02:00
|
|
|
|
2019-02-28 09:52:52 +01:00
|
|
|
// passing variable to subfunction?
|
2019-07-24 09:59:01 +02:00
|
|
|
if (Token::Match(parent, "[(,{]"))
|
2019-02-28 09:52:52 +01:00
|
|
|
;
|
|
|
|
else if (Token::simpleMatch(parent, ":")) {
|
|
|
|
while (Token::Match(parent, "[?:]"))
|
|
|
|
parent = parent->astParent();
|
|
|
|
while (Token::simpleMatch(parent, ","))
|
|
|
|
parent = parent->astParent();
|
|
|
|
if (!parent || parent->str() != "(")
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
2019-02-28 09:52:52 +01:00
|
|
|
} else
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
2019-02-28 09:52:52 +01:00
|
|
|
}
|
2016-10-23 13:54:44 +02:00
|
|
|
|
2019-09-06 21:18:45 +02:00
|
|
|
// goto start of function call and get argn
|
|
|
|
argn = 0;
|
2019-07-24 09:59:01 +02:00
|
|
|
while (tok && !Token::simpleMatch(tok, ";") && !isScopeBracket(tok)) {
|
2016-10-23 13:54:44 +02:00
|
|
|
if (tok->str() == ",")
|
2019-09-06 21:18:45 +02:00
|
|
|
++argn;
|
2019-09-05 19:36:45 +02:00
|
|
|
else if (Token::Match(tok, ")|}"))
|
2016-10-23 13:54:44 +02:00
|
|
|
tok = tok->link();
|
2019-07-24 09:59:01 +02:00
|
|
|
else if (Token::Match(tok->previous(), "%name% (|{"))
|
2019-02-28 09:52:52 +01:00
|
|
|
break;
|
2019-07-24 09:59:01 +02:00
|
|
|
else if (Token::Match(tok->previous(), "> (|{") && tok->previous()->link())
|
2019-02-28 17:02:46 +01:00
|
|
|
break;
|
2016-10-23 13:54:44 +02:00
|
|
|
tok = tok->previous();
|
|
|
|
}
|
2019-07-24 09:59:01 +02:00
|
|
|
if (!Token::Match(tok, "{|("))
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
2019-02-28 09:52:52 +01:00
|
|
|
tok = tok->previous();
|
2016-10-23 13:54:44 +02:00
|
|
|
if (tok && tok->link() && tok->str() == ">")
|
|
|
|
tok = tok->link()->previous();
|
2019-07-24 09:59:01 +02:00
|
|
|
if (!Token::Match(tok, "%name% [({<]"))
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Token * const tok1 = tok;
|
|
|
|
|
|
|
|
// address of variable
|
|
|
|
const bool addressOf = tok->astParent() && tok->astParent()->isUnaryOp("&");
|
|
|
|
|
|
|
|
int argnr;
|
|
|
|
tok = getTokenArgumentFunction(tok, argnr);
|
|
|
|
if (!tok)
|
2016-10-23 13:54:44 +02:00
|
|
|
return false; // not a function => variable not changed
|
2019-09-06 21:18:45 +02:00
|
|
|
const Token * parenTok = tok->next();
|
|
|
|
if (Token::simpleMatch(parenTok, "<") && parenTok->link())
|
|
|
|
parenTok = parenTok->link()->next();
|
|
|
|
const bool possiblyPassedByReference = (parenTok->next() == tok1 || Token::Match(tok1->previous(), ", %name% [,)}]"));
|
2016-10-23 13:54:44 +02:00
|
|
|
|
2017-09-20 12:53:25 +02:00
|
|
|
// Constructor call
|
|
|
|
if (tok->variable() && tok->variable()->nameToken() == tok) {
|
|
|
|
// Find constructor..
|
2019-07-13 16:13:21 +02:00
|
|
|
const int argCount = numberOfArguments(tok);
|
2018-04-05 08:07:22 +02:00
|
|
|
const Scope *typeScope = tok->variable()->typeScope();
|
2017-09-20 12:53:25 +02:00
|
|
|
if (typeScope) {
|
2018-10-25 06:14:27 +02:00
|
|
|
for (const Function &function : typeScope->functionList) {
|
|
|
|
if (!function.isConstructor() || function.argCount() < argCount)
|
2017-09-20 12:53:25 +02:00
|
|
|
continue;
|
2018-10-25 06:14:27 +02:00
|
|
|
const Variable *arg = function.getArgumentVar(argnr);
|
2017-09-20 14:03:56 +02:00
|
|
|
if (arg && arg->isReference() && !arg->isConst())
|
2017-09-20 12:53:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (inconclusive)
|
|
|
|
*inconclusive = true;
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-23 13:54:44 +02:00
|
|
|
|
|
|
|
if (!tok->function()) {
|
2019-03-01 15:47:08 +01:00
|
|
|
// Check if direction (in, out, inout) is specified in the library configuration and use that
|
|
|
|
if (!addressOf && settings) {
|
|
|
|
const Library::ArgumentChecks::Direction argDirection = settings->library.getArgDirection(tok, 1 + argnr);
|
|
|
|
if (argDirection == Library::ArgumentChecks::Direction::DIR_IN)
|
|
|
|
return false;
|
|
|
|
else if (argDirection == Library::ArgumentChecks::Direction::DIR_OUT ||
|
2019-03-04 22:57:40 +01:00
|
|
|
argDirection == Library::ArgumentChecks::Direction::DIR_INOUT) {
|
|
|
|
// With out or inout the direction of the content is specified, not a pointer itself, so ignore pointers for now
|
|
|
|
const ValueType * const valueType = tok1->valueType();
|
2019-09-10 19:40:08 +02:00
|
|
|
if (valueType && valueType->pointer == indirect) {
|
2019-03-04 22:57:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 15:47:08 +01:00
|
|
|
}
|
|
|
|
|
2016-10-23 13:54:44 +02:00
|
|
|
// if the library says 0 is invalid
|
|
|
|
// => it is assumed that parameter is an in parameter (TODO: this is a bad heuristic)
|
2018-08-07 09:32:16 +02:00
|
|
|
if (!addressOf && settings && settings->library.isnullargbad(tok, 1+argnr))
|
2016-10-23 13:54:44 +02:00
|
|
|
return false;
|
2019-02-28 17:02:46 +01:00
|
|
|
// possible pass-by-reference => inconclusive
|
|
|
|
if (possiblyPassedByReference) {
|
2016-10-23 13:54:44 +02:00
|
|
|
if (inconclusive != nullptr)
|
|
|
|
*inconclusive = true;
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-28 17:02:46 +01:00
|
|
|
// Safe guess: Assume that parameter is changed by function call
|
2016-10-23 13:54:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Variable *arg = tok->function()->getArgumentVar(argnr);
|
|
|
|
|
2019-08-30 18:32:45 +02:00
|
|
|
if (addressOf || (indirect > 0 && arg && arg->isPointer())) {
|
2018-08-07 18:06:51 +02:00
|
|
|
if (!(arg && arg->isConst()))
|
2018-08-07 09:32:16 +02:00
|
|
|
return true;
|
|
|
|
// If const is applied to the pointer, then the value can still be modified
|
2018-08-07 18:06:51 +02:00
|
|
|
if (arg && Token::simpleMatch(arg->typeEndToken(), "* const"))
|
2018-08-07 09:32:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-23 13:54:44 +02:00
|
|
|
|
|
|
|
return arg && !arg->isConst() && arg->isReference();
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:32:45 +02:00
|
|
|
bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, bool cpp, int depth)
|
2015-11-11 13:45:28 +01:00
|
|
|
{
|
2019-08-15 10:44:55 +02:00
|
|
|
if (!tok)
|
2019-07-24 09:59:01 +02:00
|
|
|
return false;
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *tok2 = tok;
|
|
|
|
while (Token::simpleMatch(tok2->astParent(), "*") || (Token::simpleMatch(tok2->astParent(), ".") && !Token::simpleMatch(tok2->astParent()->astParent(), "(")) ||
|
|
|
|
(Token::simpleMatch(tok2->astParent(), "[") && tok2 == tok2->astParent()->astOperand1()))
|
|
|
|
tok2 = tok2->astParent();
|
|
|
|
|
2019-09-13 08:33:30 +02:00
|
|
|
while (Token::simpleMatch(tok2->astParent(), "?") || (Token::simpleMatch(tok2->astParent(), ":") && Token::simpleMatch(tok2->astParent()->astParent(), "?")))
|
|
|
|
tok2 = tok2->astParent();
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
if (Token::Match(tok2->astParent(), "++|--"))
|
2019-07-26 07:03:21 +02:00
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
|
|
|
|
if (tok2->astParent() && tok2->astParent()->isAssignmentOp()) {
|
|
|
|
if (tok2 == tok2->astParent()->astOperand1())
|
|
|
|
return true;
|
|
|
|
// Check if assigning to a non-const lvalue
|
|
|
|
const Variable * var = getLHSVariable(tok2->astParent());
|
|
|
|
if (var && var->isReference() && !var->isConst() && var->nameToken() && var->nameToken()->next() == tok2->astParent()) {
|
|
|
|
if (!var->isLocal() || isVariableChanged(var, settings, cpp, depth - 1))
|
2017-07-09 12:50:17 +02:00
|
|
|
return true;
|
2017-07-09 12:36:33 +02:00
|
|
|
}
|
2019-08-15 10:44:55 +02:00
|
|
|
}
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
if (isLikelyStreamRead(cpp, tok->previous()))
|
|
|
|
return true;
|
2019-03-01 13:23:03 +01:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
if (isLikelyStream(cpp, tok2))
|
|
|
|
return true;
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
// Member function call
|
|
|
|
if (tok->variable() && Token::Match(tok2->astParent(), ". %name%") && isFunctionCall(tok2->astParent()->next()) && tok2->astParent()->astOperand1() == tok2) {
|
|
|
|
const Variable * var = tok->variable();
|
|
|
|
bool isConst = var && var->isConst();
|
|
|
|
if (!isConst && var) {
|
|
|
|
const ValueType * valueType = var->valueType();
|
|
|
|
isConst = (valueType && valueType->pointer == 1 && valueType->constness == 1);
|
2019-07-24 09:59:01 +02:00
|
|
|
}
|
2017-04-23 18:05:14 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *ftok = tok->tokAt(2);
|
|
|
|
const Function * fun = ftok->function();
|
|
|
|
if (!isConst && (!fun || !fun->isConst()))
|
2018-04-18 17:46:31 +02:00
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-02 21:53:51 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *ftok = tok2;
|
|
|
|
while (ftok && (!Token::Match(ftok, "[({]") || ftok->isCast()))
|
|
|
|
ftok = ftok->astParent();
|
|
|
|
|
|
|
|
if (ftok && Token::Match(ftok->link(), ")|} !!{")) {
|
|
|
|
const Token * ptok = tok2;
|
|
|
|
while (Token::Match(ptok->astParent(), ".|::|["))
|
|
|
|
ptok = ptok->astParent();
|
|
|
|
bool inconclusive = false;
|
2019-08-30 18:32:45 +02:00
|
|
|
bool isChanged = isVariableChangedByFunctionCall(ptok, indirect, settings, &inconclusive);
|
2019-08-15 10:44:55 +02:00
|
|
|
isChanged |= inconclusive;
|
|
|
|
if (isChanged)
|
2019-07-24 09:59:01 +02:00
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
}
|
2019-07-24 09:59:01 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *parent = tok2->astParent();
|
|
|
|
while (Token::Match(parent, ".|::"))
|
|
|
|
parent = parent->astParent();
|
|
|
|
if (parent && parent->tokType() == Token::eIncDecOp)
|
|
|
|
return true;
|
2017-07-09 12:36:33 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
if (Token::simpleMatch(tok2->astParent(), ":") && tok2->astParent()->astParent() && Token::simpleMatch(tok2->astParent()->astParent()->previous(), "for (")) {
|
|
|
|
const Token * varTok = tok2->astParent()->previous();
|
|
|
|
if (!varTok)
|
|
|
|
return false;
|
|
|
|
const Variable * loopVar = varTok->variable();
|
|
|
|
if (!loopVar)
|
|
|
|
return false;
|
|
|
|
if (!loopVar->isConst() && loopVar->isReference() && isVariableChanged(loopVar, settings, cpp, depth - 1))
|
2017-07-09 12:36:33 +02:00
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-24 09:59:01 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
bool isVariableChanged(const Token *start, const Token *end, const nonneg int varid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2019-08-16 07:48:54 +02:00
|
|
|
{
|
2019-08-30 18:32:45 +02:00
|
|
|
return findVariableChanged(start, end, 0, varid, globalvar, settings, cpp, depth) != nullptr;
|
2019-08-16 07:48:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 18:32:45 +02:00
|
|
|
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int varid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2019-08-15 10:44:55 +02:00
|
|
|
{
|
|
|
|
if (!precedes(start, end))
|
2019-08-16 07:48:54 +02:00
|
|
|
return nullptr;
|
2019-08-15 10:44:55 +02:00
|
|
|
if (depth < 0)
|
2019-08-16 07:48:54 +02:00
|
|
|
return start;
|
|
|
|
for (Token *tok = start; tok != end; tok = tok->next()) {
|
2019-08-15 10:44:55 +02:00
|
|
|
if (tok->varId() != varid) {
|
|
|
|
if (globalvar && Token::Match(tok, "%name% ("))
|
|
|
|
// TODO: Is global variable really changed by function call?
|
2019-08-16 07:48:54 +02:00
|
|
|
return tok;
|
2019-07-24 09:59:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-08-30 18:32:45 +02:00
|
|
|
if (isVariableChanged(tok, indirect, settings, cpp, depth))
|
2019-08-16 07:48:54 +02:00
|
|
|
return tok;
|
2015-11-11 13:45:28 +01:00
|
|
|
}
|
2019-08-16 07:48:54 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-30 18:32:45 +02:00
|
|
|
const Token* findVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int varid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2019-08-16 07:48:54 +02:00
|
|
|
{
|
2019-08-30 18:32:45 +02:00
|
|
|
return findVariableChanged(const_cast<Token*>(start), end, indirect, varid, globalvar, settings, cpp, depth);
|
2015-11-11 13:45:28 +01:00
|
|
|
}
|
2015-12-06 12:50:05 +01:00
|
|
|
|
2019-07-26 07:03:21 +02:00
|
|
|
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp, int depth)
|
2018-10-18 21:01:47 +02:00
|
|
|
{
|
2018-10-20 09:28:28 +02:00
|
|
|
if (!var)
|
2018-10-18 21:01:47 +02:00
|
|
|
return false;
|
2018-10-20 09:28:28 +02:00
|
|
|
if (!var->scope())
|
2018-10-18 21:01:47 +02:00
|
|
|
return false;
|
|
|
|
const Token * start = var->declEndToken();
|
2018-10-20 09:28:28 +02:00
|
|
|
if (!start)
|
2018-10-18 21:01:47 +02:00
|
|
|
return false;
|
2018-10-20 09:28:28 +02:00
|
|
|
if (Token::Match(start, "; %varid% =", var->declarationId()))
|
2018-10-18 21:01:47 +02:00
|
|
|
start = start->tokAt(2);
|
2019-07-26 07:03:21 +02:00
|
|
|
return isVariableChanged(start->next(), var->scope()->bodyEnd, var->declarationId(), var->isGlobal(), settings, cpp, depth);
|
2018-10-18 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:32:25 +02:00
|
|
|
bool isVariablesChanged(const Token* start,
|
|
|
|
const Token* end,
|
|
|
|
int indirect,
|
|
|
|
std::vector<const Variable*> vars,
|
|
|
|
const Settings* settings,
|
|
|
|
bool cpp)
|
|
|
|
{
|
|
|
|
std::set<int> varids;
|
|
|
|
std::transform(vars.begin(), vars.end(), std::inserter(varids, varids.begin()), [](const Variable* var) {
|
|
|
|
return var->declarationId();
|
|
|
|
});
|
2019-09-26 10:32:49 +02:00
|
|
|
const bool globalvar = std::any_of(vars.begin(), vars.end(), [](const Variable* var) {
|
|
|
|
return var->isGlobal();
|
|
|
|
});
|
2019-09-26 10:32:25 +02:00
|
|
|
for (const Token* tok = start; tok != end; tok = tok->next()) {
|
|
|
|
if (tok->varId() == 0 || varids.count(tok->varId()) == 0) {
|
|
|
|
if (globalvar && Token::Match(tok, "%name% ("))
|
|
|
|
// TODO: Is global variable really changed by function call?
|
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isVariableChanged(tok, indirect, settings, cpp))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-06 12:50:05 +01:00
|
|
|
int numberOfArguments(const Token *start)
|
|
|
|
{
|
|
|
|
int arguments=0;
|
|
|
|
const Token* const openBracket = start->next();
|
|
|
|
if (openBracket && openBracket->str()=="(" && openBracket->next() && openBracket->next()->str()!=")") {
|
|
|
|
const Token* argument=openBracket->next();
|
|
|
|
while (argument) {
|
|
|
|
++arguments;
|
|
|
|
argument = argument->nextArgument();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arguments;
|
|
|
|
}
|
2017-04-20 19:57:39 +02:00
|
|
|
|
|
|
|
std::vector<const Token *> getArguments(const Token *ftok)
|
|
|
|
{
|
2019-09-10 19:41:35 +02:00
|
|
|
const Token* tok = ftok;
|
|
|
|
if (Token::Match(tok, "%name% (|{"))
|
|
|
|
tok = ftok->next();
|
|
|
|
if (!Token::Match(tok, "(|{|["))
|
2019-09-10 19:42:17 +02:00
|
|
|
return std::vector<const Token *> {};
|
2019-03-19 06:25:10 +01:00
|
|
|
const Token *startTok = tok->astOperand2();
|
2019-09-10 19:41:35 +02:00
|
|
|
if (!startTok && tok->next() != tok->link())
|
2019-03-19 06:25:10 +01:00
|
|
|
startTok = tok->astOperand1();
|
2019-09-10 19:41:35 +02:00
|
|
|
return astFlatten(startTok, ",");
|
2017-04-20 19:57:39 +02:00
|
|
|
}
|
2017-08-29 22:35:55 +02:00
|
|
|
|
2019-04-15 06:37:27 +02:00
|
|
|
const Token *findLambdaStartToken(const Token *last)
|
|
|
|
{
|
|
|
|
if (!last || last->str() != "}")
|
|
|
|
return nullptr;
|
|
|
|
const Token* tok = last->link();
|
|
|
|
if (Token::simpleMatch(tok->astParent(), "("))
|
|
|
|
tok = tok->astParent();
|
|
|
|
if (Token::simpleMatch(tok->astParent(), "["))
|
|
|
|
return tok->astParent();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:35:55 +02:00
|
|
|
const Token *findLambdaEndToken(const Token *first)
|
|
|
|
{
|
|
|
|
if (!first || first->str() != "[")
|
|
|
|
return nullptr;
|
2018-11-10 21:30:01 +01:00
|
|
|
if (!Token::Match(first->link(), "] (|{"))
|
2018-11-10 16:40:40 +01:00
|
|
|
return nullptr;
|
2018-11-10 21:30:01 +01:00
|
|
|
if (first->astOperand1() != first->link()->next())
|
2018-11-10 16:40:40 +01:00
|
|
|
return nullptr;
|
2018-10-31 12:36:08 +01:00
|
|
|
const Token * tok = first;
|
|
|
|
|
|
|
|
if (tok->astOperand1() && tok->astOperand1()->str() == "(")
|
|
|
|
tok = tok->astOperand1();
|
|
|
|
if (tok->astOperand1() && tok->astOperand1()->str() == "{")
|
|
|
|
return tok->astOperand1()->link();
|
2017-08-29 22:35:55 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-17 14:23:04 +02:00
|
|
|
|
2019-07-24 09:59:01 +02:00
|
|
|
bool isLikelyStream(bool cpp, const Token *stream)
|
|
|
|
{
|
|
|
|
if (!cpp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Token::Match(stream->astParent(), "&|<<|>>") || !stream->astParent()->isBinaryOp())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (stream->astParent()->astOperand1() != stream)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !astIsIntegral(stream, false);
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:23:04 +02:00
|
|
|
bool isLikelyStreamRead(bool cpp, const Token *op)
|
|
|
|
{
|
|
|
|
if (!cpp)
|
|
|
|
return false;
|
|
|
|
|
2018-07-13 18:52:03 +02:00
|
|
|
if (!Token::Match(op, "&|>>") || !op->isBinaryOp())
|
2018-04-18 16:13:24 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!Token::Match(op->astOperand2(), "%name%|.|*|[") && op->str() != op->astOperand2()->str())
|
2018-04-17 20:34:31 +02:00
|
|
|
return false;
|
2018-04-17 14:23:04 +02:00
|
|
|
|
2018-04-17 20:34:31 +02:00
|
|
|
const Token *parent = op;
|
|
|
|
while (parent->astParent() && parent->astParent()->str() == op->str())
|
|
|
|
parent = parent->astParent();
|
2018-04-18 16:13:24 +02:00
|
|
|
if (parent->astParent() && !Token::Match(parent->astParent(), "%oror%|&&|(|,|!"))
|
2018-04-17 20:34:31 +02:00
|
|
|
return false;
|
2018-04-22 07:30:45 +02:00
|
|
|
if (op->str() == "&" && parent->astParent())
|
|
|
|
return false;
|
2018-04-17 20:34:31 +02:00
|
|
|
if (!parent->astOperand1() || !parent->astOperand2())
|
|
|
|
return false;
|
|
|
|
return (!parent->astOperand1()->valueType() || !parent->astOperand1()->valueType()->isIntegral());
|
2018-04-17 14:23:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 14:59:09 +01:00
|
|
|
bool isCPPCast(const Token* tok)
|
|
|
|
{
|
|
|
|
return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && tok->astOperand1()->str().find("_cast") != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2018-12-17 06:04:24 +01:00
|
|
|
bool isConstVarExpression(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
2019-01-12 18:29:08 +01:00
|
|
|
if (Token::simpleMatch(tok->previous(), "sizeof ("))
|
2018-12-17 06:04:24 +01:00
|
|
|
return true;
|
|
|
|
if (Token::Match(tok->previous(), "%name% (")) {
|
|
|
|
std::vector<const Token *> args = getArguments(tok);
|
|
|
|
return std::all_of(args.begin(), args.end(), &isConstVarExpression);
|
|
|
|
}
|
2019-02-07 14:59:09 +01:00
|
|
|
if (isCPPCast(tok)) {
|
2019-01-01 11:34:44 +01:00
|
|
|
return isConstVarExpression(tok->astOperand2());
|
|
|
|
}
|
2018-12-17 06:04:24 +01:00
|
|
|
if (Token::Match(tok, "( %type%"))
|
|
|
|
return isConstVarExpression(tok->astOperand1());
|
2019-02-22 06:39:59 +01:00
|
|
|
if (Token::Match(tok, "%cop%|[|.")) {
|
2018-12-17 06:04:24 +01:00
|
|
|
if (tok->astOperand1() && !isConstVarExpression(tok->astOperand1()))
|
|
|
|
return false;
|
|
|
|
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2()))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"))
|
|
|
|
return true;
|
|
|
|
if (tok->isEnumerator())
|
|
|
|
return true;
|
|
|
|
if (tok->variable())
|
|
|
|
return tok->variable()->isConst();
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2019-07-07 10:16:19 +02:00
|
|
|
static const Variable *getLHSVariableRecursive(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::Match(tok, "*|&|&&|[")) {
|
|
|
|
const Variable *var = getLHSVariableRecursive(tok->astOperand1());
|
|
|
|
if (var || Token::simpleMatch(tok, "["))
|
|
|
|
return var;
|
|
|
|
return getLHSVariableRecursive(tok->astOperand2());
|
|
|
|
}
|
|
|
|
if (Token::Match(tok->previous(), "this . %var%"))
|
|
|
|
return tok->next()->variable();
|
|
|
|
return tok->variable();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Variable *getLHSVariable(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "%assign%"))
|
|
|
|
return nullptr;
|
|
|
|
if (!tok->astOperand1())
|
|
|
|
return nullptr;
|
|
|
|
if (tok->astOperand1()->varId() > 0 && tok->astOperand1()->variable())
|
|
|
|
return tok->astOperand1()->variable();
|
|
|
|
return getLHSVariableRecursive(tok->astOperand1());
|
|
|
|
}
|
|
|
|
|
2019-03-23 19:00:55 +01:00
|
|
|
static bool nonLocal(const Variable* var, bool deref)
|
2018-12-02 17:01:52 +01:00
|
|
|
{
|
2019-03-23 19:00:55 +01:00
|
|
|
return !var || (!var->isLocal() && !var->isArgument()) || (deref && var->isArgument() && var->isPointer()) || var->isStatic() || var->isReference() || var->isExtern();
|
2018-12-02 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
2019-08-24 20:15:52 +02:00
|
|
|
static bool hasGccCompoundStatement(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->str() == "{" && Token::simpleMatch(tok->previous(), "( {"))
|
|
|
|
return true;
|
|
|
|
return hasGccCompoundStatement(tok->astOperand1()) || hasGccCompoundStatement(tok->astOperand2());
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:01:52 +01:00
|
|
|
static bool hasFunctionCall(const Token *tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (Token::Match(tok, "%name% ("))
|
|
|
|
// todo, const/pure function?
|
|
|
|
return true;
|
|
|
|
return hasFunctionCall(tok->astOperand1()) || hasFunctionCall(tok->astOperand2());
|
|
|
|
}
|
|
|
|
|
2019-07-18 10:56:44 +02:00
|
|
|
const Scope* PathAnalysis::findOuterScope(const Scope * scope)
|
|
|
|
{
|
|
|
|
if (!scope)
|
|
|
|
return nullptr;
|
|
|
|
if (scope->isLocal() && scope->type != Scope::eSwitch)
|
|
|
|
return findOuterScope(scope->nestedIn);
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Token* getCondTok(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::simpleMatch(tok, "("))
|
|
|
|
return getCondTok(tok->previous());
|
|
|
|
if (Token::simpleMatch(tok, "for") && Token::simpleMatch(tok->next()->astOperand2(), ";") && tok->next()->astOperand2()->astOperand2())
|
|
|
|
return tok->next()->astOperand2()->astOperand2()->astOperand1();
|
|
|
|
if (Token::simpleMatch(tok->next()->astOperand2(), ";"))
|
|
|
|
return tok->next()->astOperand2()->astOperand1();
|
|
|
|
return tok->next()->astOperand2();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<bool, bool> PathAnalysis::checkCond(const Token * tok, bool& known)
|
|
|
|
{
|
|
|
|
if (tok->hasKnownIntValue()) {
|
|
|
|
known = true;
|
|
|
|
return std::make_pair(tok->values().front().intvalue, !tok->values().front().intvalue);
|
|
|
|
}
|
|
|
|
auto it = std::find_if(tok->values().begin(), tok->values().end(), [](const ValueFlow::Value& v) {
|
|
|
|
return v.isIntValue();
|
|
|
|
});
|
|
|
|
// If all possible values are the same, then assume all paths have the same value
|
|
|
|
if (it != tok->values().end() && std::all_of(it, tok->values().end(), [&](const ValueFlow::Value& v) {
|
|
|
|
if (v.isIntValue())
|
|
|
|
return v.intvalue == it->intvalue;
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
known = false;
|
|
|
|
return std::make_pair(it->intvalue, !it->intvalue);
|
|
|
|
}
|
|
|
|
return std::make_pair(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathAnalysis::Progress PathAnalysis::forwardRecursive(const Token* tok, Info info, const std::function<PathAnalysis::Progress(const Info&)>& f) const
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return Progress::Continue;
|
|
|
|
if (tok->astOperand1() && forwardRecursive(tok->astOperand1(), info, f) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
info.tok = tok;
|
|
|
|
if (f(info) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
if (tok->astOperand2() && forwardRecursive(tok->astOperand2(), info, f) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PathAnalysis::Progress PathAnalysis::forwardRange(const Token* startToken, const Token* endToken, Info info, const std::function<PathAnalysis::Progress(const Info&)>& f) const
|
|
|
|
{
|
|
|
|
for (const Token *tok = startToken; tok && tok != endToken; tok = tok->next()) {
|
|
|
|
if (Token::Match(tok, "asm|goto|break|continue"))
|
|
|
|
return Progress::Break;
|
|
|
|
if (Token::Match(tok, "return|throw")) {
|
|
|
|
forwardRecursive(tok, info, f);
|
|
|
|
return Progress::Break;
|
|
|
|
}
|
|
|
|
if (Token::simpleMatch(tok, "}") && Token::simpleMatch(tok->link()->previous(), ") {") && Token::Match(tok->link()->linkAt(-1)->previous(), "if|while|for (")) {
|
|
|
|
const Token * blockStart = tok->link()->linkAt(-1)->previous();
|
|
|
|
const Token * condTok = getCondTok(blockStart);
|
|
|
|
if (!condTok)
|
|
|
|
continue;
|
|
|
|
info.errorPath.emplace_back(condTok, "Assuming condition is true.");
|
|
|
|
// Traverse a loop a second time
|
|
|
|
if (Token::Match(blockStart, "for|while (")) {
|
|
|
|
const Token* endCond = blockStart->linkAt(1);
|
|
|
|
bool traverseLoop = true;
|
|
|
|
// Only traverse simple for loops
|
|
|
|
if (Token::simpleMatch(blockStart, "for") && !Token::Match(endCond->tokAt(-3), "; ++|--|%var% %var%|++|-- ) {"))
|
|
|
|
traverseLoop = false;
|
|
|
|
// Traverse loop a second time
|
|
|
|
if (traverseLoop) {
|
|
|
|
// Traverse condition
|
|
|
|
if (forwardRecursive(condTok, info, f) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
// TODO: Should we traverse the body: forwardRange(tok->link(), tok, info, f)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Token::Match(tok, "if|while|for (") && Token::simpleMatch(tok->next()->link(), ") {")) {
|
|
|
|
const Token * endCond = tok->next()->link();
|
|
|
|
const Token * endBlock = endCond->next()->link();
|
|
|
|
const Token * condTok = getCondTok(tok);
|
|
|
|
if (!condTok)
|
|
|
|
continue;
|
|
|
|
// Traverse condition
|
|
|
|
if (forwardRange(tok->next(), tok->next()->link(), info, f) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
Info i = info;
|
|
|
|
i.known = false;
|
|
|
|
i.errorPath.emplace_back(condTok, "Assuming condition is true.");
|
|
|
|
|
|
|
|
// Check if condition is true or false
|
|
|
|
bool checkThen = false;
|
|
|
|
bool checkElse = false;
|
|
|
|
std::tie(checkThen, checkElse) = checkCond(condTok, i.known);
|
|
|
|
|
|
|
|
// Traverse then block
|
|
|
|
if (checkThen) {
|
|
|
|
if (forwardRange(endCond->next(), endBlock, i, f) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
}
|
|
|
|
// Traverse else block
|
|
|
|
if (Token::simpleMatch(endBlock, "} else {")) {
|
|
|
|
if (checkElse) {
|
|
|
|
i.errorPath.back().second = "Assuming condition is false.";
|
|
|
|
Progress result = forwardRange(endCond->next(), endBlock, i, f);
|
|
|
|
if (result == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
}
|
|
|
|
tok = endBlock->linkAt(2);
|
|
|
|
} else {
|
|
|
|
tok = endBlock;
|
|
|
|
}
|
|
|
|
} else if (Token::simpleMatch(tok, "} else {")) {
|
|
|
|
tok = tok->linkAt(2);
|
|
|
|
} else {
|
|
|
|
info.tok = tok;
|
|
|
|
if (f(info) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
}
|
2019-08-08 21:05:28 +02:00
|
|
|
// Prevent infinite recursion
|
2019-07-18 10:56:44 +02:00
|
|
|
if (tok->next() == start)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathAnalysis::forward(const std::function<Progress(const Info&)>& f) const
|
|
|
|
{
|
|
|
|
const Scope * endScope = findOuterScope(start->scope());
|
|
|
|
if (!endScope)
|
|
|
|
return;
|
|
|
|
const Token * endToken = endScope->bodyEnd;
|
|
|
|
Info info{start, ErrorPath{}, true};
|
|
|
|
forwardRange(start, endToken, info, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool reaches(const Token * start, const Token * dest, const Library& library, ErrorPath* errorPath)
|
|
|
|
{
|
|
|
|
PathAnalysis::Info info = PathAnalysis{start, library} .forwardFind([&](const PathAnalysis::Info& i) {
|
|
|
|
return (i.tok == dest);
|
|
|
|
});
|
|
|
|
if (!info.tok)
|
|
|
|
return false;
|
|
|
|
if (errorPath)
|
|
|
|
errorPath->insert(errorPath->end(), info.errorPath.begin(), info.errorPath.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:51:48 +02:00
|
|
|
static bool isUnchanged(const Token *startToken, const Token *endToken, const std::set<int> &exprVarIds, bool local)
|
|
|
|
{
|
|
|
|
for (const Token *tok = startToken; tok != endToken; tok = tok->next()) {
|
|
|
|
if (!local && Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {"))
|
|
|
|
// TODO: this is a quick bailout
|
|
|
|
return false;
|
|
|
|
if (tok->varId() <= 0 || exprVarIds.find(tok->varId()) == exprVarIds.end())
|
|
|
|
continue;
|
|
|
|
const Token *parent = tok;
|
|
|
|
while (parent->astParent() && !parent->astParent()->isAssignmentOp() && parent->astParent()->tokType() != Token::Type::eIncDecOp) {
|
|
|
|
if (parent->str() == "," || parent->isUnaryOp("&"))
|
|
|
|
// TODO: This is a quick bailout
|
|
|
|
return false;
|
|
|
|
parent = parent->astParent();
|
|
|
|
}
|
|
|
|
if (parent->astParent()) {
|
|
|
|
if (parent->astParent()->tokType() == Token::Type::eIncDecOp)
|
|
|
|
return false;
|
|
|
|
else if (parent->astParent()->isAssignmentOp() && parent == parent->astParent()->astOperand1())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-13 16:13:21 +02:00
|
|
|
struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *startToken, const Token *endToken, const std::set<int> &exprVarIds, bool local, bool inInnerClass)
|
2018-12-02 17:01:52 +01:00
|
|
|
{
|
|
|
|
// Parse the given tokens
|
2019-10-03 09:58:57 +02:00
|
|
|
|
|
|
|
for (const Token* tok = startToken; precedes(tok, endToken); tok = tok->next()) {
|
2018-12-02 17:01:52 +01:00
|
|
|
if (Token::simpleMatch(tok, "try {")) {
|
|
|
|
// TODO: handle try
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "break ;")) {
|
|
|
|
return Result(Result::Type::BREAK, tok);
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
if (Token::simpleMatch(tok, "goto"))
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
2019-07-03 08:17:06 +02:00
|
|
|
if (!inInnerClass && tok->str() == "{" && tok->scope()->isClassOrStruct()) {
|
|
|
|
// skip returns from local class definition
|
|
|
|
FwdAnalysis::Result result = checkRecursive(expr, tok, tok->link(), exprVarIds, local, true);
|
|
|
|
if (result.type != Result::Type::NONE)
|
|
|
|
return result;
|
|
|
|
tok=tok->link();
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
if (tok->str() == "continue")
|
|
|
|
// TODO
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
2019-01-22 21:16:27 +01:00
|
|
|
if (const Token *lambdaEndToken = findLambdaEndToken(tok)) {
|
|
|
|
tok = lambdaEndToken;
|
2019-07-03 08:17:06 +02:00
|
|
|
const Result lambdaResult = checkRecursive(expr, lambdaEndToken->link()->next(), lambdaEndToken, exprVarIds, local, inInnerClass);
|
2019-01-22 21:16:27 +01:00
|
|
|
if (lambdaResult.type == Result::Type::READ || lambdaResult.type == Result::Type::BAILOUT)
|
|
|
|
return lambdaResult;
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
if (Token::Match(tok, "return|throw")) {
|
2018-12-02 17:01:52 +01:00
|
|
|
// TODO: Handle these better
|
2018-12-13 21:01:33 +01:00
|
|
|
// Is expr variable used in expression?
|
2018-12-14 18:56:09 +01:00
|
|
|
const Token *end = tok->findExpressionStartEndTokens().second->next();
|
|
|
|
for (const Token *tok2 = tok; tok2 != end; tok2 = tok2->next()) {
|
2018-12-13 21:01:33 +01:00
|
|
|
if (!local && Token::Match(tok2, "%name% ("))
|
2018-12-14 18:56:09 +01:00
|
|
|
return Result(Result::Type::READ);
|
2018-12-13 21:01:33 +01:00
|
|
|
if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end())
|
2018-12-14 18:56:09 +01:00
|
|
|
return Result(Result::Type::READ);
|
|
|
|
}
|
2018-12-13 21:01:33 +01:00
|
|
|
|
2019-07-03 08:17:06 +02:00
|
|
|
// #9167: if the return is inside an inner class, it does not tell us anything
|
|
|
|
if (!inInnerClass) {
|
|
|
|
if (!local && mWhat == What::Reassign)
|
|
|
|
return Result(Result::Type::BAILOUT);
|
2019-03-23 19:00:55 +01:00
|
|
|
|
2019-07-03 08:17:06 +02:00
|
|
|
return Result(Result::Type::RETURN);
|
|
|
|
}
|
2018-12-13 18:52:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tok->str() == "}") {
|
2019-01-01 18:23:47 +01:00
|
|
|
// Known value => possible value
|
|
|
|
if (tok->scope() == expr->scope())
|
|
|
|
mValueFlowKnown = false;
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
Scope::ScopeType scopeType = tok->scope()->type;
|
2018-12-16 11:18:37 +01:00
|
|
|
if (scopeType == Scope::eWhile || scopeType == Scope::eFor || scopeType == Scope::eDo) {
|
2018-12-16 11:42:11 +01:00
|
|
|
// check condition
|
|
|
|
const Token *conditionStart = nullptr;
|
|
|
|
const Token *conditionEnd = nullptr;
|
|
|
|
if (Token::simpleMatch(tok->link()->previous(), ") {")) {
|
|
|
|
conditionEnd = tok->link()->previous();
|
|
|
|
conditionStart = conditionEnd->link();
|
|
|
|
} else if (Token::simpleMatch(tok->link()->previous(), "do {") && Token::simpleMatch(tok, "} while (")) {
|
|
|
|
conditionStart = tok->tokAt(2);
|
|
|
|
conditionEnd = conditionStart->link();
|
|
|
|
}
|
|
|
|
if (conditionStart && conditionEnd) {
|
2018-12-16 16:43:04 +01:00
|
|
|
bool used = false;
|
2018-12-16 11:42:11 +01:00
|
|
|
for (const Token *condTok = conditionStart; condTok != conditionEnd; condTok = condTok->next()) {
|
2018-12-16 16:43:04 +01:00
|
|
|
if (exprVarIds.find(condTok->varId()) != exprVarIds.end())
|
|
|
|
used = true;
|
2018-12-16 11:42:11 +01:00
|
|
|
}
|
2018-12-16 16:43:04 +01:00
|
|
|
if (used)
|
2018-12-16 11:42:11 +01:00
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check loop body again..
|
2019-07-03 08:17:06 +02:00
|
|
|
const struct FwdAnalysis::Result &result = checkRecursive(expr, tok->link(), tok, exprVarIds, local, inInnerClass);
|
2018-12-16 11:18:37 +01:00
|
|
|
if (result.type == Result::Type::BAILOUT || result.type == Result::Type::READ)
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-02 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok, "else {"))
|
|
|
|
tok = tok->linkAt(1);
|
|
|
|
|
2018-12-16 11:18:37 +01:00
|
|
|
if (Token::simpleMatch(tok, "asm ("))
|
2018-12-02 17:01:52 +01:00
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
2019-07-24 19:16:35 +02:00
|
|
|
if (mWhat == What::ValueFlow && (Token::Match(tok, "while|for (") || Token::simpleMatch(tok, "do {"))) {
|
2019-07-29 15:51:48 +02:00
|
|
|
const Token *bodyStart = nullptr;
|
|
|
|
const Token *conditionStart = nullptr;
|
|
|
|
if (Token::simpleMatch(tok, "do {")) {
|
|
|
|
bodyStart = tok->next();
|
|
|
|
if (Token::simpleMatch(bodyStart->link(), "} while ("))
|
|
|
|
conditionStart = bodyStart->link()->tokAt(2);
|
|
|
|
} else {
|
|
|
|
conditionStart = tok->next();
|
|
|
|
if (Token::simpleMatch(conditionStart->link(), ") {"))
|
|
|
|
bodyStart = conditionStart->link()->next();
|
|
|
|
}
|
|
|
|
|
2019-07-29 20:56:59 +02:00
|
|
|
if (!bodyStart || !conditionStart)
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
2019-07-29 15:51:48 +02:00
|
|
|
// Is expr changed in condition?
|
|
|
|
if (!isUnchanged(conditionStart, conditionStart->link(), exprVarIds, local))
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
|
|
|
// Is expr changed in loop body?
|
|
|
|
if (!isUnchanged(bodyStart, bodyStart->link(), exprVarIds, local))
|
|
|
|
return Result(Result::Type::BAILOUT);
|
2019-01-02 18:05:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-02 17:01:52 +01:00
|
|
|
if (!local && Token::Match(tok, "%name% (") && !Token::simpleMatch(tok->linkAt(1), ") {")) {
|
|
|
|
// TODO: this is a quick bailout
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
if (expr->isName() && Token::Match(tok, "%name% (") && tok->str().find("<") != std::string::npos && tok->str().find(expr->str()) != std::string::npos)
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
|
2018-12-02 17:42:18 +01:00
|
|
|
if (exprVarIds.find(tok->varId()) != exprVarIds.end()) {
|
2018-12-02 17:01:52 +01:00
|
|
|
const Token *parent = tok;
|
2018-12-16 18:32:34 +01:00
|
|
|
bool other = false;
|
2019-09-30 21:04:43 +02:00
|
|
|
bool same = tok->astParent() && isSameExpression(mCpp, false, expr, tok, mLibrary, true, false, nullptr);
|
|
|
|
while (!same && Token::Match(parent->astParent(), "*|.|::|[|(|%cop%")) {
|
2018-12-02 17:01:52 +01:00
|
|
|
parent = parent->astParent();
|
2019-09-30 21:04:43 +02:00
|
|
|
if (parent && isSameExpression(mCpp, false, expr, parent, mLibrary, true, false, nullptr)) {
|
2018-12-16 19:01:05 +01:00
|
|
|
same = true;
|
2019-01-01 18:23:47 +01:00
|
|
|
if (mWhat == What::ValueFlow) {
|
|
|
|
KnownAndToken v;
|
|
|
|
v.known = mValueFlowKnown;
|
|
|
|
v.token = parent;
|
|
|
|
mValueFlow.push_back(v);
|
|
|
|
}
|
2018-12-31 17:05:46 +01:00
|
|
|
}
|
2019-01-02 20:23:02 +01:00
|
|
|
if (Token::Match(parent, ". %var%") && parent->next()->varId() && exprVarIds.find(parent->next()->varId()) == exprVarIds.end()) {
|
2018-12-16 18:32:34 +01:00
|
|
|
other = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 20:23:02 +01:00
|
|
|
if (mWhat != What::ValueFlow && same && Token::simpleMatch(parent->astParent(), "[") && parent == parent->astParent()->astOperand2()) {
|
|
|
|
return Result(Result::Type::READ);
|
|
|
|
}
|
2018-12-16 18:32:34 +01:00
|
|
|
if (other)
|
|
|
|
continue;
|
2018-12-02 17:01:52 +01:00
|
|
|
if (Token::simpleMatch(parent->astParent(), "=") && parent == parent->astParent()->astOperand1()) {
|
|
|
|
if (!local && hasFunctionCall(parent->astParent()->astOperand2())) {
|
|
|
|
// TODO: this is a quick bailout
|
|
|
|
return Result(Result::Type::BAILOUT);
|
|
|
|
}
|
2018-12-02 17:42:18 +01:00
|
|
|
if (hasOperand(parent->astParent()->astOperand2(), expr)) {
|
2018-12-13 18:52:56 +01:00
|
|
|
if (mWhat == What::Reassign)
|
2018-12-02 18:29:16 +01:00
|
|
|
return Result(Result::Type::READ);
|
|
|
|
continue;
|
2018-12-02 17:01:52 +01:00
|
|
|
}
|
2019-08-24 20:15:52 +02:00
|
|
|
// ({ .. })
|
|
|
|
if (hasGccCompoundStatement(parent->astParent()->astOperand2()))
|
|
|
|
return Result(Result::Type::BAILOUT);
|
2018-12-02 17:42:18 +01:00
|
|
|
const bool reassign = isSameExpression(mCpp, false, expr, parent, mLibrary, false, false, nullptr);
|
2018-12-02 17:01:52 +01:00
|
|
|
if (reassign)
|
|
|
|
return Result(Result::Type::WRITE, parent->astParent());
|
|
|
|
return Result(Result::Type::READ);
|
2019-05-31 12:24:13 +02:00
|
|
|
} else if (mWhat == What::Reassign && parent->valueType() && parent->valueType()->pointer && Token::Match(parent->astParent(), "%assign%") && parent == parent->astParent()->astOperand1()) {
|
|
|
|
return Result(Result::Type::READ);
|
2018-12-27 17:27:51 +01:00
|
|
|
} else if (Token::Match(parent->astParent(), "%assign%") && !parent->astParent()->astParent() && parent == parent->astParent()->astOperand1()) {
|
2019-10-17 18:30:51 +02:00
|
|
|
if (mWhat == What::Reassign)
|
|
|
|
return Result(Result::Type::BAILOUT, parent->astParent());
|
2018-12-27 17:27:51 +01:00
|
|
|
continue;
|
2018-12-02 17:01:52 +01:00
|
|
|
} else {
|
|
|
|
// TODO: this is a quick bailout
|
2018-12-27 17:27:51 +01:00
|
|
|
return Result(Result::Type::BAILOUT, parent->astParent());
|
2018-12-02 17:01:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 18:29:08 +01:00
|
|
|
if (Token::simpleMatch(tok, ") {")) {
|
2018-12-13 18:52:56 +01:00
|
|
|
if (Token::simpleMatch(tok->link()->previous(), "switch ("))
|
|
|
|
// TODO: parse switch
|
|
|
|
return Result(Result::Type::BAILOUT);
|
2019-07-03 08:17:06 +02:00
|
|
|
const Result &result1 = checkRecursive(expr, tok->tokAt(2), tok->linkAt(1), exprVarIds, local, inInnerClass);
|
2018-12-02 17:01:52 +01:00
|
|
|
if (result1.type == Result::Type::READ || result1.type == Result::Type::BAILOUT)
|
|
|
|
return result1;
|
2019-01-01 18:23:47 +01:00
|
|
|
if (mWhat == What::ValueFlow && result1.type == Result::Type::WRITE)
|
|
|
|
mValueFlowKnown = false;
|
2018-12-02 17:01:52 +01:00
|
|
|
if (Token::simpleMatch(tok->linkAt(1), "} else {")) {
|
|
|
|
const Token *elseStart = tok->linkAt(1)->tokAt(2);
|
2019-07-03 08:17:06 +02:00
|
|
|
const Result &result2 = checkRecursive(expr, elseStart, elseStart->link(), exprVarIds, local, inInnerClass);
|
2019-01-01 18:23:47 +01:00
|
|
|
if (mWhat == What::ValueFlow && result2.type == Result::Type::WRITE)
|
|
|
|
mValueFlowKnown = false;
|
2018-12-02 17:01:52 +01:00
|
|
|
if (result2.type == Result::Type::READ || result2.type == Result::Type::BAILOUT)
|
|
|
|
return result2;
|
|
|
|
if (result1.type == Result::Type::WRITE && result2.type == Result::Type::WRITE)
|
|
|
|
return result1;
|
|
|
|
tok = elseStart->link();
|
|
|
|
} else {
|
|
|
|
tok = tok->linkAt(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result(Result::Type::NONE);
|
|
|
|
}
|
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
bool FwdAnalysis::isGlobalData(const Token *expr) const
|
2018-12-13 18:52:56 +01:00
|
|
|
{
|
|
|
|
bool globalData = false;
|
|
|
|
visitAstNodes(expr,
|
|
|
|
[&](const Token *tok) {
|
2018-12-13 21:37:21 +01:00
|
|
|
if (tok->varId() && !tok->variable()) {
|
|
|
|
// Bailout, this is probably global
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
2018-12-13 18:52:56 +01:00
|
|
|
if (tok->originalName() == "->") {
|
|
|
|
// TODO check if pointer points at local data
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
} else if (Token::Match(tok, "[*[]") && tok->astOperand1() && tok->astOperand1()->variable()) {
|
|
|
|
// TODO check if pointer points at local data
|
|
|
|
const Variable *lhsvar = tok->astOperand1()->variable();
|
|
|
|
const ValueType *lhstype = tok->astOperand1()->valueType();
|
|
|
|
if (lhsvar->isPointer()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2018-12-13 21:08:18 +01:00
|
|
|
} else if (lhsvar->isArgument() && lhsvar->isArray()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2018-12-13 18:52:56 +01:00
|
|
|
} else if (lhsvar->isArgument() && (!lhstype || (lhstype->type <= ValueType::Type::VOID && !lhstype->container))) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->varId() == 0 && tok->isName() && tok->previous()->str() != ".") {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->variable()) {
|
|
|
|
// TODO : Check references
|
|
|
|
if (tok->variable()->isReference() && tok != tok->variable()->nameToken()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
2018-12-17 15:40:15 +01:00
|
|
|
if (tok->variable()->isExtern()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->previous()->str() != "." && !tok->variable()->isLocal() && !tok->variable()->isArgument()) {
|
2018-12-13 18:52:56 +01:00
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->variable()->isArgument() && tok->variable()->isPointer() && tok != expr) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2018-12-17 15:40:15 +01:00
|
|
|
}
|
|
|
|
if (tok->variable()->isPointerArray()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2018-12-13 18:52:56 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 19:42:55 +01:00
|
|
|
// Unknown argument type => it might be some reference type..
|
|
|
|
if (mCpp && tok->str() == "." && tok->astOperand1() && tok->astOperand1()->variable() && !tok->astOperand1()->valueType()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
2018-12-13 18:52:56 +01:00
|
|
|
if (Token::Match(tok, ".|["))
|
|
|
|
return ChildrenToVisit::op1;
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
|
|
|
return globalData;
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:32:25 +02:00
|
|
|
std::set<int> FwdAnalysis::getExprVarIds(const Token* expr, bool* localOut, bool* unknownVarIdOut) const
|
2018-12-02 17:01:52 +01:00
|
|
|
{
|
2018-12-02 17:42:18 +01:00
|
|
|
// all variable ids in expr.
|
2019-07-13 16:13:21 +02:00
|
|
|
std::set<int> exprVarIds;
|
2018-12-02 17:42:18 +01:00
|
|
|
bool local = true;
|
2019-03-30 14:22:24 +01:00
|
|
|
bool unknownVarId = false;
|
2018-12-02 17:42:18 +01:00
|
|
|
visitAstNodes(expr,
|
|
|
|
[&](const Token *tok) {
|
2019-03-30 14:22:24 +01:00
|
|
|
if (tok->varId() == 0 && tok->isName() && tok->previous()->str() != ".") {
|
|
|
|
// unknown variable
|
|
|
|
unknownVarId = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
2018-12-02 17:42:18 +01:00
|
|
|
if (tok->varId() > 0) {
|
|
|
|
exprVarIds.insert(tok->varId());
|
2018-12-13 18:52:56 +01:00
|
|
|
if (!Token::simpleMatch(tok->previous(), ".")) {
|
|
|
|
const Variable *var = tok->variable();
|
|
|
|
if (var && var->isReference() && var->isLocal() && Token::Match(var->nameToken(), "%var% [=(]") && !isGlobalData(var->nameToken()->next()->astOperand2()))
|
|
|
|
return ChildrenToVisit::none;
|
2019-03-23 19:00:55 +01:00
|
|
|
const bool deref = tok->astParent() && (tok->astParent()->isUnaryOp("*") || (tok->astParent()->str() == "[" && tok == tok->astParent()->astOperand1()));
|
|
|
|
local &= !nonLocal(tok->variable(), deref);
|
2018-12-13 18:52:56 +01:00
|
|
|
}
|
2018-12-02 17:42:18 +01:00
|
|
|
}
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
2019-09-26 10:32:25 +02:00
|
|
|
if (localOut)
|
|
|
|
*localOut = local;
|
|
|
|
if (unknownVarIdOut)
|
|
|
|
*unknownVarIdOut = unknownVarId;
|
|
|
|
return exprVarIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
FwdAnalysis::Result FwdAnalysis::check(const Token* expr, const Token* startToken, const Token* endToken)
|
|
|
|
{
|
|
|
|
// all variable ids in expr.
|
|
|
|
bool local = true;
|
|
|
|
bool unknownVarId = false;
|
|
|
|
std::set<int> exprVarIds = getExprVarIds(expr, &local, &unknownVarId);
|
2018-12-02 17:42:18 +01:00
|
|
|
|
2019-03-30 14:22:24 +01:00
|
|
|
if (unknownVarId)
|
|
|
|
return Result(FwdAnalysis::Result::Type::BAILOUT);
|
|
|
|
|
2019-04-16 19:07:26 +02:00
|
|
|
if (mWhat == What::Reassign && isGlobalData(expr))
|
|
|
|
local = false;
|
|
|
|
|
2018-12-13 18:52:56 +01:00
|
|
|
// In unused values checking we do not want to check assignments to
|
|
|
|
// global data.
|
|
|
|
if (mWhat == What::UnusedValue && isGlobalData(expr))
|
2018-12-31 18:00:47 +01:00
|
|
|
return Result(FwdAnalysis::Result::Type::BAILOUT);
|
2018-12-13 18:52:56 +01:00
|
|
|
|
2019-07-03 08:17:06 +02:00
|
|
|
Result result = checkRecursive(expr, startToken, endToken, exprVarIds, local, false);
|
2018-12-02 17:01:52 +01:00
|
|
|
|
|
|
|
// Break => continue checking in outer scope
|
2019-06-17 21:25:15 +02:00
|
|
|
while (mWhat!=What::ValueFlow && result.type == FwdAnalysis::Result::Type::BREAK) {
|
2018-12-02 17:01:52 +01:00
|
|
|
const Scope *s = result.token->scope();
|
|
|
|
while (s->type == Scope::eIf)
|
|
|
|
s = s->nestedIn;
|
2018-12-16 11:18:37 +01:00
|
|
|
if (s->type != Scope::eSwitch && s->type != Scope::eWhile && s->type != Scope::eFor)
|
2018-12-02 17:01:52 +01:00
|
|
|
break;
|
2019-07-03 08:17:06 +02:00
|
|
|
result = checkRecursive(expr, s->bodyEnd->next(), endToken, exprVarIds, local, false);
|
2018-12-02 17:01:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
bool FwdAnalysis::hasOperand(const Token *tok, const Token *lhs) const
|
2018-12-02 17:01:52 +01:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (isSameExpression(mCpp, false, tok, lhs, mLibrary, false, false, nullptr))
|
|
|
|
return true;
|
|
|
|
return hasOperand(tok->astOperand1(), lhs) || hasOperand(tok->astOperand2(), lhs);
|
|
|
|
}
|
2018-12-02 18:29:16 +01:00
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
const Token *FwdAnalysis::reassign(const Token *expr, const Token *startToken, const Token *endToken)
|
2018-12-02 18:29:16 +01:00
|
|
|
{
|
2018-12-13 18:52:56 +01:00
|
|
|
mWhat = What::Reassign;
|
2018-12-02 18:29:16 +01:00
|
|
|
Result result = check(expr, startToken, endToken);
|
2018-12-31 18:00:47 +01:00
|
|
|
return result.type == FwdAnalysis::Result::Type::WRITE ? result.token : nullptr;
|
2018-12-02 18:29:16 +01:00
|
|
|
}
|
2018-12-13 18:52:56 +01:00
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
bool FwdAnalysis::unusedValue(const Token *expr, const Token *startToken, const Token *endToken)
|
2018-12-13 18:52:56 +01:00
|
|
|
{
|
2019-05-31 08:06:36 +02:00
|
|
|
if (isEscapedAlias(expr))
|
|
|
|
return false;
|
2018-12-13 18:52:56 +01:00
|
|
|
mWhat = What::UnusedValue;
|
|
|
|
Result result = check(expr, startToken, endToken);
|
2018-12-31 18:00:47 +01:00
|
|
|
return (result.type == FwdAnalysis::Result::Type::NONE || result.type == FwdAnalysis::Result::Type::RETURN) && !possiblyAliased(expr, startToken);
|
2018-12-13 18:52:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 18:23:47 +01:00
|
|
|
std::vector<FwdAnalysis::KnownAndToken> FwdAnalysis::valueFlow(const Token *expr, const Token *startToken, const Token *endToken)
|
2018-12-31 17:05:46 +01:00
|
|
|
{
|
|
|
|
mWhat = What::ValueFlow;
|
2019-01-01 18:23:47 +01:00
|
|
|
mValueFlowKnown = true;
|
2018-12-31 17:05:46 +01:00
|
|
|
check(expr, startToken, endToken);
|
|
|
|
return mValueFlow;
|
|
|
|
}
|
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) const
|
2018-12-13 18:52:56 +01:00
|
|
|
{
|
|
|
|
if (expr->isUnaryOp("*"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const bool macro = false;
|
|
|
|
const bool pure = false;
|
|
|
|
const bool followVar = false;
|
|
|
|
for (const Token *tok = startToken; tok; tok = tok->previous()) {
|
|
|
|
if (tok->str() == "{" && tok->scope()->type == Scope::eFunction)
|
2018-12-15 11:54:00 +01:00
|
|
|
break;
|
|
|
|
|
2019-01-13 07:59:41 +01:00
|
|
|
if (Token::Match(tok, "%name% (") && !Token::Match(tok, "if|while|for")) {
|
|
|
|
// Is argument passed by reference?
|
|
|
|
const std::vector<const Token*> args = getArguments(tok);
|
2019-07-13 16:13:21 +02:00
|
|
|
for (int argnr = 0; argnr < args.size(); ++argnr) {
|
2019-01-13 07:59:41 +01:00
|
|
|
if (!Token::Match(args[argnr], "%name%|.|::"))
|
|
|
|
continue;
|
|
|
|
if (tok->function() && tok->function()->getArgumentVar(argnr) && !tok->function()->getArgumentVar(argnr)->isReference() && !tok->function()->isConst())
|
|
|
|
continue;
|
|
|
|
for (const Token *subexpr = expr; subexpr; subexpr = subexpr->astOperand1()) {
|
|
|
|
if (isSameExpression(mCpp, macro, subexpr, args[argnr], mLibrary, pure, followVar))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-15 11:54:00 +01:00
|
|
|
const Token *addrOf = nullptr;
|
|
|
|
if (Token::Match(tok, "& %name% ="))
|
|
|
|
addrOf = tok->tokAt(2)->astOperand2();
|
|
|
|
else if (tok->isUnaryOp("&"))
|
|
|
|
addrOf = tok->astOperand1();
|
|
|
|
else if (Token::simpleMatch(tok, "std :: ref ("))
|
|
|
|
addrOf = tok->tokAt(3)->astOperand2();
|
|
|
|
else
|
2018-12-13 18:52:56 +01:00
|
|
|
continue;
|
2018-12-15 11:54:00 +01:00
|
|
|
|
|
|
|
for (const Token *subexpr = expr; subexpr; subexpr = subexpr->astOperand1()) {
|
|
|
|
if (isSameExpression(mCpp, macro, subexpr, addrOf, mLibrary, pure, followVar))
|
2018-12-13 18:52:56 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-13 21:01:33 +01:00
|
|
|
|
2019-05-31 08:06:36 +02:00
|
|
|
bool FwdAnalysis::isEscapedAlias(const Token* expr)
|
|
|
|
{
|
|
|
|
for (const Token *subexpr = expr; subexpr; subexpr = subexpr->astOperand1()) {
|
|
|
|
for (const ValueFlow::Value &val : subexpr->values()) {
|
|
|
|
if (!val.isLocalLifetimeValue())
|
|
|
|
continue;
|
|
|
|
const Variable* var = val.tokvalue->variable();
|
|
|
|
if (!var)
|
|
|
|
continue;
|
|
|
|
if (!var->isLocal())
|
|
|
|
return true;
|
|
|
|
if (var->isArgument())
|
|
|
|
return true;
|
2019-05-31 10:14:52 +02:00
|
|
|
|
2019-05-31 08:06:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-31 18:00:47 +01:00
|
|
|
bool FwdAnalysis::isNullOperand(const Token *expr)
|
2018-12-13 21:01:33 +01:00
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return false;
|
|
|
|
if (Token::Match(expr, "( %name% %name%| * )") && Token::Match(expr->astOperand1(), "0|NULL|nullptr"))
|
|
|
|
return true;
|
|
|
|
return Token::Match(expr, "NULL|nullptr");
|
|
|
|
}
|