Cppcheck
errortypes.cpp
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 #include "errortypes.h"
20 
21 #include "utils.h"
22 
23 static std::string typeToString(InternalError::Type type)
24 {
25  switch (type) {
26  case InternalError::Type::AST:
27  return "internalAstError";
28  case InternalError::Type::SYNTAX:
29  return "syntaxError";
30  case InternalError::Type::UNKNOWN_MACRO:
31  return "unknownMacro";
32  case InternalError::Type::INTERNAL:
33  return "internalError";
34  case InternalError::Type::LIMIT:
35  return "cppcheckLimit";
36  case InternalError::Type::INSTANTIATION:
37  return "instantiationError";
38  }
40 }
41 
42 InternalError::InternalError(const Token *tok, std::string errorMsg, Type type) :
43  InternalError(tok, std::move(errorMsg), "", type)
44 {}
45 
46 InternalError::InternalError(const Token *tok, std::string errorMsg, std::string details, Type type) :
47  token(tok), errorMessage(std::move(errorMsg)), details(std::move(details)), type(type), id(typeToString(type))
48 {}
49 
50 std::string severityToString(Severity severity)
51 {
52  switch (severity) {
53  case Severity::none:
54  return "";
55  case Severity::error:
56  return "error";
57  case Severity::warning:
58  return "warning";
59  case Severity::style:
60  return "style";
62  return "performance";
64  return "portability";
66  return "information";
67  case Severity::debug:
68  return "debug";
69  case Severity::internal:
70  return "internal";
71  }
72  throw InternalError(nullptr, "Unknown severity");
73 }
74 
75 // TODO: bail out on invalid severity
76 Severity severityFromString(const std::string& severity)
77 {
78  if (severity.empty())
79  return Severity::none;
80  if (severity == "none")
81  return Severity::none;
82  if (severity == "error")
83  return Severity::error;
84  if (severity == "warning")
85  return Severity::warning;
86  if (severity == "style")
87  return Severity::style;
88  if (severity == "performance")
89  return Severity::performance;
90  if (severity == "portability")
91  return Severity::portability;
92  if (severity == "information")
93  return Severity::information;
94  if (severity == "debug")
95  return Severity::debug;
96  if (severity == "internal")
97  return Severity::internal;
98  return Severity::none;
99 }
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
Information about a class type.
static std::string typeToString(InternalError::Type type)
Definition: errortypes.cpp:23
Severity
enum class for severity.
Definition: errortypes.h:63
Severity severityFromString(const std::string &severity)
Definition: errortypes.cpp:76
std::string severityToString(Severity severity)
Definition: errortypes.cpp:50
@ 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.
@ internal
Internal message.
NORETURN void unreachable()
Definition: utils.h:369
Simple container to be thrown when internal error is detected.
Definition: errortypes.h:36
InternalError(const Token *tok, std::string errorMsg, Type type=INTERNAL)
Definition: errortypes.cpp:42