Skip to content

Commit 4617bc2

Browse files
Fix #14064 Files are reanalyzed with --cppcheck-build-dir and inline suppressions (regression) (danmar#7741)
1 parent 8d388dc commit 4617bc2

File tree

9 files changed

+50
-8
lines changed

9 files changed

+50
-8
lines changed

lib/cppcheck.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ static simplecpp::TokenList createTokenList(const std::string& filename, std::ve
859859
return {filename, files, outputList};
860860
}
861861

862-
std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simplecpp::TokenList& tokens) const
862+
std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simplecpp::TokenList& tokens, const std::string& filePath) const
863863
{
864864
std::ostringstream toolinfo;
865865
toolinfo << (mSettings.cppcheckCfgProductName.empty() ? CPPCHECK_VERSION_STRING : mSettings.cppcheckCfgProductName);
@@ -876,7 +876,7 @@ std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simp
876876
}
877877
toolinfo << mSettings.premiumArgs;
878878
// TODO: do we need to add more options?
879-
mSuppressions.nomsg.dump(toolinfo);
879+
mSuppressions.nomsg.dump(toolinfo, filePath);
880880
return preprocessor.calculateHash(tokens, toolinfo.str());
881881
}
882882

@@ -1029,7 +1029,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
10291029

10301030
if (analyzerInformation) {
10311031
// Calculate hash so it can be compared with old hash / future hashes
1032-
const std::size_t hash = calculateHash(preprocessor, tokens1);
1032+
const std::size_t hash = calculateHash(preprocessor, tokens1, file.spath());
10331033
std::list<ErrorMessage> errors;
10341034
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, fileIndex, hash, errors)) {
10351035
while (!errors.empty()) {

lib/cppcheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class CPPCHECKLIB CppCheck {
172172
* @param tokens Token list from preprocessed file.
173173
* @return hash
174174
*/
175-
std::size_t calculateHash(const Preprocessor &preprocessor, const simplecpp::TokenList &tokens) const;
175+
std::size_t calculateHash(const Preprocessor &preprocessor, const simplecpp::TokenList &tokens, const std::string& filePath = {}) const;
176176

177177
/**
178178
* @brief Check a file using stream

lib/suppressions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,14 @@ bool SuppressionList::isSuppressed(const ::ErrorMessage &errmsg, const std::set<
512512
return isSuppressed(SuppressionList::ErrorMessage::fromErrorMessage(errmsg, macroNames));
513513
}
514514

515-
void SuppressionList::dump(std::ostream & out) const
515+
void SuppressionList::dump(std::ostream & out, const std::string& filePath) const
516516
{
517517
std::lock_guard<std::mutex> lg(mSuppressionsSync);
518518

519519
out << " <suppressions>" << std::endl;
520520
for (const Suppression &suppression : mSuppressions) {
521+
if (suppression.isInline && !suppression.fileName.empty() && !filePath.empty() && filePath != suppression.fileName)
522+
continue;
521523
out << " <suppression";
522524
out << " errorId=\"" << ErrorLogger::toxml(suppression.errorId) << '"';
523525
if (!suppression.fileName.empty())

lib/suppressions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class CPPCHECKLIB SuppressionList {
252252
* @brief Create an xml dump of suppressions
253253
* @param out stream to write XML to
254254
*/
255-
void dump(std::ostream &out) const;
255+
void dump(std::ostream &out, const std::string& filePath = {}) const;
256256

257257
/**
258258
* @brief Returns list of unmatched local (per-file) suppressions.

test/cli/inline-suppress_test.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

2-
# python -m pytest test-inline-suppress.py
2+
# python -m pytest inline-suppress_test.py
33

44
import json
55
import os
66
import pytest
77
import sys
8+
import time
89
from testutils import cppcheck
910

1011
__script_dir = os.path.dirname(os.path.abspath(__file__))
@@ -247,6 +248,35 @@ def test_build_dir(tmpdir):
247248
assert stdout == ''
248249
assert ret == 0, stdout
249250

251+
def test_build_dir_jobs_suppressions(tmpdir): #14064
252+
args = [
253+
'-q',
254+
'--template=simple',
255+
'--cppcheck-build-dir={}'.format(tmpdir),
256+
'--enable=style',
257+
'--inline-suppr',
258+
'-j4',
259+
'reanalysis'
260+
]
261+
262+
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
263+
lines = stderr.splitlines()
264+
assert lines == []
265+
assert stdout == ''
266+
assert ret == 0, stdout
267+
268+
a1Path = os.path.join(tmpdir, 'd.a1')
269+
assert os.path.exists(a1Path)
270+
mtimeOld = os.path.getmtime(a1Path)
271+
272+
time.sleep(1)
273+
274+
for _ in range(1, 10):
275+
cppcheck(args, cwd=__script_dir)
276+
277+
mtimeNew = os.path.getmtime(a1Path)
278+
assert mtimeOld == mtimeNew
279+
250280

251281
def __test_build_dir_unused_template(tmpdir, extra_args):
252282
args = [
@@ -470,4 +500,4 @@ def test_unmatched_cfg():
470500
'{}cfg.c:9:0: information: Unmatched suppression: id [unmatchedSuppression]'.format(__proj_inline_suppres_path),
471501
]
472502
assert stdout == ''
473-
assert ret == 0, stdout
503+
assert ret == 0, stdout

test/cli/reanalysis/a.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
int i; // cppcheck-suppress unusedVariable
3+
}
4+

test/cli/reanalysis/b.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int main() {}
2+

test/cli/reanalysis/c.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int main() {}
2+

test/cli/reanalysis/d.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int main() {}
2+

0 commit comments

Comments
 (0)