GUI: refactoring

This commit is contained in:
Daniel Marjamäki 2017-10-12 17:02:25 +02:00
parent 74fc6485d2
commit abf97c2c26
6 changed files with 94 additions and 74 deletions

View File

@ -22,6 +22,7 @@
#include <QDir> #include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QProcess> #include <QProcess>
#include <QSettings>
#include "checkthread.h" #include "checkthread.h"
#include "erroritem.h" #include "erroritem.h"
#include "threadresult.h" #include "threadresult.h"
@ -127,8 +128,9 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
args << ("-D" + D); args << ("-D" + D);
} }
if (!mClangPath.isEmpty()) { const QString clangPath = CheckThread::clangTidyCmd();
QDir dir(mClangPath + "/../lib/clang"); if (!clangPath.isEmpty()) {
QDir dir(clangPath + "/../lib/clang");
foreach (QString ver, dir.entryList()) { foreach (QString ver, dir.entryList()) {
QString includePath = dir.absolutePath() + '/' + ver + "/include"; QString includePath = dir.absolutePath() + '/' + ver + "/include";
if (ver[0] != '.' && QDir(includePath).exists()) { if (ver[0] != '.' && QDir(includePath).exists()) {
@ -174,12 +176,11 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
if (!buildDir.empty()) { if (!buildDir.empty()) {
analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(buildDir, fileSettings->filename, fileSettings->cfg)); analyzerInfoFile = QString::fromStdString(AnalyzerInformation::getAnalyzerInfoFile(buildDir, fileSettings->filename, fileSettings->cfg));
const QString cmd(mClangPath.isEmpty() ? QString("clang") : (mClangPath + "/clang.exe"));
QStringList args2(args); QStringList args2(args);
args2.insert(0,"-E"); args2.insert(0,"-E");
args2 << fileName; args2 << fileName;
QProcess process; QProcess process;
process.start(cmd,args2); process.start(clangCmd(),args2);
process.waitForFinished(); process.waitForFinished();
const QByteArray &ba = process.readAllStandardOutput(); const QByteArray &ba = process.readAllStandardOutput();
const quint16 chksum = qChecksum(ba.data(), ba.length()); const quint16 chksum = qChecksum(ba.data(), ba.length());
@ -223,13 +224,8 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
args.insert(2, "--"); args.insert(2, "--");
} }
#ifdef Q_OS_WIN
const QString ext = ".exe";
#else
const QString ext = "";
#endif
const QString cmd(mClangPath.isEmpty() ? ("clang-tidy" + ext) : (mClangPath + "/clang-tidy" + ext));
{ {
const QString cmd(clangTidyCmd());
QString debug(cmd.contains(" ") ? ('\"' + cmd + '\"') : cmd); QString debug(cmd.contains(" ") ? ('\"' + cmd + '\"') : cmd);
foreach (QString arg, args) { foreach (QString arg, args) {
if (arg.contains(" ")) if (arg.contains(" "))
@ -249,7 +245,7 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
} }
QProcess process; QProcess process;
process.start(cmd, args); process.start(clangTidyCmd(), args);
process.waitForFinished(600*1000); process.waitForFinished(600*1000);
const QString errout(process.readAllStandardOutput() + "\n\n\n" + process.readAllStandardError()); const QString errout(process.readAllStandardOutput() + "\n\n\n" + process.readAllStandardError());
if (!analyzerInfoFile.isEmpty()) { if (!analyzerInfoFile.isEmpty()) {
@ -262,12 +258,9 @@ void CheckThread::runAddonsAndTools(const ImportProject::FileSettings *fileSetti
parseClangErrors(addon, fileName, errout); parseClangErrors(addon, fileName, errout);
} else { } else {
QString a = CheckThread::getAddonFilePath(mDataDir, addon + ".py"); const QString a = CheckThread::getAddonFilePath(mDataDir, addon + ".py");
if (a.isEmpty()) {
a = CheckThread::getAddonFilePath(QApplication::applicationDirPath(), addon + ".py");
if (a.isEmpty()) if (a.isEmpty())
continue; continue;
}
if (dumpFile.isEmpty()) { if (dumpFile.isEmpty()) {
const std::string buildDir = mCppcheck.settings().buildDir; const std::string buildDir = mCppcheck.settings().buildDir;
@ -431,15 +424,72 @@ void CheckThread::parseClangErrors(const QString &tool, const QString &file0, QS
} }
} }
QString CheckThread::getAddonFilePath(const QString &dataDir, const QString &addonFile) QString CheckThread::clangCmd()
{ {
if (dataDir.isEmpty()) QString path = QSettings().value(SETTINGS_CLANG_PATH,QString()).toString();
return QString(); if (!path.isEmpty())
if (QFileInfo(dataDir + '/' + addonFile).exists()) path += '/';
return dataDir + '/' + addonFile; path += "clang";
if (QFileInfo(dataDir + "/addons/" + addonFile).exists()) #ifdef Q_OS_WIN
return dataDir + "/addons/" + addonFile; path += ".exe";
if (QFileInfo(dataDir + "/../addons/" + addonFile).exists()) #endif
return dataDir + "/../addons/" + addonFile;
QProcess process;
process.start(path, QStringList() << "--version");
process.waitForFinished();
if (process.exitCode() == 0)
return path;
#ifdef Q_OS_WIN
// Try to autodetect clang
if (QFileInfo("C:/Program Files/LLVM/bin/clang.exe").exists())
return "C:/Program Files/LLVM/bin/clang.exe";
#endif
return QString();
}
QString CheckThread::clangTidyCmd()
{
QString path = QSettings().value(SETTINGS_CLANG_PATH,QString()).toString();
if (!path.isEmpty())
path += '/';
path += "clang-tidy";
#ifdef Q_OS_WIN
path += ".exe";
#endif
QProcess process;
process.start(path, QStringList() << "--version");
process.waitForFinished();
if (process.exitCode() == 0)
return path;
#ifdef Q_OS_WIN
// Try to autodetect clang-tidy
if (QFileInfo("C:/Program Files/LLVM/bin/clang-tidy.exe").exists())
return "C:/Program Files/LLVM/bin/clang-tidy.exe";
#endif
return QString();
}
QString CheckThread::getAddonFilePath(const QString &dataDir, const QString &addonFile)
{
const QStringList paths = QStringList() << "/" << "/addons/" << "/../addons/";
if (!dataDir.isEmpty()) {
foreach (const QString p, paths) {
if (QFileInfo(dataDir + p + addonFile).exists())
return dataDir + p + addonFile;
}
}
const QString appPath = QApplication::applicationDirPath();
foreach (const QString p, paths) {
if (QFileInfo(dataDir + p + addonFile).exists())
return appPath + p + addonFile;
}
return QString(); return QString();
} }

View File

@ -64,10 +64,6 @@ public:
mDataDir = dataDir; mDataDir = dataDir;
} }
void setClangPath(const QString &p) {
mClangPath = p;
}
void setClangIncludePaths(const QStringList &s) { void setClangIncludePaths(const QStringList &s) {
mClangIncludePaths = s; mClangIncludePaths = s;
} }
@ -84,6 +80,22 @@ public:
void stop(); void stop();
/**
* Loop for clang and return path
* \return path to clang if found, empty if it is not found
*/
static QString clangCmd();
/**
* Loop for clang-tidy and return path
* \return path to clang-tidy if found, empty if it is not found
*/
static QString clangTidyCmd();
/**
* Look for addon and return path
* \return path to addon if found, empty if it is not found
*/
static QString getAddonFilePath(const QString &dataDir, const QString &addonFile); static QString getAddonFilePath(const QString &dataDir, const QString &addonFile);
signals: signals:
@ -133,7 +145,6 @@ private:
QStringList mAddonsAndTools; QStringList mAddonsAndTools;
QString mPythonPath; QString mPythonPath;
QString mDataDir; QString mDataDir;
QString mClangPath;
QStringList mClangIncludePaths; QStringList mClangIncludePaths;
QStringList mSuppressions; QStringList mSuppressions;
}; };

View File

@ -460,15 +460,6 @@ void MainWindow::doAnalyzeProject(ImportProject p)
mThread->setPythonPath(mSettings->value(SETTINGS_PYTHON_PATH).toString()); mThread->setPythonPath(mSettings->value(SETTINGS_PYTHON_PATH).toString());
QString clangHeaders = mSettings->value(SETTINGS_VS_INCLUDE_PATHS).toString(); QString clangHeaders = mSettings->value(SETTINGS_VS_INCLUDE_PATHS).toString();
mThread->setClangIncludePaths(clangHeaders.split(";")); mThread->setClangIncludePaths(clangHeaders.split(";"));
QString clangPath = mSettings->value(SETTINGS_CLANG_PATH,QString()).toString();
#ifdef Q_OS_WIN
if (clangPath.isEmpty()) {
// Try to autodetect clang
if (QFileInfo("C:/Program Files/LLVM/bin/clang.exe").exists())
clangPath = "C:/Program Files/LLVM/bin";
}
#endif
mThread->setClangPath(clangPath);
mThread->setSuppressions(mProjectFile->getSuppressions()); mThread->setSuppressions(mProjectFile->getSuppressions());
} }
mThread->setProject(p); mThread->setProject(p);

View File

@ -34,31 +34,6 @@
#include "cppcheck.h" #include "cppcheck.h"
#include "errorlogger.h" #include "errorlogger.h"
static QString clangTidyCmd()
{
QString path = QSettings().value(SETTINGS_CLANG_PATH,QString()).toString();
if (!path.isEmpty())
path += '/';
path += "clang-tidy";
#ifdef Q_OS_WIN
path += ".exe";
#endif
QProcess process;
process.start(path, QStringList() << "--version");
process.waitForFinished();
if (process.exitCode() == 0)
return path;
#ifdef Q_OS_WIN
// Try to autodetect clang-tidy
if (QFileInfo("C:/Program Files/LLVM/bin/clang-tidy.exe").exists())
return "C:/Program Files/LLVM/bin/clang-tidy.exe";
#endif
return QString();
}
ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent) ProjectFileDialog::ProjectFileDialog(ProjectFile *projectFile, QWidget *parent)
: QDialog(parent) : QDialog(parent)
, mProjectFile(projectFile) , mProjectFile(projectFile)
@ -164,8 +139,7 @@ void ProjectFileDialog::saveSettings() const
static void updateAddonCheckBox(QCheckBox *cb, const ProjectFile *projectFile, const QString &dataDir, const QString &addon) static void updateAddonCheckBox(QCheckBox *cb, const ProjectFile *projectFile, const QString &dataDir, const QString &addon)
{ {
cb->setChecked(projectFile->getAddons().contains(addon)); cb->setChecked(projectFile->getAddons().contains(addon));
const QString appPath = QApplication::applicationDirPath(); if (CheckThread::getAddonFilePath(dataDir, addon + ".py").isEmpty()) {
if (CheckThread::getAddonFilePath(dataDir, addon + ".py").isEmpty() && CheckThread::getAddonFilePath(appPath, addon + ".py").isEmpty()) {
cb->setEnabled(false); cb->setEnabled(false);
cb->setText(cb->text() + QObject::tr(" (Not found)")); cb->setText(cb->text() + QObject::tr(" (Not found)"));
} }
@ -194,7 +168,7 @@ void ProjectFileDialog::loadFromProjectFile(const ProjectFile *projectFile)
mUI.mAddonCert->setChecked(projectFile->getAddons().contains("cert")); mUI.mAddonCert->setChecked(projectFile->getAddons().contains("cert"));
mUI.mToolClangAnalyzer->setChecked(projectFile->getClangAnalyzer()); mUI.mToolClangAnalyzer->setChecked(projectFile->getClangAnalyzer());
mUI.mToolClangTidy->setChecked(projectFile->getClangTidy()); mUI.mToolClangTidy->setChecked(projectFile->getClangTidy());
if (clangTidyCmd().isEmpty()) { if (CheckThread::clangTidyCmd().isEmpty()) {
mUI.mToolClangTidy->setText(tr("Clang-tidy (not found)")); mUI.mToolClangTidy->setText(tr("Clang-tidy (not found)"));
mUI.mToolClangTidy->setEnabled(false); mUI.mToolClangTidy->setEnabled(false);
} }

View File

@ -96,7 +96,6 @@ void ThreadHandler::check(const Settings &settings)
mThreads[i]->setAddonsAndTools(mAddonsAndTools); mThreads[i]->setAddonsAndTools(mAddonsAndTools);
mThreads[i]->setPythonPath(mPythonPath); mThreads[i]->setPythonPath(mPythonPath);
mThreads[i]->setSuppressions(mSuppressions); mThreads[i]->setSuppressions(mSuppressions);
mThreads[i]->setClangPath(mClangPath);
mThreads[i]->setClangIncludePaths(mClangIncludePaths); mThreads[i]->setClangIncludePaths(mClangIncludePaths);
mThreads[i]->setDataDir(mDataDir); mThreads[i]->setDataDir(mDataDir);
mThreads[i]->check(settings); mThreads[i]->check(settings);

View File

@ -83,10 +83,6 @@ public:
mPythonPath = p; mPythonPath = p;
} }
void setClangPath(const QString &p) {
mClangPath = p;
}
void setClangIncludePaths(const QStringList &s) { void setClangIncludePaths(const QStringList &s) {
mClangIncludePaths = s; mClangIncludePaths = s;
} }
@ -264,7 +260,6 @@ protected:
QStringList mAddonsAndTools; QStringList mAddonsAndTools;
QStringList mSuppressions; QStringList mSuppressions;
QString mPythonPath; QString mPythonPath;
QString mClangPath;
QStringList mClangIncludePaths; QStringList mClangIncludePaths;
QString mDataDir; QString mDataDir;