2014-01-04 20:57:02 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2014-01-04 20:57:02 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifndef valueflowH
|
|
|
|
#define valueflowH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "config.h"
|
2020-02-17 10:31:08 +01:00
|
|
|
#include "mathlib.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <cstdlib>
|
2020-11-10 16:00:55 +01:00
|
|
|
#include <functional>
|
2017-05-15 20:05:11 +02:00
|
|
|
#include <list>
|
2015-07-29 19:54:57 +02:00
|
|
|
#include <string>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <utility>
|
2019-09-11 19:25:09 +02:00
|
|
|
#include <vector>
|
2015-07-29 19:54:57 +02:00
|
|
|
|
2014-01-05 20:06:46 +01:00
|
|
|
class ErrorLogger;
|
2021-10-30 22:13:58 +02:00
|
|
|
struct InferModel;
|
2014-01-05 20:06:46 +01:00
|
|
|
class Settings;
|
2017-05-27 04:33:47 +02:00
|
|
|
class SymbolDatabase;
|
|
|
|
class Token;
|
|
|
|
class TokenList;
|
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
|
|
|
class ValueType;
|
2019-01-23 07:29:16 +01:00
|
|
|
class Variable;
|
2022-02-22 09:51:44 +01:00
|
|
|
class Scope;
|
2014-01-04 20:57:02 +01:00
|
|
|
|
2021-10-30 22:13:58 +02:00
|
|
|
template<class T>
|
|
|
|
class ValuePtr;
|
|
|
|
|
2014-01-04 20:57:02 +01:00
|
|
|
namespace ValueFlow {
|
2016-05-07 20:18:07 +02:00
|
|
|
/// Constant folding of expression. This can be used before the full ValueFlow has been executed (ValueFlow::setValues).
|
2023-01-28 10:20:47 +01:00
|
|
|
const Value * valueFlowConstantFoldAST(Token *expr, const Settings *settings);
|
2016-05-07 20:18:07 +02:00
|
|
|
|
|
|
|
/// Perform valueflow analysis.
|
2015-02-01 12:10:20 +01:00
|
|
|
void setValues(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger *errorLogger, const Settings *settings);
|
2015-07-29 19:54:57 +02:00
|
|
|
|
|
|
|
std::string eitherTheConditionIsRedundant(const Token *condition);
|
Set correct type and size of string and char literals (#2275)
* Set correct type and size of string and char literals
Use that string and char literal tokens store the prefix. This makes
it possible to distinghuish between different type of string literals
(i.e., utf8 encoded strings, utf16, wide strings, etc) which have
different type.
When the tokens holding the string and character values have the correct
type, it is possible to improve Token::getStrSize() to give the correct
result for all string types. Previously, it would return the number of
characters in the string, i.e., it would give the wrong size unless
the type of the string was char*.
Since strings now can have different size (in number of bytes) and
length (in number of elements), add a new helper function that returns
the number of characters. Checkers have been updated to use the correct
functions.
Having the size makes it possible to find more problems with prefixed
strings, and to reduce false positives, for example in the buffer
overflow checker.
Also, improve the stringLiteralWrite error message to also print the
prefix of the string (if there is one).
* Add comment and update string length
2019-10-20 07:11:57 +02:00
|
|
|
|
|
|
|
size_t getSizeOf(const ValueType &vt, const Settings *settings);
|
2021-06-04 17:17:41 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Value* findValue(const std::list<Value>& values,
|
|
|
|
const Settings* settings,
|
|
|
|
const std::function<bool(const Value&)> &pred);
|
2021-08-16 09:19:07 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
std::vector<Value> isOutOfBounds(const Value& size, const Token* indexTok, bool possible = true);
|
2022-03-12 06:15:35 +01:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
Value asImpossible(Value v);
|
2021-12-20 07:28:40 +01:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
bool isContainerSizeChanged(const Token* tok, int indirect, const Settings* settings = nullptr, int depth = 20);
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
struct LifetimeToken {
|
|
|
|
const Token* token;
|
|
|
|
Value::ErrorPath errorPath;
|
|
|
|
bool addressOf;
|
|
|
|
bool inconclusive;
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
LifetimeToken() : token(nullptr), errorPath(), addressOf(false), inconclusive(false) {}
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
LifetimeToken(const Token* token, Value::ErrorPath errorPath)
|
|
|
|
: token(token), errorPath(std::move(errorPath)), addressOf(false), inconclusive(false)
|
|
|
|
{}
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
LifetimeToken(const Token* token, bool addressOf, Value::ErrorPath errorPath)
|
|
|
|
: token(token), errorPath(std::move(errorPath)), addressOf(addressOf), inconclusive(false)
|
|
|
|
{}
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
static std::vector<LifetimeToken> setAddressOf(std::vector<LifetimeToken> v, bool b) {
|
|
|
|
for (LifetimeToken& x : v)
|
|
|
|
x.addressOf = b;
|
|
|
|
return v;
|
|
|
|
}
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
static std::vector<LifetimeToken> setInconclusive(std::vector<LifetimeToken> v, bool b) {
|
|
|
|
for (LifetimeToken& x : v)
|
|
|
|
x.inconclusive = b;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
};
|
2019-10-30 17:57:46 +01:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value, const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate);
|
|
|
|
const Token *parseCompareInt(const Token *tok, Value &true_value, Value &false_value);
|
2021-03-30 14:02:28 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
CPPCHECKLIB ValuePtr<InferModel> makeIntegralInferModel();
|
2021-10-30 22:13:58 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Token* solveExprValue(const Token* expr,
|
|
|
|
const std::function<std::vector<MathLib::bigint>(const Token*)>& eval,
|
|
|
|
Value& value);
|
2022-05-04 05:59:01 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
std::vector<LifetimeToken> getLifetimeTokens(const Token* tok,
|
|
|
|
bool escape = false,
|
|
|
|
Value::ErrorPath errorPath = Value::ErrorPath{});
|
2021-04-30 17:47:08 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
bool hasLifetimeToken(const Token* tok, const Token* lifetime);
|
2019-09-11 19:25:09 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Variable* getLifetimeVariable(const Token* tok, Value::ErrorPath& errorPath, bool* addressOf = nullptr);
|
2019-01-23 07:29:16 +01:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Variable* getLifetimeVariable(const Token* tok);
|
2020-05-31 10:10:10 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
bool isLifetimeBorrowed(const Token *tok, const Settings *settings);
|
2019-05-05 11:40:59 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
std::string lifetimeMessage(const Token *tok, const Value *val, Value::ErrorPath &errorPath);
|
2019-01-29 09:47:52 +01:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
CPPCHECKLIB Value getLifetimeObjValue(const Token *tok, bool inconclusive = false);
|
2019-07-18 10:56:44 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
CPPCHECKLIB std::vector<Value> getLifetimeObjValues(const Token* tok,
|
|
|
|
bool inconclusive = false,
|
|
|
|
MathLib::bigint path = 0);
|
2019-05-31 11:16:04 +02:00
|
|
|
|
2023-01-28 10:20:47 +01:00
|
|
|
const Token* getEndOfExprScope(const Token* tok, const Scope* defaultScope = nullptr, bool smallest = true);
|
2021-01-05 16:56:38 +01:00
|
|
|
|
2023-03-02 22:05:41 +01:00
|
|
|
void combineValueProperties(const Value& value1, const Value& value2, Value& result);
|
2023-01-28 10:20:47 +01:00
|
|
|
}
|
2022-07-28 22:11:23 +02:00
|
|
|
|
2014-01-04 20:57:02 +01:00
|
|
|
#endif // valueflowH
|