Cppcheck
csvreport.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 "csvreport.h"
20 
21 #include "erroritem.h"
22 #include "report.h"
23 
24 #include <QDir>
25 #include <QFile>
26 #include <QList>
27 #include <QtGlobal>
28 
29 CsvReport::CsvReport(const QString &filename) :
30  Report(filename)
31 {}
32 
34 {
35  if (Report::create()) {
36  mTxtWriter.setDevice(Report::getFile());
37  return true;
38  }
39  return false;
40 }
41 
43 {
44  // Added 5 columns to the header.
45 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
46  mTxtWriter << "File, Line, Severity, Id, Summary" << Qt::endl;
47 #else
48  mTxtWriter << "File, Line, Severity, Id, Summary" << endl;
49 #endif
50 }
51 
53 {
54  // No footer for CSV report
55 }
56 
57 void CsvReport::writeError(const ErrorItem &error)
58 {
59  /*
60  Error as CSV line
61  gui/test.cpp,23,error,Mismatching allocation and deallocation: k
62  */
63 
64  const QString file = QDir::toNativeSeparators(error.errorPath.back().file);
65  QString line = QString("%1,%2,").arg(file).arg(error.errorPath.back().line);
66  line += QString("%1,%2,%3").arg(GuiSeverity::toString(error.severity)).arg(error.errorId).arg(error.summary);
67 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
68  mTxtWriter << line << Qt::endl;
69 #else
70  mTxtWriter << line << endl;
71 #endif
72 }
bool create() override
Create the report (file).
Definition: csvreport.cpp:33
void writeHeader() override
Write report header.
Definition: csvreport.cpp:42
void writeFooter() override
Write report footer.
Definition: csvreport.cpp:52
QTextStream mTxtWriter
Text stream writer for writing the report in text format.
Definition: csvreport.h:70
CsvReport(const QString &filename)
Definition: csvreport.cpp:29
void writeError(const ErrorItem &error) override
Write error to report.
Definition: csvreport.cpp:57
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
@ error
Programming error.