Cppcheck
txtreport.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 "txtreport.h"
20 
21 #include "erroritem.h"
22 
23 #include <QDir>
24 #include <QFile>
25 #include <QList>
26 #include <QtGlobal>
27 
28 TxtReport::TxtReport(const QString &filename) :
29  Report(filename)
30 {}
31 
33 {
34  if (Report::create()) {
35  mTxtWriter.setDevice(Report::getFile());
36  return true;
37  }
38  return false;
39 }
40 
42 {
43  // No header for txt report
44 }
45 
47 {
48  // No footer for txt report
49 }
50 
51 void TxtReport::writeError(const ErrorItem &error)
52 {
53  /*
54  Error example from the core program in text
55  [gui/test.cpp:23] -> [gui/test.cpp:14]: (error) Mismatching allocation and deallocation: k
56  */
57 
58  QString line;
59 
60  for (int i = 0; i < error.errorPath.size(); i++) {
61  const QString file = QDir::toNativeSeparators(error.errorPath[i].file);
62  line += QString("[%1:%2]").arg(file).arg(error.errorPath[i].line);
63  if (i < error.errorPath.size() - 1) {
64  line += " -> ";
65  }
66 
67  if (i == error.errorPath.size() - 1) {
68  line += ": ";
69  }
70  }
71  QString temp = "(%1";
72  if (error.inconclusive) {
73  temp += ", ";
74  temp += tr("inconclusive");
75  }
76  temp += ") ";
77  line += temp.arg(GuiSeverity::toString(error.severity));
78  line += error.summary;
79 
80 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
81  mTxtWriter << line << Qt::endl;
82 #else
83  mTxtWriter << line << endl;
84 #endif
85 }
A class containing error data for one error.
Definition: erroritem.h:72
static QString toString(Severity severity)
Definition: erroritem.h:40
A base class for reports.
Definition: report.h:34
QFile * getFile()
Get the file object where the report is written to.
Definition: report.cpp:60
virtual bool create()
Create the report (file).
Definition: report.cpp:34
void writeHeader() override
Write report header.
Definition: txtreport.cpp:41
void writeFooter() override
Write report footer.
Definition: txtreport.cpp:46
void writeError(const ErrorItem &error) override
Write error to report.
Definition: txtreport.cpp:51
TxtReport(const QString &filename)
Definition: txtreport.cpp:28
bool create() override
Create the report (file).
Definition: txtreport.cpp:32
QTextStream mTxtWriter
Text stream writer for writing the report in text format.
Definition: txtreport.h:71
@ error
Programming error.