21 #include "ui_compliancereportdialog.h"
39 #include <QCoreApplication>
40 #include <QCryptographicHash>
41 #include <QDialogButtonBox>
44 #include <QFileDialog>
49 #include <QMessageBox>
51 #include <QRegularExpression>
53 #include <QStringList>
54 #include <QTemporaryFile>
55 #include <QTextStream>
57 static void addHeaders(
const QString& file1, QSet<QString> &allFiles) {
58 if (allFiles.contains(file1))
61 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
64 const QRegularExpression re(
"^#include[ ]*\"([^\">]+)\".*");
65 QTextStream in(&file);
66 QString line = in.readLine();
68 if (line.startsWith(
"#include")) {
69 const QRegularExpressionMatch
match = re.match(line);
70 if (
match.hasMatch()) {
71 QString hfile =
match.captured(1);
72 if (file1.contains(
"/"))
73 hfile = file1.mid(0,file1.lastIndexOf(
"/") + 1) + hfile;
82 std::vector<std::string> ret;
83 std::transform(from.cbegin(), from.cend(), std::back_inserter(ret), [](
const QString& e) {
84 return e.toStdString();
92 mProjectFile(projectFile),
93 mResultsFile(std::move(resultsFile))
98 mUI->mCodingStandard->clear();
100 mUI->mCodingStandard->addItem(
"Misra C 2012");
102 std[0] = std[0].toUpper();
103 std = std.replace(
"-",
" ").replace(
" c ",
" C ").replace(
" cpp ",
" C++ ").replace(
" c++ ",
" C++ ");
104 mUI->mCodingStandard->addItem(std);
115 switch (
mUI->buttonBox->standardButton(button)) {
116 case QDialogButtonBox::StandardButton::Save:
119 case QDialogButtonBox::StandardButton::Close:
129 const QString std(
mUI->mCodingStandard->currentText().toLower().replace(
" ",
"-"));
131 const QString outFile = QFileDialog::getSaveFileName(
this,
132 tr(
"Compliance report"),
133 QDir::homePath() +
"/" + std +
"-compliance-report.html",
134 tr(
"HTML files (*.html)"));
135 if (outFile.isEmpty())
140 const QString& projectName =
mUI->mEditProjectName->text();
141 const QString& projectVersion =
mUI->mEditProjectVersion->text();
142 const bool files =
mUI->mCheckFiles->isChecked();
149 QTemporaryFile tempFiles;
150 if (files && tempFiles.open()) {
151 QTextStream out(&tempFiles);
165 p.
import(prjfile.toStdString());
167 QMessageBox msg(QMessageBox::Critical,
168 tr(
"Save compliance report"),
169 tr(
"Failed to import '%1' (%2), can not show files in compliance report").arg(prjfile).arg(QString::fromStdString(e.
errorMessage)),
178 QDir dir(inf.absoluteDir());
180 fileList.
addFile(dir.relativeFilePath(QString::fromStdString(fs.
filename())));
183 QSet<QString> allFiles;
184 for (
const QString &sourcefile: fileList.
getFileList())
186 for (
const QString& fileName: allFiles) {
188 if (f.open(QFile::ReadOnly)) {
189 QCryptographicHash hash(QCryptographicHash::Algorithm::Md5);
190 if (hash.addData(&f)) {
191 for (
auto b: hash.result())
192 out << QString::number((
unsigned char)b,16);
193 out <<
" " << fileName <<
"\n";
200 QStringList suppressions;
202 if (!suppression.errorId.empty())
203 suppressions.append(QString::fromStdString(suppression.errorId));
206 QStringList args{
"--project-name=" + projectName,
207 "--project-version=" + projectVersion,
208 "--output-file=" + outFile};
209 if (!suppressions.isEmpty())
210 args <<
"--suppressions=" + suppressions.join(
",");
212 args << (
"--" + std);
215 args <<
"--files=" + tempFiles.fileName();
218 const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
222 process.start(appPath +
"/compliance-report.exe", args);
224 process.start(appPath +
"/compliance-report", args);
226 process.waitForFinished();
227 const QString output = process.readAll();
228 if (!output.isEmpty()) {
229 QMessageBox msg(QMessageBox::Critical,
230 tr(
"Save compliance report"),
static bool match(const Token *tok, const std::string &rhs)
ProjectFile * mProjectFile
~ComplianceReportDialog() final
Ui::ComplianceReportDialog * mUI
ComplianceReportDialog(ProjectFile *projectFile, QString resultsFile)
void buttonClicked(QAbstractButton *button)
A class for listing files and directories to check.
void addFile(const QString &filepath)
Add filename to the list.
QStringList getFileList() const
Return list of filenames (to check).
void addPathList(const QStringList &paths)
Add list of filenames and directories to the list.
Importing project settings.
std::list< FileSettings > fileSettings
void ignorePaths(const std::vector< std::string > &ipaths)
Type import(const std::string &filename, Settings *settings=nullptr)
A class that reads and writes project files.
const QStringList & getCodingStandards() const
Get list of coding standards (checked by Cppcheck Premium).
const QStringList & getAddons() const
Get list addons.
const QString & getFilename() const
Get filename for the project file.
const QList< SuppressionList::Suppression > & getSuppressions() const
Get "raw" suppressions.
const QString & getProjectName() const
QStringList getCheckPaths() const
Get list of paths to check.
void setProjectName(QString projectName)
QStringList getExcludedPaths() const
Get list of paths to exclude from the check.
bool write(const QString &filename=QString())
Write project file (to disk).
const QString & getImportProject() const
static std::vector< std::string > toStdStringList(const QStringList &from)
static void addHeaders(const QString &file1, QSet< QString > &allFiles)
const std::string & filename() const
Simple container to be thrown when internal error is detected.