Skip to content

Commit 3d1281e

Browse files
committed
fetch addon infos only once and store them in Settings::addonInfos
1 parent 45d78ed commit 3d1281e

8 files changed

+56
-9
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
117117
if (!loadLibraries(settings))
118118
return false;
119119

120+
if (!loadAddons(settings))
121+
return false;
122+
120123
// Check that all include paths exist
121124
{
122125
for (std::list<std::string>::iterator iter = settings.includePaths.begin();
@@ -353,6 +356,21 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
353356
return true;
354357
}
355358

359+
bool CppCheckExecutor::loadAddons(Settings& settings)
360+
{
361+
for (const std::string &addon: settings.addons) {
362+
AddonInfo addonInfo;
363+
const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename);
364+
if (!failedToGetAddonInfo.empty()) {
365+
const ErrorMessage errmsg({}, emptyString, Severity::information, failedToGetAddonInfo, "failedToLoadAddon", Certainty::normal);
366+
reportErr(errmsg);
367+
return false;
368+
}
369+
settings.addonInfos.emplace_back(std::move(addonInfo));
370+
}
371+
return true;
372+
}
373+
356374
#ifdef _WIN32
357375
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII'
358376
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
@@ -151,6 +151,13 @@ class CppCheckExecutor : public ErrorLogger {
151151
*/
152152
bool loadLibraries(Settings& settings);
153153

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

gui/mainwindow.cpp

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

10021002
addonFilePath.replace(QChar('\\'), QChar('/'));
10031003

1004+
// TODO: use picojson to generate the JSON
10041005
QString json;
10051006
json += "{ \"script\":\"" + addonFilePath + "\"";
10061007
if (!pythonCmd.isEmpty())
@@ -1016,6 +1017,9 @@ Settings MainWindow::getCppcheckSettings()
10161017
}
10171018
json += " }";
10181019
result.addons.emplace(json.toStdString());
1020+
AddonInfo addonInfo;
1021+
addonInfo.getAddonInfo(json.toStdString(), result.exename);
1022+
result.addonInfos.emplace_back(std::move(addonInfo));
10191023
}
10201024

10211025
if (isCppcheckPremium()) {

lib/cppcheck.cpp

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

1316-
for (const std::string &addon : mSettings.addons) {
1317-
struct AddonInfo addonInfo;
1318-
const std::string &failedToGetAddonInfo = addonInfo.getAddonInfo(addon, mSettings.exename);
1319-
if (!failedToGetAddonInfo.empty()) {
1320-
reportOut(failedToGetAddonInfo, Color::FgRed);
1321-
mExitCode = 1;
1322-
continue;
1323-
}
1316+
// ensure all addons have already been resolved - TODO: remove when settings are const after creation
1317+
assert(mSettings.addonInfos.size() == mSettings.addons.size());
1318+
1319+
for (const AddonInfo &addonInfo : mSettings.addonInfos) {
13241320
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
13251321
continue;
13261322

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 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
@@ -9,3 +9,4 @@ GUI:
99
Changed interface:
1010

1111
Other:
12+
- 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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,10 @@ def test_missing_include_inline_suppr(tmpdir):
6060
args = ['--enable=missingInclude', '--inline-suppr', test_file]
6161

6262
_, _, stderr = cppcheck(args)
63-
assert stderr == ''
63+
assert stderr == ''
64+
65+
def test_invalid_addon(tmpdir):
66+
args = ['--addon=misra2', 'file.c']
67+
68+
_, _, stderr = cppcheck(args)
69+
assert stderr == 'nofile:0:0: information: Did not find addon misra2.py [failedToLoadAddon]\n'

test/testcmdlineparser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ class TestCmdlineParser : public TestFixture {
221221
TEST_CASE(typedefMaxTimeInvalid2);
222222
TEST_CASE(templateMaxTime);
223223
TEST_CASE(templateMaxTime);
224+
TEST_CASE(addon);
224225

225226
TEST_CASE(ignorepaths1);
226227
TEST_CASE(ignorepaths2);
@@ -1825,6 +1826,16 @@ class TestCmdlineParser : public TestFixture {
18251826
ASSERT_EQUALS("cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive.\n", GET_REDIRECT_OUTPUT);
18261827
}
18271828

1829+
void addon() {
1830+
REDIRECT;
1831+
const char * const argv[] = {"cppcheck", "--addon=misra", "file.cpp"};
1832+
settings.addons.clear();
1833+
ASSERT(defParser.parseFromArgs(2, argv));
1834+
ASSERT_EQUALS(1, settings.addons.size());
1835+
ASSERT_EQUALS("misra", *settings.addons.cbegin());
1836+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1837+
}
1838+
18281839
void ignorepaths1() {
18291840
REDIRECT;
18301841
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)