From f2719b3f5a380d4b3639b26b98aedebe36c96ac5 Mon Sep 17 00:00:00 2001 From: firewave Date: Thu, 23 May 2024 13:20:39 +0200 Subject: [PATCH] do not assert in markup processing for `unusedFunction` when a language is enforced --- lib/cppcheck.cpp | 3 ++- lib/tokenlist.cpp | 7 +++++-- lib/tokenlist.h | 3 ++- test/cli/other_test.py | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 60eb88951d2..7d3e3ea8e51 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -643,7 +643,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (mUnusedFunctionsCheck && mSettings.useSingleJob() && mSettings.buildDir.empty()) { // 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. Tokenizer tokenizer(mSettings, *this); - tokenizer.list.setLang(Standards::Language::C); + // enforce the language since markup files are special and do not adhere to the enforced language + tokenizer.list.setLang(Standards::Language::C, true); if (fileStream) { tokenizer.list.createTokens(*fileStream, filename); } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index fae6e123fbf..98511b08fd2 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -2190,10 +2190,13 @@ bool TokenList::isCPP() const return mLang == Standards::Language::CPP; } -void TokenList::setLang(Standards::Language lang) +void TokenList::setLang(Standards::Language lang, bool force) { ASSERT_LANG(lang != Standards::Language::None); - ASSERT_LANG(mLang == Standards::Language::None); + if (!force) + { + ASSERT_LANG(mLang == Standards::Language::None); + } mLang = lang; } diff --git a/lib/tokenlist.h b/lib/tokenlist.h index c03cf3b6382..66bb008b783 100644 --- a/lib/tokenlist.h +++ b/lib/tokenlist.h @@ -68,7 +68,8 @@ class CPPCHECKLIB TokenList { /** @return true if the code is C++ */ bool isCPP() const; - void setLang(Standards::Language lang); + // TODO: get rid of this + void setLang(Standards::Language lang, bool force = false); /** * Delete all tokens in given token list diff --git a/test/cli/other_test.py b/test/cli/other_test.py index ec3d15c7c68..bad4a7bdf7c 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -1428,3 +1428,25 @@ def test_filelist(tmpdir): for i in range(1, len(expected)+1): lines.remove('{}/11 files checked 0% done'.format(i, len(expected))) assert lines == expected + + +def test_markup_lang(tmpdir): + test_file_1 = os.path.join(tmpdir, 'test_1.qml') + with open(test_file_1, 'wt') as f: + pass + test_file_2 = os.path.join(tmpdir, 'test_2.cpp') + with open(test_file_2, 'wt') as f: + pass + + # do not assert processing markup file with enforced language + args = [ + '--library=qt', + '--enable=unusedFunction', + '--language=c++', + '-j1', + test_file_1, + test_file_2 + ] + + exitcode, stdout, _ = cppcheck(args) + assert exitcode == 0, stdout