|
Packit |
2035a7 |
/*
|
|
Packit |
2035a7 |
* Cppcheck - A tool for static C/C++ code analysis
|
|
Packit |
2035a7 |
* Copyright (C) 2007-2017 Cppcheck team.
|
|
Packit |
2035a7 |
*
|
|
Packit |
2035a7 |
* This program is free software: you can redistribute it and/or modify
|
|
Packit |
2035a7 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
2035a7 |
* the Free Software Foundation, either version 3 of the License, or
|
|
Packit |
2035a7 |
* (at your option) any later version.
|
|
Packit |
2035a7 |
*
|
|
Packit |
2035a7 |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
2035a7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
2035a7 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
2035a7 |
* GNU General Public License for more details.
|
|
Packit |
2035a7 |
*
|
|
Packit |
2035a7 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
2035a7 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
2035a7 |
*/
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
//---------------------------------------------------------------------------
|
|
Packit |
2035a7 |
#ifndef valueflowH
|
|
Packit |
2035a7 |
#define valueflowH
|
|
Packit |
2035a7 |
//---------------------------------------------------------------------------
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
#include "config.h"
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
#include <list>
|
|
Packit |
2035a7 |
#include <string>
|
|
Packit |
2035a7 |
#include <utility>
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
class ErrorLogger;
|
|
Packit |
2035a7 |
class Settings;
|
|
Packit |
2035a7 |
class SymbolDatabase;
|
|
Packit |
2035a7 |
class Token;
|
|
Packit |
2035a7 |
class TokenList;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
namespace ValueFlow {
|
|
Packit |
2035a7 |
class CPPCHECKLIB Value {
|
|
Packit |
2035a7 |
public:
|
|
Packit |
2035a7 |
typedef std::pair<const Token *, std::string> ErrorPathItem;
|
|
Packit |
2035a7 |
typedef std::list<ErrorPathItem> ErrorPath;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
explicit Value(long long val = 0) : valueType(INT), intvalue(val), tokvalue(nullptr), floatValue(0.0), moveKind(NonMovedVariable), varvalue(val), condition(nullptr), varId(0U), conditional(false), defaultArg(false), valueKind(ValueKind::Possible) {}
|
|
Packit |
2035a7 |
Value(const Token *c, long long val);
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
bool operator==(const Value &rhs) const {
|
|
Packit |
2035a7 |
if (valueType != rhs.valueType)
|
|
Packit |
2035a7 |
return false;
|
|
Packit |
2035a7 |
switch (valueType) {
|
|
Packit |
2035a7 |
case INT:
|
|
Packit |
2035a7 |
if (intvalue != rhs.intvalue)
|
|
Packit |
2035a7 |
return false;
|
|
Packit |
2035a7 |
break;
|
|
Packit |
2035a7 |
case TOK:
|
|
Packit |
2035a7 |
if (tokvalue != rhs.tokvalue)
|
|
Packit |
2035a7 |
return false;
|
|
Packit |
2035a7 |
break;
|
|
Packit |
2035a7 |
case FLOAT:
|
|
Packit |
2035a7 |
// TODO: Write some better comparison
|
|
Packit |
2035a7 |
if (floatValue > rhs.floatValue || floatValue < rhs.floatValue)
|
|
Packit |
2035a7 |
return false;
|
|
Packit |
2035a7 |
break;
|
|
Packit |
2035a7 |
case MOVED:
|
|
Packit |
2035a7 |
if (moveKind != rhs.moveKind)
|
|
Packit |
2035a7 |
return false;
|
|
Packit |
2035a7 |
break;
|
|
Packit |
2035a7 |
case UNINIT:
|
|
Packit |
2035a7 |
break;
|
|
Packit |
2035a7 |
};
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
return varvalue == rhs.varvalue &&
|
|
Packit |
2035a7 |
condition == rhs.condition &&
|
|
Packit |
2035a7 |
varId == rhs.varId &&
|
|
Packit |
2035a7 |
conditional == rhs.conditional &&
|
|
Packit |
2035a7 |
defaultArg == rhs.defaultArg &&
|
|
Packit |
2035a7 |
valueKind == rhs.valueKind;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
std::string infoString() const;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
enum ValueType { INT, TOK, FLOAT, MOVED, UNINIT } valueType;
|
|
Packit |
2035a7 |
bool isIntValue() const {
|
|
Packit |
2035a7 |
return valueType == INT;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
bool isTokValue() const {
|
|
Packit |
2035a7 |
return valueType == TOK;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
bool isFloatValue() const {
|
|
Packit |
2035a7 |
return valueType == FLOAT;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
bool isMovedValue() const {
|
|
Packit |
2035a7 |
return valueType == MOVED;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
bool isUninitValue() const {
|
|
Packit |
2035a7 |
return valueType == UNINIT;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** int value */
|
|
Packit |
2035a7 |
long long intvalue;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** token value - the token that has the value. this is used for pointer aliases, strings, etc. */
|
|
Packit |
2035a7 |
const Token *tokvalue;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** float value */
|
|
Packit |
2035a7 |
double floatValue;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** kind of moved */
|
|
Packit |
2035a7 |
enum MoveKind {NonMovedVariable, MovedVariable, ForwardedVariable} moveKind;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** For calculated values - variable value that calculated value depends on */
|
|
Packit |
2035a7 |
long long varvalue;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** Condition that this value depends on */
|
|
Packit |
2035a7 |
const Token *condition;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
ErrorPath errorPath;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** For calculated values - varId that calculated value depends on */
|
|
Packit |
2035a7 |
unsigned int varId;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** Conditional value */
|
|
Packit |
2035a7 |
bool conditional;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** Is this value passed as default parameter to the function? */
|
|
Packit |
2035a7 |
bool defaultArg;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
static const char * toString(MoveKind moveKind) {
|
|
Packit |
2035a7 |
switch (moveKind) {
|
|
Packit |
2035a7 |
case NonMovedVariable:
|
|
Packit |
2035a7 |
return "NonMovedVariable";
|
|
Packit |
2035a7 |
case MovedVariable:
|
|
Packit |
2035a7 |
return "MovedVariable";
|
|
Packit |
2035a7 |
case ForwardedVariable:
|
|
Packit |
2035a7 |
return "ForwardedVariable";
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
return "";
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/** How known is this value */
|
|
Packit |
2035a7 |
enum ValueKind {
|
|
Packit |
2035a7 |
/** This value is possible, other unlisted values may also be possible */
|
|
Packit |
2035a7 |
Possible,
|
|
Packit |
2035a7 |
/** Only listed values are possible */
|
|
Packit |
2035a7 |
Known,
|
|
Packit |
2035a7 |
/** Inconclusive */
|
|
Packit |
2035a7 |
Inconclusive
|
|
Packit |
2035a7 |
} valueKind;
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
void setKnown() {
|
|
Packit |
2035a7 |
valueKind = ValueKind::Known;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
bool isKnown() const {
|
|
Packit |
2035a7 |
return valueKind == ValueKind::Known;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
void setPossible() {
|
|
Packit |
2035a7 |
valueKind = ValueKind::Possible;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
bool isPossible() const {
|
|
Packit |
2035a7 |
return valueKind == ValueKind::Possible;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
void setInconclusive(bool inconclusive = true) {
|
|
Packit |
2035a7 |
if (inconclusive)
|
|
Packit |
2035a7 |
valueKind = ValueKind::Inconclusive;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
bool isInconclusive() const {
|
|
Packit |
2035a7 |
return valueKind == ValueKind::Inconclusive;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
void changeKnownToPossible() {
|
|
Packit |
2035a7 |
if (isKnown())
|
|
Packit |
2035a7 |
valueKind = ValueKind::Possible;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
bool errorSeverity() const {
|
|
Packit |
2035a7 |
return !condition && !defaultArg;
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
};
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/// Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFlow::setValues).
|
|
Packit |
2035a7 |
const ValueFlow::Value * valueFlowConstantFoldAST(const Token *expr, const Settings *settings);
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
/// Perform valueflow analysis.
|
|
Packit |
2035a7 |
void setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings);
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
std::string eitherTheConditionIsRedundant(const Token *condition);
|
|
Packit |
2035a7 |
}
|
|
Packit |
2035a7 |
|
|
Packit |
2035a7 |
#endif // valueflowH
|