Skip to content

Commit cbf12c8

Browse files
committed
fetch addon infos only once and store them in Settings::addonInfos
1 parent a12a2c3 commit cbf12c8

File tree

9 files changed

+177
-130
lines changed

9 files changed

+177
-130
lines changed

Makefile

Lines changed: 118 additions & 118 deletions
Large diffs are not rendered by default.

cli/cppcheckexecutor.cpp

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

153+
if (!loadAddons(settings))
154+
return false;
155+
153156
// Check that all include paths exist
154157
{
155158
for (std::list<std::string>::iterator iter = settings.includePaths.begin();
@@ -406,6 +409,20 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
406409
return result;
407410
}
408411

412+
bool CppCheckExecutor::loadAddons(Settings& settings)
413+
{
414+
for (const std::string &addon: settings.addons) {
415+
AddonInfo addonInfo;
416+
const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename);
417+
if (!failedToGetAddonInfo.empty()) {
418+
std::cout << failedToGetAddonInfo << std::endl;
419+
return false;
420+
}
421+
settings.addonInfos.emplace_back(std::move(addonInfo));
422+
}
423+
return true;
424+
}
425+
409426
#ifdef _WIN32
410427
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII'
411428
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
* @brief Write the checkers report
158165
*/

gui/mainwindow.cpp

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

10001000
addonFilePath.replace(QChar('\\'), QChar('/'));
10011001

1002+
// TODO: use picojson to generate the JSON
10021003
QString json;
10031004
json += "{ \"script\":\"" + addonFilePath + "\"";
10041005
if (!pythonCmd.isEmpty())
@@ -1014,6 +1015,9 @@ Settings MainWindow::getCppcheckSettings()
10141015
}
10151016
json += " }";
10161017
result.addons.emplace(json.toStdString());
1018+
AddonInfo addonInfo;
1019+
addonInfo.getAddonInfo(json.toStdString(), result.exename);
1020+
result.addonInfos.emplace_back(std::move(addonInfo));
10171021
}
10181022

10191023
if (isCppcheckPremium()) {

lib/cppcheck.cpp

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

1388-
for (const std::string &addon : mSettings.addons) {
1389-
struct AddonInfo addonInfo;
1390-
const std::string &failedToGetAddonInfo = addonInfo.getAddonInfo(addon, mSettings.exename);
1391-
if (!failedToGetAddonInfo.empty()) {
1392-
reportOut(failedToGetAddonInfo, Color::FgRed);
1393-
mExitCode = 1;
1394-
continue;
1395-
}
1388+
// ensure all addons have already been resolved - TODO: remove when settings are const after creation
1389+
assert(mSettings.addonInfos.size() == mSettings.addons.size());
1390+
1391+
for (const AddonInfo &addonInfo : mSettings.addonInfos) {
13961392
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
13971393
continue;
13981394

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Changed interface:
1414

1515
Other:
1616
- Windows builds now default to the `native` platform instead of `win32A` or `win64`. Please specify it explicitly if you depedent on it.
17-
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
17+
- The undocumented and deprecated command-line options `--template <template>` and `--template-format <template>` has been removed. Please use `--template=` and `--template-format=` instead.
18+
- 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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,9 @@ def test_invalid_addon_json(tmpdir):
403403
args = ['--addon={}'.format(addon_file), '--enable=all', test_file]
404404

405405
exitcode, stdout, stderr = cppcheck(args)
406-
assert exitcode == 0 # TODO: needs to be 1
406+
assert exitcode == 1
407407
lines = stdout.splitlines()
408408
assert lines == [
409-
'Checking {} ...'.format(test_file),
410-
'Loading {} failed. syntax error at line 2 near: '.format(addon_file),
411409
'Loading {} failed. syntax error at line 2 near: '.format(addon_file)
412410
]
413411
assert stderr == ''
@@ -531,3 +529,12 @@ class A {
531529

532530
_, _, stderr = cppcheck(args)
533531
assert stderr == "{}:4:0: style: The function 'f' is never used. [unusedFunction]\n".format(test_h_file)
532+
533+
534+
def test_missing_addon(tmpdir):
535+
args = ['--addon=misra2', 'file.c']
536+
537+
exitcode, stdout, stderr = cppcheck(args)
538+
assert exitcode == 1
539+
assert stdout == 'Did not find addon misra2.py\n'
540+
assert stderr == ""

test/testcmdlineparser.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class TestCmdlineParser : public TestFixture {
274274
TEST_CASE(typedefMaxTimeInvalid2);
275275
TEST_CASE(templateMaxTime);
276276
TEST_CASE(templateMaxTime);
277+
TEST_CASE(addon);
277278

278279
TEST_CASE(ignorepaths1);
279280
TEST_CASE(ignorepaths2);
@@ -1871,6 +1872,16 @@ class TestCmdlineParser : public TestFixture {
18711872
ASSERT_EQUALS("cppcheck: error: argument to '--typedef-max-time=' is not valid - needs to be positive.\n", logger->str());
18721873
}
18731874

1875+
void addon() {
1876+
REDIRECT;
1877+
const char * const argv[] = {"cppcheck", "--addon=misra", "file.cpp"};
1878+
settings->addons.clear();
1879+
ASSERT(parser->parseFromArgs(3, argv));
1880+
ASSERT_EQUALS(1, settings->addons.size());
1881+
ASSERT_EQUALS("misra", *settings->addons.cbegin());
1882+
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
1883+
}
1884+
18741885
void ignorepaths1() {
18751886
REDIRECT;
18761887
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};

0 commit comments

Comments
 (0)