Fix #12144 (Error messages from addons has the wrong file0) (#5621)

This commit is contained in:
Daniel Marjamäki 2023-11-04 17:07:30 +01:00 committed by GitHub
parent 72a36172aa
commit 0dc53ac7fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -522,7 +522,7 @@ unsigned int CppCheck::check(const std::string &path)
}
// run addons
executeAddons(dumpFile);
executeAddons(dumpFile, path);
} catch (const InternalError &e) {
const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, path, "Bailing out from analysis: Processing Clang AST dump failed");
@ -967,7 +967,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
fdump.close();
}
executeAddons(dumpFile);
executeAddons(dumpFile, Path::simplifyPath(filename));
} catch (const TerminateException &) {
// Analysis is terminated
@ -1371,15 +1371,15 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token
}
#endif
void CppCheck::executeAddons(const std::string& dumpFile)
void CppCheck::executeAddons(const std::string& dumpFile, const std::string& file0)
{
if (!dumpFile.empty()) {
std::vector<std::string> f{dumpFile};
executeAddons(f);
executeAddons(f, file0);
}
}
void CppCheck::executeAddons(const std::vector<std::string>& files)
void CppCheck::executeAddons(const std::vector<std::string>& files, const std::string& file0)
{
if (mSettings.addons.empty() || files.empty())
return;
@ -1444,7 +1444,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
}
else if (!mSettings.severity.isEnabled(errmsg.severity))
continue;
errmsg.file0 = ((files.size() == 1) ? files[0] : "");
errmsg.file0 = file0;
reportErr(errmsg);
}
@ -1463,7 +1463,7 @@ void CppCheck::executeAddonsWholeProgram(const std::map<std::string, std::size_t
}
try {
executeAddons(ctuInfoFiles);
executeAddons(ctuInfoFiles, "");
} catch (const InternalError& e) {
const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, "", "Bailing out from analysis: Whole program analysis failed");
reportErr(errmsg);

View File

@ -182,8 +182,8 @@ private:
/**
* Execute addons
*/
void executeAddons(const std::vector<std::string>& files);
void executeAddons(const std::string &dumpFile);
void executeAddons(const std::vector<std::string>& files, const std::string& file0);
void executeAddons(const std::string &dumpFile, const std::string& file0);
/**
* Execute addons

View File

@ -211,6 +211,18 @@ def test_execute_addon_failure_2(tmpdir):
assert stderr == "{}:0:0: error: Bailing out from analysis: Checking file failed: Failed to execute addon 'naming' - exitcode is {} [internalError]\n\n^\n".format(test_file, ec)
def test_execute_addon_file0(tmpdir):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write('void foo() {}\n')
args = ['--xml', '--addon=misra', '--enable=style', test_file]
_, _, stderr = cppcheck(args)
assert 'misra-c2012-8.2' in stderr
assert '.dump' not in stderr
# TODO: find a test case which always fails
@pytest.mark.skip
def test_internal_error(tmpdir):