Skip to content

Commit 0dc53ac

Browse files
authored
Fix #12144 (Error messages from addons has the wrong file0) (#5621)
1 parent 72a3617 commit 0dc53ac

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

lib/cppcheck.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ unsigned int CppCheck::check(const std::string &path)
522522
}
523523

524524
// run addons
525-
executeAddons(dumpFile);
525+
executeAddons(dumpFile, path);
526526

527527
} catch (const InternalError &e) {
528528
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
967967
fdump.close();
968968
}
969969

970-
executeAddons(dumpFile);
970+
executeAddons(dumpFile, Path::simplifyPath(filename));
971971

972972
} catch (const TerminateException &) {
973973
// Analysis is terminated
@@ -1371,15 +1371,15 @@ void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &token
13711371
}
13721372
#endif
13731373

1374-
void CppCheck::executeAddons(const std::string& dumpFile)
1374+
void CppCheck::executeAddons(const std::string& dumpFile, const std::string& file0)
13751375
{
13761376
if (!dumpFile.empty()) {
13771377
std::vector<std::string> f{dumpFile};
1378-
executeAddons(f);
1378+
executeAddons(f, file0);
13791379
}
13801380
}
13811381

1382-
void CppCheck::executeAddons(const std::vector<std::string>& files)
1382+
void CppCheck::executeAddons(const std::vector<std::string>& files, const std::string& file0)
13831383
{
13841384
if (mSettings.addons.empty() || files.empty())
13851385
return;
@@ -1444,7 +1444,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
14441444
}
14451445
else if (!mSettings.severity.isEnabled(errmsg.severity))
14461446
continue;
1447-
errmsg.file0 = ((files.size() == 1) ? files[0] : "");
1447+
errmsg.file0 = file0;
14481448

14491449
reportErr(errmsg);
14501450
}
@@ -1463,7 +1463,7 @@ void CppCheck::executeAddonsWholeProgram(const std::map<std::string, std::size_t
14631463
}
14641464

14651465
try {
1466-
executeAddons(ctuInfoFiles);
1466+
executeAddons(ctuInfoFiles, "");
14671467
} catch (const InternalError& e) {
14681468
const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, "", "Bailing out from analysis: Whole program analysis failed");
14691469
reportErr(errmsg);

lib/cppcheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ class CPPCHECKLIB CppCheck : ErrorLogger {
182182
/**
183183
* Execute addons
184184
*/
185-
void executeAddons(const std::vector<std::string>& files);
186-
void executeAddons(const std::string &dumpFile);
185+
void executeAddons(const std::vector<std::string>& files, const std::string& file0);
186+
void executeAddons(const std::string &dumpFile, const std::string& file0);
187187

188188
/**
189189
* Execute addons

test/cli/test-other.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ def test_execute_addon_failure_2(tmpdir):
211211
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)
212212

213213

214+
def test_execute_addon_file0(tmpdir):
215+
test_file = os.path.join(tmpdir, 'test.c')
216+
with open(test_file, 'wt') as f:
217+
f.write('void foo() {}\n')
218+
219+
args = ['--xml', '--addon=misra', '--enable=style', test_file]
220+
221+
_, _, stderr = cppcheck(args)
222+
assert 'misra-c2012-8.2' in stderr
223+
assert '.dump' not in stderr
224+
225+
214226
# TODO: find a test case which always fails
215227
@pytest.mark.skip
216228
def test_internal_error(tmpdir):

0 commit comments

Comments
 (0)