Skip to content

Commit ee405e5

Browse files
committed
fetch addon infos only once and store them in Settings::addonInfos
1 parent 7f1fe97 commit ee405e5

File tree

8 files changed

+57
-8
lines changed

8 files changed

+57
-8
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
131131
if (!loadLibraries(settings))
132132
return false;
133133

134+
if (!loadAddons(settings))
135+
return false;
136+
134137
// Check that all include paths exist
135138
{
136139
for (std::list<std::string>::iterator iter = settings.includePaths.begin();
@@ -358,6 +361,20 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
358361
return result;
359362
}
360363

364+
bool CppCheckExecutor::loadAddons(Settings& settings)
365+
{
366+
for (const std::string &addon: settings.addons) {
367+
AddonInfo addonInfo;
368+
const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename);
369+
if (!failedToGetAddonInfo.empty()) {
370+
std::cout << failedToGetAddonInfo << std::endl;
371+
return false;
372+
}
373+
settings.addonInfos.emplace_back(std::move(addonInfo));
374+
}
375+
return true;
376+
}
377+
361378
#ifdef _WIN32
362379
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII'
363380
static inline std::string ansiToOEM(const std::string &msg, bool doConvert)

cli/cppcheckexecutor.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ class CppCheckExecutor : public ErrorLogger {
153153
*/
154154
bool loadLibraries(Settings& settings);
155155

156+
/**
157+
* @brief Load addons
158+
* @param settings Settings
159+
* @return Returns true if successful
160+
*/
161+
bool loadAddons(Settings& settings);
162+
156163
/**
157164
* Pointer to current settings; set while check() is running for reportError().
158165
*/

gui/mainwindow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ Settings MainWindow::getCppcheckSettings()
997997

998998
addonFilePath.replace(QChar('\\'), QChar('/'));
999999

1000+
// TODO: use picojson to generate the JSON
10001001
QString json;
10011002
json += "{ \"script\":\"" + addonFilePath + "\"";
10021003
if (!pythonCmd.isEmpty())
@@ -1012,6 +1013,9 @@ Settings MainWindow::getCppcheckSettings()
10121013
}
10131014
json += " }";
10141015
result.addons.emplace(json.toStdString());
1016+
AddonInfo addonInfo;
1017+
addonInfo.getAddonInfo(json.toStdString(), result.exename);
1018+
result.addonInfos.emplace_back(std::move(addonInfo));
10151019
}
10161020

10171021
if (isCppcheckPremium()) {

lib/cppcheck.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,14 +1331,10 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
13311331
fout << f << std::endl;
13321332
}
13331333

1334-
for (const std::string &addon : mSettings.addons) {
1335-
struct AddonInfo addonInfo;
1336-
const std::string &failedToGetAddonInfo = addonInfo.getAddonInfo(addon, mSettings.exename);
1337-
if (!failedToGetAddonInfo.empty()) {
1338-
reportOut(failedToGetAddonInfo, Color::FgRed);
1339-
mExitCode = 1;
1340-
continue;
1341-
}
1334+
// ensure all addons have already been resolved - TODO: remove when settings are const after creation
1335+
assert(mSettings.addonInfos.size() == mSettings.addons.size());
1336+
1337+
for (const AddonInfo &addonInfo : mSettings.addonInfos) {
13421338
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
13431339
continue;
13441340

lib/settings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define settingsH
2222
//---------------------------------------------------------------------------
2323

24+
#include "addoninfo.h"
2425
#include "config.h"
2526
#include "errortypes.h"
2627
#include "importproject.h"
@@ -104,6 +105,9 @@ class CPPCHECKLIB WARN_UNUSED Settings {
104105
/** @brief addons, either filename of python/json file or json data */
105106
std::unordered_set<std::string> addons;
106107

108+
/** @brief the loaded addons infos */
109+
std::vector<AddonInfo> addonInfos;
110+
107111
/** @brief Path to the python interpreter to be used to run addons. */
108112
std::string addonPython;
109113

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Deprecations:
2020

2121
Other:
2222
- "USE_QT6=On" will no longer fallback to Qt5 when Qt6 is not found.
23+
- If a addon cannot be found it will bail out immediately instead of continously writing errors and failing the analysis at the end.

test/cli/test-other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@ def test_progress_j(tmpdir):
141141
assert exitcode == 0
142142
assert stdout == "Checking {} ...\n".format(test_file)
143143
assert stderr == ""
144+
145+
146+
def test_invalid_addon(tmpdir):
147+
args = ['--addon=misra2', 'file.c']
148+
149+
exitcode, stdout, stderr = cppcheck(args)
150+
assert exitcode == 1
151+
assert stdout == '(information) Did not find addon misra2.py\n'
152+
assert stderr == ""

test/testcmdlineparser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ class TestCmdlineParser : public TestFixture {
244244
TEST_CASE(typedefMaxTimeInvalid2);
245245
TEST_CASE(templateMaxTime);
246246
TEST_CASE(templateMaxTime);
247+
TEST_CASE(addon);
247248

248249
TEST_CASE(ignorepaths1);
249250
TEST_CASE(ignorepaths2);
@@ -1899,6 +1900,16 @@ class TestCmdlineParser : public TestFixture {
18991900
ASSERT_EQUALS("cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive.\n", GET_REDIRECT_OUTPUT);
19001901
}
19011902

1903+
void addon() {
1904+
REDIRECT;
1905+
const char * const argv[] = {"cppcheck", "--addon=misra", "file.cpp"};
1906+
settings.addons.clear();
1907+
ASSERT(defParser.parseFromArgs(2, argv));
1908+
ASSERT_EQUALS(1, settings.addons.size());
1909+
ASSERT_EQUALS("misra", *settings.addons.cbegin());
1910+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1911+
}
1912+
19021913
void ignorepaths1() {
19031914
REDIRECT;
19041915
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)