|
| 1 | +#include "compliancereportdialog.h" |
| 2 | +#include "ui_compliancereportdialog.h" |
| 3 | + |
| 4 | +#include "filelist.h" |
| 5 | +#include "projectfile.h" |
| 6 | + |
| 7 | +#include <QCryptographicHash> |
| 8 | +#include <QFile> |
| 9 | +#include <QFileDialog> |
| 10 | +#include <QFileInfo> |
| 11 | +#include <QMessageBox> |
| 12 | +#include <QProcess> |
| 13 | +#include <QRegularExpression> |
| 14 | +#include <QSet> |
| 15 | +#include <QTemporaryFile> |
| 16 | +#include <QTextStream> |
| 17 | + |
| 18 | +static void addHeaders(const QString& file1, QSet<QString> &allFiles) { |
| 19 | + if (allFiles.contains(file1)) |
| 20 | + return; |
| 21 | + QFile file(file1); |
| 22 | + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 23 | + return; |
| 24 | + allFiles << file1; |
| 25 | + const QRegularExpression re("^#include[ ]*\"([^\">]+)\".*"); |
| 26 | + QTextStream in(&file); |
| 27 | + QString line = in.readLine(); |
| 28 | + while (!in.atEnd()) { |
| 29 | + if (line.startsWith("#include")) { |
| 30 | + const QRegularExpressionMatch match = re.match(line); |
| 31 | + if (match.hasMatch()) { |
| 32 | + QString hfile = match.captured(1); |
| 33 | + if (file1.contains("/")) |
| 34 | + hfile = file1.mid(0,file1.lastIndexOf("/") + 1) + hfile; |
| 35 | + addHeaders(hfile, allFiles); |
| 36 | + } |
| 37 | + } |
| 38 | + line = in.readLine(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +static std::vector<std::string> toStdStringList(const QStringList& from) { |
| 43 | + std::vector<std::string> ret; |
| 44 | + std::transform(from.cbegin(), from.cend(), std::back_inserter(ret), [](const QString& e) { |
| 45 | + return e.toStdString(); |
| 46 | + }); |
| 47 | + return ret; |
| 48 | +} |
| 49 | + |
| 50 | +ComplianceReportDialog::ComplianceReportDialog(ProjectFile* projectFile, QString resultsFile) : |
| 51 | + QDialog(nullptr), |
| 52 | + mUI(new Ui::ComplianceReportDialog), |
| 53 | + mProjectFile(projectFile), |
| 54 | + mResultsFile(std::move(resultsFile)) |
| 55 | +{ |
| 56 | + mUI->setupUi(this); |
| 57 | + mUI->mEditProjectName->setText(projectFile->getProjectName()); |
| 58 | + connect(mUI->buttonBox, &QDialogButtonBox::clicked, this, &ComplianceReportDialog::buttonClicked); |
| 59 | +} |
| 60 | + |
| 61 | +ComplianceReportDialog::~ComplianceReportDialog() |
| 62 | +{ |
| 63 | + delete mUI; |
| 64 | +} |
| 65 | + |
| 66 | +void ComplianceReportDialog::buttonClicked(QAbstractButton* button) |
| 67 | +{ |
| 68 | + switch (mUI->buttonBox->standardButton(button)) { |
| 69 | + case QDialogButtonBox::StandardButton::Save: |
| 70 | + save(); |
| 71 | + break; |
| 72 | + case QDialogButtonBox::StandardButton::Close: |
| 73 | + close(); |
| 74 | + break; |
| 75 | + default: |
| 76 | + break; |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +void ComplianceReportDialog::save() |
| 81 | +{ |
| 82 | + const QString outFile = QFileDialog::getSaveFileName(this, |
| 83 | + tr("Compliance report"), |
| 84 | + QDir::homePath() + "/misra-c-2012-compliance-report.html", |
| 85 | + tr("HTML files (*.html)")); |
| 86 | + if (outFile.isEmpty()) |
| 87 | + return; |
| 88 | + |
| 89 | + close(); |
| 90 | + |
| 91 | + const QString& projectName = mUI->mEditProjectName->text(); |
| 92 | + const QString& projectVersion = mUI->mEditProjectVersion->text(); |
| 93 | + const bool files = mUI->mCheckFiles->isChecked(); |
| 94 | + |
| 95 | + if (projectName != mProjectFile->getProjectName()) { |
| 96 | + mProjectFile->setProjectName(projectName); |
| 97 | + mProjectFile->write(); |
| 98 | + } |
| 99 | + |
| 100 | + QTemporaryFile tempFiles; |
| 101 | + if (files && tempFiles.open()) { |
| 102 | + QTextStream out(&tempFiles); |
| 103 | + FileList fileList; |
| 104 | + fileList.addPathList(mProjectFile->getCheckPaths()); |
| 105 | + if (!mProjectFile->getImportProject().isEmpty()) { |
| 106 | + QFileInfo inf(mProjectFile->getFilename()); |
| 107 | + |
| 108 | + QString prjfile; |
| 109 | + if (QFileInfo(mProjectFile->getImportProject()).isAbsolute()) |
| 110 | + prjfile = mProjectFile->getImportProject(); |
| 111 | + else |
| 112 | + prjfile = inf.canonicalPath() + '/' + mProjectFile->getImportProject(); |
| 113 | + |
| 114 | + ImportProject p; |
| 115 | + try { |
| 116 | + p.import(prjfile.toStdString()); |
| 117 | + } catch (InternalError &e) { |
| 118 | + QMessageBox msg(QMessageBox::Critical, |
| 119 | + tr("Save compliance report"), |
| 120 | + tr("Failed to import '%1', can not show files in compliance report").arg(prjfile), |
| 121 | + QMessageBox::Ok, |
| 122 | + this); |
| 123 | + msg.exec(); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + p.ignorePaths(toStdStringList(mProjectFile->getExcludedPaths())); |
| 128 | + |
| 129 | + QDir dir(inf.absoluteDir()); |
| 130 | + for (const ImportProject::FileSettings& fs: p.fileSettings) |
| 131 | + fileList.addFile(dir.relativeFilePath(QString::fromStdString(fs.filename))); |
| 132 | + } |
| 133 | + |
| 134 | + QSet<QString> allFiles; |
| 135 | + for (const QString &sourcefile: fileList.getFileList()) |
| 136 | + addHeaders(sourcefile, allFiles); |
| 137 | + for (const QString& fileName: allFiles) { |
| 138 | + QFile f(fileName); |
| 139 | + if (f.open(QFile::ReadOnly)) { |
| 140 | + QCryptographicHash hash(QCryptographicHash::Algorithm::Md5); |
| 141 | + if (hash.addData(&f)) { |
| 142 | + for (auto b: hash.result()) |
| 143 | + out << QString::number((unsigned char)b,16); |
| 144 | + out << " " << fileName << "\n"; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + tempFiles.close(); |
| 149 | + } |
| 150 | + |
| 151 | + QStringList args{"--compliant=misra-c2012-1.1", |
| 152 | + "--compliant=misra-c2012-1.2", |
| 153 | + "--project-name=" + projectName, |
| 154 | + "--project-version=" + projectVersion, |
| 155 | + "--output-file=" + outFile}; |
| 156 | + if (files) |
| 157 | + args << "--files=" + tempFiles.fileName(); |
| 158 | + args << mResultsFile; |
| 159 | + |
| 160 | + const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath(); |
| 161 | + |
| 162 | + QProcess process; |
| 163 | +#ifdef Q_OS_WIN |
| 164 | + process.start(appPath + "/compliance-report.exe", args); |
| 165 | +#else |
| 166 | + process.start(appPath + "/compliance-report", args); |
| 167 | +#endif |
| 168 | + process.waitForFinished(); |
| 169 | +} |
0 commit comments