2018-12-25 21:11:23 +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.
|
2018-12-25 21:11:23 +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 ctuH
|
|
|
|
#define ctuH
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "config.h"
|
2018-12-25 21:11:23 +01:00
|
|
|
#include "check.h"
|
2020-05-23 07:16:49 +02:00
|
|
|
#include "errorlogger.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "mathlib.h"
|
2023-01-26 22:23:22 +01:00
|
|
|
#include "vfvalue.h"
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
2020-05-23 07:16:49 +02:00
|
|
|
#include <map>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <string>
|
2022-09-16 18:59:15 +02:00
|
|
|
#include <utility>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <vector>
|
2020-05-23 07:16:49 +02:00
|
|
|
|
|
|
|
class Function;
|
2022-01-27 19:03:20 +01:00
|
|
|
class Settings;
|
|
|
|
class Token;
|
|
|
|
class Tokenizer;
|
|
|
|
|
|
|
|
namespace tinyxml2 {
|
|
|
|
class XMLElement;
|
|
|
|
}
|
2020-05-23 07:16:49 +02:00
|
|
|
|
2018-12-25 21:11:23 +01:00
|
|
|
/// @addtogroup Core
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief Whole program analysis (ctu=Cross Translation Unit) */
|
|
|
|
namespace CTU {
|
2018-12-28 23:09:44 +01:00
|
|
|
class CPPCHECKLIB FileInfo : public Check::FileInfo {
|
2018-12-25 21:11:23 +01:00
|
|
|
public:
|
2019-07-31 22:56:16 +02:00
|
|
|
enum class InvalidValueType { null, uninit, bufferOverflow };
|
2018-12-30 16:23:25 +01:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
std::string toString() const override;
|
2018-12-25 21:11:23 +01:00
|
|
|
|
|
|
|
struct Location {
|
|
|
|
Location() = default;
|
|
|
|
Location(const Tokenizer *tokenizer, const Token *tok);
|
2022-09-16 18:59:15 +02:00
|
|
|
Location(std::string fileName, nonneg int lineNumber, nonneg int column) : fileName(std::move(fileName)), lineNumber(lineNumber), column(column) {}
|
2018-12-25 21:11:23 +01:00
|
|
|
std::string fileName;
|
2022-05-09 20:28:21 +02:00
|
|
|
nonneg int lineNumber{};
|
|
|
|
nonneg int column{};
|
2018-12-25 21:11:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UnsafeUsage {
|
|
|
|
UnsafeUsage() = default;
|
2022-09-16 18:59:15 +02:00
|
|
|
UnsafeUsage(std::string myId, nonneg int myArgNr, std::string myArgumentName, Location location, MathLib::bigint value) : myId(std::move(myId)), myArgNr(myArgNr), myArgumentName(std::move(myArgumentName)), location(std::move(location)), value(value) {}
|
2018-12-30 11:55:39 +01:00
|
|
|
std::string myId;
|
2022-05-09 20:28:21 +02:00
|
|
|
nonneg int myArgNr{};
|
2018-12-30 11:55:39 +01:00
|
|
|
std::string myArgumentName;
|
2018-12-25 21:11:23 +01:00
|
|
|
Location location;
|
2022-05-09 20:28:21 +02:00
|
|
|
MathLib::bigint value{};
|
2018-12-25 21:11:23 +01:00
|
|
|
std::string toString() const;
|
|
|
|
};
|
|
|
|
|
2018-12-30 11:55:39 +01:00
|
|
|
class CallBase {
|
|
|
|
public:
|
|
|
|
CallBase() = default;
|
2022-09-16 18:59:15 +02:00
|
|
|
CallBase(std::string callId, int callArgNr, std::string callFunctionName, Location loc)
|
|
|
|
: callId(std::move(callId)), callArgNr(callArgNr), callFunctionName(std::move(callFunctionName)), location(std::move(loc))
|
2018-12-30 11:55:39 +01:00
|
|
|
{}
|
|
|
|
CallBase(const Tokenizer *tokenizer, const Token *callToken);
|
2023-08-16 17:13:36 +02:00
|
|
|
virtual ~CallBase() = default;
|
2023-03-02 21:02:29 +01:00
|
|
|
CallBase(const CallBase&) = default;
|
2018-12-30 11:55:39 +01:00
|
|
|
std::string callId;
|
2022-05-09 20:28:21 +02:00
|
|
|
int callArgNr{};
|
2018-12-30 11:55:39 +01:00
|
|
|
std::string callFunctionName;
|
2018-12-25 21:11:23 +01:00
|
|
|
Location location;
|
2018-12-30 11:55:39 +01:00
|
|
|
protected:
|
|
|
|
std::string toBaseXmlString() const;
|
|
|
|
bool loadBaseFromXml(const tinyxml2::XMLElement *xmlElement);
|
2018-12-25 21:11:23 +01:00
|
|
|
};
|
|
|
|
|
2018-12-30 11:55:39 +01:00
|
|
|
class FunctionCall : public CallBase {
|
|
|
|
public:
|
|
|
|
std::string callArgumentExpression;
|
2018-12-30 18:31:37 +01:00
|
|
|
MathLib::bigint callArgValue;
|
2018-12-30 11:55:39 +01:00
|
|
|
ValueFlow::Value::ValueType callValueType;
|
2020-05-23 07:16:49 +02:00
|
|
|
std::vector<ErrorMessage::FileLocation> callValuePath;
|
2018-12-30 18:31:37 +01:00
|
|
|
bool warning;
|
2018-12-30 11:55:39 +01:00
|
|
|
|
|
|
|
std::string toXmlString() const;
|
|
|
|
bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
|
|
|
|
};
|
|
|
|
|
|
|
|
class NestedCall : public CallBase {
|
|
|
|
public:
|
2018-12-25 21:11:23 +01:00
|
|
|
NestedCall() = default;
|
|
|
|
|
2022-09-16 18:59:15 +02:00
|
|
|
NestedCall(std::string myId, nonneg int myArgNr, const std::string &callId, nonneg int callArgnr, const std::string &callFunctionName, const Location &location)
|
2018-12-30 11:55:39 +01:00
|
|
|
: CallBase(callId, callArgnr, callFunctionName, location),
|
2022-09-16 18:59:15 +02:00
|
|
|
myId(std::move(myId)),
|
2021-08-07 20:51:18 +02:00
|
|
|
myArgNr(myArgNr) {}
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2018-12-30 11:55:39 +01:00
|
|
|
NestedCall(const Tokenizer *tokenizer, const Function *myFunction, const Token *callToken);
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2018-12-30 11:55:39 +01:00
|
|
|
std::string toXmlString() const;
|
|
|
|
bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
|
|
|
|
|
|
|
|
std::string myId;
|
2022-05-09 20:28:21 +02:00
|
|
|
nonneg int myArgNr{};
|
2018-12-25 21:11:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
std::list<FunctionCall> functionCalls;
|
|
|
|
std::list<NestedCall> nestedCalls;
|
|
|
|
|
|
|
|
void loadFromXml(const tinyxml2::XMLElement *xmlElement);
|
2018-12-30 16:23:25 +01:00
|
|
|
std::map<std::string, std::list<const CallBase *>> getCallsMap() const;
|
2018-12-26 15:56:10 +01:00
|
|
|
|
2021-01-09 20:32:38 +01:00
|
|
|
static std::list<ErrorMessage::FileLocation> getErrorPath(InvalidValueType invalidValue,
|
2021-08-07 20:51:18 +02:00
|
|
|
const UnsafeUsage &unsafeUsage,
|
|
|
|
const std::map<std::string, std::list<const CallBase *>> &callsMap,
|
|
|
|
const char info[],
|
|
|
|
const FunctionCall ** const functionCallPtr,
|
|
|
|
bool warning);
|
2018-12-25 21:11:23 +01:00
|
|
|
};
|
|
|
|
|
2019-02-03 17:51:02 +01:00
|
|
|
extern int maxCtuDepth;
|
|
|
|
|
2018-12-28 23:09:44 +01:00
|
|
|
CPPCHECKLIB std::string toString(const std::list<FileInfo::UnsafeUsage> &unsafeUsage);
|
2018-12-26 15:56:10 +01:00
|
|
|
|
2018-12-28 23:09:44 +01:00
|
|
|
CPPCHECKLIB std::string getFunctionId(const Tokenizer *tokenizer, const Function *function);
|
2018-12-25 21:11:23 +01:00
|
|
|
|
|
|
|
/** @brief Parse current TU and extract file info */
|
2018-12-28 23:09:44 +01:00
|
|
|
CPPCHECKLIB FileInfo *getFileInfo(const Tokenizer *tokenizer);
|
2018-12-25 21:11:23 +01:00
|
|
|
|
2019-03-23 08:36:10 +01:00
|
|
|
CPPCHECKLIB std::list<FileInfo::UnsafeUsage> getUnsafeUsage(const Tokenizer *tokenizer, const Settings *settings, const Check *check, bool (*isUnsafeUsage)(const Check *check, const Token *argtok, MathLib::bigint *value));
|
2018-12-26 19:17:49 +01:00
|
|
|
|
2018-12-28 23:09:44 +01:00
|
|
|
CPPCHECKLIB std::list<FileInfo::UnsafeUsage> loadUnsafeUsageListFromXml(const tinyxml2::XMLElement *xmlElement);
|
2018-12-25 21:11:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#endif // ctuH
|