Cppcheck
ctu.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 //---------------------------------------------------------------------------
21 #ifndef ctuH
22 #define ctuH
23 //---------------------------------------------------------------------------
24 
25 #include "config.h"
26 #include "check.h"
27 #include "errorlogger.h"
28 #include "mathlib.h"
29 #include "vfvalue.h"
30 
31 #include <list>
32 #include <map>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 class Function;
38 class Settings;
39 class Token;
40 class Tokenizer;
41 
42 namespace tinyxml2 {
43  class XMLElement;
44 }
45 
46 /// @addtogroup Core
47 /// @{
48 
49 
50 /** @brief Whole program analysis (ctu=Cross Translation Unit) */
51 namespace CTU {
53  public:
54  enum class InvalidValueType { null, uninit, bufferOverflow };
55 
56  std::string toString() const override;
57 
58  struct Location {
59  Location() = default;
60  Location(const Tokenizer &tokenizer, const Token *tok);
61  Location(std::string fileName, nonneg int lineNumber, nonneg int column) : fileName(std::move(fileName)), lineNumber(lineNumber), column(column) {}
62  std::string fileName;
63  nonneg int lineNumber{};
64  nonneg int column{};
65  };
66 
67  struct UnsafeUsage {
68  UnsafeUsage() = default;
69  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) {}
70  std::string myId;
71  nonneg int myArgNr{};
72  std::string myArgumentName;
74  MathLib::bigint value{};
75  std::string toString() const;
76  };
77 
78  class CallBase {
79  public:
80  CallBase() = default;
81  CallBase(std::string callId, int callArgNr, std::string callFunctionName, Location loc)
82  : callId(std::move(callId)), callArgNr(callArgNr), callFunctionName(std::move(callFunctionName)), location(std::move(loc))
83  {}
84  CallBase(const Tokenizer &tokenizer, const Token *callToken);
85  virtual ~CallBase() = default;
86  CallBase(const CallBase&) = default;
87  std::string callId;
88  int callArgNr{};
89  std::string callFunctionName;
91  protected:
92  std::string toBaseXmlString() const;
93  bool loadBaseFromXml(const tinyxml2::XMLElement *xmlElement);
94  };
95 
96  class FunctionCall : public CallBase {
97  public:
101  std::vector<ErrorMessage::FileLocation> callValuePath;
102  bool warning;
103 
104  std::string toXmlString() const;
105  bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
106  };
107 
108  class NestedCall : public CallBase {
109  public:
110  NestedCall() = default;
111 
112  NestedCall(std::string myId, nonneg int myArgNr, const std::string &callId, nonneg int callArgnr, const std::string &callFunctionName, const Location &location)
113  : CallBase(callId, callArgnr, callFunctionName, location),
114  myId(std::move(myId)),
115  myArgNr(myArgNr) {}
116 
117  NestedCall(const Tokenizer &tokenizer, const Function *myFunction, const Token *callToken);
118 
119  std::string toXmlString() const;
120  bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
121 
122  std::string myId;
123  nonneg int myArgNr{};
124  };
125 
126  std::list<FunctionCall> functionCalls;
127  std::list<NestedCall> nestedCalls;
128 
129  void loadFromXml(const tinyxml2::XMLElement *xmlElement);
130  std::map<std::string, std::list<const CallBase *>> getCallsMap() const;
131 
132  static std::list<ErrorMessage::FileLocation> getErrorPath(InvalidValueType invalidValue,
133  const UnsafeUsage &unsafeUsage,
134  const std::map<std::string, std::list<const CallBase *>> &callsMap,
135  const char info[],
136  const FunctionCall ** const functionCallPtr,
137  bool warning);
138  };
139 
140  extern int maxCtuDepth;
141 
142  CPPCHECKLIB std::string toString(const std::list<FileInfo::UnsafeUsage> &unsafeUsage);
143 
144  CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function);
145 
146  /** @brief Parse current TU and extract file info */
147  CPPCHECKLIB FileInfo *getFileInfo(const Tokenizer &tokenizer);
148 
149  CPPCHECKLIB std::list<FileInfo::UnsafeUsage> getUnsafeUsage(const Tokenizer &tokenizer, const Settings &settings, bool (*isUnsafeUsage)(const Settings &settings, const Token *argtok, MathLib::bigint *value));
150 
151  CPPCHECKLIB std::list<FileInfo::UnsafeUsage> loadUnsafeUsageListFromXml(const tinyxml2::XMLElement *xmlElement);
152 }
153 
154 /// @}
155 //---------------------------------------------------------------------------
156 #endif // ctuH
static bool isUnsafeUsage(const Settings &settings, const Token *vartok, MathLib::bigint *value)
Location location
Definition: ctu.h:90
CallBase(std::string callId, int callArgNr, std::string callFunctionName, Location loc)
Definition: ctu.h:81
CallBase(const CallBase &)=default
std::string callFunctionName
Definition: ctu.h:89
virtual ~CallBase()=default
std::string callId
Definition: ctu.h:87
std::string callArgumentExpression
Definition: ctu.h:98
ValueFlow::Value::ValueType callValueType
Definition: ctu.h:100
MathLib::bigint callArgValue
Definition: ctu.h:99
std::vector< ErrorMessage::FileLocation > callValuePath
Definition: ctu.h:101
std::string myId
Definition: ctu.h:122
NestedCall(std::string myId, nonneg int myArgNr, const std::string &callId, nonneg int callArgnr, const std::string &callFunctionName, const Location &location)
Definition: ctu.h:112
std::list< NestedCall > nestedCalls
Definition: ctu.h:127
std::list< FunctionCall > functionCalls
Definition: ctu.h:126
InvalidValueType
Definition: ctu.h:54
Base class used for whole-program analysis.
Definition: check.h:103
long long bigint
Definition: mathlib.h:68
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
#define CPPCHECKLIB
Definition: config.h:35
#define nonneg
Definition: config.h:138
Whole program analysis (ctu=Cross Translation Unit)
Definition: check.h:35
CPPCHECKLIB std::list< FileInfo::UnsafeUsage > getUnsafeUsage(const Tokenizer &tokenizer, const Settings &settings, bool(*isUnsafeUsage)(const Settings &settings, const Token *argtok, MathLib::bigint *value))
Definition: ctu.cpp:472
CPPCHECKLIB std::string toString(const std::list< FileInfo::UnsafeUsage > &unsafeUsage)
Definition: ctu.cpp:151
CPPCHECKLIB std::list< FileInfo::UnsafeUsage > loadUnsafeUsageListFromXml(const tinyxml2::XMLElement *xmlElement)
Definition: ctu.cpp:257
int maxCtuDepth
Definition: ctu.cpp:58
CPPCHECKLIB std::string getFunctionId(const Tokenizer &tokenizer, const Function *function)
Definition: ctu.cpp:60
CPPCHECKLIB FileInfo * getFileInfo(const Tokenizer &tokenizer)
Parse current TU and extract file info.
Definition: ctu.cpp:309
Definition: check.h:31
Location(std::string fileName, nonneg int lineNumber, nonneg int column)
Definition: ctu.h:61
std::string fileName
Definition: ctu.h:62
std::string myArgumentName
Definition: ctu.h:72
UnsafeUsage(std::string myId, nonneg int myArgNr, std::string myArgumentName, Location location, MathLib::bigint value)
Definition: ctu.h:69
std::string myId
Definition: ctu.h:70