1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 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 "vfvalue.h"
#include "errortypes.h"
#include "mathlib.h"
#include "token.h"
#include <sstream>
#include <string>
namespace ValueFlow {
Value::Value(const Token *c, MathLib::bigint val, Bound b)
: bound(b),
safe(false),
conditional(false),
macro(false),
defaultArg(false),
intvalue(val),
varvalue(val),
condition(c) {
errorPath.emplace_back(c, "Assuming that condition '" + c->expressionString() + "' is not redundant");
}
void Value::assumeCondition(const Token *tok) {
condition = tok;
errorPath.emplace_back(tok, "Assuming that condition '" + tok->expressionString() + "' is not redundant");
}
std::string Value::toString() const {
std::stringstream ss;
if (this->isImpossible())
ss << "!";
if (this->bound == Bound::Lower)
ss << ">=";
if (this->bound == Bound::Upper)
ss << "<=";
switch (this->valueType) {
case ValueType::INT:
ss << this->intvalue;
break;
case ValueType::TOK:
ss << this->tokvalue->str();
break;
case ValueType::FLOAT:
ss << MathLib::toString(this->floatValue);
break;
case ValueType::MOVED:
ss << toString(this->moveKind);
break;
case ValueType::UNINIT:
ss << "Uninit";
break;
case ValueType::BUFFER_SIZE:
case ValueType::CONTAINER_SIZE:
ss << "size=" << this->intvalue;
break;
case ValueType::ITERATOR_START:
ss << "start=" << this->intvalue;
break;
case ValueType::ITERATOR_END:
ss << "end=" << this->intvalue;
break;
case ValueType::LIFETIME:
ss << "lifetime[" << toString(this->lifetimeKind) << "]=("
<< this->tokvalue->expressionString() << ")";
break;
case ValueType::SYMBOLIC:
ss << "symbolic=(" << this->tokvalue->expressionString();
if (this->intvalue > 0)
ss << "+" << this->intvalue;
else if (this->intvalue < 0)
ss << "-" << -this->intvalue;
ss << ")";
break;
}
if (this->indirect > 0)
for (int i = 0; i < this->indirect; i++)
ss << "*";
if (this->path > 0)
ss << "@" << this->path;
return ss.str();
}
std::string Value::infoString() const {
switch (valueType) {
case ValueType::INT:
return MathLib::toString(intvalue);
case ValueType::TOK:
return tokvalue->str();
case ValueType::FLOAT:
return MathLib::toString(floatValue);
case ValueType::MOVED:
return "<Moved>";
case ValueType::UNINIT:
return "<Uninit>";
case ValueType::BUFFER_SIZE:
case ValueType::CONTAINER_SIZE:
return "size=" + MathLib::toString(intvalue);
case ValueType::ITERATOR_START:
return "start=" + MathLib::toString(intvalue);
case ValueType::ITERATOR_END:
return "end=" + MathLib::toString(intvalue);
case ValueType::LIFETIME:
return "lifetime=" + tokvalue->str();
case ValueType::SYMBOLIC:
{
std::string result = "symbolic=" + tokvalue->expressionString();
if (intvalue > 0)
result += "+" + MathLib::toString(intvalue);
else if (intvalue < 0)
result += "-" + MathLib::toString(-intvalue);
return result;
}
}
throw InternalError(nullptr, "Invalid ValueFlow Value type");
}
const char *Value::toString(MoveKind moveKind) {
switch (moveKind) {
case MoveKind::NonMovedVariable:
return "NonMovedVariable";
case MoveKind::MovedVariable:
return "MovedVariable";
case MoveKind::ForwardedVariable:
return "ForwardedVariable";
}
return "";
}
const char *Value::toString(LifetimeKind lifetimeKind) {
switch (lifetimeKind) {
case LifetimeKind::Object:
return "Object";
case LifetimeKind::SubObject:
return "SubObject";
case LifetimeKind::Lambda:
return "Lambda";
case LifetimeKind::Iterator:
return "Iterator";
case LifetimeKind::Address:
return "Address";
}
return "";
}
bool Value::sameToken(const Token *tok1, const Token *tok2) {
if (tok1 == tok2)
return true;
if (!tok1)
return false;
if (tok1->exprId() == 0 || tok2->exprId() == 0)
return false;
return tok1->exprId() == tok2->exprId();
}
const char *Value::toString(LifetimeScope lifetimeScope) {
switch (lifetimeScope) {
case LifetimeScope::Local:
return "Local";
case LifetimeScope::Argument:
return "Argument";
case LifetimeScope::SubFunction:
return "SubFunction";
case LifetimeScope::ThisPointer:
return "ThisPointer";
case LifetimeScope::ThisValue:
return "ThisValue";
}
return "";
}
const char *Value::toString(Bound bound) {
switch (bound) {
case Bound::Point:
return "Point";
case Bound::Upper:
return "Upper";
case Bound::Lower:
return "Lower";
}
return "";
}
}
|