Skip to content

Commit

Permalink
fixed #12763 - do not assert in markup processing for `unusedFunction…
Browse files Browse the repository at this point in the history
…` when a language is enforced (#6425)
  • Loading branch information
firewave authored May 23, 2024
1 parent 2a1500f commit 9c21862
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 5 additions & 2 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion lib/tokenlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,3 +1465,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

1 comment on commit 9c21862

@danmar
Copy link
Owner

@danmar danmar commented on 9c21862 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't have a "markup" enum constant anyway. if we have a markup enum constant then we can skip certain steps for markup files for instance symboldatabase , valueflow , etc. or do you skip those steps in some other way? We also don't want to run the usual checkers on qml files as far as I know.

Spontanously it feels a bit weird to change the language for a markup file. I can see why --language will determine the language for a markup file. But at the same time.. a qml file does not have C or C++ code.. and if the user provides configuration that says qml is a markup extension then maybe we can respect that.

Please sign in to comment.