cppcheck/lib/astutils.cpp

1040 lines
39 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2018-01-14 15:37:52 +01:00
* Copyright (C) 2007-2018 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#include "astutils.h"
2017-05-27 04:33:47 +02:00
#include "library.h"
#include "mathlib.h"
#include "settings.h"
2015-08-03 09:20:50 +02:00
#include "symboldatabase.h"
#include "token.h"
2017-05-27 04:33:47 +02:00
#include "valueflow.h"
#include <list>
#include <stack>
void visitAstNodes(const Token *ast, std::function<ChildrenToVisit(const Token *)> visitor)
{
std::stack<const Token *> tokens;
tokens.push(ast);
while (!tokens.empty()) {
const Token *tok = tokens.top();
tokens.pop();
if (!tok)
continue;
ChildrenToVisit c = visitor(tok);
if (c == ChildrenToVisit::done)
break;
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2)
tokens.push(tok->astOperand1());
if (c == ChildrenToVisit::op1 || c == ChildrenToVisit::op1_and_op2)
tokens.push(tok->astOperand2());
}
}
2015-08-03 09:20:50 +02: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)
{
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
}
bool astIsIntegral(const Token *tok, bool unknown)
{
2015-10-11 14:11:11 +02:00
const ValueType *vt = tok ? tok->valueType() : nullptr;
if (!vt)
return unknown;
2015-10-11 14:11:11 +02:00
return vt->isIntegral() && vt->pointer == 0U;
}
bool astIsFloat(const Token *tok, bool unknown)
{
const ValueType *vt = tok ? tok->valueType() : nullptr;
if (!vt)
return unknown;
return vt->type >= ValueType::Type::FLOAT && vt->pointer == 0U;
}
2015-08-03 09:20:50 +02:00
bool astIsBool(const Token *tok)
{
return tok && (tok->isBoolean() || (tok->valueType() && tok->valueType()->type == ValueType::Type::BOOL && !tok->valueType()->pointer));
}
bool astIsPointer(const Token *tok)
{
return tok && tok->valueType() && tok->valueType()->pointer;
}
bool astIsIterator(const Token *tok)
{
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::ITERATOR;
}
bool astIsContainer(const Token *tok)
{
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::CONTAINER;
}
std::string astCanonicalType(const Token *expr)
{
if (!expr)
return "";
if (expr->variable()) {
const Variable *var = expr->variable();
std::string ret;
for (const Token *type = var->typeStartToken(); Token::Match(type,"%name%|::") && type != var->nameToken(); type = type->next()) {
if (!Token::Match(type, "const|static"))
ret += type->str();
}
return ret;
}
// TODO: handle expressions
return "";
}
2016-10-18 21:44:02 +02:00
static bool match(const Token *tok, const std::string &rhs)
{
if (tok->str() == rhs)
return true;
if (tok->isName() && !tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs)
2016-10-18 21:44:02 +02:00
return true;
return false;
}
2015-08-03 09:20:50 +02:00
const Token * astIsVariableComparison(const Token *tok, const std::string &comp, const std::string &rhs, const Token **vartok)
{
if (!tok)
return nullptr;
const Token *ret = nullptr;
if (tok->isComparisonOp()) {
2016-10-18 21:44:02 +02:00
if (tok->astOperand1() && match(tok->astOperand1(), rhs)) {
2015-08-03 09:20:50 +02:00
// Invert comparator
std::string s = tok->str();
if (s[0] == '>')
s[0] = '<';
else if (s[0] == '<')
s[0] = '>';
if (s == comp) {
ret = tok->astOperand2();
}
2016-10-18 21:44:02 +02:00
} else if (tok->str() == comp && tok->astOperand2() && match(tok->astOperand2(), rhs)) {
2015-08-03 09:20:50 +02:00
ret = tok->astOperand1();
}
} else if (comp == "!=" && rhs == std::string("0")) {
ret = tok;
} else if (comp == "==" && rhs == std::string("0")) {
if (tok->str() == "!") {
2015-08-03 09:20:50 +02:00
ret = tok->astOperand1();
// 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();
if (ret && ret->varId() == 0U)
ret = nullptr;
if (vartok)
*vartok = ret;
return ret;
}
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)
return true;
}
return false;
}
const Token * nextAfterAstRightmostLeaf(const Token * tok)
{
const Token * rightmostLeaf = tok;
if (!rightmostLeaf || !rightmostLeaf->astOperand1())
return nullptr;
do {
if (rightmostLeaf->astOperand2())
rightmostLeaf = rightmostLeaf->astOperand2();
else
rightmostLeaf = rightmostLeaf->astOperand1();
} while (rightmostLeaf->astOperand1());
2018-11-10 21:30:01 +01:00
while (Token::Match(rightmostLeaf->next(), "]|)") && !hasToken(rightmostLeaf->next()->link(), rightmostLeaf->next(), tok))
rightmostLeaf = rightmostLeaf->next();
2018-11-10 21:30:01 +01:00
if (rightmostLeaf->str() == "{" && rightmostLeaf->link())
rightmostLeaf = rightmostLeaf->link();
return rightmostLeaf->next();
}
static const Token * getVariableInitExpression(const Variable * var)
2015-08-03 09:20:50 +02:00
{
2018-08-07 18:06:51 +02:00
if (!var || !var->declEndToken())
return nullptr;
2018-08-07 18:06:51 +02:00
if (Token::Match(var->declEndToken(), "; %varid% =", var->declarationId()))
return var->declEndToken()->tokAt(2)->astOperand2();
return var->declEndToken()->astOperand2();
}
2018-08-07 18:06:51 +02:00
static bool isInLoopCondition(const Token * tok)
{
return Token::Match(tok->astTop()->previous(), "for|while (");
}
/// If tok2 comes after tok1
bool precedes(const Token * tok1, const Token * tok2)
{
2018-09-08 09:07:59 +02:00
if (!tok1)
return false;
2018-09-08 09:07:59 +02:00
if (!tok2)
return false;
return tok1->progressValue() < tok2->progressValue();
}
static bool isAliased(const Token * startTok, const Token * endTok, unsigned int varid)
{
for (const Token *tok = startTok; tok != endTok; tok = tok->next()) {
if (Token::Match(tok, "= & %varid% ;", varid))
return true;
}
return false;
}
static bool exprDependsOnThis(const Token *expr)
{
if (!expr)
return false;
// calling nonstatic method?
if (Token::Match(expr->previous(), "!!:: %name% (") && expr->function() && expr->function()->nestedIn && expr->function()->nestedIn->isClassOrStruct()) {
// is it a method of this?
const Scope *nestedIn = expr->scope()->functionOf;
if (nestedIn && nestedIn->function)
nestedIn = nestedIn->function->token->scope();
while (nestedIn && nestedIn != expr->function()->nestedIn) {
nestedIn = nestedIn->nestedIn;
}
return nestedIn == expr->function()->nestedIn;
}
return exprDependsOnThis(expr->astOperand1()) || exprDependsOnThis(expr->astOperand2());
}
/// 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.
static const Token * followVariableExpression(const Token * tok, bool cpp, const Token * end = nullptr)
{
2018-08-07 18:06:51 +02:00
if (!tok)
return tok;
// Skip following variables that is across multiple files
2018-09-08 09:07:59 +02:00
if (end && end->fileIndex() != tok->fileIndex())
return tok;
// Skip array access
2018-08-07 18:06:51 +02:00
if (Token::Match(tok, "%var% ["))
return tok;
// Skip pointer indirection
2018-08-07 18:06:51 +02:00
if (tok->astParent() && tok->isUnaryOp("*"))
return tok;
// Skip following variables if it is used in an assignment
if (Token::Match(tok->next(), "%assign%"))
return tok;
const Variable * var = tok->variable();
const Token * varTok = getVariableInitExpression(var);
2018-08-07 18:06:51 +02:00
if (!varTok)
return tok;
// Bailout. If variable value depends on value of "this".
if (exprDependsOnThis(varTok))
return tok;
// Skip array access
2018-08-07 18:06:51 +02:00
if (Token::simpleMatch(varTok, "["))
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())
return tok;
2018-08-07 18:06:51 +02:00
if (var->isStatic() && !var->isConst())
return tok;
2018-08-07 18:06:51 +02:00
if (var->isArgument())
return tok;
const Token * lastTok = precedes(tok, end) ? end : tok;
// If this is in a loop then check if variables are modified in the entire scope
const Token * endToken = (isInLoopCondition(tok) || isInLoopCondition(varTok) || var->scope() != tok->scope()) ? var->scope()->bodyEnd : lastTok;
if (!var->isConst() && (!precedes(varTok, endToken) || isVariableChanged(varTok, endToken, tok->varId(), false, nullptr, cpp)))
return tok;
if (precedes(varTok, endToken) && isAliased(varTok, endToken, tok->varId()))
return tok;
// Start at beginning of initialization
const Token * startToken = varTok;
2018-08-07 18:06:51 +02:00
while (Token::Match(startToken, "%op%|.|(|{") && startToken->astOperand1())
startToken = startToken->astOperand1();
// Skip if the variable its referring to is modified
2018-08-07 18:06:51 +02:00
for (const Token * tok2 = startToken; tok2 != endToken; tok2 = tok2->next()) {
if (Token::simpleMatch(tok2, ";"))
break;
2018-08-07 18:06:51 +02:00
if (tok2->astParent() && tok2->isUnaryOp("*"))
return tok;
2018-08-07 18:06:51 +02:00
if (tok2->tokType() == Token::eIncDecOp ||
tok2->isAssignmentOp() ||
Token::Match(tok2, "%name% .|[|++|--|%assign%")) {
return tok;
}
2018-08-07 18:06:51 +02:00
if (const Variable * var2 = tok2->variable()) {
2018-09-08 09:07:59 +02:00
if (!var2->scope())
return tok;
const Token * endToken2 = var2->scope() != tok->scope() ? var2->scope()->bodyEnd : endToken;
2018-08-07 18:06:51 +02:00
if (!var2->isLocal() && !var2->isConst() && !var2->isArgument())
return tok;
2018-08-07 18:06:51 +02:00
if (var2->isStatic() && !var2->isConst())
return tok;
if (!var2->isConst() && (!precedes(tok2, endToken2) || isVariableChanged(tok2, endToken2, tok2->varId(), false, nullptr, cpp)))
return tok;
if (precedes(tok2, endToken2) && isAliased(tok2, endToken2, tok2->varId()))
return tok;
2018-09-08 09:07:59 +02:00
// Recognized as a variable but the declaration is unknown
} else if (tok2->varId() > 0) {
return tok;
2018-09-17 17:16:32 +02:00
} else if (tok2->tokType() == Token::eName && !Token::Match(tok2, "sizeof|decltype|typeof") && !tok2->function()) {
return tok;
}
}
return varTok;
}
static void followVariableExpressionError(const Token *tok1, const Token *tok2, ErrorPath* errors)
{
2018-08-07 18:06:51 +02:00
if (!errors)
return;
2018-08-07 18:06:51 +02:00
if (!tok1)
return;
2018-08-07 18:06:51 +02:00
if (!tok2)
return;
ErrorPathItem item = std::make_pair(tok2, "'" + tok1->str() + "' is assigned value '" + tok2->expressionString() + "' here.");
2018-08-18 15:45:50 +02:00
if (std::find(errors->begin(), errors->end(), item) != errors->end())
return;
errors->push_back(item);
}
bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
{
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();
}
// Skip double not
2018-07-15 14:45:33 +02:00
if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=")) {
return isSameExpression(cpp, macro, tok1->astOperand1()->astOperand1(), tok2, library, pure, followVar, errors);
}
2018-07-15 14:45:33 +02:00
if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=")) {
return isSameExpression(cpp, macro, tok1, tok2->astOperand1()->astOperand1(), library, pure, followVar, errors);
}
// Follow variable
if (followVar && tok1->str() != tok2->str() && (Token::Match(tok1, "%var%") || Token::Match(tok2, "%var%"))) {
const Token * varTok1 = followVariableExpression(tok1, cpp, tok2);
if (varTok1->str() == tok2->str()) {
followVariableExpressionError(tok1, varTok1, errors);
return isSameExpression(cpp, macro, varTok1, tok2, library, true, errors);
}
const Token * varTok2 = followVariableExpression(tok2, cpp, tok1);
2018-08-07 18:06:51 +02:00
if (tok1->str() == varTok2->str()) {
followVariableExpressionError(tok2, varTok2, errors);
return isSameExpression(cpp, macro, tok1, varTok2, library, true, errors);
}
2018-08-07 18:06:51 +02:00
if (varTok1->str() == varTok2->str()) {
followVariableExpressionError(tok1, varTok1, errors);
followVariableExpressionError(tok2, varTok2, errors);
return isSameExpression(cpp, macro, varTok1, varTok2, library, true, errors);
}
}
if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str() || tok1->originalName() != tok2->originalName()) {
2015-08-03 09:20:50 +02:00
if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) ||
(Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) {
return isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure, followVar, errors) &&
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure, followVar, errors);
2015-08-03 09:20:50 +02:00
}
return false;
}
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
2015-08-03 09:20:50 +02:00
return false;
if (tok1->isComplex() != tok2->isComplex())
return false;
if (tok1->isLong() != tok2->isLong())
return false;
if (tok1->isUnsigned() != tok2->isUnsigned())
return false;
if (tok1->isSigned() != tok2->isSigned())
return false;
2018-04-08 20:11:44 +02:00
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {
if (!tok1->function()) {
if (Token::simpleMatch(tok1->previous(), ".")) {
const Token *lhs = tok1->previous();
while (Token::Match(lhs, "(|.|["))
lhs = lhs->astOperand1();
const bool lhsIsConst = (lhs->variable() && lhs->variable()->isConst()) ||
(lhs->valueType() && lhs->valueType()->constness > 0) ||
(Token::Match(lhs, "%var% . %name% (") && library.isFunctionConst(lhs->tokAt(2)));
if (!lhsIsConst)
return false;
} else {
const Token * ftok = tok1;
if (Token::simpleMatch(tok1->previous(), "::"))
ftok = tok1->previous();
if (!library.isFunctionConst(ftok) && !ftok->isAttributeConst() && !ftok->isAttributePure())
return false;
}
} else {
if (tok1->function() && !tok1->function()->isConst() && !tok1->function()->isAttributeConst() && !tok1->function()->isAttributePure())
return false;
}
2015-08-03 09:20:50 +02:00
}
// templates/casts
if ((Token::Match(tok1, "%name% <") && tok1->next()->link()) ||
(Token::Match(tok2, "%name% <") && tok2->next()->link())) {
// non-const template function that is not a dynamic_cast => return false
if (pure && Token::simpleMatch(tok1->next()->link(), "> (") &&
2015-08-03 09:20:50 +02:00
!(tok1->function() && tok1->function()->isConst()) &&
tok1->str() != "dynamic_cast")
return false;
// some template/cast stuff.. check that the template arguments are same
const Token *t1 = tok1->next();
const Token *t2 = tok2->next();
const Token *end1 = t1->link();
const Token *end2 = t2->link();
while (t1 && t2 && t1 != end1 && t2 != end2) {
if (t1->str() != t2->str())
return false;
t1 = t1->next();
t2 = t2->next();
}
if (t1 != end1 || t2 != end2)
return false;
}
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;
// cast => assert that the casts are equal
2018-10-01 11:53:32 +02:00
if (tok1->str() == "(" && tok1->previous() &&
!tok1->previous()->isName() &&
!(tok1->previous()->str() == ">" && tok1->previous()->link())) {
2015-08-03 09:20:50 +02:00
const Token *t1 = tok1->next();
const Token *t2 = tok2->next();
while (t1 && t2 &&
t1->str() == t2->str() &&
t1->isLong() == t2->isLong() &&
t1->isUnsigned() == t2->isUnsigned() &&
t1->isSigned() == t2->isSigned() &&
(t1->isName() || t1->str() == "*")) {
2015-08-03 09:20:50 +02:00
t1 = t1->next();
t2 = t2->next();
}
if (!t1 || !t2 || t1->str() != ")" || t2->str() != ")")
return false;
}
bool noncommutativeEquals =
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand1(), library, pure, followVar, errors);
noncommutativeEquals = noncommutativeEquals &&
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand2(), library, pure, followVar, errors);
2015-08-03 09:20:50 +02:00
if (noncommutativeEquals)
2015-08-03 09:20:50 +02:00
return true;
// in c++, a+b might be different to b+a, depending on the type of a and b
if (cpp && tok1->str() == "+" && tok1->isBinaryOp()) {
const ValueType* vt1 = tok1->astOperand1()->valueType();
const ValueType* vt2 = tok1->astOperand2()->valueType();
if (!(vt1 && (vt1->type >= ValueType::VOID || vt1->pointer) && vt2 && (vt2->type >= ValueType::VOID || vt2->pointer)))
return false;
}
const bool commutative = tok1->isBinaryOp() && Token::Match(tok1, "%or%|%oror%|+|*|&|&&|^|==|!=");
bool commutativeEquals = commutative &&
isSameExpression(cpp, macro, tok1->astOperand2(), tok2->astOperand1(), library, pure, followVar, errors);
commutativeEquals = commutativeEquals &&
isSameExpression(cpp, macro, tok1->astOperand1(), tok2->astOperand2(), library, pure, followVar, errors);
2015-08-03 09:20:50 +02:00
return commutativeEquals;
2015-08-03 09:20:50 +02:00
}
2018-03-24 07:58:37 +01:00
bool isEqualKnownValue(const Token * const tok1, const Token * const tok2)
{
return tok1->hasKnownValue() && tok2->hasKnownValue() && tok1->values() == tok2->values();
}
bool isDifferentKnownValues(const Token * const tok1, const Token * const tok2)
{
return tok1->hasKnownValue() && tok2->hasKnownValue() && tok1->values() != tok2->values();
}
static bool isZeroBoundCond(const Token * const cond)
{
2018-05-11 14:49:31 +02:00
if (cond == nullptr)
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;
}
bool isOppositeCond(bool isNot, bool cpp, const Token * const cond1, const Token * const cond2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
2015-08-03 09:20:50 +02:00
{
if (!cond1 || !cond2)
return false;
if (cond1->str() == "!") {
if (cond2->str() == "!=") {
if (cond2->astOperand1() && cond2->astOperand1()->str() == "0")
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")
return isSameExpression(cpp, true, cond1->astOperand1(), cond2->astOperand1(), library, pure, followVar, errors);
2015-08-03 09:20:50 +02:00
}
return isSameExpression(cpp, true, cond1->astOperand1(), cond2, library, pure, followVar, errors);
2015-08-03 09:20:50 +02:00
}
if (cond2->str() == "!")
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() == "==") {
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());
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());
}
// TODO: Handle reverse conditions
2018-04-05 08:21:43 +02:00
if (Library::isContainerYield(cond1, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond2->astOperand1(), Library::Container::SIZE, "size") &&
cond1->astOperand1()->astOperand1()->varId() == cond2->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !isZeroBoundCond(cond2);
2018-03-24 07:58:37 +01:00
}
2018-04-05 08:21:43 +02:00
if (Library::isContainerYield(cond2, Library::Container::EMPTY, "empty") &&
Library::isContainerYield(cond1->astOperand1(), Library::Container::SIZE, "size") &&
cond2->astOperand1()->astOperand1()->varId() == cond1->astOperand1()->astOperand1()->astOperand1()->varId()) {
return !isZeroBoundCond(cond1);
2018-03-24 07:58:37 +01: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;
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();
} 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] = '>';
}
if (!isNot && comp2.empty()) {
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] = '>';
}
if (cond2->astOperand2()->hasKnownIntValue()) {
expr2 = cond2->astOperand1();
value2 = cond2->astOperand2();
} else if (cond2->astOperand1()->hasKnownIntValue()) {
expr2 = cond2->astOperand2();
value2 = cond2->astOperand1();
if (op2[0] == '>')
op2[0] = '<';
else if (op2[0] == '<')
op2[0] = '>';
}
if (!expr1 || !value1 || !expr2 || !value2) {
return false;
}
if (!isSameExpression(cpp, true, expr1, expr2, library, pure, followVar, errors))
return false;
const ValueFlow::Value &rhsValue1 = value1->values().front();
const ValueFlow::Value &rhsValue2 = value2->values().front();
if (op1 == "<" || op1 == "<=")
return (op2 == "==" || op2 == ">" || op2 == ">=") && (rhsValue1.intvalue < rhsValue2.intvalue);
else if (op1 == ">=" || op1 == ">")
return (op2 == "==" || op2 == "<" || op2 == "<=") && (rhsValue1.intvalue > rhsValue2.intvalue);
return false;
}
2015-08-03 09:20:50 +02:00
// is condition opposite?
return ((comp1 == "==" && comp2 == "!=") ||
(comp1 == "!=" && comp2 == "==") ||
(comp1 == "<" && comp2 == ">=") ||
(comp1 == "<=" && comp2 == ">") ||
(comp1 == ">" && comp2 == "<=") ||
(comp1 == ">=" && comp2 == "<") ||
(!isNot && ((comp1 == "<" && comp2 == ">") ||
(comp1 == ">" && comp2 == "<") ||
(comp1 == "==" && (comp2 == "!=" || comp2 == ">" || comp2 == "<")) ||
2018-08-05 22:40:21 +02:00
((comp1 == "!=" || comp1 == ">" || comp1 == "<") && comp2 == "==")
)));
2015-08-03 09:20:50 +02:00
}
bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * const tok2, const Library& library, bool pure, bool followVar, ErrorPath* errors)
{
if (!tok1 || !tok2)
return false;
if (isOppositeCond(true, cpp, tok1, tok2, library, pure, followVar, errors))
return true;
if (tok1->isUnaryOp("-"))
return isSameExpression(cpp, true, tok1->astOperand1(), tok2, library, pure, followVar, errors);
if (tok2->isUnaryOp("-"))
return isSameExpression(cpp, true, tok2->astOperand1(), tok1, library, pure, followVar, errors);
return false;
}
bool isConstExpression(const Token *tok, const Library& library, bool pure, bool cpp)
2015-08-03 09:20:50 +02:00
{
if (!tok)
return true;
if (tok->isName() && tok->next()->str() == "(") {
2016-12-06 12:31:16 +01:00
if (!tok->function() && !Token::Match(tok->previous(), ".|::") && !library.isFunctionConst(tok->str(), pure))
2015-08-03 09:20:50 +02:00
return false;
else if (tok->function() && !tok->function()->isConst())
return false;
}
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())
return false;
2018-10-21 20:28:46 +02:00
if (isLikelyStreamRead(cpp, tok))
return false;
2015-08-03 09:20:50 +02:00
// bailout when we see ({..})
if (tok->str() == "{")
return false;
return isConstExpression(tok->astOperand1(), library, pure, cpp) && isConstExpression(tok->astOperand2(), library, pure, cpp);
2015-08-03 09:20:50 +02:00
}
bool isWithoutSideEffects(bool cpp, const Token* tok)
{
if (!cpp)
return true;
while (tok && tok->astOperand2() && tok->astOperand2()->str() != "(")
tok = tok->astOperand2();
if (tok && tok->varId()) {
const Variable* var = tok->variable();
return var && (!var->isClass() || var->isPointer() || var->isStlType());
}
return true;
}
bool isUniqueExpression(const Token* tok)
{
if (!tok)
return true;
if (tok->function()) {
const Function * fun = tok->function();
const Scope * scope = fun->nestedIn;
if (!scope)
return true;
const std::string returnType = fun->retType ? fun->retType->name() : fun->retDef->stringifyList(fun->tokenDef);
for (const Function& f:scope->functionList) {
if (f.type != Function::eFunction)
continue;
const std::string freturnType = f.retType ? f.retType->name() : f.retDef->stringifyList(f.tokenDef);
if (f.argumentList.size() == fun->argumentList.size() &&
returnType == freturnType &&
f.name() != fun->name()) {
return false;
}
}
} else if (tok->variable()) {
const Variable * var = tok->variable();
const Scope * scope = var->scope();
if (!scope)
return true;
const Type * varType = var->type();
// Iterate over the variables in scope and the parameters of the function if possible
const Function * fun = scope->function;
const std::list<Variable>* setOfVars[] = {&scope->varlist, fun ? &fun->argumentList : nullptr};
if (varType) {
for (const std::list<Variable>* vars:setOfVars) {
if (!vars)
continue;
for (const Variable& v:*vars) {
if (v.type() && v.type()->name() == varType->name() && v.name() != var->name()) {
return false;
}
}
}
} else {
for (const std::list<Variable>* vars:setOfVars) {
if (!vars)
continue;
for (const Variable& v:*vars) {
if (v.isFloatingType() == var->isFloatingType() &&
v.isEnumType() == var->isEnumType() &&
v.isClass() == var->isClass() &&
v.isArray() == var->isArray() &&
v.isPointer() == var->isPointer() &&
v.name() != var->name())
return false;
}
}
}
} else if (!isUniqueExpression(tok->astOperand1())) {
return false;
}
return isUniqueExpression(tok->astOperand2());
}
bool isReturnScope(const Token * const endToken)
{
if (!endToken || endToken->str() != "}")
return false;
const Token *prev = endToken->previous();
while (prev && Token::simpleMatch(prev->previous(), "; ;"))
prev = prev->previous();
if (prev && Token::simpleMatch(prev->previous(), "} ;"))
prev = prev->previous();
if (Token::simpleMatch(prev, "}")) {
if (Token::simpleMatch(prev->link()->tokAt(-2), "} else {"))
return isReturnScope(prev) && isReturnScope(prev->link()->tokAt(-2));
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
!Token::findsimplematch(prev->link(), "break", prev)) {
return true;
}
if (Token::simpleMatch(prev->link()->previous(), ") {") &&
Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "return (")) {
return true;
}
if (Token::Match(prev->link()->previous(), "[;{}] {"))
return isReturnScope(prev);
} else if (Token::simpleMatch(prev, ";")) {
// noreturn function
if (Token::simpleMatch(prev->previous(), ") ;") && Token::Match(prev->linkAt(-1)->tokAt(-2), "[;{}] %name% ("))
return true;
// return/goto statement
prev = prev->previous();
while (prev && !Token::Match(prev, ";|{|}|return|goto|throw|continue|break"))
prev = prev->previous();
return prev && prev->isName();
}
return false;
}
bool isVariableChangedByFunctionCall(const Token *tok, unsigned int varid, const Settings *settings, bool *inconclusive)
{
if (!tok)
return false;
if (tok->varId() == varid)
return isVariableChangedByFunctionCall(tok, settings, inconclusive);
return isVariableChangedByFunctionCall(tok->astOperand1(), varid, settings, inconclusive) ||
isVariableChangedByFunctionCall(tok->astOperand2(), varid, settings, inconclusive);
}
bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings, bool *inconclusive)
{
if (!tok)
return false;
// address of variable
const bool addressOf = Token::simpleMatch(tok->previous(), "&");
// passing variable to subfunction?
if (Token::Match(tok->tokAt(-2), ") & %name% [,)]") && Token::Match(tok->linkAt(-2)->previous(), "[,(] ("))
;
else if (Token::Match(tok->tokAt(addressOf?-2:-1), "[(,] &| %name% [,)]"))
;
else if (Token::Match(tok->tokAt(addressOf?-2:-1), "[?:] &| %name% [:,)]")) {
const Token *parent = tok->astParent();
if (parent == tok->previous() && parent->str() == "&")
parent = parent->astParent();
while (Token::Match(parent, "[?:]"))
parent = parent->astParent();
while (Token::simpleMatch(parent, ","))
parent = parent->astParent();
if (!parent || parent->str() != "(")
return false;
} else
return false;
// reinterpret_cast etc..
if (Token::Match(tok->tokAt(-3), "> ( & %name% ) [,)]") &&
tok->linkAt(-3) &&
Token::Match(tok->linkAt(-3)->tokAt(-2), "[,(] %type% <"))
tok = tok->linkAt(-3);
// goto start of function call and get argnr
unsigned int argnr = 0;
while (tok && tok->str() != "(") {
if (tok->str() == ",")
++argnr;
else if (tok->str() == ")")
tok = tok->link();
tok = tok->previous();
}
tok = tok ? tok->previous() : nullptr;
if (tok && tok->link() && tok->str() == ">")
tok = tok->link()->previous();
if (!Token::Match(tok, "%name% [(<]"))
return false; // not a function => variable not changed
// Constructor call
if (tok->variable() && tok->variable()->nameToken() == tok) {
// Find constructor..
const unsigned int argCount = numberOfArguments(tok);
2018-04-05 08:07:22 +02:00
const Scope *typeScope = tok->variable()->typeScope();
if (typeScope) {
2018-10-25 06:14:27 +02:00
for (const Function &function : typeScope->functionList) {
if (!function.isConstructor() || function.argCount() < argCount)
continue;
2018-10-25 06:14:27 +02:00
const Variable *arg = function.getArgumentVar(argnr);
if (arg && arg->isReference() && !arg->isConst())
return true;
}
return false;
}
if (inconclusive)
*inconclusive = true;
return false;
}
if (!tok->function()) {
// if the library says 0 is invalid
// => it is assumed that parameter is an in parameter (TODO: this is a bad heuristic)
if (!addressOf && settings && settings->library.isnullargbad(tok, 1+argnr))
return false;
// addressOf => inconclusive
if (!addressOf) {
if (inconclusive != nullptr)
*inconclusive = true;
return false;
}
return true;
}
const Variable *arg = tok->function()->getArgumentVar(argnr);
if (addressOf) {
2018-08-07 18:06:51 +02:00
if (!(arg && arg->isConst()))
return true;
// If const is applied to the pointer, then the value can still be modified
2018-08-07 18:06:51 +02:00
if (arg && Token::simpleMatch(arg->typeEndToken(), "* const"))
return true;
}
return arg && !arg->isConst() && arg->isReference();
}
bool isVariableChanged(const Token *start, const Token *end, const unsigned int varid, bool globalvar, const Settings *settings, bool cpp)
{
for (const Token *tok = start; tok != end; tok = tok->next()) {
if (tok->varId() != varid) {
if (globalvar && Token::Match(tok, "%name% ("))
// TODO: Is global variable really changed by function call?
return true;
continue;
}
if (Token::Match(tok, "%name% %assign%|++|--"))
return true;
if (Token::Match(tok->previous(), "++|-- %name%"))
return true;
2018-04-18 17:46:31 +02:00
if (isLikelyStreamRead(cpp, tok->previous()))
return true;
// Member function call
2018-08-27 13:48:46 +02:00
if (Token::Match(tok, "%name% . %name% (")) {
const Variable * var = tok->variable();
bool isConst = var && var->isConst();
2018-08-27 13:48:46 +02:00
if (!isConst && var) {
const ValueType * valueType = var->valueType();
isConst = (valueType && valueType->pointer == 1 && valueType->constness == 1);
}
2018-08-27 13:48:46 +02:00
const Token *ftok = tok->tokAt(2);
const Function * fun = ftok->function();
2018-08-27 13:48:46 +02:00
if (!isConst && (!fun || !fun->isConst()))
return true;
}
const Token *ftok = tok;
while (ftok && !Token::Match(ftok, "[({[]"))
ftok = ftok->astParent();
if (ftok && Token::Match(ftok->link(), ") !!{")) {
bool inconclusive = false;
bool isChanged = isVariableChangedByFunctionCall(tok, settings, &inconclusive);
isChanged |= inconclusive;
if (isChanged)
return true;
}
const Token *parent = tok->astParent();
while (Token::Match(parent, ".|::"))
parent = parent->astParent();
if (parent && parent->tokType() == Token::eIncDecOp)
return true;
}
return false;
}
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp)
{
2018-10-20 09:28:28 +02:00
if (!var)
return false;
2018-10-20 09:28:28 +02:00
if (!var->scope())
return false;
const Token * start = var->declEndToken();
2018-10-20 09:28:28 +02:00
if (!start)
return false;
2018-10-20 09:28:28 +02:00
if (Token::Match(start, "; %varid% =", var->declarationId()))
start = start->tokAt(2);
return isVariableChanged(start->next(), var->scope()->bodyEnd, var->declarationId(), var->isGlobal(), settings, cpp);
}
int numberOfArguments(const Token *start)
{
int arguments=0;
const Token* const openBracket = start->next();
if (openBracket && openBracket->str()=="(" && openBracket->next() && openBracket->next()->str()!=")") {
const Token* argument=openBracket->next();
while (argument) {
++arguments;
argument = argument->nextArgument();
}
}
return arguments;
}
static void getArgumentsRecursive(const Token *tok, std::vector<const Token *> *arguments)
{
if (!tok)
return;
if (tok->str() == ",") {
getArgumentsRecursive(tok->astOperand1(), arguments);
getArgumentsRecursive(tok->astOperand2(), arguments);
} else {
arguments->push_back(tok);
}
}
std::vector<const Token *> getArguments(const Token *ftok)
{
std::vector<const Token *> arguments;
getArgumentsRecursive(ftok->next()->astOperand2(), &arguments);
return arguments;
}
const Token *findLambdaEndToken(const Token *first)
{
if (!first || first->str() != "[")
return nullptr;
2018-11-10 21:30:01 +01:00
if (!Token::Match(first->link(), "] (|{"))
return nullptr;
2018-11-10 21:30:01 +01:00
if (first->astOperand1() != first->link()->next())
return nullptr;
const Token * tok = first;
if (tok->astOperand1() && tok->astOperand1()->str() == "(")
tok = tok->astOperand1();
if (tok->astOperand1() && tok->astOperand1()->str() == "{")
return tok->astOperand1()->link();
return nullptr;
}
bool isLikelyStreamRead(bool cpp, const Token *op)
{
if (!cpp)
return false;
if (!Token::Match(op, "&|>>") || !op->isBinaryOp())
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 20:34:31 +02:00
const Token *parent = op;
while (parent->astParent() && parent->astParent()->str() == op->str())
parent = parent->astParent();
if (parent->astParent() && !Token::Match(parent->astParent(), "%oror%|&&|(|,|!"))
2018-04-17 20:34:31 +02:00
return false;
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());
}