Skip to content

Commit

Permalink
fetch addon infos only once and store them in Settings::addonInfos
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 5, 2023
1 parent 1828800 commit be1006b
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 129 deletions.
236 changes: 118 additions & 118 deletions Makefile

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions cli/cppcheckexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ bool CppCheckExecutor::parseFromArgs(Settings &settings, int argc, const char* c
if (!loadLibraries(settings))
return false;

if (!loadAddons(settings))
return false;

// Check that all include paths exist
{
for (std::list<std::string>::iterator iter = settings.includePaths.begin();
Expand Down Expand Up @@ -406,6 +409,20 @@ bool CppCheckExecutor::loadLibraries(Settings& settings)
return result;
}

bool CppCheckExecutor::loadAddons(Settings& settings)
{
for (const std::string &addon: settings.addons) {
AddonInfo addonInfo;
const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename);
if (!failedToGetAddonInfo.empty()) {
std::cout << failedToGetAddonInfo << std::endl;
return false;
}
settings.addonInfos.emplace_back(std::move(addonInfo));
}
return true;
}

#ifdef _WIN32
// fix trac ticket #439 'Cppcheck reports wrong filename for filenames containing 8-bit ASCII'
static inline std::string ansiToOEM(const std::string &msg, bool doConvert)
Expand Down
7 changes: 7 additions & 0 deletions cli/cppcheckexecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class CppCheckExecutor : public ErrorLogger {
*/
bool loadLibraries(Settings& settings);

/**
* @brief Load addons
* @param settings Settings
* @return Returns true if successful
*/
bool loadAddons(Settings& settings);

/**
* @brief Write the checkers report
*/
Expand Down
4 changes: 4 additions & 0 deletions gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ Settings MainWindow::getCppcheckSettings()

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

// TODO: use picojson to generate the JSON
QString json;
json += "{ \"script\":\"" + addonFilePath + "\"";
if (!pythonCmd.isEmpty())
Expand All @@ -1014,6 +1015,9 @@ Settings MainWindow::getCppcheckSettings()
}
json += " }";
result.addons.emplace(json.toStdString());
AddonInfo addonInfo;
addonInfo.getAddonInfo(json.toStdString(), result.exename);
result.addonInfos.emplace_back(std::move(addonInfo));
}

if (isCppcheckPremium()) {
Expand Down
12 changes: 4 additions & 8 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1388,14 +1388,10 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
fout << f << std::endl;
}

for (const std::string &addon : mSettings.addons) {
struct AddonInfo addonInfo;
const std::string &failedToGetAddonInfo = addonInfo.getAddonInfo(addon, mSettings.exename);
if (!failedToGetAddonInfo.empty()) {
reportOut(failedToGetAddonInfo, Color::FgRed);
mExitCode = 1;
continue;
}
// ensure all addons have already been resolved - TODO: remove when settings are const after creation
assert(mSettings.addonInfos.size() == mSettings.addons.size());

for (const AddonInfo &addonInfo : mSettings.addonInfos) {
if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
continue;

Expand Down
4 changes: 4 additions & 0 deletions lib/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define settingsH
//---------------------------------------------------------------------------

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

/** @brief the loaded addons infos */
std::vector<AddonInfo> addonInfos;

/** @brief Path to the python interpreter to be used to run addons. */
std::string addonPython;

Expand Down
1 change: 1 addition & 0 deletions releasenotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Other:
- "--showtime=summary" will now show a single summary at the end instead of showing it after each file when using the thread execution (default on Windows)
- added "--showtime=none" to disable any previously specified showtime report. "--showtime=" without parameter is no longer valid.
- Multiple "--project" options are now prohibited. These would have overlapped each other and leads to unexpected behavior. Please use separate runs with a single "--project" option instead.
- If a addon cannot be found it will bail out immediately instead of continously writing errors and failing the analysis at the end.
13 changes: 10 additions & 3 deletions test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,9 @@ def test_invalid_addon_json(tmpdir):
args = ['--addon={}'.format(addon_file), '--enable=all', test_file]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0 # TODO: needs to be 1
assert exitcode == 1
lines = stdout.splitlines()
assert lines == [
'Checking {} ...'.format(test_file),
'Loading {} failed. syntax error at line 2 near: '.format(addon_file),
'Loading {} failed. syntax error at line 2 near: '.format(addon_file)
]
assert stderr == ''
Expand Down Expand Up @@ -554,3 +552,12 @@ def test_showtime_top5_file(tmpdir):
assert lines[i].endswith(' - 1 result(s))')
assert lines[6].startswith('Overall time:')
assert stderr == ''


def test_missing_addon(tmpdir):
args = ['--addon=misra2', 'file.c']

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 1
assert stdout == 'Did not find addon misra2.py\n'
assert stderr == ""
11 changes: 11 additions & 0 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class TestCmdlineParser : public TestFixture {
TEST_CASE(projectMultiple);
TEST_CASE(projectEmpty);
TEST_CASE(projectMissing);
TEST_CASE(addon);

TEST_CASE(ignorepaths1);
TEST_CASE(ignorepaths2);
Expand Down Expand Up @@ -1981,6 +1982,16 @@ class TestCmdlineParser : public TestFixture {
ASSERT_EQUALS("cppcheck: error: failed to open project 'project.cppcheck'. The file does not exist.\n", logger->str());
}

void addon() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--addon=misra", "file.cpp"};
settings->addons.clear();
ASSERT(parser->parseFromArgs(3, argv));
ASSERT_EQUALS(1, settings->addons.size());
ASSERT_EQUALS("misra", *settings->addons.cbegin());
ASSERT_EQUALS("", GET_REDIRECT_OUTPUT);
}

void ignorepaths1() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};
Expand Down

0 comments on commit be1006b

Please sign in to comment.