Skip to content

Commit 8cabf85

Browse files
danmarfirewave
andauthored
fixed #12763 - do not assert in markup processing for unusedFunction when a language is enforced (#6447)
cherry pick from 9c21862 Co-authored-by: Oliver Stöneberg <[email protected]>
1 parent c3d2d80 commit 8cabf85

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib/cppcheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
643643
if (mUnusedFunctionsCheck && mSettings.useSingleJob() && mSettings.buildDir.empty()) {
644644
// this is not a real source file - we just want to tokenize it. treat it as C anyways as the language needs to be determined.
645645
Tokenizer tokenizer(mSettings, *this);
646-
tokenizer.list.setLang(Standards::Language::C);
646+
// enforce the language since markup files are special and do not adhere to the enforced language
647+
tokenizer.list.setLang(Standards::Language::C, true);
647648
if (fileStream) {
648649
tokenizer.list.createTokens(*fileStream, filename);
649650
}

lib/tokenlist.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,10 +2181,13 @@ bool TokenList::isCPP() const
21812181
return mLang == Standards::Language::CPP;
21822182
}
21832183

2184-
void TokenList::setLang(Standards::Language lang)
2184+
void TokenList::setLang(Standards::Language lang, bool force)
21852185
{
21862186
ASSERT_LANG(lang != Standards::Language::None);
2187-
ASSERT_LANG(mLang == Standards::Language::None);
2187+
if (!force)
2188+
{
2189+
ASSERT_LANG(mLang == Standards::Language::None);
2190+
}
21882191

21892192
mLang = lang;
21902193
}

lib/tokenlist.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class CPPCHECKLIB TokenList {
6868
/** @return true if the code is C++ */
6969
bool isCPP() const;
7070

71-
void setLang(Standards::Language lang);
71+
// TODO: get rid of this
72+
void setLang(Standards::Language lang, bool force = false);
7273

7374
/**
7475
* Delete all tokens in given token list

test/cli/other_test.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,4 +1357,26 @@ def test_rule(tmpdir):
13571357
lines = stderr.splitlines()
13581358
assert lines == [
13591359
"{}:4:0: style: found 'f' [rule]".format(test_file)
1360-
]
1360+
]
1361+
1362+
1363+
def test_markup_lang(tmpdir):
1364+
test_file_1 = os.path.join(tmpdir, 'test_1.qml')
1365+
with open(test_file_1, 'wt') as f:
1366+
pass
1367+
test_file_2 = os.path.join(tmpdir, 'test_2.cpp')
1368+
with open(test_file_2, 'wt') as f:
1369+
pass
1370+
1371+
# do not assert processing markup file with enforced language
1372+
args = [
1373+
'--library=qt',
1374+
'--enable=unusedFunction',
1375+
'--language=c++',
1376+
'-j1',
1377+
test_file_1,
1378+
test_file_2
1379+
]
1380+
1381+
exitcode, stdout, _ = cppcheck(args)
1382+
assert exitcode == 0, stdout

0 commit comments

Comments
 (0)