2015-08-02 21:57:32 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 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
|
|
|
|
2020-06-28 22:28:08 +02:00
|
|
|
#include "config.h"
|
2021-01-22 11:00:57 +01:00
|
|
|
#include "errortypes.h"
|
2022-01-04 10:21:54 +01:00
|
|
|
#include "infer.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"
|
2022-02-11 19:44:08 +01:00
|
|
|
#include "utils.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "valueflow.h"
|
2022-01-04 10:21:54 +01:00
|
|
|
#include "valueptr.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h"
|
|
|
|
|
2022-03-25 11:32:16 +01:00
|
|
|
#include "checkclass.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2020-05-10 20:32:59 +02:00
|
|
|
#include <algorithm>
|
2022-09-16 07:15:49 +02:00
|
|
|
#include <cassert>
|
2020-09-18 07:44:26 +02:00
|
|
|
#include <functional>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <initializer_list>
|
2019-09-28 06:24:32 +02:00
|
|
|
#include <iterator>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <list>
|
2021-08-23 09:03:48 +02:00
|
|
|
#include <set>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
2020-09-18 07:44:26 +02:00
|
|
|
#include <utility>
|
2018-11-23 06:53:43 +01:00
|
|
|
|
2021-01-27 19:49:13 +01:00
|
|
|
const Token* findExpression(const nonneg int exprid,
|
2021-01-23 17:52:01 +01:00
|
|
|
const Token* start,
|
|
|
|
const Token* end,
|
|
|
|
const std::function<bool(const Token*)>& pred)
|
|
|
|
{
|
|
|
|
if (exprid == 0)
|
|
|
|
return nullptr;
|
2022-08-21 17:39:21 +02:00
|
|
|
if (!precedes(start, end))
|
|
|
|
return nullptr;
|
2021-01-23 17:52:01 +01:00
|
|
|
for (const Token* tok = start; tok != end; tok = tok->next()) {
|
|
|
|
if (tok->exprId() != exprid)
|
|
|
|
continue;
|
|
|
|
if (pred(tok))
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-01 10:51:44 +02:00
|
|
|
static int findArgumentPosRecursive(const Token* tok, const Token* tokToFind, bool &found, nonneg int depth=0)
|
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
if (!tok || depth >= 100)
|
|
|
|
return -1;
|
|
|
|
if (tok->str() == ",") {
|
|
|
|
int res = findArgumentPosRecursive(tok->astOperand1(), tokToFind, found, depth);
|
|
|
|
if (res == -1)
|
|
|
|
return -1;
|
|
|
|
if (found)
|
|
|
|
return res;
|
2022-10-02 07:12:40 +02:00
|
|
|
const int argn = res;
|
2021-09-01 10:51:44 +02:00
|
|
|
res = findArgumentPosRecursive(tok->astOperand2(), tokToFind, found, depth);
|
|
|
|
if (res == -1)
|
|
|
|
return -1;
|
|
|
|
return argn + res;
|
|
|
|
}
|
2023-06-20 18:43:21 +02:00
|
|
|
if (tokToFind == tok)
|
|
|
|
found = true;
|
|
|
|
return 1;
|
2021-09-01 10:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int findArgumentPos(const Token* tok, const Token* tokToFind){
|
|
|
|
bool found = false;
|
2022-10-02 07:12:40 +02:00
|
|
|
const int argn = findArgumentPosRecursive(tok, tokToFind, found, 0);
|
2021-09-01 10:51:44 +02:00
|
|
|
if (found)
|
|
|
|
return argn - 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getArgumentPos(const Token* ftok, const Token* tokToFind){
|
|
|
|
const Token* tok = ftok;
|
|
|
|
if (Token::Match(tok, "%name% (|{"))
|
|
|
|
tok = ftok->next();
|
|
|
|
if (!Token::Match(tok, "(|{|["))
|
|
|
|
return -1;
|
|
|
|
const Token* startTok = tok->astOperand2();
|
|
|
|
if (!startTok && tok->next() != tok->link())
|
|
|
|
startTok = tok->astOperand1();
|
|
|
|
return findArgumentPos(startTok, tokToFind);
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:58:16 +02:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2023-03-02 22:05:41 +01:00
|
|
|
static void astFlattenRecursive(T* tok, std::vector<T*>& result, const char* op, nonneg int depth = 0)
|
2019-08-23 06:23:21 +02:00
|
|
|
{
|
|
|
|
++depth;
|
|
|
|
if (!tok || depth >= 100)
|
|
|
|
return;
|
|
|
|
if (tok->str() == op) {
|
|
|
|
astFlattenRecursive(tok->astOperand1(), result, op, depth);
|
|
|
|
astFlattenRecursive(tok->astOperand2(), result, op, depth);
|
|
|
|
} else {
|
2023-03-02 22:05:41 +01:00
|
|
|
result.push_back(tok);
|
2019-08-23 06:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Token*> astFlatten(const Token* tok, const char* op)
|
|
|
|
{
|
|
|
|
std::vector<const Token*> result;
|
2023-03-02 22:05:41 +01:00
|
|
|
astFlattenRecursive(tok, result, op);
|
2019-08-23 06:23:21 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-15 10:58:16 +02:00
|
|
|
std::vector<Token*> astFlatten(Token* tok, const char* op)
|
|
|
|
{
|
|
|
|
std::vector<Token*> result;
|
2023-03-02 22:05:41 +01:00
|
|
|
astFlattenRecursive(tok, result, op);
|
2021-10-15 10:58:16 +02:00
|
|
|
return result;
|
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
|
2022-05-13 06:51:07 +02:00
|
|
|
nonneg int astCount(const Token* tok, const char* op, int depth)
|
|
|
|
{
|
|
|
|
--depth;
|
|
|
|
if (!tok || depth < 0)
|
|
|
|
return 0;
|
|
|
|
if (tok->str() == op)
|
|
|
|
return astCount(tok->astOperand1(), op, depth) + astCount(tok->astOperand2(), op, depth);
|
2023-06-20 18:43:21 +02:00
|
|
|
return 1;
|
2022-05-13 06:51:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-29 08:38:50 +02:00
|
|
|
bool astHasToken(const Token* root, const Token * tok)
|
|
|
|
{
|
|
|
|
if (!root)
|
|
|
|
return false;
|
2021-09-25 11:55:49 +02:00
|
|
|
while (tok->astParent() && tok != root)
|
|
|
|
tok = tok->astParent();
|
|
|
|
return root == tok;
|
2019-08-29 08:38:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-02 23:09:49 +02:00
|
|
|
bool astIsGenericChar(const Token* tok)
|
|
|
|
{
|
2021-10-05 18:04:48 +02:00
|
|
|
return !astIsPointer(tok) && tok && tok->valueType() && (tok->valueType()->type == ValueType::Type::CHAR || tok->valueType()->type == ValueType::Type::WCHAR_T);
|
2021-10-02 23:09:49 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:19:56 +01:00
|
|
|
bool astIsPrimitive(const Token* tok)
|
|
|
|
{
|
|
|
|
const ValueType* vt = tok ? tok->valueType() : nullptr;
|
|
|
|
if (!vt)
|
|
|
|
return false;
|
|
|
|
return vt->isPrimitive();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-30 14:02:28 +02:00
|
|
|
bool astIsUnsigned(const Token* tok)
|
|
|
|
{
|
|
|
|
return tok && tok->valueType() && tok->valueType()->sign == ValueType::UNSIGNED;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-11-11 08:00:05 +01:00
|
|
|
bool astIsUniqueSmartPointer(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!astIsSmartPointer(tok))
|
|
|
|
return false;
|
|
|
|
if (!tok->valueType()->smartPointer)
|
|
|
|
return false;
|
|
|
|
return tok->valueType()->smartPointer->unique;
|
|
|
|
}
|
|
|
|
|
2018-11-17 09:41:59 +01:00
|
|
|
bool astIsIterator(const Token *tok)
|
|
|
|
{
|
|
|
|
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::ITERATOR;
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:28:19 +02:00
|
|
|
bool astIsContainer(const Token* tok) {
|
|
|
|
return getLibraryContainer(tok) != nullptr && !astIsIterator(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool astIsContainerView(const Token* tok)
|
2018-11-17 09:41:59 +01:00
|
|
|
{
|
2021-10-05 08:28:19 +02:00
|
|
|
const Library::Container* container = getLibraryContainer(tok);
|
|
|
|
return container && !astIsIterator(tok) && container->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool astIsContainerOwned(const Token* tok) {
|
|
|
|
return astIsContainer(tok) && !astIsContainerView(tok);
|
2018-11-17 09:41:59 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 17:40:09 +02:00
|
|
|
static const Token* getContainerFunction(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok || !tok->valueType() || !tok->valueType()->container)
|
|
|
|
return nullptr;
|
|
|
|
const Token* parent = tok->astParent();
|
|
|
|
if (Token::Match(parent, ". %name% (") && astIsLHS(tok)) {
|
|
|
|
return parent->next();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-06-19 08:17:05 +02:00
|
|
|
Library::Container::Action astContainerAction(const Token* tok, const Token** ftok)
|
2022-06-16 17:40:09 +02:00
|
|
|
{
|
2022-06-19 08:17:05 +02:00
|
|
|
const Token* ftok2 = getContainerFunction(tok);
|
|
|
|
if (ftok)
|
|
|
|
*ftok = ftok2;
|
|
|
|
if (!ftok2)
|
2022-06-16 17:40:09 +02:00
|
|
|
return Library::Container::Action::NO_ACTION;
|
2022-06-19 08:17:05 +02:00
|
|
|
return tok->valueType()->container->getAction(ftok2->str());
|
|
|
|
}
|
2023-03-09 17:06:53 +01:00
|
|
|
|
2022-06-19 08:17:05 +02:00
|
|
|
Library::Container::Yield astContainerYield(const Token* tok, const Token** ftok)
|
|
|
|
{
|
|
|
|
const Token* ftok2 = getContainerFunction(tok);
|
|
|
|
if (ftok)
|
|
|
|
*ftok = ftok2;
|
|
|
|
if (!ftok2)
|
|
|
|
return Library::Container::Yield::NO_YIELD;
|
|
|
|
return tok->valueType()->container->getYield(ftok2->str());
|
2022-06-16 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-09 17:06:53 +01:00
|
|
|
Library::Container::Yield astFunctionYield(const Token* tok, const Settings* settings, const Token** ftok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return Library::Container::Yield::NO_YIELD;
|
|
|
|
|
|
|
|
const auto* function = settings->library.getFunction(tok);
|
|
|
|
if (!function)
|
|
|
|
return Library::Container::Yield::NO_YIELD;
|
|
|
|
|
|
|
|
if (ftok)
|
|
|
|
*ftok = tok;
|
|
|
|
return function->containerYield;
|
|
|
|
}
|
|
|
|
|
2022-06-03 19:24:59 +02:00
|
|
|
bool astIsRangeBasedForDecl(const Token* tok)
|
|
|
|
{
|
|
|
|
return Token::simpleMatch(tok->astParent(), ":") && Token::simpleMatch(tok->astParent()->astParent(), "(");
|
|
|
|
}
|
|
|
|
|
2023-04-01 09:38:40 +02:00
|
|
|
std::string astCanonicalType(const Token *expr, bool pointedToType)
|
2015-08-10 09:41:06 +02:00
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return "";
|
2023-04-01 09:38:40 +02:00
|
|
|
std::pair<const Token*, const Token*> decl = Token::typeDecl(expr, pointedToType);
|
2020-09-02 07:13:15 +02:00
|
|
|
if (decl.first && decl.second) {
|
2015-08-10 09:41:06 +02:00
|
|
|
std::string ret;
|
2020-09-02 07:13:15 +02:00
|
|
|
for (const Token *type = decl.first; Token::Match(type,"%name%|::") && type != decl.second; type = type->next()) {
|
2015-08-10 09:41:06 +02:00
|
|
|
if (!Token::Match(type, "const|static"))
|
|
|
|
ret += type->str();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
2022-08-14 12:44:09 +02:00
|
|
|
} else if (comp == "!=" && rhs == "0") {
|
2020-11-10 15:59:51 +01:00
|
|
|
if (tok->str() == "!") {
|
|
|
|
ret = tok->astOperand1();
|
|
|
|
// handle (!(x==0)) as (x!=0)
|
|
|
|
astIsVariableComparison(ret, "==", "0", &ret);
|
|
|
|
} else
|
|
|
|
ret = tok;
|
2022-08-14 12:44:09 +02:00
|
|
|
} else if (comp == "==" && rhs == "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();
|
2022-07-24 12:15:04 +02:00
|
|
|
if (ret && ret->str() == "=" && ret->astOperand1() && ret->astOperand1()->varId())
|
|
|
|
ret = ret->astOperand1();
|
2019-01-08 20:53:53 +01:00
|
|
|
else if (ret && ret->varId() == 0U)
|
2015-08-03 09:20:50 +02:00
|
|
|
ret = nullptr;
|
|
|
|
if (vartok)
|
|
|
|
*vartok = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-28 18:54:55 +01:00
|
|
|
bool isVariableDecl(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Variable* var = tok->variable();
|
|
|
|
if (!var)
|
|
|
|
return false;
|
|
|
|
if (var->nameToken() == tok)
|
|
|
|
return true;
|
2022-08-20 20:50:14 +02:00
|
|
|
const Token * const varDeclEndToken = var->declEndToken();
|
2023-03-17 13:51:55 +01:00
|
|
|
return Token::Match(varDeclEndToken, "; %var%") && varDeclEndToken->next() == tok;
|
2022-02-28 18:54:55 +01:00
|
|
|
}
|
|
|
|
|
2022-08-20 20:52:10 +02:00
|
|
|
bool isStlStringType(const Token* tok)
|
|
|
|
{
|
|
|
|
return Token::Match(tok, "std :: string|wstring|u16string|u32string !!::") ||
|
|
|
|
(Token::simpleMatch(tok, "std :: basic_string <") && !Token::simpleMatch(tok->linkAt(3), "> ::"));
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:01:11 +01:00
|
|
|
bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknown)
|
2019-10-08 09:28:39 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (Token::simpleMatch(tok, "."))
|
2019-11-03 22:02:10 +01:00
|
|
|
return (tok->originalName() != "->" && isTemporary(cpp, tok->astOperand1(), library)) ||
|
|
|
|
isTemporary(cpp, tok->astOperand2(), library);
|
2019-10-08 09:28:39 +02:00
|
|
|
if (Token::Match(tok, ",|::"))
|
2019-11-03 22:02:10 +01:00
|
|
|
return isTemporary(cpp, tok->astOperand2(), library);
|
2019-10-10 06:52:11 +02:00
|
|
|
if (tok->isCast() || (cpp && isCPPCast(tok)))
|
2019-11-03 22:02:10 +01:00
|
|
|
return isTemporary(cpp, tok->astOperand2(), library);
|
2022-12-15 18:34:43 +01:00
|
|
|
if (Token::Match(tok, ".|[|++|--|%name%|%assign%"))
|
2019-10-08 09:28:39 +02:00
|
|
|
return false;
|
|
|
|
if (tok->isUnaryOp("*"))
|
|
|
|
return false;
|
|
|
|
if (Token::Match(tok, "&|<<|>>") && isLikelyStream(cpp, tok->astOperand1()))
|
|
|
|
return false;
|
2022-12-15 18:34:43 +01:00
|
|
|
if (Token::simpleMatch(tok, "?")) {
|
|
|
|
const Token* branchTok = tok->astOperand2();
|
2023-03-07 12:44:54 +01:00
|
|
|
if (!branchTok->astOperand1() || !branchTok->astOperand1()->valueType())
|
2022-12-15 18:34:43 +01:00
|
|
|
return false;
|
2022-12-19 22:28:59 +01:00
|
|
|
if (!branchTok->astOperand2()->valueType())
|
|
|
|
return false;
|
2022-12-15 18:34:43 +01:00
|
|
|
return !branchTok->astOperand1()->valueType()->isTypeEqual(branchTok->astOperand2()->valueType());
|
|
|
|
}
|
2020-09-11 05:59:19 +02:00
|
|
|
if (Token::simpleMatch(tok, "(") && tok->astOperand1() &&
|
|
|
|
(tok->astOperand2() || Token::simpleMatch(tok->next(), ")"))) {
|
2023-01-02 17:46:03 +01:00
|
|
|
if (Token::simpleMatch(tok->astOperand1(), "typeid"))
|
|
|
|
return false;
|
2020-09-09 01:30:45 +02:00
|
|
|
if (tok->valueType()) {
|
|
|
|
return tok->valueType()->reference == Reference::None;
|
|
|
|
}
|
2019-11-03 22:02:10 +01:00
|
|
|
const Token* ftok = nullptr;
|
2020-09-11 05:58:17 +02:00
|
|
|
if (Token::simpleMatch(tok->previous(), ">") && tok->previous()->link())
|
2019-11-03 22:02:10 +01:00
|
|
|
ftok = tok->previous()->link()->previous();
|
|
|
|
else
|
|
|
|
ftok = tok->previous();
|
|
|
|
if (!ftok)
|
|
|
|
return false;
|
2023-06-20 18:43:21 +02:00
|
|
|
if (const Function * f = ftok->function())
|
2019-11-03 22:02:10 +01:00
|
|
|
return !Function::returnsReference(f, true);
|
2023-06-20 18:43:21 +02:00
|
|
|
if (ftok->type())
|
2021-10-15 10:59:40 +02:00
|
|
|
return true;
|
2023-06-20 18:43:21 +02:00
|
|
|
if (library) {
|
2019-11-03 22:02:10 +01:00
|
|
|
std::string returnType = library->returnValueType(ftok);
|
|
|
|
return !returnType.empty() && returnType.back() != '&';
|
|
|
|
}
|
2023-06-20 18:43:21 +02:00
|
|
|
return unknown;
|
2019-10-29 19:12:58 +01:00
|
|
|
}
|
2020-04-01 22:35:41 +02:00
|
|
|
if (tok->isCast())
|
|
|
|
return false;
|
|
|
|
// Currying a function is unknown in cppcheck
|
|
|
|
if (Token::simpleMatch(tok, "(") && Token::simpleMatch(tok->astOperand1(), "("))
|
|
|
|
return unknown;
|
2020-09-17 08:33:16 +02:00
|
|
|
if (Token::simpleMatch(tok, "{") && Token::simpleMatch(tok->astParent(), "return") && tok->astOperand1() &&
|
|
|
|
!tok->astOperand2())
|
|
|
|
return isTemporary(cpp, tok->astOperand1(), library);
|
2019-10-08 09:28:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2020-11-10 16:00:55 +01:00
|
|
|
static T* previousBeforeAstLeftmostLeafGeneric(T* tok)
|
|
|
|
{
|
2021-07-18 07:46:31 +02:00
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
2020-11-10 16:00:55 +01:00
|
|
|
T* leftmostLeaf = tok;
|
2021-07-18 07:46:31 +02:00
|
|
|
while (leftmostLeaf->astOperand1())
|
2020-11-10 16:00:55 +01:00
|
|
|
leftmostLeaf = leftmostLeaf->astOperand1();
|
|
|
|
return leftmostLeaf->previous();
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:15:36 +01:00
|
|
|
const Token* previousBeforeAstLeftmostLeaf(const Token* tok)
|
|
|
|
{
|
|
|
|
return previousBeforeAstLeftmostLeafGeneric(tok);
|
|
|
|
}
|
|
|
|
Token* previousBeforeAstLeftmostLeaf(Token* tok)
|
|
|
|
{
|
|
|
|
return previousBeforeAstLeftmostLeafGeneric(tok);
|
|
|
|
}
|
2020-11-10 16:00:55 +01:00
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2020-02-13 16:27:06 +01:00
|
|
|
static T* nextAfterAstRightmostLeafGeneric(T* tok)
|
2018-10-18 11:56:23 +02:00
|
|
|
{
|
2023-02-08 21:07:16 +01:00
|
|
|
T * rightmostLeaf = tok;
|
2018-11-10 16:40:40 +01:00
|
|
|
if (!rightmostLeaf || !rightmostLeaf->astOperand1())
|
2018-10-18 11:56:23 +02:00
|
|
|
return nullptr;
|
2018-11-10 16:40:40 +01:00
|
|
|
do {
|
2023-02-08 21:07:16 +01:00
|
|
|
if (T* lam = findLambdaEndToken(rightmostLeaf)) {
|
2020-09-05 07:56:01 +02:00
|
|
|
rightmostLeaf = lam;
|
|
|
|
break;
|
|
|
|
}
|
2021-08-04 21:07:31 +02:00
|
|
|
if (rightmostLeaf->astOperand2() && precedes(rightmostLeaf, rightmostLeaf->astOperand2()))
|
2018-11-10 16:40:40 +01:00
|
|
|
rightmostLeaf = rightmostLeaf->astOperand2();
|
2021-08-04 21:07:31 +02:00
|
|
|
else if (rightmostLeaf->astOperand1() && precedes(rightmostLeaf, rightmostLeaf->astOperand1()))
|
2018-11-10 16:40:40 +01:00
|
|
|
rightmostLeaf = rightmostLeaf->astOperand1();
|
2021-08-04 21:07:31 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while (rightmostLeaf->astOperand1() || rightmostLeaf->astOperand2());
|
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();
|
2021-08-04 21:07:31 +02:00
|
|
|
if (Token::Match(rightmostLeaf, "{|(|[") && rightmostLeaf->link())
|
2018-11-10 16:40:40 +01:00
|
|
|
rightmostLeaf = rightmostLeaf->link();
|
|
|
|
return rightmostLeaf->next();
|
2018-10-18 11:56:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
const Token* nextAfterAstRightmostLeaf(const Token* tok)
|
|
|
|
{
|
|
|
|
return nextAfterAstRightmostLeafGeneric(tok);
|
|
|
|
}
|
|
|
|
Token* nextAfterAstRightmostLeaf(Token* tok)
|
|
|
|
{
|
|
|
|
return nextAfterAstRightmostLeafGeneric(tok);
|
|
|
|
}
|
2020-02-13 16:27:06 +01: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;
|
2021-10-05 08:23:47 +02:00
|
|
|
if (Token::Match(parent->previous(), "%name% (") ||
|
|
|
|
(Token::simpleMatch(parent->previous(), "> (") && parent->previous()->link()))
|
|
|
|
return parent;
|
2019-08-15 21:14:27 +02:00
|
|
|
return astParentSkipParens(parent);
|
|
|
|
}
|
|
|
|
|
2019-11-05 07:10:32 +01:00
|
|
|
const Token* getParentMember(const Token * tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return tok;
|
|
|
|
const Token * parent = tok->astParent();
|
|
|
|
if (!Token::simpleMatch(parent, "."))
|
|
|
|
return tok;
|
2021-11-01 19:23:15 +01:00
|
|
|
if (astIsRHS(tok)) {
|
|
|
|
if (Token::simpleMatch(parent->astOperand1(), "."))
|
|
|
|
return parent->astOperand1()->astOperand2();
|
2019-11-05 07:10:32 +01:00
|
|
|
return parent->astOperand1();
|
2021-11-01 19:23:15 +01:00
|
|
|
}
|
2019-11-05 07:10:32 +01:00
|
|
|
const Token * gparent = parent->astParent();
|
|
|
|
if (!Token::simpleMatch(gparent, ".") || gparent->astOperand2() != parent)
|
|
|
|
return tok;
|
|
|
|
if (gparent->astOperand1())
|
|
|
|
return gparent->astOperand1();
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:10:10 +02:00
|
|
|
const Token* getParentLifetime(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return tok;
|
2022-02-07 18:45:32 +01:00
|
|
|
// Skipping checking for variable if its a pointer-to-member
|
|
|
|
if (!Token::simpleMatch(tok->previous(), ". *")) {
|
|
|
|
const Variable* var = tok->variable();
|
|
|
|
// TODO: Call getLifetimeVariable for deeper analysis
|
|
|
|
if (!var)
|
|
|
|
return tok;
|
|
|
|
if (var->isLocal() || var->isArgument())
|
|
|
|
return tok;
|
|
|
|
}
|
2020-05-31 10:10:10 +02:00
|
|
|
const Token* parent = getParentMember(tok);
|
|
|
|
if (parent != tok)
|
|
|
|
return getParentLifetime(parent);
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2022-02-24 06:50:34 +01:00
|
|
|
static std::vector<const Token*> getParentMembers(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return {};
|
|
|
|
if (!Token::simpleMatch(tok->astParent(), "."))
|
|
|
|
return {tok};
|
2022-08-21 17:39:21 +02:00
|
|
|
const Token* parent = tok->astParent();
|
2022-02-24 06:50:34 +01:00
|
|
|
while (Token::simpleMatch(parent->astParent(), "."))
|
|
|
|
parent = parent->astParent();
|
|
|
|
std::vector<const Token*> result;
|
|
|
|
for (const Token* tok2 : astFlatten(parent, ".")) {
|
|
|
|
if (Token::simpleMatch(tok2, "(") && Token::simpleMatch(tok2->astOperand1(), ".")) {
|
|
|
|
std::vector<const Token*> sub = getParentMembers(tok2->astOperand1());
|
2022-12-30 15:13:47 +01:00
|
|
|
result.insert(result.end(), sub.cbegin(), sub.cend());
|
2022-02-24 06:50:34 +01:00
|
|
|
}
|
|
|
|
result.push_back(tok2);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token* getParentLifetime(bool cpp, const Token* tok, const Library* library)
|
|
|
|
{
|
|
|
|
std::vector<const Token*> members = getParentMembers(tok);
|
|
|
|
if (members.size() < 2)
|
|
|
|
return tok;
|
|
|
|
// Find the first local variable or temporary
|
2022-12-30 15:13:47 +01:00
|
|
|
auto it = std::find_if(members.crbegin(), members.crend(), [&](const Token* tok2) {
|
2022-02-24 06:50:34 +01:00
|
|
|
const Variable* var = tok2->variable();
|
2023-06-20 18:43:21 +02:00
|
|
|
if (var)
|
2022-02-24 06:50:34 +01:00
|
|
|
return var->isLocal() || var->isArgument();
|
2023-06-20 18:43:21 +02:00
|
|
|
return isTemporary(cpp, tok2, library);
|
2022-02-24 06:50:34 +01:00
|
|
|
});
|
|
|
|
if (it == members.rend())
|
|
|
|
return tok;
|
|
|
|
// If any of the submembers are borrowed types then stop
|
2022-12-30 15:13:47 +01:00
|
|
|
if (std::any_of(it.base() - 1, members.cend() - 1, [&](const Token* tok2) {
|
2022-02-24 06:50:34 +01:00
|
|
|
if (astIsPointer(tok2) || astIsContainerView(tok2) || astIsIterator(tok2))
|
|
|
|
return true;
|
|
|
|
if (!astIsUniqueSmartPointer(tok2)) {
|
|
|
|
if (astIsSmartPointer(tok2))
|
|
|
|
return true;
|
|
|
|
const Token* dotTok = tok2->next();
|
|
|
|
if (!Token::simpleMatch(dotTok, ".")) {
|
|
|
|
const Token* endTok = nextAfterAstRightmostLeaf(tok2);
|
|
|
|
if (!endTok)
|
|
|
|
dotTok = tok2->next();
|
|
|
|
else if (Token::simpleMatch(endTok, "."))
|
|
|
|
dotTok = endTok;
|
|
|
|
else if (Token::simpleMatch(endTok->next(), "."))
|
|
|
|
dotTok = endTok->next();
|
|
|
|
}
|
|
|
|
// If we are dereferencing the member variable then treat it as borrowed
|
|
|
|
if (Token::simpleMatch(dotTok, ".") && dotTok->originalName() == "->")
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const Variable* var = tok2->variable();
|
|
|
|
return var && var->isReference();
|
|
|
|
}))
|
|
|
|
return nullptr;
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
2022-09-04 10:24:45 +02:00
|
|
|
static bool isInConstructorList(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (!astIsRHS(tok))
|
|
|
|
return false;
|
|
|
|
const Token* parent = tok->astParent();
|
|
|
|
if (!Token::Match(parent, "{|("))
|
|
|
|
return false;
|
|
|
|
if (!Token::Match(parent->previous(), "%var% {|("))
|
|
|
|
return false;
|
|
|
|
if (!parent->astOperand1() || !parent->astOperand2())
|
|
|
|
return false;
|
|
|
|
do {
|
|
|
|
parent = parent->astParent();
|
|
|
|
} while (Token::simpleMatch(parent, ","));
|
|
|
|
return Token::simpleMatch(parent, ":") && !Token::simpleMatch(parent->astParent(), "?");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* settings, const Token** parent)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return {};
|
|
|
|
if (!tok->astParent())
|
|
|
|
return {};
|
|
|
|
if (isInConstructorList(tok)) {
|
|
|
|
if (parent)
|
|
|
|
*parent = tok->astParent()->astOperand1();
|
|
|
|
if (tok->astParent()->astOperand1()->valueType())
|
|
|
|
return {*tok->astParent()->astOperand1()->valueType()};
|
|
|
|
return {};
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (Token::Match(tok->astParent(), "(|{|,")) {
|
2022-09-04 10:24:45 +02:00
|
|
|
int argn = -1;
|
|
|
|
const Token* ftok = getTokenArgumentFunction(tok, argn);
|
|
|
|
const Token* typeTok = nullptr;
|
|
|
|
if (ftok && argn >= 0) {
|
|
|
|
if (ftok->function()) {
|
|
|
|
std::vector<ValueType> result;
|
|
|
|
const Token* nameTok = nullptr;
|
|
|
|
for (const Variable* var : getArgumentVars(ftok, argn)) {
|
|
|
|
if (!var)
|
|
|
|
continue;
|
|
|
|
if (!var->valueType())
|
|
|
|
continue;
|
|
|
|
nameTok = var->nameToken();
|
|
|
|
result.push_back(*var->valueType());
|
|
|
|
}
|
|
|
|
if (result.size() == 1 && nameTok && parent) {
|
|
|
|
*parent = nameTok;
|
|
|
|
}
|
|
|
|
return result;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (const Type* t = Token::typeOf(ftok, &typeTok)) {
|
2022-09-04 10:24:45 +02:00
|
|
|
if (astIsPointer(typeTok))
|
|
|
|
return {*typeTok->valueType()};
|
|
|
|
const Scope* scope = t->classScope;
|
|
|
|
// Check for aggregate constructors
|
|
|
|
if (scope && scope->numConstructors == 0 && t->derivedFrom.empty() &&
|
|
|
|
(t->isClassType() || t->isStructType()) && numberOfArguments(ftok) < scope->varlist.size()) {
|
|
|
|
assert(argn < scope->varlist.size());
|
2022-12-30 15:13:47 +01:00
|
|
|
auto it = std::next(scope->varlist.cbegin(), argn);
|
2022-09-04 10:24:45 +02:00
|
|
|
if (it->valueType())
|
|
|
|
return {*it->valueType()};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (settings && Token::Match(tok->astParent()->tokAt(-2), ". push_back|push_front|insert|push (") &&
|
|
|
|
astIsContainer(tok->astParent()->tokAt(-2)->astOperand1())) {
|
|
|
|
const Token* contTok = tok->astParent()->tokAt(-2)->astOperand1();
|
|
|
|
const ValueType* vtCont = contTok->valueType();
|
|
|
|
if (!vtCont->containerTypeToken)
|
|
|
|
return {};
|
2023-03-07 12:22:06 +01:00
|
|
|
ValueType vtParent = ValueType::parseDecl(vtCont->containerTypeToken, *settings, true); // TODO: set isCpp
|
2022-09-04 10:24:45 +02:00
|
|
|
return {std::move(vtParent)};
|
|
|
|
}
|
|
|
|
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {
|
|
|
|
*parent = tok->astParent();
|
|
|
|
}
|
|
|
|
if (tok->astParent()->valueType())
|
|
|
|
return {*tok->astParent()->valueType()};
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:45:10 +01:00
|
|
|
bool astIsLHS(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Token* parent = tok->astParent();
|
|
|
|
if (!parent)
|
|
|
|
return false;
|
|
|
|
if (!parent->astOperand1())
|
|
|
|
return false;
|
|
|
|
if (!parent->astOperand2())
|
|
|
|
return false;
|
|
|
|
return parent->astOperand1() == tok;
|
|
|
|
}
|
|
|
|
bool astIsRHS(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Token* parent = tok->astParent();
|
|
|
|
if (!parent)
|
|
|
|
return false;
|
|
|
|
if (!parent->astOperand1())
|
|
|
|
return false;
|
|
|
|
if (!parent->astOperand2())
|
|
|
|
return false;
|
|
|
|
return parent->astOperand2() == tok;
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2020-02-13 16:27:06 +01:00
|
|
|
static T* getCondTokImpl(T* 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();
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2020-02-13 16:27:06 +01:00
|
|
|
static T* getCondTokFromEndImpl(T* endBlock)
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(endBlock, "}"))
|
|
|
|
return nullptr;
|
|
|
|
T* startBlock = endBlock->link();
|
|
|
|
if (!Token::simpleMatch(startBlock, "{"))
|
|
|
|
return nullptr;
|
2023-06-20 18:43:21 +02:00
|
|
|
if (Token::simpleMatch(startBlock->previous(), ")"))
|
2020-02-13 16:27:06 +01:00
|
|
|
return getCondTok(startBlock->previous()->link());
|
2023-06-20 18:43:21 +02:00
|
|
|
if (Token::simpleMatch(startBlock->tokAt(-2), "} else {"))
|
2020-02-13 16:27:06 +01:00
|
|
|
return getCondTokFromEnd(startBlock->tokAt(-2));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-11-14 18:30:36 +01:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
|
|
|
static T* getInitTokImpl(T* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::Match(tok, "%name% ("))
|
|
|
|
return getInitTokImpl(tok->next());
|
|
|
|
if (tok->str() != "(")
|
|
|
|
return nullptr;
|
|
|
|
if (!Token::simpleMatch(tok->astOperand2(), ";"))
|
|
|
|
return nullptr;
|
|
|
|
if (Token::simpleMatch(tok->astOperand2()->astOperand1(), ";"))
|
|
|
|
return nullptr;
|
|
|
|
return tok->astOperand2()->astOperand1();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
|
|
|
static T* getStepTokImpl(T* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::Match(tok, "%name% ("))
|
|
|
|
return getStepTokImpl(tok->next());
|
|
|
|
if (tok->str() != "(")
|
|
|
|
return nullptr;
|
|
|
|
if (!Token::simpleMatch(tok->astOperand2(), ";"))
|
|
|
|
return nullptr;
|
|
|
|
if (!Token::simpleMatch(tok->astOperand2()->astOperand2(), ";"))
|
|
|
|
return nullptr;
|
|
|
|
return tok->astOperand2()->astOperand2()->astOperand2();
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
Token* getCondTok(Token* tok)
|
|
|
|
{
|
|
|
|
return getCondTokImpl(tok);
|
|
|
|
}
|
|
|
|
const Token* getCondTok(const Token* tok)
|
|
|
|
{
|
|
|
|
return getCondTokImpl(tok);
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
Token* getCondTokFromEnd(Token* endBlock)
|
|
|
|
{
|
|
|
|
return getCondTokFromEndImpl(endBlock);
|
|
|
|
}
|
|
|
|
const Token* getCondTokFromEnd(const Token* endBlock)
|
|
|
|
{
|
|
|
|
return getCondTokFromEndImpl(endBlock);
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-11-14 18:30:36 +01:00
|
|
|
Token* getInitTok(Token* tok) {
|
|
|
|
return getInitTokImpl(tok);
|
|
|
|
}
|
|
|
|
const Token* getInitTok(const Token* tok) {
|
|
|
|
return getInitTokImpl(tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token* getStepTok(Token* tok) {
|
|
|
|
return getStepTokImpl(tok);
|
|
|
|
}
|
|
|
|
const Token* getStepTok(const Token* tok) {
|
|
|
|
return getStepTokImpl(tok);
|
|
|
|
}
|
|
|
|
|
2020-12-25 08:43:14 +01:00
|
|
|
const Token *findNextTokenFromBreak(const Token *breakToken)
|
|
|
|
{
|
|
|
|
const Scope *scope = breakToken->scope();
|
|
|
|
while (scope) {
|
|
|
|
if (scope->isLoopScope() || scope->type == Scope::ScopeType::eSwitch) {
|
|
|
|
if (scope->type == Scope::ScopeType::eDo && Token::simpleMatch(scope->bodyEnd, "} while ("))
|
|
|
|
return scope->bodyEnd->linkAt(2)->next();
|
|
|
|
return scope->bodyEnd;
|
|
|
|
}
|
|
|
|
scope = scope->nestedIn;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2020-12-24 22:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:41:27 +02:00
|
|
|
bool extractForLoopValues(const Token *forToken,
|
2023-03-02 22:05:41 +01:00
|
|
|
nonneg int &varid,
|
|
|
|
bool &knownInitValue,
|
|
|
|
MathLib::bigint &initValue,
|
|
|
|
bool &partialCond,
|
|
|
|
MathLib::bigint &stepValue,
|
|
|
|
MathLib::bigint &lastValue)
|
2020-06-28 13:41:27 +02:00
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(forToken, "for (") || !Token::simpleMatch(forToken->next()->astOperand2(), ";"))
|
|
|
|
return false;
|
|
|
|
const Token *initExpr = forToken->next()->astOperand2()->astOperand1();
|
|
|
|
const Token *condExpr = forToken->next()->astOperand2()->astOperand2()->astOperand1();
|
|
|
|
const Token *incExpr = forToken->next()->astOperand2()->astOperand2()->astOperand2();
|
2020-06-29 21:01:43 +02:00
|
|
|
if (!initExpr || !initExpr->isBinaryOp() || initExpr->str() != "=" || !Token::Match(initExpr->astOperand1(), "%var%"))
|
2020-06-28 13:41:27 +02:00
|
|
|
return false;
|
2023-01-28 10:20:47 +01:00
|
|
|
std::vector<MathLib::bigint> minInitValue = getMinValue(ValueFlow::makeIntegralInferModel(), initExpr->astOperand2()->values());
|
2023-03-02 22:05:41 +01:00
|
|
|
varid = initExpr->astOperand1()->varId();
|
|
|
|
knownInitValue = initExpr->astOperand2()->hasKnownIntValue();
|
|
|
|
initValue = minInitValue.empty() ? 0 : minInitValue.front();
|
|
|
|
partialCond = Token::Match(condExpr, "%oror%|&&");
|
2020-06-29 21:53:14 +02:00
|
|
|
visitAstNodes(condExpr, [varid, &condExpr](const Token *tok) {
|
|
|
|
if (Token::Match(tok, "%oror%|&&"))
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
2023-03-02 22:05:41 +01:00
|
|
|
if (Token::Match(tok, "<|<=") && tok->isBinaryOp() && tok->astOperand1()->varId() == varid && tok->astOperand2()->hasKnownIntValue()) {
|
2020-06-29 21:53:14 +02:00
|
|
|
if (Token::Match(condExpr, "%oror%|&&") || tok->astOperand2()->getKnownIntValue() < condExpr->astOperand2()->getKnownIntValue())
|
|
|
|
condExpr = tok;
|
|
|
|
}
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
});
|
2023-03-02 22:05:41 +01:00
|
|
|
if (!Token::Match(condExpr, "<|<=") || !condExpr->isBinaryOp() || condExpr->astOperand1()->varId() != varid || !condExpr->astOperand2()->hasKnownIntValue())
|
2020-06-28 13:41:27 +02:00
|
|
|
return false;
|
2023-03-02 22:05:41 +01:00
|
|
|
if (!incExpr || !incExpr->isUnaryOp("++") || incExpr->astOperand1()->varId() != varid)
|
2020-06-28 13:41:27 +02:00
|
|
|
return false;
|
2023-03-02 22:05:41 +01:00
|
|
|
stepValue = 1;
|
2020-06-28 13:41:27 +02:00
|
|
|
if (condExpr->str() == "<")
|
2023-03-02 22:05:41 +01:00
|
|
|
lastValue = condExpr->astOperand2()->getKnownIntValue() - 1;
|
2020-06-28 13:41:27 +02:00
|
|
|
else
|
2023-03-02 22:05:41 +01:00
|
|
|
lastValue = condExpr->astOperand2()->getKnownIntValue();
|
2020-06-28 13:41:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-07 09:32:16 +02:00
|
|
|
static const Token * getVariableInitExpression(const Variable * var)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
2021-01-16 19:05:51 +01:00
|
|
|
if (!var)
|
|
|
|
return nullptr;
|
|
|
|
const Token *varDeclEndToken = var->declEndToken();
|
|
|
|
if (!varDeclEndToken)
|
2018-08-07 09:32:16 +02:00
|
|
|
return nullptr;
|
2021-01-16 19:05:51 +01:00
|
|
|
if (Token::Match(varDeclEndToken, "; %varid% =", var->declarationId()))
|
|
|
|
return varDeclEndToken->tokAt(2)->astOperand2();
|
|
|
|
return varDeclEndToken->astOperand2();
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-18 16:57:22 +01:00
|
|
|
const Token* isInLoopCondition(const Token* tok)
|
2018-08-07 18:06:51 +02:00
|
|
|
{
|
2023-01-18 16:57:22 +01:00
|
|
|
const Token* top = tok->astTop();
|
|
|
|
return top && Token::Match(top->previous(), "for|while (") ? top : nullptr;
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-11-25 22:34:00 +01:00
|
|
|
if (tok1 == tok2)
|
|
|
|
return false;
|
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)
|
2021-07-07 08:20:32 +02:00
|
|
|
return true;
|
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
|
|
|
|
2021-11-25 22:34:00 +01:00
|
|
|
/// If tok1 comes after tok2
|
2022-01-13 17:24:26 +01:00
|
|
|
bool succeeds(const Token* tok1, const Token* tok2)
|
2021-11-25 22:34:00 +01:00
|
|
|
{
|
|
|
|
if (tok1 == tok2)
|
|
|
|
return false;
|
|
|
|
if (!tok1)
|
|
|
|
return false;
|
|
|
|
if (!tok2)
|
|
|
|
return true;
|
|
|
|
return tok1->index() > tok2->index();
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:27:09 +02:00
|
|
|
bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive)
|
2019-09-11 19:25:09 +02:00
|
|
|
{
|
|
|
|
if (tok->varId() == varid)
|
|
|
|
return false;
|
|
|
|
for (const ValueFlow::Value &val : tok->values()) {
|
|
|
|
if (!val.isLocalLifetimeValue())
|
|
|
|
continue;
|
2020-09-20 14:27:09 +02:00
|
|
|
if (val.tokvalue->varId() == varid) {
|
|
|
|
if (val.isInconclusive()) {
|
|
|
|
if (inconclusive)
|
|
|
|
*inconclusive = true;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-11 19:25:09 +02:00
|
|
|
return true;
|
2020-09-20 14:27:09 +02:00
|
|
|
}
|
2019-09-11 19:25:09 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-28 22:40:59 +02:00
|
|
|
bool isAliasOf(const Token* tok, const Token* expr, bool* inconclusive)
|
|
|
|
{
|
|
|
|
const bool pointer = astIsPointer(tok);
|
|
|
|
const ValueFlow::Value* value = nullptr;
|
|
|
|
const Token* r = findAstNode(expr, [&](const Token* childTok) {
|
|
|
|
for (const ValueFlow::Value& val : tok->values()) {
|
|
|
|
if (val.isImpossible())
|
|
|
|
continue;
|
|
|
|
if (val.isLocalLifetimeValue() || (pointer && val.isSymbolicValue() && val.intvalue == 0)) {
|
|
|
|
if (findAstNode(val.tokvalue,
|
|
|
|
[&](const Token* aliasTok) {
|
|
|
|
return aliasTok->exprId() == childTok->exprId();
|
|
|
|
})) {
|
2022-08-21 17:39:21 +02:00
|
|
|
if (val.isInconclusive() && inconclusive != nullptr) {
|
2022-06-28 22:40:59 +02:00
|
|
|
value = &val;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (!r && value && inconclusive)
|
|
|
|
*inconclusive = true;
|
|
|
|
return r || value;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-10-23 14:47:10 +02:00
|
|
|
bool exprDependsOnThis(const Token* expr, bool onVar, nonneg int depth)
|
2018-10-07 18:30:29 +02:00
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return false;
|
2021-04-19 09:15:03 +02:00
|
|
|
if (expr->str() == "this")
|
|
|
|
return true;
|
2019-03-26 18:57:01 +01:00
|
|
|
if (depth >= 1000)
|
|
|
|
// Abort recursion to avoid stack overflow
|
|
|
|
return true;
|
|
|
|
++depth;
|
2022-06-10 20:17:57 +02:00
|
|
|
|
2018-10-07 18:30:29 +02:00
|
|
|
// calling nonstatic method?
|
2022-06-10 20:17:57 +02:00
|
|
|
if (Token::Match(expr, "%name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct() && !expr->function()->isStatic()) {
|
2018-10-07 18:30:29 +02:00
|
|
|
// is it a method of this?
|
2020-07-13 20:55:45 +02:00
|
|
|
const Scope* fScope = expr->scope();
|
2020-07-13 19:40:01 +02:00
|
|
|
while (!fScope->functionOf && fScope->nestedIn)
|
|
|
|
fScope = fScope->nestedIn;
|
2021-11-12 20:05:43 +01:00
|
|
|
|
|
|
|
const Scope* classScope = fScope->functionOf;
|
|
|
|
if (classScope && classScope->function)
|
|
|
|
classScope = classScope->function->token->scope();
|
|
|
|
|
|
|
|
if (classScope && classScope->isClassOrStruct())
|
|
|
|
return contains(classScope->findAssociatedScopes(), expr->function()->nestedIn);
|
|
|
|
return false;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (onVar && expr->variable()) {
|
2019-09-30 21:04:43 +02:00
|
|
|
const Variable* var = expr->variable();
|
2022-08-02 18:18:46 +02:00
|
|
|
return ((var->isPrivate() || var->isPublic() || var->isProtected()) && !var->isStatic());
|
2018-10-07 18:30:29 +02:00
|
|
|
}
|
2019-09-30 21:04:43 +02:00
|
|
|
if (Token::simpleMatch(expr, "."))
|
2021-10-23 14:47:10 +02:00
|
|
|
return exprDependsOnThis(expr->astOperand1(), onVar, depth);
|
|
|
|
return exprDependsOnThis(expr->astOperand1(), onVar, depth) || exprDependsOnThis(expr->astOperand2(), onVar, depth);
|
2018-10-07 18:30:29 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 17:13:44 +01:00
|
|
|
static bool hasUnknownVars(const Token* startTok)
|
|
|
|
{
|
2021-01-05 12:07:27 +01:00
|
|
|
bool result = false;
|
|
|
|
visitAstNodes(startTok, [&](const Token* tok) {
|
|
|
|
if (tok->varId() > 0 && !tok->variable()) {
|
|
|
|
result = true;
|
|
|
|
return ChildrenToVisit::done;
|
|
|
|
}
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-29 17:06:33 +02:00
|
|
|
bool isStructuredBindingVariable(const Variable* var)
|
2022-01-02 08:14:50 +01:00
|
|
|
{
|
|
|
|
if (!var)
|
|
|
|
return false;
|
|
|
|
const Token* tok = var->nameToken();
|
2022-05-29 17:06:33 +02:00
|
|
|
while (tok && Token::Match(tok->astParent(), "[|,|:"))
|
2022-01-02 08:14:50 +01:00
|
|
|
tok = tok->astParent();
|
2022-05-29 17:06:33 +02:00
|
|
|
return tok && (tok->str() == "[" || Token::simpleMatch(tok->previous(), "] :")); // TODO: remove workaround when #11105 is fixed
|
2022-01-02 08:14:50 +01: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;
|
2021-01-05 12:07:27 +01:00
|
|
|
if (hasUnknownVars(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;
|
2022-01-02 08:14:50 +01:00
|
|
|
if (isStructuredBindingVariable(var))
|
|
|
|
return tok;
|
2022-04-07 10:42:57 +02:00
|
|
|
// assigning a floating point value to an integer does not preserve the value
|
|
|
|
if (var->valueType() && var->valueType()->isIntegral() && varTok->valueType() && varTok->valueType()->isFloat())
|
|
|
|
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;
|
2021-01-05 12:07:27 +01:00
|
|
|
const Token* startToken = nextAfterAstRightmostLeaf(varTok);
|
|
|
|
if (!startToken)
|
|
|
|
startToken = varTok;
|
|
|
|
if (varTok->exprId() == 0) {
|
|
|
|
if (!varTok->isLiteral())
|
2020-04-18 19:54:55 +02:00
|
|
|
return tok;
|
2021-01-11 08:00:13 +01:00
|
|
|
} else if (!precedes(startToken, endToken)) {
|
|
|
|
return tok;
|
2021-01-05 12:07:27 +01:00
|
|
|
} else if (isExpressionChanged(varTok, startToken, endToken, nullptr, cpp)) {
|
|
|
|
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.");
|
2022-12-30 15:13:47 +01:00
|
|
|
if (std::find(errors->cbegin(), errors->cend(), item) != errors->cend())
|
2018-08-18 07:32:30 +02:00
|
|
|
return;
|
2022-09-08 09:21:35 +02:00
|
|
|
errors->push_back(std::move(item));
|
2018-08-07 09:32:16 +02:00
|
|
|
}
|
2018-09-28 08:38:24 +02:00
|
|
|
|
2022-09-29 21:41:32 +02:00
|
|
|
SmallVector<ReferenceToken> followAllReferences(const Token* tok,
|
2021-09-05 07:35:33 +02:00
|
|
|
bool temporary,
|
|
|
|
bool inconclusive,
|
|
|
|
ErrorPath errors,
|
|
|
|
int depth)
|
2021-01-22 11:00:57 +01:00
|
|
|
{
|
2021-02-09 15:27:46 +01:00
|
|
|
struct ReferenceTokenLess {
|
2021-02-10 11:42:00 +01:00
|
|
|
bool operator()(const ReferenceToken& x, const ReferenceToken& y) const {
|
2021-02-09 15:27:46 +01:00
|
|
|
return x.token < y.token;
|
|
|
|
}
|
|
|
|
};
|
2022-09-29 21:41:32 +02:00
|
|
|
SmallVector<ReferenceToken> refs_result;
|
2021-01-22 11:00:57 +01:00
|
|
|
if (!tok)
|
2022-09-29 21:41:32 +02:00
|
|
|
return refs_result;
|
|
|
|
if (depth < 0) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
const Variable *var = tok->variable();
|
|
|
|
if (var && var->declarationId() == tok->varId()) {
|
2022-01-02 08:14:50 +01:00
|
|
|
if (var->nameToken() == tok || isStructuredBindingVariable(var)) {
|
2022-09-29 21:41:32 +02:00
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (var->isReference() || var->isRValueReference()) {
|
2022-08-20 20:50:14 +02:00
|
|
|
const Token * const varDeclEndToken = var->declEndToken();
|
2022-09-29 21:41:32 +02:00
|
|
|
if (!varDeclEndToken) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
if (var->isArgument()) {
|
2022-08-20 20:50:14 +02:00
|
|
|
errors.emplace_back(varDeclEndToken, "Passed to reference.");
|
2022-09-29 21:41:32 +02:00
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (Token::simpleMatch(varDeclEndToken, "=")) {
|
2022-08-20 20:50:14 +02:00
|
|
|
if (astHasToken(varDeclEndToken, tok))
|
2022-09-29 21:41:32 +02:00
|
|
|
return refs_result;
|
2022-08-20 20:50:14 +02:00
|
|
|
errors.emplace_back(varDeclEndToken, "Assigned to reference.");
|
|
|
|
const Token *vartok = varDeclEndToken->astOperand2();
|
2021-09-05 07:35:33 +02:00
|
|
|
if (vartok == tok || (!temporary && isTemporary(true, vartok, nullptr, true) &&
|
2022-09-29 21:41:32 +02:00
|
|
|
(var->isConst() || var->isRValueReference()))) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
if (vartok)
|
2021-09-05 07:35:33 +02:00
|
|
|
return followAllReferences(vartok, temporary, inconclusive, std::move(errors), depth - 1);
|
2021-01-22 11:00:57 +01:00
|
|
|
} else {
|
2022-09-29 21:41:32 +02:00
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
2021-01-22 11:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-09 15:27:46 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "?") && Token::simpleMatch(tok->astOperand2(), ":")) {
|
|
|
|
std::set<ReferenceToken, ReferenceTokenLess> result;
|
|
|
|
const Token* tok2 = tok->astOperand2();
|
|
|
|
|
2022-09-29 21:41:32 +02:00
|
|
|
auto refs = followAllReferences(tok2->astOperand1(), temporary, inconclusive, errors, depth - 1);
|
2022-12-30 15:13:47 +01:00
|
|
|
result.insert(refs.cbegin(), refs.cend());
|
2021-09-05 07:35:33 +02:00
|
|
|
refs = followAllReferences(tok2->astOperand2(), temporary, inconclusive, errors, depth - 1);
|
2022-12-30 15:13:47 +01:00
|
|
|
result.insert(refs.cbegin(), refs.cend());
|
2021-02-09 15:27:46 +01:00
|
|
|
|
2022-09-29 21:41:32 +02:00
|
|
|
if (!inconclusive && result.size() != 1) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
2021-02-09 15:27:46 +01:00
|
|
|
|
2022-09-29 21:41:32 +02:00
|
|
|
if (!result.empty()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
refs_result.insert(refs_result.end(), result.cbegin(), result.cend());
|
2022-09-29 21:41:32 +02:00
|
|
|
return refs_result;
|
|
|
|
}
|
2021-02-09 15:27:46 +01:00
|
|
|
|
2023-03-02 21:45:15 +01:00
|
|
|
} else if (tok->previous() && tok->previous()->function() && Token::Match(tok->previous(), "%name% (")) {
|
2021-01-22 11:00:57 +01:00
|
|
|
const Function *f = tok->previous()->function();
|
2023-03-02 21:45:15 +01:00
|
|
|
if (!Function::returnsReference(f)) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
|
|
|
std::set<ReferenceToken, ReferenceTokenLess> result;
|
|
|
|
std::vector<const Token*> returns = Function::findReturns(f);
|
|
|
|
for (const Token* returnTok : returns) {
|
|
|
|
if (returnTok == tok)
|
|
|
|
continue;
|
|
|
|
for (const ReferenceToken& rt :
|
|
|
|
followAllReferences(returnTok, temporary, inconclusive, errors, depth - returns.size())) {
|
|
|
|
const Variable* argvar = rt.token->variable();
|
|
|
|
if (!argvar) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
|
|
|
if (argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {
|
|
|
|
const int n = getArgumentPos(argvar, f);
|
|
|
|
if (n < 0) {
|
2022-09-29 21:41:32 +02:00
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
2023-03-02 21:45:15 +01:00
|
|
|
std::vector<const Token*> args = getArguments(tok->previous());
|
|
|
|
if (n >= args.size()) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
|
|
|
}
|
|
|
|
const Token* argTok = args[n];
|
|
|
|
ErrorPath er = errors;
|
|
|
|
er.emplace_back(returnTok, "Return reference.");
|
|
|
|
er.emplace_back(tok->previous(), "Called function passing '" + argTok->expressionString() + "'.");
|
|
|
|
auto refs =
|
|
|
|
followAllReferences(argTok, temporary, inconclusive, std::move(er), depth - returns.size());
|
|
|
|
result.insert(refs.cbegin(), refs.cend());
|
|
|
|
if (!inconclusive && result.size() > 1) {
|
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
2021-02-10 11:42:00 +01:00
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-02 21:45:15 +01:00
|
|
|
}
|
|
|
|
if (!result.empty()) {
|
|
|
|
refs_result.insert(refs_result.end(), result.cbegin(), result.cend());
|
|
|
|
return refs_result;
|
2021-01-22 11:00:57 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-29 21:41:32 +02:00
|
|
|
refs_result.push_back({tok, std::move(errors)});
|
|
|
|
return refs_result;
|
2021-02-09 15:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Token* followReferences(const Token* tok, ErrorPath* errors)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
2022-09-29 21:41:32 +02:00
|
|
|
auto refs = followAllReferences(tok, true, false);
|
2021-02-09 15:27:46 +01:00
|
|
|
if (refs.size() == 1) {
|
|
|
|
if (errors)
|
2022-10-06 20:12:07 +02:00
|
|
|
*errors = std::move(refs.front().errors);
|
2021-02-09 15:27:46 +01:00
|
|
|
return refs.front().token;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2021-01-22 11:00:57 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 13:37:28 +01:00
|
|
|
static bool isSameLifetime(const Token * const tok1, const Token * const tok2)
|
|
|
|
{
|
2023-01-28 10:20:47 +01:00
|
|
|
ValueFlow::Value v1 = ValueFlow::getLifetimeObjValue(tok1);
|
2022-08-21 17:39:21 +02:00
|
|
|
if (!v1.isLifetimeValue())
|
|
|
|
return false;
|
2023-01-28 10:20:47 +01:00
|
|
|
ValueFlow::Value v2 = ValueFlow::getLifetimeObjValue(tok2);
|
2022-08-21 17:39:21 +02:00
|
|
|
if (!v2.isLifetimeValue())
|
2020-12-23 13:37:28 +01:00
|
|
|
return false;
|
|
|
|
return v1.tokvalue == v2.tokvalue;
|
|
|
|
}
|
|
|
|
|
2022-09-16 18:58:59 +02:00
|
|
|
static bool compareKnownValue(const Token * const tok1, const Token * const tok2, const std::function<bool(const ValueFlow::Value&, const ValueFlow::Value&, bool)> &compare)
|
2019-09-05 15:15:58 +02:00
|
|
|
{
|
2021-01-16 19:05:51 +01:00
|
|
|
static const auto isKnownFn = std::mem_fn(&ValueFlow::Value::isKnown);
|
|
|
|
|
2022-12-30 15:13:47 +01:00
|
|
|
const auto v1 = std::find_if(tok1->values().cbegin(), tok1->values().cend(), isKnownFn);
|
2021-01-16 19:05:51 +01:00
|
|
|
if (v1 == tok1->values().end()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-30 21:29:35 +02:00
|
|
|
if (v1->isNonValue() || v1->isContainerSizeValue() || v1->isSymbolicValue())
|
2021-01-16 19:05:51 +01:00
|
|
|
return false;
|
2022-12-30 15:13:47 +01:00
|
|
|
const auto v2 = std::find_if(tok2->values().cbegin(), tok2->values().cend(), isKnownFn);
|
2021-01-16 19:05:51 +01:00
|
|
|
if (v2 == tok2->values().end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (v1->valueType != v2->valueType) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const bool sameLifetime = isSameLifetime(tok1, tok2);
|
|
|
|
return compare(*v1, *v2, sameLifetime);
|
2019-09-05 15:15:58 +02:00
|
|
|
}
|
|
|
|
|
2020-12-23 13:37:28 +01:00
|
|
|
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2)
|
|
|
|
{
|
|
|
|
return compareKnownValue(tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) {
|
|
|
|
bool r = v1.equalValue(v2);
|
|
|
|
if (v1.isIteratorValue()) {
|
|
|
|
r &= sameLifetime;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-11 15:10:15 +01:00
|
|
|
static inline bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2)
|
2019-09-05 15:15:58 +02:00
|
|
|
{
|
2020-12-23 13:37:28 +01:00
|
|
|
return compareKnownValue(tok1, tok2, [&](const ValueFlow::Value& v1, const ValueFlow::Value& v2, bool sameLifetime) {
|
|
|
|
bool r = v1.equalValue(v2);
|
|
|
|
if (v1.isIteratorValue()) {
|
|
|
|
r &= sameLifetime;
|
|
|
|
}
|
|
|
|
return !r;
|
2019-09-05 15:15:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-04 14:25:00 +02:00
|
|
|
static inline bool isSameConstantValue(bool macro, const Token* tok1, const Token* tok2)
|
2020-02-01 07:22:41 +01:00
|
|
|
{
|
|
|
|
if (tok1 == nullptr || tok2 == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2022-05-04 14:25:00 +02:00
|
|
|
auto adjustForCast = [](const Token* tok) {
|
2022-09-05 21:36:26 +02:00
|
|
|
if (tok->astOperand2() && Token::Match(tok->previous(), "%type% (|{") && tok->previous()->isStandardType())
|
2022-05-04 14:25:00 +02:00
|
|
|
return tok->astOperand2();
|
|
|
|
return tok;
|
|
|
|
};
|
2022-08-21 17:39:21 +02:00
|
|
|
|
2022-05-04 14:25:00 +02:00
|
|
|
tok1 = adjustForCast(tok1);
|
2022-08-21 17:39:21 +02:00
|
|
|
if (!tok1->isNumber())
|
|
|
|
return false;
|
2022-05-04 14:25:00 +02:00
|
|
|
tok2 = adjustForCast(tok2);
|
2022-08-21 17:39:21 +02:00
|
|
|
if (!tok2->isNumber())
|
2020-02-01 07:22:41 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ValueType * v1 = tok1->valueType();
|
|
|
|
const ValueType * v2 = tok2->valueType();
|
|
|
|
|
|
|
|
if (!v1 || !v2 || v1->sign != v2->sign || v1->type != v2->type || v1->pointer != v2->pointer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return isEqualKnownValue(tok1, tok2);
|
|
|
|
}
|
|
|
|
|
2021-05-04 13:46:46 +02:00
|
|
|
|
|
|
|
static bool isForLoopCondition(const Token * const tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Token *const parent = tok->astParent();
|
|
|
|
return Token::simpleMatch(parent, ";") && parent->astOperand1() == tok &&
|
|
|
|
Token::simpleMatch(parent->astParent(), ";") &&
|
|
|
|
Token::simpleMatch(parent->astParent()->astParent(), "(") &&
|
|
|
|
parent->astParent()->astParent()->astOperand1()->str() == "for";
|
|
|
|
}
|
|
|
|
|
2022-12-15 14:31:02 +01:00
|
|
|
static bool isForLoopIncrement(const Token* const tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Token *const parent = tok->astParent();
|
|
|
|
return Token::simpleMatch(parent, ";") && parent->astOperand2() == tok &&
|
|
|
|
Token::simpleMatch(parent->astParent(), ";") &&
|
|
|
|
Token::simpleMatch(parent->astParent()->astParent(), "(") &&
|
|
|
|
parent->astParent()->astParent()->astOperand1()->str() == "for";
|
|
|
|
}
|
|
|
|
|
2022-09-04 10:24:45 +02:00
|
|
|
bool isUsedAsBool(const Token* const tok)
|
2021-05-04 13:46:46 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
2022-12-15 14:31:02 +01:00
|
|
|
if (isForLoopIncrement(tok))
|
|
|
|
return false;
|
2021-05-04 13:46:46 +02:00
|
|
|
if (astIsBool(tok))
|
|
|
|
return true;
|
2022-09-04 10:24:45 +02:00
|
|
|
if (Token::Match(tok, "!|&&|%oror%|%comp%"))
|
|
|
|
return true;
|
|
|
|
const Token* parent = tok->astParent();
|
2021-05-04 13:46:46 +02:00
|
|
|
if (!parent)
|
|
|
|
return false;
|
2022-09-04 10:24:45 +02:00
|
|
|
if (Token::Match(parent, "&&|!|%oror%"))
|
2021-05-04 13:46:46 +02:00
|
|
|
return true;
|
2022-09-04 10:24:45 +02:00
|
|
|
if (parent->isCast())
|
|
|
|
return isUsedAsBool(parent);
|
|
|
|
if (parent->isUnaryOp("*"))
|
|
|
|
return isUsedAsBool(parent);
|
|
|
|
if (Token::Match(parent, "==|!=") && tok->astSibling()->hasKnownIntValue() &&
|
|
|
|
tok->astSibling()->values().front().intvalue == 0)
|
2021-10-30 22:13:58 +02:00
|
|
|
return true;
|
2022-09-04 10:24:45 +02:00
|
|
|
if (parent->str() == "(" && astIsRHS(tok) && Token::Match(parent->astOperand1(), "if|while"))
|
|
|
|
return true;
|
|
|
|
if (Token::simpleMatch(parent, "?") && astIsLHS(tok))
|
|
|
|
return true;
|
|
|
|
if (isForLoopCondition(tok))
|
|
|
|
return true;
|
|
|
|
if (!Token::Match(parent, "%cop%")) {
|
|
|
|
std::vector<ValueType> vtParents = getParentValueTypes(tok);
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::any_of(vtParents.cbegin(), vtParents.cend(), [&](const ValueType& vt) {
|
2022-09-04 10:24:45 +02:00
|
|
|
return vt.pointer == 0 && vt.type == ValueType::BOOL;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
2021-05-04 13:46:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:48:48 +02:00
|
|
|
static bool astIsBoolLike(const Token* tok)
|
|
|
|
{
|
2021-05-04 13:46:46 +02:00
|
|
|
return astIsBool(tok) || isUsedAsBool(tok);
|
2020-08-31 08:48:48 +02:00
|
|
|
}
|
|
|
|
|
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
|
2023-07-07 23:30:59 +02:00
|
|
|
if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) {
|
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
|
|
|
}
|
2023-07-07 23:30:59 +02:00
|
|
|
if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=") && astIsBoolLike(tok1)) {
|
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
|
|
|
}
|
2021-12-11 15:10:15 +01:00
|
|
|
const bool tok_str_eq = tok1->str() == tok2->str();
|
|
|
|
if (!tok_str_eq && isDifferentKnownValues(tok1, tok2))
|
2019-09-05 15:15:58 +02:00
|
|
|
return false;
|
2020-02-01 07:22:41 +01:00
|
|
|
if (isSameConstantValue(macro, tok1, tok2))
|
|
|
|
return true;
|
|
|
|
|
2018-09-28 08:38:24 +02:00
|
|
|
// Follow variable
|
2022-06-08 16:58:57 +02:00
|
|
|
if (followVar && !tok_str_eq && (tok1->varId() || tok2->varId())) {
|
2018-09-07 20:16:38 +02:00
|
|
|
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
|
2020-02-03 09:35:24 +01:00
|
|
|
if ((varTok1->str() == tok2->str()) || isSameConstantValue(macro, varTok1, tok2)) {
|
2018-08-07 09:32:16 +02:00
|
|
|
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);
|
2020-02-03 09:35:24 +01:00
|
|
|
if ((tok1->str() == varTok2->str()) || isSameConstantValue(macro, tok1, varTok2)) {
|
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
|
|
|
}
|
2020-02-03 09:35:24 +01:00
|
|
|
if ((varTok1->str() == varTok2->str()) || isSameConstantValue(macro, varTok1, varTok2)) {
|
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
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
// Follow references
|
2021-12-11 15:10:15 +01:00
|
|
|
if (!tok_str_eq) {
|
2021-01-22 11:00:57 +01:00
|
|
|
const Token* refTok1 = followReferences(tok1, errors);
|
|
|
|
const Token* refTok2 = followReferences(tok2, errors);
|
2023-01-18 16:59:56 +01:00
|
|
|
if (refTok1 != tok1 || refTok2 != tok2) {
|
|
|
|
if (refTok1 && !refTok1->varId() && refTok2 && !refTok2->varId()) { // complex reference expression
|
|
|
|
const Token *start = refTok1, *end = refTok2;
|
|
|
|
if (!precedes(start, end))
|
|
|
|
std::swap(start, end);
|
|
|
|
if (isExpressionChanged(start, start, end, nullptr, cpp))
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
return isSameExpression(cpp, macro, refTok1, refTok2, library, pure, followVar, errors);
|
2023-01-18 16:59:56 +01:00
|
|
|
}
|
2021-01-22 11:00:57 +01:00
|
|
|
}
|
2021-12-11 15:10:15 +01:00
|
|
|
if (tok1->varId() != tok2->varId() || !tok_str_eq || tok1->originalName() != tok2->originalName()) {
|
2021-08-07 20:51:18 +02:00
|
|
|
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||
|
2015-08-03 09:20:50 +02:00
|
|
|
(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
|
|
|
}
|
2020-08-31 08:48:48 +02:00
|
|
|
const Token* condTok = nullptr;
|
|
|
|
const Token* exprTok = nullptr;
|
|
|
|
if (Token::Match(tok1, "==|!=")) {
|
|
|
|
condTok = tok1;
|
|
|
|
exprTok = tok2;
|
|
|
|
} else if (Token::Match(tok2, "==|!=")) {
|
|
|
|
condTok = tok2;
|
|
|
|
exprTok = tok1;
|
|
|
|
}
|
|
|
|
if (condTok && condTok->astOperand1() && condTok->astOperand2() && !Token::Match(exprTok, "%comp%")) {
|
|
|
|
const Token* varTok1 = nullptr;
|
|
|
|
const Token* varTok2 = exprTok;
|
|
|
|
const ValueFlow::Value* value = nullptr;
|
|
|
|
if (condTok->astOperand1()->hasKnownIntValue()) {
|
|
|
|
value = &condTok->astOperand1()->values().front();
|
|
|
|
varTok1 = condTok->astOperand2();
|
|
|
|
} else if (condTok->astOperand2()->hasKnownIntValue()) {
|
|
|
|
value = &condTok->astOperand2()->values().front();
|
|
|
|
varTok1 = condTok->astOperand1();
|
|
|
|
}
|
2022-08-21 17:39:21 +02:00
|
|
|
const bool exprIsNot = Token::simpleMatch(exprTok, "!");
|
|
|
|
if (exprIsNot)
|
2020-08-31 08:48:48 +02:00
|
|
|
varTok2 = exprTok->astOperand1();
|
|
|
|
bool compare = false;
|
|
|
|
if (value) {
|
2022-08-21 17:39:21 +02:00
|
|
|
if (value->intvalue == 0 && exprIsNot && Token::simpleMatch(condTok, "==")) {
|
2020-08-31 08:48:48 +02:00
|
|
|
compare = true;
|
2022-08-21 17:39:21 +02:00
|
|
|
} else if (value->intvalue == 0 && !exprIsNot && Token::simpleMatch(condTok, "!=")) {
|
2020-08-31 08:48:48 +02:00
|
|
|
compare = true;
|
2022-08-21 17:39:21 +02:00
|
|
|
} else if (value->intvalue != 0 && exprIsNot && Token::simpleMatch(condTok, "!=")) {
|
2020-08-31 08:48:48 +02:00
|
|
|
compare = true;
|
2022-08-21 17:39:21 +02:00
|
|
|
} else if (value->intvalue != 0 && !exprIsNot && Token::simpleMatch(condTok, "==")) {
|
2020-08-31 08:48:48 +02:00
|
|
|
compare = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (compare && astIsBoolLike(varTok1) && astIsBoolLike(varTok2))
|
|
|
|
return isSameExpression(cpp, macro, varTok1, varTok2, library, pure, followVar, errors);
|
|
|
|
|
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-31 08:24:05 +01:00
|
|
|
auto flagsDiffer = [](const Token* tok1, const Token* tok2, bool macro) {
|
|
|
|
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
|
|
|
|
return true;
|
|
|
|
if (tok1->isComplex() != tok2->isComplex())
|
|
|
|
return true;
|
|
|
|
if (tok1->isLong() != tok2->isLong())
|
|
|
|
return true;
|
|
|
|
if (tok1->isUnsigned() != tok2->isUnsigned())
|
|
|
|
return true;
|
|
|
|
if (tok1->isSigned() != tok2->isSigned())
|
|
|
|
return true;
|
2017-02-22 21:13:36 +01:00
|
|
|
return false;
|
2021-12-31 08:24:05 +01:00
|
|
|
};
|
|
|
|
if (flagsDiffer(tok1, tok2, macro))
|
2017-02-22 21:13:36 +01:00
|
|
|
return false;
|
2021-12-31 08:24:05 +01:00
|
|
|
|
2020-02-15 16:43:06 +01:00
|
|
|
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof" && !(tok1->variable() && tok1 == tok1->variable()->nameToken())) {
|
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();
|
2020-08-22 07:05:21 +02:00
|
|
|
if (!lhs)
|
|
|
|
return false;
|
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 {
|
2023-03-12 19:57:11 +01:00
|
|
|
if (!tok1->function()->isConst() && !tok1->function()->isAttributeConst() &&
|
|
|
|
!tok1->function()->isAttributePure())
|
2018-07-08 15:58:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
// templates/casts
|
2023-03-02 21:45:15 +01:00
|
|
|
if ((tok1->next() && tok1->next()->link() && Token::Match(tok1, "%name% <")) ||
|
|
|
|
(tok2->next() && tok2->next()->link() && Token::Match(tok2, "%name% <"))) {
|
2015-08-03 09:20:50 +02:00
|
|
|
|
|
|
|
// 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) {
|
2021-12-31 08:24:05 +01:00
|
|
|
if (t1->str() != t2->str() || flagsDiffer(t1, t2, macro))
|
2015-08-03 09:20:50 +02:00
|
|
|
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() &&
|
2021-12-31 08:24:05 +01:00
|
|
|
!flagsDiffer(t1, t2, macro) &&
|
2016-08-01 21:53:43 +02:00
|
|
|
(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;
|
|
|
|
|
2022-09-08 22:08:38 +02:00
|
|
|
if (isSameExpression(cpp, true, cond1, cond2, library, pure, followVar, errors))
|
|
|
|
return false;
|
|
|
|
|
2020-09-09 15:39:36 +02:00
|
|
|
if (!isNot && cond1->str() == "&&" && cond2->str() == "&&") {
|
2020-09-07 10:56:02 +02:00
|
|
|
for (const Token* tok1: {
|
2021-08-07 20:51:18 +02:00
|
|
|
cond1->astOperand1(), cond1->astOperand2()
|
2020-09-07 10:56:02 +02:00
|
|
|
}) {
|
|
|
|
for (const Token* tok2: {
|
2021-08-07 20:51:18 +02:00
|
|
|
cond2->astOperand1(), cond2->astOperand2()
|
2020-09-07 10:56:02 +02:00
|
|
|
}) {
|
2020-09-07 07:53:41 +02:00
|
|
|
if (isSameExpression(cpp, true, tok1, tok2, library, pure, followVar, errors)) {
|
|
|
|
if (isOppositeCond(isNot, cpp, tok1->astSibling(), tok2->astSibling(), library, pure, followVar, errors))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 22:08:38 +02:00
|
|
|
if (cond1->str() != cond2->str() && (cond1->str() == "||" || cond2->str() == "||")) {
|
|
|
|
const Token* orCond = nullptr;
|
|
|
|
const Token* otherCond = nullptr;
|
|
|
|
if (cond1->str() == "||") {
|
|
|
|
orCond = cond1;
|
|
|
|
otherCond = cond2;
|
|
|
|
}
|
|
|
|
if (cond2->str() == "||") {
|
|
|
|
orCond = cond2;
|
|
|
|
otherCond = cond1;
|
|
|
|
}
|
|
|
|
return isOppositeCond(isNot, cpp, orCond->astOperand1(), otherCond, library, pure, followVar, errors) &&
|
|
|
|
isOppositeCond(isNot, cpp, orCond->astOperand2(), otherCond, library, pure, followVar, errors);
|
|
|
|
}
|
|
|
|
|
2015-08-03 09:20:50 +02:00
|
|
|
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
|
|
|
}
|
2021-01-25 17:23:47 +01:00
|
|
|
if (!isUsedAsBool(cond2))
|
|
|
|
return false;
|
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") &&
|
2021-01-29 10:26:57 +01:00
|
|
|
isSameExpression(cpp,
|
|
|
|
true,
|
|
|
|
cond1->astOperand1()->astOperand1(),
|
|
|
|
cond2->astOperand1()->astOperand1()->astOperand1(),
|
|
|
|
library,
|
|
|
|
pure,
|
|
|
|
followVar,
|
|
|
|
errors)) {
|
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") &&
|
2021-01-29 10:26:57 +01:00
|
|
|
isSameExpression(cpp,
|
|
|
|
true,
|
|
|
|
cond2->astOperand1()->astOperand1(),
|
|
|
|
cond1->astOperand1()->astOperand1()->astOperand1(),
|
|
|
|
library,
|
|
|
|
pure,
|
|
|
|
followVar,
|
|
|
|
errors)) {
|
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);
|
2023-06-20 18:43:21 +02:00
|
|
|
if (op1 == ">=" || op1 == ">")
|
2017-09-22 14:01:20 +02:00
|
|
|
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 == "==") ||
|
2021-08-07 20:51:18 +02:00
|
|
|
(comp1 == "<" && comp2 == ">=") ||
|
2015-08-03 09:20:50 +02:00
|
|
|
(comp1 == "<=" && comp2 == ">") ||
|
2021-08-07 20:51:18 +02:00
|
|
|
(comp1 == ">" && comp2 == "<=") ||
|
2015-08-03 09:20:50 +02:00
|
|
|
(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 == "==")
|
2021-08-07 20:51:18 +02:00
|
|
|
)));
|
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;
|
2021-12-10 18:06:45 +01:00
|
|
|
if (tok1->isUnaryOp("-") && !(tok2->astParent() && tok2->astParent()->tokType() == Token::eBitOp))
|
2018-09-28 08:38:24 +02:00
|
|
|
return isSameExpression(cpp, true, tok1->astOperand1(), tok2, library, pure, followVar, errors);
|
2021-12-10 18:06:45 +01:00
|
|
|
if (tok2->isUnaryOp("-") && !(tok2->astParent() && tok2->astParent()->tokType() == Token::eBitOp))
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-18 07:46:31 +02:00
|
|
|
static bool functionModifiesArguments(const Function* f)
|
|
|
|
{
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::any_of(f->argumentList.cbegin(), f->argumentList.cend(), [](const Variable& var) {
|
2021-07-18 07:46:31 +02:00
|
|
|
if (var.isReference() || var.isPointer())
|
|
|
|
return !var.isConst();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isConstFunctionCall(const Token* ftok, const Library& library)
|
|
|
|
{
|
2022-09-03 23:29:06 +02:00
|
|
|
if (isSizeOfEtc(ftok))
|
|
|
|
return true;
|
2021-07-18 07:46:31 +02:00
|
|
|
if (!Token::Match(ftok, "%name% ("))
|
|
|
|
return false;
|
|
|
|
if (const Function* f = ftok->function()) {
|
|
|
|
if (f->isAttributePure() || f->isAttributeConst())
|
|
|
|
return true;
|
|
|
|
// Any modified arguments
|
|
|
|
if (functionModifiesArguments(f))
|
|
|
|
return false;
|
2021-07-22 07:22:26 +02:00
|
|
|
if (Function::returnsVoid(f))
|
|
|
|
return false;
|
2021-07-18 07:46:31 +02:00
|
|
|
// Member function call
|
2022-05-12 06:21:33 +02:00
|
|
|
if (Token::simpleMatch(ftok->previous(), ".") || exprDependsOnThis(ftok->next())) {
|
2021-07-18 07:46:31 +02:00
|
|
|
if (f->isConst())
|
|
|
|
return true;
|
|
|
|
// Check for const overloaded function that just return the const version
|
|
|
|
if (!Function::returnsConst(f)) {
|
|
|
|
std::vector<const Function*> fs = f->getOverloadedFunctions();
|
2022-12-30 15:13:47 +01:00
|
|
|
if (std::any_of(fs.cbegin(), fs.cend(), [&](const Function* g) {
|
2021-08-07 20:51:18 +02:00
|
|
|
if (f == g)
|
2021-07-18 07:46:31 +02:00
|
|
|
return false;
|
2021-07-18 10:01:22 +02:00
|
|
|
if (f->argumentList.size() != g->argumentList.size())
|
|
|
|
return false;
|
|
|
|
if (functionModifiesArguments(g))
|
|
|
|
return false;
|
|
|
|
if (g->isConst() && Function::returnsConst(g))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}))
|
2021-08-07 20:51:18 +02:00
|
|
|
return true;
|
2021-07-18 07:46:31 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-20 18:43:21 +02:00
|
|
|
if (f->argumentList.empty())
|
|
|
|
return f->isConstexpr();
|
2021-10-06 08:39:58 +02:00
|
|
|
} else if (Token::Match(ftok->previous(), ". %name% (") && ftok->previous()->originalName() != "->" &&
|
|
|
|
astIsSmartPointer(ftok->previous()->astOperand1())) {
|
|
|
|
return Token::Match(ftok, "get|get_deleter ( )");
|
|
|
|
} else if (Token::Match(ftok->previous(), ". %name% (") && astIsContainer(ftok->previous()->astOperand1())) {
|
|
|
|
const Library::Container* container = ftok->previous()->astOperand1()->valueType()->container;
|
|
|
|
if (!container)
|
|
|
|
return false;
|
|
|
|
if (container->getYield(ftok->str()) != Library::Container::Yield::NO_YIELD)
|
|
|
|
return true;
|
|
|
|
if (container->getAction(ftok->str()) == Library::Container::Action::FIND)
|
|
|
|
return true;
|
|
|
|
return false;
|
2022-05-09 20:26:52 +02:00
|
|
|
} else if (const Library::Function* lf = library.getFunction(ftok)) {
|
|
|
|
if (lf->ispure)
|
|
|
|
return true;
|
|
|
|
if (lf->containerYield != Library::Container::Yield::NO_YIELD)
|
|
|
|
return true;
|
|
|
|
if (lf->containerAction == Library::Container::Action::FIND)
|
|
|
|
return true;
|
|
|
|
return false;
|
2021-07-18 07:46:31 +02:00
|
|
|
} else {
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool memberFunction = Token::Match(ftok->previous(), ". %name% (");
|
2021-07-18 07:46:31 +02:00
|
|
|
bool constMember = !memberFunction;
|
|
|
|
if (Token::Match(ftok->tokAt(-2), "%var% . %name% (")) {
|
|
|
|
const Variable* var = ftok->tokAt(-2)->variable();
|
|
|
|
if (var)
|
|
|
|
constMember = var->isConst();
|
|
|
|
}
|
|
|
|
// TODO: Only check const on lvalues
|
|
|
|
std::vector<const Token*> args = getArguments(ftok);
|
2022-04-28 10:48:37 +02:00
|
|
|
if (args.empty())
|
2021-07-18 07:46:31 +02:00
|
|
|
return false;
|
2022-12-30 15:13:47 +01:00
|
|
|
return constMember && std::all_of(args.cbegin(), args.cend(), [](const Token* tok) {
|
2021-07-18 10:01:22 +02:00
|
|
|
const Variable* var = tok->variable();
|
|
|
|
if (var)
|
|
|
|
return var->isConst();
|
|
|
|
return false;
|
|
|
|
});
|
2021-07-18 07:46:31 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-18 16:52:04 +01:00
|
|
|
bool isConstExpression(const Token *tok, const Library& library, bool cpp)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return true;
|
2021-07-18 07:46:31 +02:00
|
|
|
if (tok->variable() && tok->variable()->isVolatile())
|
|
|
|
return false;
|
2015-08-03 09:20:50 +02:00
|
|
|
if (tok->isName() && tok->next()->str() == "(") {
|
2021-07-18 07:46:31 +02:00
|
|
|
if (!isConstFunctionCall(tok, library))
|
2015-08-03 09:20:50 +02:00
|
|
|
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;
|
2022-12-18 16:52:04 +01:00
|
|
|
return isConstExpression(tok->astOperand1(), library, cpp) && isConstExpression(tok->astOperand2(), library, cpp);
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 07:59:29 +02:00
|
|
|
bool isWithoutSideEffects(bool cpp, const Token* tok, bool checkArrayAccess, bool checkReference)
|
2015-08-03 09:20:50 +02:00
|
|
|
{
|
|
|
|
if (!cpp)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (tok && tok->astOperand2() && tok->astOperand2()->str() != "(")
|
|
|
|
tok = tok->astOperand2();
|
|
|
|
if (tok && tok->varId()) {
|
|
|
|
const Variable* var = tok->variable();
|
2022-03-27 07:59:29 +02:00
|
|
|
return var && ((!var->isClass() && (checkReference || !var->isReference())) || var->isPointer() || (checkArrayAccess ? var->isStlType() && !var->isStlType(CheckClass::stl_containers_not_const) : var->isStlType()));
|
2015-08-03 09:20:50 +02:00
|
|
|
}
|
|
|
|
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);
|
2023-03-09 17:06:27 +01:00
|
|
|
if (!std::all_of(scope->functionList.begin(), scope->functionList.end(), [&](const Function& f) {
|
2018-06-14 22:39:26 +02:00
|
|
|
if (f.type != Function::eFunction)
|
2023-03-09 17:06:27 +01:00
|
|
|
return true;
|
2018-06-14 22:39:26 +02:00
|
|
|
|
2022-01-16 12:34:20 +01:00
|
|
|
const std::string freturnType = f.retType ? f.retType->name() : f.retDef->stringifyList(f.returnDefEnd());
|
2023-03-17 13:51:55 +01:00
|
|
|
return f.argumentList.size() != fun->argumentList.size() || returnType != freturnType || f.name() == fun->name();
|
2023-03-09 17:06:27 +01: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;
|
2019-04-28 07:40:00 +02:00
|
|
|
|
2022-08-21 17:21:02 +02:00
|
|
|
auto pred = [=](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();
|
|
|
|
};
|
2022-08-21 17:33:43 +02:00
|
|
|
if (std::any_of(scope->varlist.cbegin(), scope->varlist.cend(), pred))
|
2022-08-21 17:21:02 +02:00
|
|
|
return false;
|
|
|
|
if (fun) {
|
2022-08-21 17:33:43 +02:00
|
|
|
if (std::any_of(fun->argumentList.cbegin(), fun->argumentList.cend(), pred))
|
2019-04-28 07:40:00 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:01:32 +02:00
|
|
|
static bool isEscaped(const Token* tok, bool functionsScope, const Library* library)
|
2019-08-08 07:46:47 +02:00
|
|
|
{
|
2020-06-17 19:01:32 +02:00
|
|
|
if (library && library->isnoreturn(tok))
|
|
|
|
return true;
|
2019-08-08 07:46:47 +02:00
|
|
|
if (functionsScope)
|
|
|
|
return Token::simpleMatch(tok, "throw");
|
2023-06-20 18:43:21 +02:00
|
|
|
return Token::Match(tok, "return|throw");
|
2019-08-08 07:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:01:32 +02:00
|
|
|
static bool isEscapedOrJump(const Token* tok, bool functionsScope, const Library* library)
|
2019-08-08 07:46:47 +02:00
|
|
|
{
|
2020-06-17 19:01:32 +02:00
|
|
|
if (library && library->isnoreturn(tok))
|
|
|
|
return true;
|
2019-08-08 07:46:47 +02:00
|
|
|
if (functionsScope)
|
|
|
|
return Token::simpleMatch(tok, "throw");
|
2023-06-20 18:43:21 +02:00
|
|
|
return Token::Match(tok, "return|goto|throw|continue|break");
|
2019-08-08 07:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
bool isEscapeFunction(const Token* ftok, const Library* library)
|
|
|
|
{
|
|
|
|
if (!Token::Match(ftok, "%name% ("))
|
|
|
|
return false;
|
|
|
|
const Function* function = ftok->function();
|
|
|
|
if (function) {
|
|
|
|
if (function->isEscapeFunction())
|
|
|
|
return true;
|
|
|
|
if (function->isAttributeNoreturn())
|
|
|
|
return true;
|
|
|
|
} else if (library) {
|
|
|
|
if (library->isnoreturn(ftok))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:30:05 +02:00
|
|
|
static bool hasNoreturnFunction(const Token* tok, const Library* library, const Token** unknownFunc)
|
2020-05-21 08:47:48 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
const Token* ftok = tok->str() == "(" ? tok->previous() : nullptr;
|
|
|
|
while (Token::simpleMatch(ftok, "("))
|
|
|
|
ftok = ftok->astOperand1();
|
|
|
|
if (ftok) {
|
|
|
|
const Function * function = ftok->function();
|
|
|
|
if (function) {
|
|
|
|
if (function->isEscapeFunction())
|
|
|
|
return true;
|
|
|
|
if (function->isAttributeNoreturn())
|
|
|
|
return true;
|
|
|
|
} else if (library && library->isnoreturn(ftok)) {
|
|
|
|
return true;
|
|
|
|
} else if (Token::Match(ftok, "exit|abort")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (unknownFunc && !function && library && library->functions.count(library->getFunctionName(ftok)) == 0)
|
|
|
|
*unknownFunc = ftok;
|
|
|
|
return false;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (tok->isConstOp()) {
|
2020-05-23 07:30:05 +02:00
|
|
|
return hasNoreturnFunction(tok->astOperand1(), library, unknownFunc) || hasNoreturnFunction(tok->astOperand2(), library, unknownFunc);
|
2020-05-21 08:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
bool isReturnScope(const Token* const endToken, const Library* library, const Token** unknownFunc, 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 {"))
|
2020-02-13 16:27:06 +01:00
|
|
|
return isReturnScope(prev, library, unknownFunc, functionScope) &&
|
|
|
|
isReturnScope(prev->link()->tokAt(-2), library, unknownFunc, functionScope);
|
2021-06-19 13:59:48 +02:00
|
|
|
// TODO: Check all cases
|
|
|
|
if (!functionScope && Token::simpleMatch(prev->link()->previous(), ") {") &&
|
2016-01-16 18:52:34 +01:00
|
|
|
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
|
|
|
|
!Token::findsimplematch(prev->link(), "break", prev)) {
|
2021-11-07 06:51:51 +01:00
|
|
|
return isReturnScope(prev, library, unknownFunc, functionScope);
|
2016-01-16 18:52:34 +01:00
|
|
|
}
|
2020-06-17 19:01:32 +02:00
|
|
|
if (isEscaped(prev->link()->astTop(), functionScope, library))
|
2016-01-17 12:38:49 +01:00
|
|
|
return true;
|
2016-01-18 15:39:20 +01:00
|
|
|
if (Token::Match(prev->link()->previous(), "[;{}] {"))
|
2020-02-13 16:27:06 +01:00
|
|
|
return isReturnScope(prev, library, unknownFunc, functionScope);
|
2016-01-16 18:52:34 +01:00
|
|
|
} else if (Token::simpleMatch(prev, ";")) {
|
2020-05-23 07:30:05 +02:00
|
|
|
if (prev->tokAt(-2) && hasNoreturnFunction(prev->tokAt(-2)->astTop(), library, unknownFunc))
|
2020-05-21 08:47:48 +02:00
|
|
|
return true;
|
|
|
|
// Unknown symbol
|
|
|
|
if (Token::Match(prev->tokAt(-2), ";|}|{ %name% ;") && prev->previous()->isIncompleteVar()) {
|
|
|
|
if (unknownFunc)
|
|
|
|
*unknownFunc = prev->previous();
|
2019-08-08 07:46:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-14 18:31:10 +01:00
|
|
|
if (Token::simpleMatch(prev->previous(), ") ;") && prev->previous()->link() &&
|
2020-06-17 19:01:32 +02:00
|
|
|
isEscaped(prev->previous()->link()->astTop(), functionScope, library))
|
2018-12-14 18:31:10 +01:00
|
|
|
return true;
|
2020-06-17 19:01:32 +02:00
|
|
|
if (isEscaped(prev->previous()->astTop(), functionScope, library))
|
2018-12-14 18:31:10 +01:00
|
|
|
return true;
|
2016-01-16 18:52:34 +01:00
|
|
|
// return/goto statement
|
|
|
|
prev = prev->previous();
|
2020-06-17 19:01:32 +02:00
|
|
|
while (prev && !Token::Match(prev, ";|{|}") && !isEscapedOrJump(prev, functionScope, library))
|
2016-01-16 18:52:34 +01:00
|
|
|
prev = prev->previous();
|
|
|
|
return prev && prev->isName();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-22 21:50:46 +01:00
|
|
|
bool isWithinScope(const Token* tok, const Variable* var, Scope::ScopeType type)
|
|
|
|
{
|
|
|
|
if (!tok || !var)
|
|
|
|
return false;
|
|
|
|
const Scope* scope = tok->scope();
|
|
|
|
while (scope && scope != var->scope()) {
|
|
|
|
if (scope->type == type)
|
|
|
|
return true;
|
|
|
|
scope = scope->nestedIn;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:34:00 +01:00
|
|
|
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
|
|
|
T* getTokenArgumentFunctionImpl(T* 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
|
|
|
{
|
2021-11-25 22:34:00 +01:00
|
|
|
T* 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();
|
2020-11-11 22:47:23 +01:00
|
|
|
if (Token::Match(parent, "[+-]") && parent->valueType() && parent->valueType()->pointer)
|
|
|
|
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
|
|
|
|
2021-11-25 22:34:00 +01:00
|
|
|
T* argtok = tok;
|
2020-06-26 12:56:56 +02:00
|
|
|
while (argtok && argtok->astParent() && (!Token::Match(argtok->astParent(), ",|(|{") || argtok->astParent()->isCast())) {
|
2020-06-25 19:54:46 +02:00
|
|
|
argtok = argtok->astParent();
|
|
|
|
}
|
|
|
|
if (!argtok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::simpleMatch(argtok, ","))
|
|
|
|
argtok = argtok->astOperand1();
|
|
|
|
tok = argtok;
|
2020-06-26 12:56:56 +02:00
|
|
|
while (Token::Match(tok->astParent(), ",|(|{")) {
|
2020-06-25 19:54:46 +02:00
|
|
|
tok = tok->astParent();
|
|
|
|
if (Token::Match(tok, "(|{"))
|
2019-02-28 17:02:46 +01:00
|
|
|
break;
|
2016-10-23 13:54:44 +02:00
|
|
|
}
|
2021-09-01 10:51:44 +02:00
|
|
|
argn = getArgumentPos(tok, argtok);
|
2020-06-25 19:54:46 +02:00
|
|
|
if (argn == -1)
|
|
|
|
return nullptr;
|
2019-07-24 09:59:01 +02:00
|
|
|
if (!Token::Match(tok, "{|("))
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
2021-06-03 07:31:46 +02:00
|
|
|
if (tok->astOperand2())
|
|
|
|
tok = tok->astOperand1();
|
2021-05-20 13:54:00 +02:00
|
|
|
while (tok && (tok->isUnaryOp("*") || tok->str() == "["))
|
|
|
|
tok = tok->astOperand1();
|
2020-06-26 12:56:56 +02:00
|
|
|
while (Token::simpleMatch(tok, "."))
|
2020-06-25 19:54:46 +02:00
|
|
|
tok = tok->astOperand2();
|
2020-06-26 12:56:56 +02:00
|
|
|
while (Token::simpleMatch(tok, "::")) {
|
2020-09-20 22:37:28 +02:00
|
|
|
// If there is only a op1 and not op2, then this is a global scope
|
|
|
|
if (!tok->astOperand2() && tok->astOperand1()) {
|
|
|
|
tok = tok->astOperand1();
|
|
|
|
break;
|
|
|
|
}
|
2020-06-25 19:54:46 +02:00
|
|
|
tok = tok->astOperand2();
|
2020-06-26 12:56:56 +02:00
|
|
|
if (Token::simpleMatch(tok, "<") && tok->link())
|
2020-06-25 19:54:46 +02:00
|
|
|
tok = tok->astOperand1();
|
|
|
|
}
|
2016-10-23 13:54:44 +02:00
|
|
|
if (tok && tok->link() && tok->str() == ">")
|
|
|
|
tok = tok->link()->previous();
|
2020-06-25 19:54:46 +02:00
|
|
|
if (!Token::Match(tok, "%name%|(|{"))
|
2019-09-06 21:18:45 +02:00
|
|
|
return nullptr;
|
2022-05-21 08:25:09 +02:00
|
|
|
// Skip labels
|
|
|
|
if (Token::Match(tok, "%name% :"))
|
|
|
|
return nullptr;
|
2019-09-06 21:18:45 +02:00
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:34:00 +01:00
|
|
|
const Token* getTokenArgumentFunction(const Token* tok, int& argn) {
|
|
|
|
return getTokenArgumentFunctionImpl(tok, argn);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token* getTokenArgumentFunction(Token* tok, int& argn) {
|
|
|
|
return getTokenArgumentFunctionImpl(tok, argn);
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:31:46 +02:00
|
|
|
std::vector<const Variable*> getArgumentVars(const Token* tok, int argnr)
|
2020-06-25 19:54:46 +02:00
|
|
|
{
|
2020-09-10 05:23:42 +02:00
|
|
|
std::vector<const Variable*> result;
|
2020-06-25 19:54:46 +02:00
|
|
|
if (!tok)
|
2020-09-10 05:23:42 +02:00
|
|
|
return result;
|
2021-06-03 07:31:46 +02:00
|
|
|
if (tok->function()) {
|
|
|
|
const Variable* argvar = tok->function()->getArgumentVar(argnr);
|
|
|
|
if (argvar)
|
|
|
|
return {argvar};
|
2023-06-20 18:43:21 +02:00
|
|
|
return result;
|
2021-06-03 07:31:46 +02:00
|
|
|
}
|
2022-08-21 17:39:21 +02:00
|
|
|
if (tok->variable() || Token::simpleMatch(tok, "{") || Token::Match(tok->previous(), "%type% (|{")) {
|
2020-06-25 19:54:46 +02:00
|
|
|
const Type* type = Token::typeOf(tok);
|
|
|
|
if (!type)
|
2020-09-10 05:23:42 +02:00
|
|
|
return result;
|
2020-06-25 19:54:46 +02:00
|
|
|
const Scope* typeScope = type->classScope;
|
2020-07-21 21:09:46 +02:00
|
|
|
if (!typeScope)
|
2020-09-10 05:23:42 +02:00
|
|
|
return result;
|
2022-08-21 17:39:21 +02:00
|
|
|
const bool tokIsBrace = Token::simpleMatch(tok, "{");
|
2021-06-03 07:31:46 +02:00
|
|
|
// Aggregate constructor
|
2022-08-21 17:39:21 +02:00
|
|
|
if (tokIsBrace && typeScope->numConstructors == 0 && argnr < typeScope->varlist.size()) {
|
2022-12-30 15:13:47 +01:00
|
|
|
auto it = std::next(typeScope->varlist.cbegin(), argnr);
|
2021-06-03 07:31:46 +02:00
|
|
|
return {&*it};
|
|
|
|
}
|
2020-06-25 19:54:46 +02:00
|
|
|
const int argCount = numberOfArguments(tok);
|
2022-08-21 17:39:21 +02:00
|
|
|
const bool constructor = tokIsBrace || (tok->variable() && tok->variable()->nameToken() == tok);
|
2020-06-25 19:54:46 +02:00
|
|
|
for (const Function &function : typeScope->functionList) {
|
|
|
|
if (function.argCount() < argCount)
|
|
|
|
continue;
|
2020-09-10 05:23:42 +02:00
|
|
|
if (constructor && !function.isConstructor())
|
2020-06-25 19:54:46 +02:00
|
|
|
continue;
|
2020-09-10 05:23:42 +02:00
|
|
|
if (!constructor && !Token::simpleMatch(function.token, "operator()"))
|
|
|
|
continue;
|
2021-06-03 07:31:46 +02:00
|
|
|
const Variable* argvar = function.getArgumentVar(argnr);
|
|
|
|
if (argvar)
|
|
|
|
result.push_back(argvar);
|
2020-06-25 19:54:46 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 05:23:42 +02:00
|
|
|
return result;
|
2020-06-25 19:54:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 09:16:26 +02:00
|
|
|
static bool isCPPCastKeyword(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
2021-09-28 20:34:21 +02:00
|
|
|
return endsWith(tok->str(), "_cast");
|
2020-08-22 09:16:26 +02:00
|
|
|
}
|
|
|
|
|
2021-06-03 07:31:46 +02:00
|
|
|
static bool isTrivialConstructor(const Token* tok)
|
|
|
|
{
|
|
|
|
const Token* typeTok = nullptr;
|
|
|
|
const Type* t = Token::typeOf(tok, &typeTok);
|
|
|
|
if (t)
|
|
|
|
return false;
|
|
|
|
if (typeTok->valueType() && typeTok->valueType()->isPrimitive())
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-23 08:22:41 +01:00
|
|
|
static bool isArray(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
if (tok->variable())
|
|
|
|
return tok->variable()->isArray();
|
|
|
|
if (Token::simpleMatch(tok, "."))
|
|
|
|
return isArray(tok->astOperand2());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:18:45 +02:00
|
|
|
bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Settings *settings, bool *inconclusive)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
|
|
|
|
2020-06-26 12:56:56 +02:00
|
|
|
if (Token::simpleMatch(tok, ","))
|
2020-06-25 19:54:46 +02:00
|
|
|
return false;
|
|
|
|
|
2019-09-06 21:18:45 +02:00
|
|
|
const Token * const tok1 = tok;
|
|
|
|
|
|
|
|
// address of variable
|
2023-06-16 18:43:32 +02:00
|
|
|
const bool addressOf = tok->astParent() && tok->astParent()->isUnaryOp("&");
|
|
|
|
if (addressOf)
|
2022-04-11 07:23:44 +02:00
|
|
|
indirect++;
|
2019-09-06 21:18:45 +02:00
|
|
|
|
|
|
|
int argnr;
|
|
|
|
tok = getTokenArgumentFunction(tok, argnr);
|
|
|
|
if (!tok)
|
2016-10-23 13:54:44 +02:00
|
|
|
return false; // not a function => variable not changed
|
2021-06-03 07:31:46 +02:00
|
|
|
if (Token::simpleMatch(tok, "{") && isTrivialConstructor(tok))
|
|
|
|
return false;
|
2021-09-08 06:56:45 +02:00
|
|
|
if (tok->isKeyword() && !isCPPCastKeyword(tok) && tok->str().compare(0,8,"operator") != 0)
|
2020-08-22 09:16:26 +02:00
|
|
|
return false;
|
2021-11-07 18:19:56 +01:00
|
|
|
// A functional cast won't modify the variable
|
|
|
|
if (Token::Match(tok, "%type% (|{") && tok->tokType() == Token::eType && astIsPrimitive(tok->next()))
|
|
|
|
return false;
|
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
|
|
|
|
2022-06-08 16:58:57 +02:00
|
|
|
if (!tok->function() && !tok->variable() && tok->isName()) {
|
2022-04-11 07:23:44 +02:00
|
|
|
if (settings) {
|
|
|
|
// Check if direction (in, out, inout) is specified in the library configuration and use that
|
2019-03-01 15:47:08 +01:00
|
|
|
const Library::ArgumentChecks::Direction argDirection = settings->library.getArgDirection(tok, 1 + argnr);
|
|
|
|
if (argDirection == Library::ArgumentChecks::Direction::DIR_IN)
|
|
|
|
return false;
|
2022-10-06 20:12:07 +02:00
|
|
|
|
|
|
|
const bool requireNonNull = settings->library.isnullargbad(tok, 1 + argnr);
|
|
|
|
if (argDirection == Library::ArgumentChecks::Direction::DIR_OUT ||
|
|
|
|
argDirection == Library::ArgumentChecks::Direction::DIR_INOUT) {
|
2022-04-11 07:23:44 +02:00
|
|
|
if (indirect == 0 && isArray(tok1))
|
|
|
|
return true;
|
2022-10-06 20:12:07 +02:00
|
|
|
const bool requireInit = settings->library.isuninitargbad(tok, 1 + argnr);
|
2022-04-11 07:23:44 +02:00
|
|
|
// Assume that if the variable must be initialized then the indirection is 1
|
|
|
|
if (indirect > 0 && requireInit && requireNonNull)
|
2019-03-04 22:57:40 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-12-20 20:29:19 +01:00
|
|
|
if (Token::simpleMatch(tok->tokAt(-2), "std :: tie"))
|
|
|
|
return true;
|
2022-04-11 07:23: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)
|
|
|
|
if (indirect == 0 && requireNonNull)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-10 05:23:42 +02:00
|
|
|
std::vector<const Variable*> args = getArgumentVars(tok, argnr);
|
|
|
|
bool conclusive = false;
|
2020-09-10 08:02:45 +02:00
|
|
|
for (const Variable *arg:args) {
|
2020-09-10 05:23:42 +02:00
|
|
|
if (!arg)
|
|
|
|
continue;
|
|
|
|
conclusive = true;
|
2022-04-11 07:23:44 +02:00
|
|
|
if (indirect > 0) {
|
2023-04-02 20:36:23 +02:00
|
|
|
if (arg->isPointer() && !(arg->valueType() && arg->valueType()->isConst(indirect)))
|
2020-09-10 05:23:42 +02:00
|
|
|
return true;
|
2023-06-16 18:43:32 +02:00
|
|
|
if (indirect > 1 && addressOf && arg->isPointer() && (!arg->valueType() || !arg->valueType()->isConst(indirect-1)))
|
|
|
|
return true;
|
2023-03-20 19:54:31 +01:00
|
|
|
if (arg->isArray() || (!arg->isPointer() && (!arg->valueType() || arg->valueType()->type == ValueType::UNKNOWN_TYPE)))
|
2021-04-18 21:42:27 +02:00
|
|
|
return true;
|
2020-09-10 05:23:42 +02:00
|
|
|
}
|
|
|
|
if (!arg->isConst() && arg->isReference())
|
2018-08-07 09:32:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-10 05:23:42 +02:00
|
|
|
if (!conclusive && inconclusive) {
|
|
|
|
*inconclusive = true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-10-23 13:54:44 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2021-12-16 22:32:15 +01:00
|
|
|
|
|
|
|
if (indirect == 0 && isConstVarExpression(tok))
|
|
|
|
return false;
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *tok2 = tok;
|
2020-07-20 06:25:35 +02:00
|
|
|
int derefs = 0;
|
2020-07-23 10:09:06 +02:00
|
|
|
while (Token::simpleMatch(tok2->astParent(), "*") ||
|
2020-07-20 06:25:35 +02:00
|
|
|
(Token::simpleMatch(tok2->astParent(), ".") && !Token::simpleMatch(tok2->astParent()->astParent(), "(")) ||
|
2023-06-15 14:42:58 +02:00
|
|
|
(tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->") ||
|
2020-07-20 06:25:35 +02:00
|
|
|
(Token::simpleMatch(tok2->astParent(), "[") && tok2 == tok2->astParent()->astOperand1())) {
|
2022-01-02 08:14:10 +01:00
|
|
|
if (tok2->astParent() && (tok2->astParent()->isUnaryOp("*") || (astIsLHS(tok2) && tok2->astParent()->originalName() == "->")))
|
2020-07-20 06:25:35 +02:00
|
|
|
derefs++;
|
|
|
|
if (derefs > indirect)
|
|
|
|
break;
|
2022-06-03 00:18:35 +02:00
|
|
|
if (tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->")
|
2022-01-02 08:14:10 +01:00
|
|
|
tok2 = tok2->astParent();
|
2019-08-15 10:44:55 +02:00
|
|
|
tok2 = tok2->astParent();
|
2020-07-20 06:25:35 +02:00
|
|
|
}
|
2019-08-15 10:44:55 +02:00
|
|
|
|
2023-06-15 14:42:58 +02:00
|
|
|
if (tok2->astParent() && tok2->astParent()->isUnaryOp("&")) {
|
|
|
|
const Token* parent = tok2->astParent();
|
|
|
|
while (parent->astParent() && parent->astParent()->isCast())
|
|
|
|
parent = parent->astParent();
|
|
|
|
if (parent->astParent() && parent->astParent()->isUnaryOp("*"))
|
|
|
|
tok2 = parent->astParent();
|
|
|
|
}
|
|
|
|
|
2022-12-18 22:07:43 +01:00
|
|
|
while ((Token::simpleMatch(tok2, ":") && Token::simpleMatch(tok2->astParent(), "?")) ||
|
|
|
|
(Token::simpleMatch(tok2->astParent(), ":") && Token::simpleMatch(tok2->astParent()->astParent(), "?")))
|
2019-09-13 08:33:30 +02:00
|
|
|
tok2 = tok2->astParent();
|
|
|
|
|
2022-06-08 16:58:57 +02:00
|
|
|
if (tok2->astParent() && tok2->astParent()->tokType() == Token::eIncDecOp)
|
2019-07-26 07:03:21 +02:00
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
|
2022-05-05 06:54:27 +02:00
|
|
|
auto skipRedundantPtrOp = [](const Token* tok, const Token* parent) {
|
|
|
|
const Token* gparent = parent ? parent->astParent() : nullptr;
|
2022-06-03 00:18:35 +02:00
|
|
|
while (parent && gparent && ((parent->isUnaryOp("*") && gparent->isUnaryOp("&")) || (parent->isUnaryOp("&") && gparent->isUnaryOp("*")))) {
|
2022-05-05 06:54:27 +02:00
|
|
|
tok = gparent;
|
|
|
|
parent = gparent->astParent();
|
|
|
|
if (parent)
|
|
|
|
gparent = parent->astParent();
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
};
|
|
|
|
tok2 = skipRedundantPtrOp(tok2, tok2->astParent());
|
|
|
|
|
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
|
|
|
|
2023-03-21 18:16:40 +01:00
|
|
|
const ValueType* vt = tok->variable() ? tok->variable()->valueType() : tok->valueType();
|
|
|
|
|
2023-03-26 15:12:49 +02:00
|
|
|
// Check addressof
|
2023-06-16 18:43:32 +02:00
|
|
|
if (tok2->astParent() && tok2->astParent()->isUnaryOp("&")) {
|
|
|
|
if (isVariableChanged(tok2->astParent(), indirect + 1, settings, depth - 1))
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// If its already const then it cant be modified
|
|
|
|
if (vt && vt->isConst(indirect))
|
|
|
|
return false;
|
2023-03-26 15:12:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 07:58:19 +02:00
|
|
|
if (cpp && Token::Match(tok2->astParent(), ">>|&") && astIsRHS(tok2) && isLikelyStreamRead(cpp, tok2->astParent()))
|
2019-08-15 10:44:55 +02:00
|
|
|
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
|
2023-03-21 18:16:40 +01:00
|
|
|
if (Token::Match(tok2->astParent(), ". %name%") && isFunctionCall(tok2->astParent()->next()) &&
|
|
|
|
tok2->astParent()->astOperand1() == tok2) {
|
2021-07-18 07:46:31 +02:00
|
|
|
// Member function cannot change what `this` points to
|
|
|
|
if (indirect == 0 && astIsPointer(tok))
|
|
|
|
return false;
|
2017-04-23 18:05:14 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Token *ftok = tok->tokAt(2);
|
2023-03-21 18:16:40 +01:00
|
|
|
if (astIsContainer(tok) && vt && vt->container) {
|
|
|
|
const Library::Container* c = vt->container;
|
2022-12-15 18:35:19 +01:00
|
|
|
const Library::Container::Action action = c->getAction(ftok->str());
|
|
|
|
if (contains({Library::Container::Action::INSERT,
|
|
|
|
Library::Container::Action::ERASE,
|
|
|
|
Library::Container::Action::CHANGE,
|
|
|
|
Library::Container::Action::CHANGE_CONTENT,
|
|
|
|
Library::Container::Action::CHANGE_INTERNAL,
|
|
|
|
Library::Container::Action::CLEAR,
|
|
|
|
Library::Container::Action::PUSH,
|
|
|
|
Library::Container::Action::POP,
|
|
|
|
Library::Container::Action::RESIZE},
|
|
|
|
action))
|
|
|
|
return true;
|
|
|
|
const Library::Container::Yield yield = c->getYield(ftok->str());
|
|
|
|
// If accessing element check if the element is changed
|
2023-06-20 18:43:21 +02:00
|
|
|
if (contains({Library::Container::Yield::ITEM, Library::Container::Yield::AT_INDEX}, yield))
|
2022-12-15 18:35:19 +01:00
|
|
|
return isVariableChanged(ftok->next(), indirect, settings, cpp, depth - 1);
|
2023-06-20 18:43:21 +02:00
|
|
|
|
|
|
|
if (contains({Library::Container::Yield::BUFFER,
|
|
|
|
Library::Container::Yield::BUFFER_NT,
|
|
|
|
Library::Container::Yield::START_ITERATOR,
|
|
|
|
Library::Container::Yield::ITERATOR},
|
|
|
|
yield)) {
|
2022-12-15 18:35:19 +01:00
|
|
|
return isVariableChanged(ftok->next(), indirect + 1, settings, cpp, depth - 1);
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (contains({Library::Container::Yield::SIZE,
|
|
|
|
Library::Container::Yield::EMPTY,
|
|
|
|
Library::Container::Yield::END_ITERATOR},
|
|
|
|
yield)) {
|
2022-12-15 18:35:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-06-07 20:45:48 +02:00
|
|
|
if ((settings && settings->library.isFunctionConst(ftok)) || (astIsSmartPointer(tok) && ftok->str() == "get")) // TODO: replace with action/yield?
|
2023-03-21 18:16:40 +01:00
|
|
|
return false;
|
2021-04-18 21:42:27 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
const Function * fun = ftok->function();
|
2021-04-18 21:42:27 +02:00
|
|
|
if (!fun)
|
2018-04-18 17:46:31 +02:00
|
|
|
return true;
|
2021-04-18 21:42:27 +02:00
|
|
|
return !fun->isConst();
|
2019-08-15 10:44:55 +02:00
|
|
|
}
|
2017-09-02 21:53:51 +02:00
|
|
|
|
2023-03-21 18:16:40 +01:00
|
|
|
// Member pointer
|
|
|
|
if (Token::Match(tok2->astParent(), ". * ( & %name% ::")) {
|
|
|
|
const Token* ftok = tok2->astParent()->linkAt(2)->previous();
|
|
|
|
// TODO: Check for pointer to member variable
|
|
|
|
if (!ftok->function() || !ftok->function()->isConst())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Token::simpleMatch(tok2, "[") && astIsContainer(tok) && vt && vt->container && vt->container->stdAssociativeLike)
|
|
|
|
return true;
|
|
|
|
|
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
|
|
|
|
2021-05-08 20:46:46 +02:00
|
|
|
// structured binding, nonconst reference variable in lhs
|
|
|
|
if (Token::Match(tok2->astParent(), ":|=") && tok2 == tok2->astParent()->astOperand2() && Token::simpleMatch(tok2->astParent()->previous(), "]")) {
|
|
|
|
const Token *typeStart = tok2->astParent()->previous()->link()->previous();
|
|
|
|
if (Token::simpleMatch(typeStart, "&"))
|
|
|
|
typeStart = typeStart->previous();
|
|
|
|
if (typeStart && Token::Match(typeStart->previous(), "[;{}(] auto &| [")) {
|
|
|
|
for (const Token *vartok = typeStart->tokAt(2); vartok != tok2; vartok = vartok->next()) {
|
|
|
|
if (vartok->varId()) {
|
|
|
|
const Variable* refvar = vartok->variable();
|
|
|
|
if (!refvar || (!refvar->isConst() && refvar->isReference()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
if (Token::simpleMatch(tok2->astParent(), ":") && tok2->astParent()->astParent() && Token::simpleMatch(tok2->astParent()->astParent()->previous(), "for (")) {
|
2021-05-26 04:52:55 +02:00
|
|
|
// TODO: Check if container is empty or not
|
|
|
|
if (astIsLHS(tok2))
|
|
|
|
return true;
|
2019-08-15 10:44:55 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-01-01 08:06:20 +01:00
|
|
|
|
2021-01-01 08:56:17 +01:00
|
|
|
if (indirect > 0) {
|
2021-01-01 08:06:20 +01:00
|
|
|
// check for `*(ptr + 1) = new_value` case
|
|
|
|
parent = tok2->astParent();
|
|
|
|
while (parent && parent->isArithmeticalOp() && parent->isBinaryOp()) {
|
|
|
|
parent = parent->astParent();
|
|
|
|
}
|
2021-01-01 08:56:17 +01:00
|
|
|
if (Token::simpleMatch(parent, "*")) {
|
2021-01-01 08:06:20 +01:00
|
|
|
if (parent->astParent() && parent->astParent()->isAssignmentOp() &&
|
|
|
|
(parent->astParent()->astOperand1() == parent)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-24 09:59:01 +02:00
|
|
|
|
2020-09-14 09:17:29 +02:00
|
|
|
bool isVariableChanged(const Token *start, const Token *end, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2019-08-16 07:48:54 +02:00
|
|
|
{
|
2020-09-14 09:17:29 +02:00
|
|
|
return findVariableChanged(start, end, 0, exprid, globalvar, settings, cpp, depth) != nullptr;
|
2019-08-16 07:48:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-14 09:17:29 +02:00
|
|
|
bool isVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2020-07-20 06:25:35 +02:00
|
|
|
{
|
2020-09-14 09:17:29 +02:00
|
|
|
return findVariableChanged(start, end, indirect, exprid, globalvar, settings, cpp, depth) != nullptr;
|
2020-07-20 06:25:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 09:03:48 +02:00
|
|
|
const Token* findExpression(const Token* start, const nonneg int exprid)
|
2020-09-18 07:44:26 +02:00
|
|
|
{
|
2023-05-22 19:53:51 +02:00
|
|
|
const Function* f = Scope::nestedInFunction(start->scope());
|
2020-09-18 07:44:26 +02:00
|
|
|
if (!f)
|
|
|
|
return nullptr;
|
|
|
|
const Scope* scope = f->functionScope;
|
|
|
|
if (!scope)
|
|
|
|
return nullptr;
|
|
|
|
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
|
|
|
|
if (tok->exprId() != exprid)
|
|
|
|
continue;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thread-unsafe memoization
|
|
|
|
template<class F, class R=decltype(std::declval<F>()())>
|
|
|
|
static std::function<R()> memoize(F f)
|
|
|
|
{
|
|
|
|
bool init = false;
|
|
|
|
R result{};
|
|
|
|
return [=]() mutable -> R {
|
|
|
|
if (init)
|
|
|
|
return result;
|
|
|
|
result = f();
|
|
|
|
init = true;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:03:48 +02:00
|
|
|
template<class F,
|
|
|
|
REQUIRES("F must be a function that returns a Token class",
|
|
|
|
std::is_convertible<decltype(std::declval<F>()()), const Token*> )>
|
|
|
|
static bool isExpressionChangedAt(const F& getExprTok,
|
|
|
|
const Token* tok,
|
|
|
|
int indirect,
|
|
|
|
const nonneg int exprid,
|
|
|
|
bool globalvar,
|
|
|
|
const Settings* settings,
|
|
|
|
bool cpp,
|
|
|
|
int depth)
|
|
|
|
{
|
2022-01-02 08:15:38 +01:00
|
|
|
if (depth < 0)
|
|
|
|
return true;
|
2021-08-23 09:03:48 +02:00
|
|
|
if (tok->exprId() != exprid) {
|
2022-05-11 20:01:13 +02:00
|
|
|
if (globalvar && !tok->isKeyword() && Token::Match(tok, "%name% (") && !(tok->function() && tok->function()->isAttributePure()))
|
2021-08-23 09:03:48 +02:00
|
|
|
// TODO: Is global variable really changed by function call?
|
|
|
|
return true;
|
2022-01-02 08:15:38 +01:00
|
|
|
bool aliased = false;
|
|
|
|
// If we can't find the expression then assume it is an alias
|
2022-08-17 13:50:39 +02:00
|
|
|
auto expr = getExprTok();
|
|
|
|
if (!expr)
|
2022-01-02 08:15:38 +01:00
|
|
|
aliased = true;
|
2022-06-28 22:40:59 +02:00
|
|
|
if (!aliased)
|
2022-08-17 13:50:39 +02:00
|
|
|
aliased = isAliasOf(tok, expr);
|
2022-01-02 08:15:38 +01:00
|
|
|
if (!aliased)
|
|
|
|
return false;
|
2023-03-21 18:16:40 +01:00
|
|
|
if (isVariableChanged(tok, indirect + 1, settings, cpp, depth))
|
2022-01-02 08:15:38 +01:00
|
|
|
return true;
|
|
|
|
// TODO: Try to traverse the lambda function
|
|
|
|
if (Token::Match(tok, "%var% ("))
|
|
|
|
return true;
|
2021-08-23 09:03:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (isVariableChanged(tok, indirect, settings, cpp, depth));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isExpressionChangedAt(const Token* expr,
|
|
|
|
const Token* tok,
|
|
|
|
int indirect,
|
|
|
|
bool globalvar,
|
|
|
|
const Settings* settings,
|
|
|
|
bool cpp,
|
|
|
|
int depth)
|
|
|
|
{
|
|
|
|
return isExpressionChangedAt([&] {
|
|
|
|
return expr;
|
|
|
|
}, tok, indirect, expr->exprId(), globalvar, settings, cpp, depth);
|
|
|
|
}
|
|
|
|
|
2020-09-14 09:17:29 +02:00
|
|
|
Token* findVariableChanged(Token *start, const Token *end, int indirect, const nonneg int exprid, 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;
|
2021-08-07 20:51:18 +02:00
|
|
|
auto getExprTok = memoize([&] {
|
|
|
|
return findExpression(start, exprid);
|
|
|
|
});
|
2019-08-16 07:48:54 +02:00
|
|
|
for (Token *tok = start; tok != end; tok = tok->next()) {
|
2021-08-23 09:03:48 +02:00
|
|
|
if (isExpressionChangedAt(getExprTok, tok, indirect, exprid, globalvar, 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;
|
|
|
|
}
|
|
|
|
|
2020-09-14 09:17:29 +02:00
|
|
|
const Token* findVariableChanged(const Token *start, const Token *end, int indirect, const nonneg int exprid, bool globalvar, const Settings *settings, bool cpp, int depth)
|
2019-08-16 07:48:54 +02:00
|
|
|
{
|
2020-09-14 09:17:29 +02:00
|
|
|
return findVariableChanged(const_cast<Token*>(start), end, indirect, exprid, 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);
|
2022-01-02 08:15:38 +01:00
|
|
|
return isExpressionChanged(var->nameToken(), start->next(), var->scope()->bodyEnd, 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,
|
2022-12-30 15:13:47 +01:00
|
|
|
const std::vector<const Variable*> &vars,
|
2019-09-26 10:32:25 +02:00
|
|
|
const Settings* settings,
|
|
|
|
bool cpp)
|
|
|
|
{
|
|
|
|
std::set<int> varids;
|
2022-12-30 15:13:47 +01:00
|
|
|
std::transform(vars.cbegin(), vars.cend(), std::inserter(varids, varids.begin()), [](const Variable* var) {
|
2019-09-26 10:32:25 +02:00
|
|
|
return var->declarationId();
|
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
const bool globalvar = std::any_of(vars.cbegin(), vars.cend(), [](const Variable* var) {
|
2019-09-26 10:32:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-18 07:46:31 +02:00
|
|
|
bool isThisChanged(const Token* tok, int indirect, const Settings* settings, bool cpp)
|
|
|
|
{
|
2021-10-15 10:57:40 +02:00
|
|
|
if ((Token::Match(tok->previous(), "%name% (") && !Token::simpleMatch(tok->astOperand1(), ".")) ||
|
|
|
|
Token::Match(tok->tokAt(-3), "this . %name% (")) {
|
2021-07-18 07:46:31 +02:00
|
|
|
if (tok->previous()->function()) {
|
2022-08-02 18:18:46 +02:00
|
|
|
return (!tok->previous()->function()->isConst() && !tok->previous()->function()->isStatic());
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (!tok->previous()->isKeyword()) {
|
2021-07-18 07:46:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isVariableChanged(tok, indirect, settings, cpp))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:55:45 +02:00
|
|
|
bool isThisChanged(const Token* start, const Token* end, int indirect, const Settings* settings, bool cpp)
|
2020-07-13 19:40:01 +02:00
|
|
|
{
|
2021-01-11 08:00:13 +01:00
|
|
|
if (!precedes(start, end))
|
|
|
|
return false;
|
2020-07-13 19:40:01 +02:00
|
|
|
for (const Token* tok = start; tok != end; tok = tok->next()) {
|
|
|
|
if (!exprDependsOnThis(tok))
|
|
|
|
continue;
|
2021-07-18 07:46:31 +02:00
|
|
|
if (isThisChanged(tok, indirect, settings, cpp))
|
2020-07-13 19:40:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:15:31 +01:00
|
|
|
bool isExpressionChanged(const Token* expr, const Token* start, const Token* end, const Settings* settings, bool cpp, int depth)
|
|
|
|
{
|
2022-01-02 08:15:38 +01:00
|
|
|
if (depth < 0)
|
|
|
|
return true;
|
2021-01-11 08:00:13 +01:00
|
|
|
if (!precedes(start, end))
|
|
|
|
return false;
|
2020-12-03 07:15:31 +01:00
|
|
|
const Token* result = findAstNode(expr, [&](const Token* tok) {
|
|
|
|
if (exprDependsOnThis(tok) && isThisChanged(start, end, false, settings, cpp)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool global = false;
|
|
|
|
if (tok->variable()) {
|
|
|
|
if (tok->variable()->isConst())
|
|
|
|
return false;
|
2022-07-10 11:33:24 +02:00
|
|
|
global = !tok->variable()->isLocal() && !tok->variable()->isArgument() &&
|
|
|
|
!(tok->variable()->isMember() && !tok->variable()->isStatic());
|
2022-04-28 10:48:37 +02:00
|
|
|
} else if (tok->isIncompleteVar() && !tok->isIncompleteConstant()) {
|
|
|
|
global = true;
|
2020-12-03 07:15:31 +01:00
|
|
|
}
|
2021-08-23 09:03:48 +02:00
|
|
|
|
|
|
|
if (tok->exprId() > 0) {
|
|
|
|
for (const Token* tok2 = start; tok2 != end; tok2 = tok2->next()) {
|
2022-12-08 10:40:55 +01:00
|
|
|
int indirect = 0;
|
|
|
|
if (const ValueType* vt = tok->valueType()) {
|
|
|
|
indirect = vt->pointer;
|
|
|
|
if (vt->type == ValueType::ITERATOR)
|
|
|
|
++indirect;
|
|
|
|
}
|
|
|
|
if (isExpressionChangedAt(tok, tok2, indirect, global, settings, cpp, depth))
|
2021-08-23 09:03:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-12-03 07:15:31 +01:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-13 06:51:07 +02:00
|
|
|
const Token* getArgumentStart(const Token* ftok)
|
|
|
|
{
|
|
|
|
const Token* tok = ftok;
|
|
|
|
if (Token::Match(tok, "%name% (|{"))
|
|
|
|
tok = ftok->next();
|
|
|
|
if (!Token::Match(tok, "(|{|["))
|
|
|
|
return nullptr;
|
|
|
|
const Token* startTok = tok->astOperand2();
|
|
|
|
if (!startTok && tok->next() != tok->link())
|
|
|
|
startTok = tok->astOperand1();
|
|
|
|
return startTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
int numberOfArguments(const Token* ftok) {
|
|
|
|
return astCount(getArgumentStart(ftok), ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
int numberOfArgumentsWithoutAst(const Token* start)
|
2015-12-06 12:50:05 +01:00
|
|
|
{
|
2023-01-18 20:38:37 +01:00
|
|
|
int arguments = 0;
|
|
|
|
const Token* openBracket = start->next();
|
|
|
|
while (Token::simpleMatch(openBracket, ")"))
|
|
|
|
openBracket = openBracket->next();
|
2015-12-06 12:50:05 +01:00
|
|
|
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
|
|
|
|
2022-05-13 06:51:07 +02:00
|
|
|
std::vector<const Token*> getArguments(const Token* ftok) {
|
|
|
|
return astFlatten(getArgumentStart(ftok), ",");
|
2017-04-20 19:57:39 +02:00
|
|
|
}
|
2017-08-29 22:35:55 +02:00
|
|
|
|
2021-01-11 18:47:38 +01:00
|
|
|
int getArgumentPos(const Variable* var, const Function* f)
|
|
|
|
{
|
2022-12-30 15:13:47 +01:00
|
|
|
auto arg_it = std::find_if(f->argumentList.cbegin(), f->argumentList.cend(), [&](const Variable& v) {
|
2021-01-11 18:47:38 +01:00
|
|
|
return v.nameToken() == var->nameToken();
|
|
|
|
});
|
|
|
|
if (arg_it == f->argumentList.end())
|
|
|
|
return -1;
|
2022-12-30 15:13:47 +01:00
|
|
|
return std::distance(f->argumentList.cbegin(), arg_it);
|
2021-01-11 18:47:38 +01:00
|
|
|
}
|
|
|
|
|
2023-02-23 18:05:31 +01:00
|
|
|
const Token* getIteratorExpression(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (tok->isUnaryOp("*"))
|
|
|
|
return nullptr;
|
|
|
|
if (!tok->isName()) {
|
|
|
|
const Token* iter1 = getIteratorExpression(tok->astOperand1());
|
|
|
|
if (iter1)
|
|
|
|
return iter1;
|
|
|
|
if (tok->str() == "(")
|
|
|
|
return nullptr;
|
|
|
|
const Token* iter2 = getIteratorExpression(tok->astOperand2());
|
|
|
|
if (iter2)
|
|
|
|
return iter2;
|
|
|
|
} else if (Token::Match(tok, "begin|cbegin|rbegin|crbegin|end|cend|rend|crend (")) {
|
|
|
|
if (Token::Match(tok->previous(), ". %name% ( ) !!."))
|
|
|
|
return tok->previous()->astOperand1();
|
|
|
|
if (!Token::simpleMatch(tok->previous(), ".") && Token::Match(tok, "%name% ( !!)") &&
|
|
|
|
!Token::simpleMatch(tok->linkAt(1), ") ."))
|
|
|
|
return tok->next()->astOperand2();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-10-04 07:55:17 +02:00
|
|
|
bool isIteratorPair(std::vector<const Token*> args)
|
|
|
|
{
|
2023-02-23 18:05:31 +01:00
|
|
|
if (args.size() != 2)
|
|
|
|
return false;
|
|
|
|
if (astIsPointer(args[0]) && astIsPointer(args[1]))
|
|
|
|
return true;
|
|
|
|
// Check if iterator is from same container
|
|
|
|
const Token* tok1 = nullptr;
|
|
|
|
const Token* tok2 = nullptr;
|
|
|
|
if (astIsIterator(args[0]) && astIsIterator(args[1])) {
|
|
|
|
tok1 = ValueFlow::getLifetimeObjValue(args[0]).tokvalue;
|
|
|
|
tok2 = ValueFlow::getLifetimeObjValue(args[1]).tokvalue;
|
|
|
|
if (!tok1 || !tok2)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
tok1 = getIteratorExpression(args[0]);
|
|
|
|
tok2 = getIteratorExpression(args[1]);
|
|
|
|
}
|
|
|
|
if (tok1 && tok2)
|
|
|
|
return tok1->exprId() == tok2->exprId();
|
|
|
|
return tok1 || tok2;
|
2021-10-04 07:55:17 +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;
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T>
|
2020-02-13 16:27:06 +01:00
|
|
|
T* findLambdaEndTokenGeneric(T* first)
|
2017-08-29 22:35:55 +02:00
|
|
|
{
|
2023-05-31 20:55:39 +02:00
|
|
|
auto maybeLambda = [](T* tok) -> bool {
|
|
|
|
while (Token::Match(tok, "*|%name%|::|>")) {
|
|
|
|
if (tok->link())
|
|
|
|
tok = tok->link()->previous();
|
|
|
|
else {
|
|
|
|
if (tok->str() == ">")
|
|
|
|
return true;
|
|
|
|
if (tok->str() == "new")
|
|
|
|
return false;
|
|
|
|
tok = tok->previous();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-08-29 22:35:55 +02:00
|
|
|
if (!first || first->str() != "[")
|
|
|
|
return nullptr;
|
2023-05-31 20:55:39 +02:00
|
|
|
if (!maybeLambda(first->previous()))
|
|
|
|
return nullptr;
|
2023-06-27 16:19:12 +02:00
|
|
|
if (!Token::Match(first->link(), "] (|{|<"))
|
2018-11-10 16:40:40 +01:00
|
|
|
return nullptr;
|
2023-06-27 16:19:12 +02:00
|
|
|
const Token* roundOrCurly = first->link()->next();
|
|
|
|
if (roundOrCurly->link() && roundOrCurly->str() == "<")
|
|
|
|
roundOrCurly = roundOrCurly->link()->next();
|
|
|
|
if (first->astOperand1() != roundOrCurly)
|
2018-11-10 16:40:40 +01:00
|
|
|
return nullptr;
|
2023-02-08 21:07:16 +01:00
|
|
|
T * tok = first;
|
2018-10-31 12:36:08 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
const Token* findLambdaEndToken(const Token* first)
|
|
|
|
{
|
|
|
|
return findLambdaEndTokenGeneric(first);
|
|
|
|
}
|
|
|
|
Token* findLambdaEndToken(Token* first)
|
|
|
|
{
|
|
|
|
return findLambdaEndTokenGeneric(first);
|
|
|
|
}
|
2020-02-13 16:27:06 +01: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();
|
2023-07-07 15:54:07 +02:00
|
|
|
if (parent->astParent() && !Token::Match(parent->astParent(), "%oror%|&&|(|,|.|!|;|return"))
|
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)
|
|
|
|
{
|
2020-08-22 09:16:26 +02:00
|
|
|
return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && isCPPCastKeyword(tok->astOperand1());
|
2019-02-07 14:59:09 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 19:06:19 +02:00
|
|
|
bool isConstVarExpression(const Token *tok, std::function<bool(const Token*)> skipPredicate)
|
2018-12-17 06:04:24 +01:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return false;
|
2021-12-16 22:32:15 +01:00
|
|
|
if (tok->str() == "?" && tok->astOperand2() && tok->astOperand2()->str() == ":") // ternary operator
|
|
|
|
return isConstVarExpression(tok->astOperand2()->astOperand1()) && isConstVarExpression(tok->astOperand2()->astOperand2()); // left and right of ":"
|
2022-08-07 19:06:19 +02:00
|
|
|
if (skipPredicate && skipPredicate(tok))
|
2020-01-03 19:35:28 +01:00
|
|
|
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% (")) {
|
2022-08-07 19:06:19 +02:00
|
|
|
if (Token::simpleMatch(tok->astOperand1(), ".") && !isConstVarExpression(tok->astOperand1(), skipPredicate))
|
2020-01-03 19:35:28 +01:00
|
|
|
return false;
|
2018-12-17 06:04:24 +01:00
|
|
|
std::vector<const Token *> args = getArguments(tok);
|
2022-08-07 19:06:19 +02:00
|
|
|
if (args.empty() && tok->previous()->function() && tok->previous()->function()->isConstexpr())
|
|
|
|
return true;
|
2022-12-30 15:13:47 +01:00
|
|
|
return !args.empty() && std::all_of(args.cbegin(), args.cend(), [&](const Token* t) {
|
2022-08-07 19:06:19 +02:00
|
|
|
return isConstVarExpression(t, skipPredicate);
|
2020-01-04 00:06:27 +01:00
|
|
|
});
|
2018-12-17 06:04:24 +01:00
|
|
|
}
|
2019-02-07 14:59:09 +01:00
|
|
|
if (isCPPCast(tok)) {
|
2022-08-07 19:06:19 +02:00
|
|
|
return isConstVarExpression(tok->astOperand2(), skipPredicate);
|
2019-01-01 11:34:44 +01:00
|
|
|
}
|
2018-12-17 06:04:24 +01:00
|
|
|
if (Token::Match(tok, "( %type%"))
|
2022-08-07 19:06:19 +02:00
|
|
|
return isConstVarExpression(tok->astOperand1(), skipPredicate);
|
2019-10-20 07:44:56 +02:00
|
|
|
if (tok->str() == "::" && tok->hasKnownValue())
|
2022-08-07 19:06:19 +02:00
|
|
|
return isConstVarExpression(tok->astOperand2(), skipPredicate);
|
2019-02-22 06:39:59 +01:00
|
|
|
if (Token::Match(tok, "%cop%|[|.")) {
|
2022-08-07 19:06:19 +02:00
|
|
|
if (tok->astOperand1() && !isConstVarExpression(tok->astOperand1(), skipPredicate))
|
2018-12-17 06:04:24 +01:00
|
|
|
return false;
|
2022-08-07 19:06:19 +02:00
|
|
|
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2(), skipPredicate))
|
2018-12-17 06:04:24 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok, "%bool%|%num%|%str%|%char%|nullptr|NULL"))
|
|
|
|
return true;
|
|
|
|
if (tok->isEnumerator())
|
|
|
|
return true;
|
|
|
|
if (tok->variable())
|
2020-01-03 19:35:28 +01:00
|
|
|
return tok->variable()->isConst() && tok->variable()->nameToken() && tok->variable()->nameToken()->hasKnownValue();
|
2018-12-17 06:04:24 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-02 17:01:52 +01:00
|
|
|
|
2022-09-11 12:32:01 +02:00
|
|
|
static ExprUsage getFunctionUsage(const Token* tok, int indirect, const Settings* settings)
|
|
|
|
{
|
|
|
|
const bool addressOf = tok->astParent() && tok->astParent()->isUnaryOp("&");
|
|
|
|
|
|
|
|
int argnr;
|
|
|
|
const Token* ftok = getTokenArgumentFunction(tok, argnr);
|
|
|
|
if (!ftok)
|
|
|
|
return ExprUsage::None;
|
|
|
|
if (ftok->function()) {
|
|
|
|
std::vector<const Variable*> args = getArgumentVars(ftok, argnr);
|
|
|
|
for (const Variable* arg : args) {
|
|
|
|
if (!arg)
|
|
|
|
continue;
|
|
|
|
if (arg->isReference())
|
|
|
|
return ExprUsage::PassedByReference;
|
|
|
|
}
|
|
|
|
if (!args.empty() && indirect == 0 && !addressOf)
|
|
|
|
return ExprUsage::Used;
|
2023-06-17 10:20:20 +02:00
|
|
|
} else if (ftok->isControlFlowKeyword()) {
|
|
|
|
return ExprUsage::Used;
|
|
|
|
} else if (ftok->str() == "{") {
|
|
|
|
return ExprUsage::Used;
|
2022-09-11 12:32:01 +02:00
|
|
|
} else {
|
|
|
|
const bool isnullbad = settings->library.isnullargbad(ftok, argnr + 1);
|
|
|
|
if (indirect == 0 && astIsPointer(tok) && !addressOf && isnullbad)
|
|
|
|
return ExprUsage::Used;
|
|
|
|
bool hasIndirect = false;
|
|
|
|
const bool isuninitbad = settings->library.isuninitargbad(ftok, argnr + 1, indirect, &hasIndirect);
|
|
|
|
if (isuninitbad && (!addressOf || isnullbad))
|
|
|
|
return ExprUsage::Used;
|
|
|
|
}
|
|
|
|
return ExprUsage::Inconclusive;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprUsage getExprUsage(const Token* tok, int indirect, const Settings* settings)
|
|
|
|
{
|
|
|
|
if (indirect > 0 && tok->astParent()) {
|
|
|
|
if (Token::Match(tok->astParent(), "%assign%") && astIsRHS(tok))
|
|
|
|
return ExprUsage::NotUsed;
|
|
|
|
if (tok->astParent()->isConstOp())
|
|
|
|
return ExprUsage::NotUsed;
|
|
|
|
if (tok->astParent()->isCast())
|
|
|
|
return ExprUsage::NotUsed;
|
|
|
|
}
|
|
|
|
if (indirect == 0) {
|
|
|
|
if (Token::Match(tok->astParent(), "%cop%|%assign%|++|--") && !Token::simpleMatch(tok->astParent(), "=") &&
|
|
|
|
!tok->astParent()->isUnaryOp("&"))
|
|
|
|
return ExprUsage::Used;
|
|
|
|
if (Token::simpleMatch(tok->astParent(), "=") && astIsRHS(tok))
|
|
|
|
return ExprUsage::Used;
|
|
|
|
// Function call or index
|
2022-12-07 09:14:22 +01:00
|
|
|
if (((Token::simpleMatch(tok->astParent(), "(") && !tok->astParent()->isCast()) || (Token::simpleMatch(tok->astParent(), "[") && tok->valueType())) &&
|
2022-09-11 12:32:01 +02:00
|
|
|
(astIsLHS(tok) || Token::simpleMatch(tok->astParent(), "( )")))
|
|
|
|
return ExprUsage::Used;
|
|
|
|
}
|
|
|
|
return getFunctionUsage(tok, indirect, settings);
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
static void getLHSVariablesRecursive(std::vector<const Variable*>& vars, const Token* tok)
|
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return;
|
|
|
|
if (vars.empty() && Token::Match(tok, "*|&|&&|[")) {
|
|
|
|
getLHSVariablesRecursive(vars, tok->astOperand1());
|
|
|
|
if (!vars.empty() || Token::simpleMatch(tok, "["))
|
|
|
|
return;
|
|
|
|
getLHSVariablesRecursive(vars, tok->astOperand2());
|
|
|
|
} else if (Token::Match(tok->previous(), "this . %var%")) {
|
|
|
|
getLHSVariablesRecursive(vars, tok->next());
|
|
|
|
} else if (Token::simpleMatch(tok, ".")) {
|
|
|
|
getLHSVariablesRecursive(vars, tok->astOperand1());
|
|
|
|
getLHSVariablesRecursive(vars, tok->astOperand2());
|
2021-11-01 19:23:15 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "::")) {
|
|
|
|
getLHSVariablesRecursive(vars, tok->astOperand2());
|
2020-02-13 16:27:06 +01:00
|
|
|
} else if (tok->variable()) {
|
|
|
|
vars.push_back(tok->variable());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Variable*> getLHSVariables(const Token* tok)
|
|
|
|
{
|
|
|
|
std::vector<const Variable*> result;
|
2022-02-02 22:30:49 +01:00
|
|
|
if (!Token::Match(tok, "%assign%|(|{"))
|
2020-02-13 16:27:06 +01:00
|
|
|
return result;
|
|
|
|
if (!tok->astOperand1())
|
|
|
|
return result;
|
|
|
|
if (tok->astOperand1()->varId() > 0 && tok->astOperand1()->variable())
|
|
|
|
return {tok->astOperand1()->variable()};
|
|
|
|
getLHSVariablesRecursive(result, tok->astOperand1());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:31:46 +02:00
|
|
|
static const Token* getLHSVariableRecursive(const Token* tok)
|
2019-07-07 10:16:19 +02:00
|
|
|
{
|
|
|
|
if (!tok)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::Match(tok, "*|&|&&|[")) {
|
2021-06-03 07:31:46 +02:00
|
|
|
const Token* vartok = getLHSVariableRecursive(tok->astOperand1());
|
|
|
|
if ((vartok && vartok->variable()) || Token::simpleMatch(tok, "["))
|
|
|
|
return vartok;
|
2019-07-07 10:16:19 +02:00
|
|
|
return getLHSVariableRecursive(tok->astOperand2());
|
|
|
|
}
|
|
|
|
if (Token::Match(tok->previous(), "this . %var%"))
|
2021-06-03 07:31:46 +02:00
|
|
|
return tok->next();
|
|
|
|
return tok;
|
2019-07-07 10:16:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Variable *getLHSVariable(const Token *tok)
|
|
|
|
{
|
2022-06-08 16:58:57 +02:00
|
|
|
if (!tok || !tok->isAssignmentOp())
|
2019-07-07 10:16:19 +02:00
|
|
|
return nullptr;
|
|
|
|
if (!tok->astOperand1())
|
|
|
|
return nullptr;
|
|
|
|
if (tok->astOperand1()->varId() > 0 && tok->astOperand1()->variable())
|
|
|
|
return tok->astOperand1()->variable();
|
2021-06-03 07:31:46 +02:00
|
|
|
const Token* vartok = getLHSVariableRecursive(tok->astOperand1());
|
|
|
|
if (!vartok)
|
|
|
|
return nullptr;
|
|
|
|
return vartok->variable();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token* getLHSVariableToken(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!Token::Match(tok, "%assign%"))
|
|
|
|
return nullptr;
|
|
|
|
if (!tok->astOperand1())
|
|
|
|
return nullptr;
|
|
|
|
if (tok->astOperand1()->varId() > 0)
|
|
|
|
return tok->astOperand1();
|
|
|
|
const Token* vartok = getLHSVariableRecursive(tok->astOperand1());
|
2022-02-24 06:50:34 +01:00
|
|
|
if (vartok && vartok->variable() && vartok->variable()->nameToken() == vartok)
|
|
|
|
return vartok;
|
|
|
|
return tok->astOperand1();
|
2019-07-07 10:16:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
const Token* findAllocFuncCallToken(const Token *expr, const Library &library)
|
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return nullptr;
|
|
|
|
if (Token::Match(expr, "[+-]")) {
|
|
|
|
const Token *tok1 = findAllocFuncCallToken(expr->astOperand1(), library);
|
|
|
|
return tok1 ? tok1 : findAllocFuncCallToken(expr->astOperand2(), library);
|
|
|
|
}
|
|
|
|
if (expr->isCast())
|
|
|
|
return findAllocFuncCallToken(expr->astOperand2() ? expr->astOperand2() : expr->astOperand1(), library);
|
|
|
|
if (Token::Match(expr->previous(), "%name% (") && library.getAllocFuncInfo(expr->astOperand1()))
|
|
|
|
return expr->astOperand1();
|
|
|
|
return (Token::simpleMatch(expr, "new") && expr->astOperand1()) ? expr : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-09 11:16:08 +01:00
|
|
|
bool isNullOperand(const Token *expr)
|
|
|
|
{
|
|
|
|
if (!expr)
|
|
|
|
return false;
|
|
|
|
if (Token::Match(expr, "static_cast|const_cast|dynamic_cast|reinterpret_cast <"))
|
|
|
|
expr = expr->astParent();
|
|
|
|
else if (!expr->isCast())
|
|
|
|
return Token::Match(expr, "NULL|nullptr");
|
|
|
|
if (expr->valueType() && expr->valueType()->pointer == 0)
|
|
|
|
return false;
|
|
|
|
const Token *castOp = expr->astOperand2() ? expr->astOperand2() : expr->astOperand1();
|
|
|
|
return Token::Match(castOp, "NULL|nullptr") || (MathLib::isInt(castOp->str()) && MathLib::isNullValue(castOp->str()));
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:22 +01:00
|
|
|
bool isGlobalData(const Token *expr, bool cpp)
|
|
|
|
{
|
2021-09-24 07:30:17 +02:00
|
|
|
// function call that returns reference => assume global data
|
|
|
|
if (expr && expr->str() == "(" && expr->valueType() && expr->valueType()->reference != Reference::None) {
|
|
|
|
if (expr->isBinaryOp())
|
|
|
|
return true;
|
|
|
|
if (expr->astOperand1() && precedes(expr->astOperand1(), expr))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:22 +01:00
|
|
|
bool globalData = false;
|
2021-05-18 20:35:00 +02:00
|
|
|
bool var = false;
|
2020-02-16 16:02:22 +01:00
|
|
|
visitAstNodes(expr,
|
2021-08-07 20:51:18 +02:00
|
|
|
[expr, cpp, &globalData, &var](const Token *tok) {
|
2021-05-18 20:35:00 +02:00
|
|
|
if (tok->varId())
|
|
|
|
var = true;
|
2020-02-16 16:02:22 +01:00
|
|
|
if (tok->varId() && !tok->variable()) {
|
|
|
|
// Bailout, this is probably global
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->originalName() == "->") {
|
|
|
|
// TODO check if pointer points at local data
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (Token::Match(tok, "[*[]") && tok->astOperand1() && tok->astOperand1()->variable()) {
|
2020-02-16 16:02:22 +01:00
|
|
|
// 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;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (lhsvar->isArgument() && lhsvar->isArray()) {
|
2020-02-16 16:02:22 +01:00
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
2023-06-20 18:43:21 +02:00
|
|
|
}
|
|
|
|
if (lhsvar->isArgument() && (!lhstype || (lhstype->type <= ValueType::Type::VOID && !lhstype->container))) {
|
2020-02-16 16:02:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (tok->variable()->isExtern()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->previous()->str() != "." && !tok->variable()->isLocal() && !tok->variable()->isArgument()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->variable()->isArgument() && tok->variable()->isPointer() && tok != expr) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (tok->variable()->isPointerArray()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Unknown argument type => it might be some reference type..
|
|
|
|
if (cpp && tok->str() == "." && tok->astOperand1() && tok->astOperand1()->variable() && !tok->astOperand1()->valueType()) {
|
|
|
|
globalData = true;
|
|
|
|
return ChildrenToVisit::none;
|
|
|
|
}
|
|
|
|
if (Token::Match(tok, ".|["))
|
|
|
|
return ChildrenToVisit::op1;
|
|
|
|
return ChildrenToVisit::op1_and_op2;
|
|
|
|
});
|
2021-05-18 20:35:00 +02:00
|
|
|
return globalData || !var;
|
2020-02-16 16:02:22 +01:00
|
|
|
}
|
|
|
|
|
2021-10-03 18:12:29 +02:00
|
|
|
bool isSizeOfEtc(const Token *tok)
|
|
|
|
{
|
|
|
|
return Token::Match(tok, "sizeof|typeof|offsetof|decltype|__typeof__ (");
|
|
|
|
}
|