Cppcheck
erroritem.cpp
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 #include "erroritem.h"
20 
21 #include "common.h"
22 
23 #include <list>
24 
26  : file(QString::fromStdString(loc.getfile(false)))
27  , line(loc.line)
28  , column(loc.column)
29  , info(QString::fromStdString(loc.getinfo()))
30 {}
31 
32 bool operator==(const QErrorPathItem &i1, const QErrorPathItem &i2)
33 {
34  return i1.file == i2.file && i1.column == i2.column && i1.line == i2.line && i1.info == i2.info;
35 }
36 
38  : severity(Severity::none)
39  , inconclusive(false)
40  , cwe(-1)
41  , hash(0)
42 {}
43 
45  : file0(QString::fromStdString(errmsg.file0))
46  , errorId(QString::fromStdString(errmsg.id))
47  , severity(errmsg.severity)
48  , inconclusive(errmsg.certainty == Certainty::inconclusive)
49  , summary(QString::fromStdString(errmsg.shortMessage()))
50  , message(QString::fromStdString(errmsg.verboseMessage()))
51  , cwe(errmsg.cwe.id)
52  , hash(errmsg.hash)
53  , symbolNames(QString::fromStdString(errmsg.symbolNames()))
54 {
55  for (std::list<ErrorMessage::FileLocation>::const_iterator loc = errmsg.callStack.cbegin();
56  loc != errmsg.callStack.cend();
57  ++loc) {
58  errorPath << QErrorPathItem(*loc);
59  }
60 }
61 
62 QString ErrorItem::tool() const
63 {
64  if (errorId == CLANG_ANALYZER)
65  return CLANG_ANALYZER;
66  if (errorId.startsWith(CLANG_TIDY))
67  return CLANG_TIDY;
68  if (errorId.startsWith("clang-"))
69  return "clang";
70  return "cppcheck";
71 }
72 
73 QString ErrorItem::toString() const
74 {
75  QString str = errorPath.back().file + " - " + errorId + " - ";
76  if (inconclusive)
77  str += "inconclusive ";
78  str += GuiSeverity::toString(severity) +"\n";
79  str += summary + "\n";
80  str += message + "\n";
81  for (const QErrorPathItem& i : errorPath) {
82  str += " " + i.file + ": " + QString::number(i.line) + "\n";
83  }
84  return str;
85 }
86 
87 bool ErrorItem::sameCID(const ErrorItem &errorItem1, const ErrorItem &errorItem2)
88 {
89  if (errorItem1.hash || errorItem2.hash)
90  return errorItem1.hash == errorItem2.hash;
91 
92  // fallback
93  return errorItem1.errorId == errorItem2.errorId &&
94  errorItem1.errorPath == errorItem2.errorPath &&
95  errorItem1.file0 == errorItem2.file0 &&
96  errorItem1.message == errorItem2.message &&
97  errorItem1.inconclusive == errorItem2.inconclusive &&
98  errorItem1.severity == errorItem2.severity;
99 }
A class containing error data for one error.
Definition: erroritem.h:72
QString toString() const
Convert error item to string.
Definition: erroritem.cpp:73
bool inconclusive
Definition: erroritem.h:87
QString file0
Definition: erroritem.h:84
QString tool() const
Definition: erroritem.cpp:62
QString summary
Definition: erroritem.h:88
Severity severity
Definition: erroritem.h:86
static bool sameCID(const ErrorItem &errorItem1, const ErrorItem &errorItem2)
Compare "CID".
Definition: erroritem.cpp:87
unsigned long long hash
Definition: erroritem.h:91
QString message
Definition: erroritem.h:89
QList< QErrorPathItem > errorPath
Definition: erroritem.h:92
QString errorId
Definition: erroritem.h:85
File name and line number.
Definition: errorlogger.h:55
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
std::list< FileLocation > callStack
Definition: errorlogger.h:164
static QString toString(Severity severity)
Definition: erroritem.h:40
A class containing data for one error path item.
Definition: erroritem.h:52
QString file
Definition: erroritem.h:56
QString info
Definition: erroritem.h:59
Severity
enum class for severity.
Definition: errortypes.h:63
Certainty
Definition: errortypes.h:54
#define CLANG_TIDY
Definition: common.h:29
#define CLANG_ANALYZER
Definition: common.h:28
bool operator==(const QErrorPathItem &i1, const QErrorPathItem &i2)
Definition: erroritem.cpp:32