2015-08-02 21:57:32 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 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"
|
|
|
|
|
|
|
|
#include <list>
|
2015-08-03 09:20:50 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
2017-03-27 18:48:34 +02:00
|
|
|
if (tok->isName() && !tok->varId() && tok->values().size() == 1U && tok->values().front().isKnown() && 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")) {
|
|
|
|
if (tok->str() == "!")
|
|
|
|
ret = tok->astOperand1();
|
|
|
|
}
|
|
|
|
while (ret && ret->str() == ".")
|
|
|
|
ret = ret->astOperand2();
|
|
|
|
if (ret && ret->varId() == 0U)
|
|
|
|
ret = nullptr;
|
|
|
|
if (vartok)
|
|
|
|
*vartok = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:31:16 +01:00
|
|
|
bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
|
|
|
if (tok1 == nullptr && tok2 == nullptr)
|
|
|
|
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();
|
|
|
|
}
|
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,"<=|>="))) {
|
2016-12-06 12:31:16 +01:00
|
|
|
return isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure) &&
|
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-25 18:31:21 +01:00
|
|
|
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro()))
|
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;
|
2015-08-03 09:20:50 +02:00
|
|
|
if (tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {
|
2016-12-06 12:31:16 +01:00
|
|
|
if (!tok1->function() && !Token::Match(tok1->previous(), ".|::") && pure && !library.isFunctionConst(tok1->str(), true) && !tok1->isAttributeConst() && !tok1->isAttributePure())
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
|
|
|
else if (tok1->function() && !tok1->function()->isConst() && !tok1->function()->isAttributeConst() && !tok1->function()->isAttributePure())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
if (Token::simpleMatch(tok1->next()->link(), "> (") &&
|
|
|
|
!(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;
|
|
|
|
if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal
|
|
|
|
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 =
|
2016-12-06 12:31:16 +01:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand1(), library, pure);
|
2016-08-01 13:33:56 +02:00
|
|
|
noncommutativeEquals = noncommutativeEquals &&
|
2016-12-06 12:31:16 +01:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand2(), library, pure);
|
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
|
2017-09-01 22:15:09 +02:00
|
|
|
if (cpp && tok1->str() == "+" && tok1->astOperand2()) {
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
const bool commutative = tok1->astOperand1() && tok1->astOperand2() && Token::Match(tok1, "%or%|%oror%|+|*|&|&&|^|==|!=");
|
2016-08-01 13:33:56 +02:00
|
|
|
bool commutativeEquals = commutative &&
|
2016-12-06 12:31:16 +01:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure);
|
2016-08-01 13:33:56 +02:00
|
|
|
commutativeEquals = commutativeEquals &&
|
2016-12-06 12:31:16 +01:00
|
|
|
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure);
|
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
|
|
|
}
|
|
|
|
|
2016-12-06 12:31:16 +01:00
|
|
|
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure)
|
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")
|
2016-12-06 12:31:16 +01:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand2(), library, pure);
|
2015-08-03 09:20:50 +02:00
|
|
|
if (cond2->astOperand2() && cond2->astOperand2()->str() == "0")
|
2016-12-06 12:31:16 +01:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
2016-12-06 12:31:16 +01:00
|
|
|
return isSameExpression(cpp, true, cond1->astOperand1(), cond2, library, pure);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cond2->str() == "!")
|
2016-12-06 12:31:16 +01:00
|
|
|
return isOppositeCond(isNot, cpp, cond2, cond1, library, pure);
|
2015-08-03 09:20:50 +02: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;
|
2016-12-06 12:31:16 +01:00
|
|
|
if (isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure) &&
|
|
|
|
isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand2(), library, pure)) {
|
2015-08-03 09:20:50 +02:00
|
|
|
comp2 = cond2->str();
|
2016-12-06 12:31:16 +01:00
|
|
|
} else if (isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand2(), library, pure) &&
|
|
|
|
isSameExpression(cpp, true, cond1->astOperand2(), cond2->astOperand1(), library, pure)) {
|
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;
|
|
|
|
}
|
|
|
|
if (!isSameExpression(cpp, true, expr1, expr2, library, pure))
|
|
|
|
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 == ">") ||
|
|
|
|
(comp1 == ">" && comp2 == "<"))));
|
|
|
|
}
|
|
|
|
|
2016-12-06 12:31:16 +01:00
|
|
|
bool isConstExpression(const Token *tok, const Library& library, bool pure)
|
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;
|
|
|
|
// bailout when we see ({..})
|
|
|
|
if (tok->str() == "{")
|
|
|
|
return false;
|
2016-12-06 12:31:16 +01:00
|
|
|
return isConstExpression(tok->astOperand1(), library, pure) && isConstExpression(tok->astOperand2(), library, pure);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-16 18:52:34 +01:00
|
|
|
bool isReturnScope(const Token * const endToken)
|
|
|
|
{
|
|
|
|
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 {"))
|
|
|
|
return isReturnScope(prev) && isReturnScope(prev->link()->tokAt(-2));
|
|
|
|
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
|
|
|
|
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
|
|
|
|
!Token::findsimplematch(prev->link(), "break", prev)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-17 12:38:49 +01:00
|
|
|
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
|
|
|
|
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "return (")) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-18 15:39:20 +01:00
|
|
|
if (Token::Match(prev->link()->previous(), "[;{}] {"))
|
|
|
|
return isReturnScope(prev);
|
2016-01-16 18:52:34 +01:00
|
|
|
} else if (Token::simpleMatch(prev, ";")) {
|
|
|
|
// noreturn function
|
|
|
|
if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% ("))
|
|
|
|
return true;
|
|
|
|
// return/goto statement
|
|
|
|
prev = prev->previous();
|
|
|
|
while (prev && !Token::Match(prev, ";|{|}|return|goto|throw|continue|break"))
|
|
|
|
prev = prev->previous();
|
|
|
|
return prev && prev->isName();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-23 13:54:44 +02:00
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings, bool *inconclusive)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// address of variable
|
|
|
|
const bool addressOf = tok && Token::simpleMatch(tok->previous(), "&");
|
|
|
|
|
|
|
|
// passing variable to subfunction?
|
|
|
|
if (Token::Match(tok->tokAt(-2), ") & %name% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] ("))
|
|
|
|
;
|
|
|
|
else if (Token::Match(tok->tokAt(addressOf?-2:-1), "[(,] &| %name% [,)]"))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// reinterpret_cast etc..
|
|
|
|
if (Token::Match(tok->tokAt(-3), "> ( & %name% ) [,)]") &&
|
|
|
|
tok->linkAt(-3) &&
|
|
|
|
Token::Match(tok->linkAt(-3)->tokAt(-2), "[,(] %type% <"))
|
|
|
|
tok = tok->linkAt(-3);
|
|
|
|
|
|
|
|
// goto start of function call and get argnr
|
|
|
|
unsigned int argnr = 0;
|
|
|
|
while (tok && tok->str() != "(") {
|
|
|
|
if (tok->str() == ",")
|
|
|
|
++argnr;
|
|
|
|
else if (tok->str() == ")")
|
|
|
|
tok = tok->link();
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
|
|
|
tok = tok ? tok->previous() : nullptr;
|
|
|
|
if (tok && tok->link() && tok->str() == ">")
|
|
|
|
tok = tok->link()->previous();
|
2017-09-20 22:09:09 +02:00
|
|
|
if (!Token::Match(tok, "%name% [(<]"))
|
2016-10-23 13:54:44 +02:00
|
|
|
return false; // not a function => variable not changed
|
|
|
|
|
2017-09-20 12:53:25 +02:00
|
|
|
// Constructor call
|
|
|
|
if (tok->variable() && tok->variable()->nameToken() == tok) {
|
|
|
|
// Find constructor..
|
2017-09-20 13:28:45 +02:00
|
|
|
const unsigned int argCount = numberOfArguments(tok);
|
2017-09-20 12:53:25 +02:00
|
|
|
const ::Scope *typeScope = tok->variable()->typeScope();
|
|
|
|
if (typeScope) {
|
|
|
|
for (std::list<Function>::const_iterator it = typeScope->functionList.begin(); it != typeScope->functionList.end(); ++it) {
|
2017-09-20 14:03:56 +02:00
|
|
|
if (!it->isConstructor() || it->argCount() < argCount)
|
2017-09-20 12:53:25 +02:00
|
|
|
continue;
|
|
|
|
const Variable *arg = it->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()) {
|
|
|
|
// if the library says 0 is invalid
|
|
|
|
// => it is assumed that parameter is an in parameter (TODO: this is a bad heuristic)
|
|
|
|
if (!addressOf && settings->library.isnullargbad(tok, 1+argnr))
|
|
|
|
return false;
|
|
|
|
// addressOf => inconclusive
|
|
|
|
if (!addressOf) {
|
|
|
|
if (inconclusive != nullptr)
|
|
|
|
*inconclusive = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Variable *arg = tok->function()->getArgumentVar(argnr);
|
|
|
|
|
|
|
|
if (addressOf && !(arg && arg->isConst()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return arg && !arg->isConst() && arg->isReference();
|
|
|
|
}
|
|
|
|
|
2017-07-09 12:50:17 +02:00
|
|
|
bool isVariableChanged(const Token *start, const Token *end, const unsigned int varid, bool globalvar, const Settings *settings)
|
2015-11-11 13:45:28 +01:00
|
|
|
{
|
|
|
|
for (const Token *tok = start; tok != end; tok = tok->next()) {
|
2017-07-09 12:36:33 +02:00
|
|
|
if (tok->varId() != varid) {
|
2017-07-09 12:50:17 +02:00
|
|
|
if (globalvar && Token::Match(tok, "%name% ("))
|
|
|
|
// TODO: Is global variable really changed by function call?
|
|
|
|
return true;
|
2017-07-09 12:36:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2017-07-09 12:36:33 +02:00
|
|
|
if (Token::Match(tok, "%name% %assign%|++|--"))
|
|
|
|
return true;
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2017-07-09 12:36:33 +02:00
|
|
|
if (Token::Match(tok->previous(), "++|-- %name%"))
|
|
|
|
return true;
|
2017-04-23 18:05:14 +02:00
|
|
|
|
2017-09-02 21:53:51 +02:00
|
|
|
if (Token::simpleMatch(tok->previous(), ">>")) {
|
|
|
|
const Token *shr = tok->previous();
|
|
|
|
if (Token::simpleMatch(shr->astParent(), ">>"))
|
|
|
|
return true;
|
|
|
|
const Token *lhs = shr->astOperand1();
|
2017-09-11 13:45:36 +02:00
|
|
|
if (!lhs || !lhs->valueType() || !lhs->valueType()->isIntegral())
|
2017-09-02 21:53:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-09 12:36:33 +02:00
|
|
|
const Token *ftok = tok;
|
|
|
|
while (ftok && !Token::Match(ftok, "[({[]"))
|
|
|
|
ftok = ftok->astParent();
|
2015-11-11 13:45:28 +01:00
|
|
|
|
2017-07-09 12:36:33 +02:00
|
|
|
if (ftok && Token::Match(ftok->link(), ") !!{")) {
|
|
|
|
bool inconclusive = false;
|
|
|
|
bool isChanged = isVariableChangedByFunctionCall(tok, settings, &inconclusive);
|
|
|
|
isChanged |= inconclusive;
|
|
|
|
if (isChanged)
|
2015-11-11 13:45:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-09 12:36:33 +02:00
|
|
|
|
|
|
|
const Token *parent = tok->astParent();
|
|
|
|
while (Token::Match(parent, ".|::"))
|
|
|
|
parent = parent->astParent();
|
|
|
|
if (parent && parent->tokType() == Token::eIncDecOp)
|
|
|
|
return true;
|
2015-11-11 13:45:28 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
static void getArgumentsRecursive(const Token *tok, std::vector<const Token *> *arguments)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return;
|
|
|
|
if (tok->str() == ",") {
|
|
|
|
getArgumentsRecursive(tok->astOperand1(), arguments);
|
|
|
|
getArgumentsRecursive(tok->astOperand2(), arguments);
|
|
|
|
} else {
|
|
|
|
arguments->push_back(tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Token *> getArguments(const Token *ftok)
|
|
|
|
{
|
|
|
|
std::vector<const Token *> arguments;
|
|
|
|
getArgumentsRecursive(ftok->next()->astOperand2(), &arguments);
|
|
|
|
return arguments;
|
|
|
|
}
|
2017-08-29 22:35:55 +02:00
|
|
|
|
|
|
|
const Token *findLambdaEndToken(const Token *first)
|
|
|
|
{
|
|
|
|
if (!first || first->str() != "[")
|
|
|
|
return nullptr;
|
|
|
|
const Token* tok = first->link();
|
|
|
|
if (Token::simpleMatch(tok, "] {"))
|
|
|
|
return tok->linkAt(1);
|
|
|
|
if (!Token::simpleMatch(tok, "] ("))
|
|
|
|
return nullptr;
|
|
|
|
tok = tok->linkAt(1)->next();
|
|
|
|
if (tok && tok->str() == "constexpr")
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok && tok->str() == "mutable")
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok && tok->str() == "{")
|
|
|
|
return tok->link();
|
|
|
|
return nullptr;
|
|
|
|
}
|