Skip to content

Commit 122cc6c

Browse files
committed
fetch addon infos only once and store them in Settings::addonInfos
1 parent 5586826 commit 122cc6c

7 files changed

Lines changed: 39 additions & 9 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ int CppCheckExecutor::check_internal(CppCheck& cppcheck)
286286
return EXIT_FAILURE;
287287
}
288288

289+
for (const std::string &addon : settings.addons) {
290+
AddonInfo addonInfo;
291+
const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename);
292+
if (!failedToGetAddonInfo.empty()) {
293+
const ErrorMessage errmsg({}, emptyString, Severity::information, failedToGetAddonInfo, "failedToLoadAddon", Certainty::normal);
294+
reportErr(errmsg);
295+
return EXIT_FAILURE;
296+
}
297+
settings.addonInfos.emplace_back(std::move(addonInfo));
298+
}
299+
289300
if (settings.reportProgress)
290301
mLatestProgressOutputTime = std::time(nullptr);
291302

gui/mainwindow.cpp

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

994994
addonFilePath.replace(QChar('\\'), QChar('/'));
995995

996+
// TODO: use picojson to generate the JSON
996997
QString json;
997998
json += "{ \"script\":\"" + addonFilePath + "\"";
998999
if (!pythonCmd.isEmpty())
@@ -1008,6 +1009,9 @@ Settings MainWindow::getCppcheckSettings()
10081009
}
10091010
json += " }";
10101011
result.addons.emplace(json.toStdString());
1012+
AddonInfo addonInfo;
1013+
addonInfo.getAddonInfo(json.toStdString(), result.exename);
1014+
result.addonInfos.emplace_back(std::move(addonInfo));
10111015
}
10121016

10131017
if (isCppcheckPremium()) {

lib/cppcheck.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,14 +1327,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
13271327
fout << f << std::endl;
13281328
}
13291329

1330-
for (const std::string &addon : mSettings.addons) {
1331-
struct AddonInfo addonInfo;
1332-
const std::string &failedToGetAddonInfo = addonInfo.getAddonInfo(addon, mSettings.exename);
1333-
if (!failedToGetAddonInfo.empty()) {
1334-
reportOut(failedToGetAddonInfo, Color::FgRed);
1335-
mExitCode = 1;
1336-
continue;
1337-
}
1330+
for (const AddonInfo &addonInfo : mSettings.addonInfos) {
13381331
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
13391332
continue;
13401333

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"
@@ -110,6 +111,9 @@ class CPPCHECKLIB Settings {
110111
/** @brief addons, either filename of python/json file or json data */
111112
std::unordered_set<std::string> addons;
112113

114+
/** @brief the loaded addons infos */
115+
std::vector<AddonInfo> addonInfos;
116+
113117
/** @brief Path to the python interpreter to be used to run addons. */
114118
std::string addonPython;
115119

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ release notes for cppcheck-2.11
1414
- `constVariableReference`
1515
- `constVariablePointer`
1616
- More command-line parameters will now check if the given integer argument is actually valid. Several other internal string-to-integer conversions will not be error checked.
17+
- 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
@@ -222,6 +222,7 @@ class TestCmdlineParser : public TestFixture {
222222
TEST_CASE(typedefMaxTimeInvalid2);
223223
TEST_CASE(templateMaxTime);
224224
TEST_CASE(templateMaxTime);
225+
TEST_CASE(addon);
225226

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

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

0 commit comments

Comments
 (0)