Cppcheck
errortypes.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 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 #ifndef errortypesH
21 #define errortypesH
22 //---------------------------------------------------------------------------
23 
24 #include "config.h"
25 
26 #include <stdexcept>
27 #include <list>
28 #include <string>
29 #include <utility>
30 
31 /// @addtogroup Core
32 /// @{
33 class Token;
34 
35 /** @brief Simple container to be thrown when internal error is detected. */
37  enum Type {AST, SYNTAX, UNKNOWN_MACRO, INTERNAL, LIMIT, INSTANTIATION};
38 
39  InternalError(const Token *tok, std::string errorMsg, Type type = INTERNAL);
40  InternalError(const Token *tok, std::string errorMsg, std::string details, Type type = INTERNAL);
41 
42  const Token *token;
43  std::string errorMessage;
44  std::string details;
46  std::string id;
47 };
48 
49 class TerminateException : public std::runtime_error {
50 public:
51  TerminateException() : std::runtime_error("terminate") {}
52 };
53 
54 enum class Certainty {
56 };
57 
58 enum class Checks {
60 };
61 
62 /** @brief enum class for severity. Used when reporting errors. */
63 enum class Severity {
64  /**
65  * No severity (default value).
66  */
67  none,
68  /**
69  * Programming error.
70  * This indicates severe error like memory leak etc.
71  * The error is certain.
72  */
73  error,
74  /**
75  * Warning.
76  * Used for dangerous coding style that can cause severe runtime errors.
77  * For example: forgetting to initialize a member variable in a constructor.
78  */
79  warning,
80  /**
81  * Style warning.
82  * Used for general code cleanup recommendations. Fixing these
83  * will not fix any bugs but will make the code easier to maintain.
84  * For example: redundant code, unreachable code, etc.
85  */
86  style,
87  /**
88  * Performance warning.
89  * Not an error as is but suboptimal code and fixing it probably leads
90  * to faster performance of the compiled code.
91  */
93  /**
94  * Portability warning.
95  * This warning indicates the code is not properly portable for
96  * different platforms and bitnesses (32/64 bit). If the code is meant
97  * to compile in different platforms and bitnesses these warnings
98  * should be fixed.
99  */
100  portability,
101  /**
102  * Checking information.
103  * Information message about the checking (process) itself. These
104  * messages inform about header files not found etc issues that are
105  * not errors in the code but something user needs to know.
106  */
107  information,
108  /**
109  * Debug message.
110  * Debug-mode message useful for the developers.
111  */
112  debug,
113  /**
114  * Internal message.
115  * Message will not be shown to the user.
116  * Tracking what checkers is executed, tracking suppressed critical errors, etc.
117  */
118  internal
119 };
120 
121 CPPCHECKLIB std::string severityToString(Severity severity);
122 CPPCHECKLIB Severity severityFromString(const std::string &severity);
123 
124 struct CWE {
125  explicit CWE(unsigned short cweId) : id(cweId) {}
126  unsigned short id;
127 };
128 
129 using ErrorPathItem = std::pair<const Token *, std::string>;
130 using ErrorPath = std::list<ErrorPathItem>;
131 
132 /// @}
133 //---------------------------------------------------------------------------
134 #endif // errortypesH
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
Information about a class type.
#define CPPCHECKLIB
Definition: config.h:35
Severity
enum class for severity.
Definition: errortypes.h:63
Certainty
Definition: errortypes.h:54
std::pair< const Token *, std::string > ErrorPathItem
Definition: errortypes.h:129
Checks
Definition: errortypes.h:58
CPPCHECKLIB Severity severityFromString(const std::string &severity)
Definition: errortypes.cpp:76
CPPCHECKLIB std::string severityToString(Severity severity)
Definition: errortypes.cpp:50
std::list< ErrorPathItem > ErrorPath
Definition: errortypes.h:130
@ none
No severity (default value).
@ warning
Warning.
@ portability
Portability warning.
@ style
Style warning.
@ debug
Debug message.
@ information
Checking information.
@ performance
Performance warning.
@ error
Programming error.
@ missingInclude
@ internalCheck
@ unusedFunction
CWE(unsigned short cweId)
Definition: errortypes.h:125
unsigned short id
Definition: errortypes.h:126
Simple container to be thrown when internal error is detected.
Definition: errortypes.h:36
std::string id
Definition: errortypes.h:46
std::string errorMessage
Definition: errortypes.h:43
std::string details
Definition: errortypes.h:44
const Token * token
Definition: errortypes.h:42