@@ -486,6 +486,11 @@ void MainWindow::saveSettings() const
486486
487487void MainWindow::doAnalyzeProject (ImportProject p, const bool checkLibrary, const bool checkConfiguration)
488488{
489+ QPair<bool ,Settings> checkSettingsPair = getCppcheckSettings ();
490+ if (!checkSettingsPair.first )
491+ return ;
492+ Settings& checkSettings = checkSettingsPair.second ;
493+
489494 clearResults ();
490495
491496 mIsLogfileLoaded = false ;
@@ -522,7 +527,6 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons
522527 checkLockDownUI (); // lock UI while checking
523528
524529 mUI ->mResults ->setCheckDirectory (checkPath);
525- Settings checkSettings = getCppcheckSettings ();
526530 checkSettings.force = false ;
527531 checkSettings.checkLibrary = checkLibrary;
528532 checkSettings.checkConfiguration = checkConfiguration;
@@ -550,9 +554,14 @@ void MainWindow::doAnalyzeProject(ImportProject p, const bool checkLibrary, cons
550554
551555void MainWindow::doAnalyzeFiles (const QStringList &files, const bool checkLibrary, const bool checkConfiguration)
552556{
553- if (files.isEmpty ()) {
557+ if (files.isEmpty ())
554558 return ;
555- }
559+
560+ QPair<bool , Settings> checkSettingsPair = getCppcheckSettings ();
561+ if (!checkSettingsPair.first )
562+ return ;
563+ Settings& checkSettings = checkSettingsPair.second ;
564+
556565 clearResults ();
557566
558567 mIsLogfileLoaded = false ;
@@ -591,7 +600,6 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
591600 checkLockDownUI (); // lock UI while checking
592601
593602 mUI ->mResults ->setCheckDirectory (checkPath);
594- Settings checkSettings = getCppcheckSettings ();
595603 checkSettings.checkLibrary = checkLibrary;
596604 checkSettings.checkConfiguration = checkConfiguration;
597605
@@ -614,6 +622,11 @@ void MainWindow::doAnalyzeFiles(const QStringList &files, const bool checkLibrar
614622
615623void MainWindow::analyzeCode (const QString& code, const QString& filename)
616624{
625+ const QPair<bool , Settings>& checkSettingsPair = getCppcheckSettings ();
626+ if (!checkSettingsPair.first )
627+ return ;
628+ const Settings& checkSettings = checkSettingsPair.second ;
629+
617630 // Initialize dummy ThreadResult as ErrorLogger
618631 ThreadResult result;
619632 result.setFiles (QStringList (filename));
@@ -628,7 +641,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
628641
629642 // Create CppCheck instance
630643 CppCheck cppcheck (result, true , nullptr );
631- cppcheck.settings () = getCppcheckSettings () ;
644+ cppcheck.settings () = checkSettings ;
632645
633646 // Check
634647 checkLockDownUI ();
@@ -905,13 +918,12 @@ bool MainWindow::tryLoadLibrary(Library *library, const QString& filename)
905918 return true ;
906919}
907920
908- void MainWindow::loadAddon (Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
921+ QString MainWindow::loadAddon (Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon)
909922{
910- QString addonFilePath = ProjectFile::getAddonFilePath (filesDir, addon);
911- if (addonFilePath.isEmpty ())
912- return ; // TODO: report an error
923+ const QString addonFilePath = fromNativePath (ProjectFile::getAddonFilePath (filesDir, addon));
913924
914- addonFilePath.replace (QChar (' \\ ' ), QChar (' /' ));
925+ if (addonFilePath.isEmpty ())
926+ return tr (" File not found: '%1'" ).arg (addon);
915927
916928 picojson::object obj;
917929 obj[" script" ] = picojson::value (addonFilePath.toStdString ());
@@ -929,42 +941,54 @@ void MainWindow::loadAddon(Settings &settings, const QString &filesDir, const QS
929941 obj[" args" ] = picojson::value (arg.toStdString ());
930942 }
931943 }
932- picojson::value json;
933- json.set (std::move (obj));
934- std::string json_str = json.serialize ();
944+
945+ const std::string& json_str = picojson::value (obj).serialize ();
935946
936947 AddonInfo addonInfo;
937- addonInfo.getAddonInfo (json_str, settings.exename ); // TODO: handle error
948+ const std::string errmsg = addonInfo.getAddonInfo (json_str, settings.exename );
949+ if (!errmsg.empty ())
950+ return tr (" Failed to load/setup addon %1: %2" ).arg (addon, QString::fromStdString (errmsg));
938951 settings.addonInfos .emplace_back (std::move (addonInfo));
939952
940- settings.addons .emplace (std::move (json_str));
953+ settings.addons .emplace (json_str);
954+
955+ return " " ;
941956}
942957
943- Settings MainWindow::getCppcheckSettings ()
958+ QPair< bool , Settings> MainWindow::getCppcheckSettings ()
944959{
945960 saveSettings (); // Save settings
946961
962+ Settings::terminate (true );
947963 Settings result;
948964
949965 result.exename = QCoreApplication::applicationFilePath ().toStdString ();
950966
951967 const bool std = tryLoadLibrary (&result.library , " std.cfg" );
952- if (!std)
953- QMessageBox::critical (this , tr (" Error" ), tr (" Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured." ).arg (" std.cfg" ));
968+ if (!std) {
969+ QMessageBox::critical (this , tr (" Error" ), tr (" Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.\n\n Analysis is aborted." ).arg (" std.cfg" ));
970+ return {false , {}};
971+ }
954972
955973 const QString filesDir (getDataDir ());
956974 const QString pythonCmd = fromNativePath (mSettings ->value (SETTINGS_PYTHON_PATH).toString ());
957975
958976 {
959977 const QString cfgErr = QString::fromStdString (result.loadCppcheckCfg ());
960- if (!cfgErr.isEmpty ())
961- QMessageBox::critical (this , tr (" Error" ), tr (" Failed to load %1 - %2" ).arg (" cppcheck.cfg" ).arg (cfgErr));
978+ if (!cfgErr.isEmpty ()) {
979+ QMessageBox::critical (this , tr (" Error" ), tr (" Failed to load %1 - %2\n\n Analysis is aborted." ).arg (" cppcheck.cfg" ).arg (cfgErr));
980+ return {false , {}};
981+ }
962982
963983 const auto cfgAddons = result.addons ;
964984 result.addons .clear ();
965985 for (const std::string& addon : cfgAddons) {
966986 // TODO: support addons which are a script and not a file
967- loadAddon (result, filesDir, pythonCmd, QString::fromStdString (addon));
987+ const QString addonError = loadAddon (result, filesDir, pythonCmd, QString::fromStdString (addon));
988+ if (!addonError.isEmpty ()) {
989+ QMessageBox::critical (this , tr (" Error" ), tr (" %1\n\n Analysis is aborted." ).arg (addonError));
990+ return {false , {}};
991+ }
968992 }
969993 }
970994
@@ -1045,7 +1069,11 @@ Settings MainWindow::getCppcheckSettings()
10451069 result.checkUnknownFunctionReturn .insert (s.toStdString ());
10461070
10471071 for (const QString& addon : mProjectFile ->getAddons ()) {
1048- loadAddon (result, filesDir, pythonCmd, addon);
1072+ const QString addonError = loadAddon (result, filesDir, pythonCmd, addon);
1073+ if (!addonError.isEmpty ()) {
1074+ QMessageBox::critical (this , tr (" Error" ), tr (" %1\n\n Analysis is aborted." ).arg (addonError));
1075+ return {false , {}};
1076+ }
10491077 }
10501078
10511079 if (isCppcheckPremium ()) {
@@ -1099,7 +1127,7 @@ Settings MainWindow::getCppcheckSettings()
10991127
11001128 Settings::terminate (false );
11011129
1102- return result;
1130+ return { true , std::move ( result)} ;
11031131}
11041132
11051133void MainWindow::analysisDone ()
@@ -1223,6 +1251,11 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
12231251 if (mThread ->isChecking ())
12241252 return ;
12251253
1254+ const QPair<bool , Settings> checkSettingsPair = getCppcheckSettings ();
1255+ if (!checkSettingsPair.first )
1256+ return ;
1257+ const Settings& checkSettings = checkSettingsPair.second ;
1258+
12261259 // Clear details, statistics and progress
12271260 mUI ->mResults ->clear (false );
12281261 for (int i = 0 ; i < files.size (); ++i)
@@ -1242,7 +1275,6 @@ void MainWindow::reAnalyzeSelected(const QStringList& files)
12421275 // considered in "Modified Files Check" performed after "Selected Files Check"
12431276 // TODO: Should we store per file CheckStartTime?
12441277 QDateTime saveCheckStartTime = mThread ->getCheckStartTime ();
1245- const Settings& checkSettings = getCppcheckSettings ();
12461278 mThread ->check (checkSettings);
12471279 mUI ->mResults ->setCheckSettings (checkSettings);
12481280 mThread ->setCheckStartTime (saveCheckStartTime);
@@ -1254,6 +1286,11 @@ void MainWindow::reAnalyze(bool all)
12541286 if (files.empty ())
12551287 return ;
12561288
1289+ const QPair<bool , Settings>& checkSettingsPair = getCppcheckSettings ();
1290+ if (!checkSettingsPair.first )
1291+ return ;
1292+ const Settings& checkSettings = checkSettingsPair.second ;
1293+
12571294 // Clear details, statistics and progress
12581295 mUI ->mResults ->clear (all);
12591296
@@ -1268,7 +1305,6 @@ void MainWindow::reAnalyze(bool all)
12681305 qDebug () << " Rechecking project file" << mProjectFile ->getFilename ();
12691306
12701307 mThread ->setCheckFiles (all);
1271- const Settings& checkSettings = getCppcheckSettings ();
12721308 mThread ->check (checkSettings);
12731309 mUI ->mResults ->setCheckSettings (checkSettings);
12741310}
0 commit comments