Skip to content

Commit

Permalink
Fix #12389 (GUI: premiumaddon is not executed properly anymore)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Jan 30, 2024
1 parent cecbe18 commit f10f746
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
43 changes: 24 additions & 19 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,34 +907,39 @@ bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)

void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
{
QString addonFilePath = ProjectFile::getAddonFilePath(filesDir, addon);
if (addonFilePath.isEmpty())
return; // TODO: report an error

addonFilePath.replace(QChar('\\'), QChar('/'));
const QString addonFilePath = fromNativePath(ProjectFile::getAddonFilePath(filesDir, addon));

picojson::object obj;
obj["script"] = picojson::value(addonFilePath.toStdString());
if (!pythonCmd.isEmpty())
obj["python"] = picojson::value(pythonCmd.toStdString());

if (!isCppcheckPremium() && addon == "misra") {
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
if (!misraFile.isEmpty()) {
QString arg;
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
arg = "--misra-pdf=" + misraFile;
else
arg = "--rule-texts=" + misraFile;
obj["args"] = picojson::value(arg.toStdString());
if (addonFilePath.isEmpty()) // Ensure there will be "fileNotFound" errors
obj["script"] = picojson::value(addon.toStdString());
else {
obj["script"] = picojson::value(addonFilePath.toStdString());
if (!pythonCmd.isEmpty())
obj["python"] = picojson::value(pythonCmd.toStdString());

if (!isCppcheckPremium() && addon == "misra") {
const QString misraFile = fromNativePath(mSettings->value(SETTINGS_MISRA_FILE).toString());
if (!misraFile.isEmpty()) {
QString arg;
if (misraFile.endsWith(".pdf", Qt::CaseInsensitive))
arg = "--misra-pdf=" + misraFile;
else
arg = "--rule-texts=" + misraFile;
obj["args"] = picojson::value(arg.toStdString());
}
}
}

picojson::value json;
json.set(std::move(obj));
std::string json_str = json.serialize();

AddonInfo addonInfo;
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
if (addonFilePath.isEmpty()) {
addonInfo.name = addon.toStdString();
} else {
addonInfo.getAddonInfo(json_str, settings.exename); // TODO: handle error
}
settings.addonInfos.emplace_back(std::move(addonInfo));

settings.addons.emplace(std::move(json_str));
Expand Down
3 changes: 3 additions & 0 deletions gui/projectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,9 @@ void ProjectFile::SafeChecks::saveToXml(QXmlStreamWriter &xmlWriter) const

QString ProjectFile::getAddonFilePath(QString filesDir, const QString &addon)
{
if (QFile(addon).exists())
return addon;

if (!filesDir.endsWith("/"))
filesDir += "/";

Expand Down
19 changes: 19 additions & 0 deletions gui/test/projectfile/testprojectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <QList>
#include <QStringList>
#include <QTemporaryDir>
#include <QtTest>

// Mock...
Expand Down Expand Up @@ -116,4 +117,22 @@ void TestProjectFile::loadSimpleNoroot() const
QCOMPARE(defines[0], QString("FOO"));
}

void TestProjectFile::getAddonFilePath() const
{
QTemporaryDir tempdir;
QVERIFY(tempdir.isValid());
const QString filepath(tempdir.path() + "/addon.py");

QFile file(filepath);
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
file.write("");
file.close();

// Relative path to addon
QCOMPARE(ProjectFile::getAddonFilePath(tempdir.path(), "addon"), filepath);

// Absolute path to addon
QCOMPARE(ProjectFile::getAddonFilePath("/not/exist", filepath), filepath);
}

QTEST_MAIN(TestProjectFile)
2 changes: 2 additions & 0 deletions gui/test/projectfile/testprojectfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ private slots:
void loadSimple() const;
void loadSimpleWithIgnore() const;
void loadSimpleNoroot() const;

void getAddonFilePath() const;
};
20 changes: 20 additions & 0 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,21 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
return mExitCode;
}

void CppCheck::fileNotFoundError(const std::string &filename, const std::string& msg)
{
const ErrorMessage::FileLocation loc1(filename, 0, 0);
std::list<ErrorMessage::FileLocation> callstack(1, loc1);

ErrorMessage errmsg(callstack,
emptyString,
Severity::error,
msg,
"fileNotFound",
Certainty::normal);

mErrorLogger.reportErr(errmsg);
}

// TODO: replace with ErrorMessage::fromInternalError()
void CppCheck::internalError(const std::string &filename, const std::string &msg)
{
Expand Down Expand Up @@ -1440,6 +1455,11 @@ void CppCheck::executeAddons(const std::vector<std::string>& files, const std::s
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
continue;

if (addonInfo.scriptFile.empty() && addonInfo.executable.empty()) {
fileNotFoundError(file0, "Fail to execute addon " + addonInfo.name);
continue;
}

const std::vector<picojson::value> results =
executeAddon(addonInfo, mSettings.addonPython, fileList.empty() ? files[0] : fileList, mSettings.premiumArgs, mExecuteCommand);

Expand Down
3 changes: 3 additions & 0 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
bool hasRule(const std::string &tokenlist) const;
#endif

/** @brief A file is not found => report error message */
void fileNotFoundError(const std::string &filename, const std::string& msg);

/** @brief There has been an internal error => Report information message */
void internalError(const std::string &filename, const std::string &msg);

Expand Down
1 change: 1 addition & 0 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
const std::set<std::string> ErrorLogger::mCriticalErrorIds{
"cppcheckError",
"cppcheckLimit",
"fileNotFound",
"internalAstError",
"instantiationError",
"internalError",
Expand Down

0 comments on commit f10f746

Please sign in to comment.