Skip to content

Commit 2c3b42f

Browse files
committed
cleaned up and test some enforcedLang handling [skip ci]
1 parent 5a9d0b3 commit 2c3b42f

5 files changed

Lines changed: 74 additions & 18 deletions

File tree

gui/checkthread.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ void CheckThread::run()
140140
qDebug() << "Whole program analysis";
141141
std::list<FileWithDetails> files2;
142142
std::transform(mFiles.cbegin(), mFiles.cend(), std::back_inserter(files2), [&](const QString& file) {
143+
// TODO: apply enforcedLanguage
143144
return FileWithDetails{file.toStdString(), Path::identify(file.toStdString(), mSettings.cppHeaderProbe), 0};
144145
});
145146
cppcheck.analyseWholeProgram(mSettings.buildDir, files2, {}, ctuInfo);
@@ -151,6 +152,7 @@ void CheckThread::run()
151152
QString file = mResult.getNextFile();
152153
while (!file.isEmpty() && mState == Running) {
153154
qDebug() << "Checking file" << file;
155+
// TODO: apply enforcedLanguage
154156
cppcheck.check(FileWithDetails(file.toStdString()));
155157
runAddonsAndTools(mSettings, nullptr, file);
156158
emit fileChecked(file);

gui/mainwindow.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ void MainWindow::analyzeCode(const QString& code, const QString& filename)
702702
checkLockDownUI();
703703
clearResults();
704704
mUI->mResults->checkingStarted(1);
705+
// TODO: apply enforcedLanguage
705706
cppcheck.check(FileWithDetails(filename.toStdString()), code.toStdString());
706707
analysisDone();
707708

lib/cppcheck.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,20 @@ static void createDumpFile(const Settings& settings,
366366
std::ofstream fout(getCtuInfoFileName(dumpFile));
367367
}
368368

369-
// TODO: enforcedLang should be already applied in FileWithDetails object
370369
std::string language;
371-
switch (settings.enforcedLang) {
370+
371+
assert(file.lang() != Standards::Language::None);
372+
373+
switch (file.lang()) {
372374
case Standards::Language::C:
373375
language = " language=\"c\"";
374376
break;
375377
case Standards::Language::CPP:
376378
language = " language=\"cpp\"";
377379
break;
378380
case Standards::Language::None:
379-
{
380-
// TODO: get language from FileWithDetails object
381-
// TODO: error out on unknown language?
382-
const Standards::Language lang = Path::identify(file.spath(), settings.cppHeaderProbe);
383-
if (lang == Standards::Language::CPP)
384-
language = " language=\"cpp\"";
385-
else if (lang == Standards::Language::C)
386-
language = " language=\"c\"";
387381
break;
388382
}
389-
}
390383

391384
fdump << "<?xml version=\"1.0\"?>\n";
392385
fdump << "<dumps" << language << ">\n";
@@ -619,13 +612,12 @@ std::string CppCheck::getLibraryDumpData() const {
619612
return out;
620613
}
621614

622-
std::string CppCheck::getClangFlags(Standards::Language fileLang) const {
615+
std::string CppCheck::getClangFlags(Standards::Language lang) const {
623616
std::string flags;
624617

625-
const Standards::Language lang = mSettings.enforcedLang != Standards::None ? mSettings.enforcedLang : fileLang;
618+
assert(lang != Standards::Language::None);
626619

627620
switch (lang) {
628-
case Standards::Language::None:
629621
case Standards::Language::C:
630622
flags = "-x c ";
631623
if (!mSettings.standards.stdValueC.empty())
@@ -636,6 +628,8 @@ std::string CppCheck::getClangFlags(Standards::Language fileLang) const {
636628
if (!mSettings.standards.stdValueCPP.empty())
637629
flags += "-std=" + mSettings.standards.stdValueCPP + " ";
638630
break;
631+
case Standards::Language::None:
632+
break;
639633
}
640634

641635
for (const std::string &i: mSettings.includePaths)
@@ -674,7 +668,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
674668
#endif
675669

676670
const std::string args2 = "-fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " +
677-
getClangFlags(Path::identify(file.spath(), mSettings.cppHeaderProbe)) +
671+
getClangFlags(file.lang()) +
678672
file.spath();
679673
const std::string redirect2 = clangStderr.empty() ? "2>&1" : ("2> " + clangStderr);
680674
if (mSettings.verbose && !mSettings.quiet) {

lib/cppcheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ class CPPCHECKLIB CppCheck {
153153

154154
/**
155155
* @brief Get the clang command line flags using the Settings
156-
* @param fileLang language guessed from filename
156+
* @param lang language guessed from filename
157157
* @return Clang command line flags
158158
*/
159-
std::string getClangFlags(Standards::Language fileLang) const;
159+
std::string getClangFlags(Standards::Language lang) const;
160160

161161
private:
162162
#ifdef HAVE_RULES

test/cli/dumpfile_test.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
21
# python -m pytest dumpfile_test.py
32

43
import os
4+
import pathlib
55

66
from testutils import cppcheck
7+
import xml.etree.ElementTree as ET
78

89

910
def test_libraries(tmpdir): #13701
@@ -20,3 +21,61 @@ def test_libraries(tmpdir): #13701
2021
dump = f.read()
2122
assert '<library lib="posix"/>' in dump
2223
assert dump.find('<library lib="posix"/>') < dump.find('<dump cfg=')
24+
25+
26+
def __test_language(tmp_path, file_ext, exp_lang, force_lang=None):
27+
test_file = tmp_path / ('test.' + file_ext)
28+
with open(test_file, 'wt'):
29+
pass
30+
31+
args = [
32+
'--dump',
33+
str(test_file)
34+
]
35+
if force_lang:
36+
args += ['--language=' + force_lang]
37+
38+
exitcode, stdout, stderr = cppcheck(args)
39+
assert exitcode == 0, stdout if stdout else stderr
40+
41+
dump_s = pathlib.Path(str(test_file) + '.dump').read_text()
42+
43+
dump_xml = ET.fromstring(dump_s)
44+
assert 'language' in dump_xml.attrib
45+
assert dump_xml.attrib['language'] == exp_lang
46+
47+
48+
def test_language_c(tmp_path):
49+
__test_language(tmp_path, 'c', exp_lang='c')
50+
51+
52+
def test_language_c_force_c(tmp_path):
53+
__test_language(tmp_path, 'c', force_lang='c', exp_lang='c')
54+
55+
56+
def test_language_c_force_cpp(tmp_path):
57+
__test_language(tmp_path, 'c', force_lang='c++', exp_lang='cpp')
58+
59+
60+
def test_language_cpp(tmp_path):
61+
__test_language(tmp_path, 'cpp', exp_lang='cpp')
62+
63+
64+
def test_language_cpp_force_cpp(tmp_path):
65+
__test_language(tmp_path, 'cpp', force_lang='c++', exp_lang='cpp')
66+
67+
68+
def test_language_cpp_force_c(tmp_path):
69+
__test_language(tmp_path, 'cpp', force_lang='c', exp_lang='c')
70+
71+
72+
def test_language_h(tmp_path):
73+
__test_language(tmp_path, 'h', exp_lang='c')
74+
75+
76+
def test_language_h_force_c(tmp_path):
77+
__test_language(tmp_path, 'h', force_lang='c', exp_lang='c')
78+
79+
80+
def test_language_h_force_cpp(tmp_path):
81+
__test_language(tmp_path, 'h', force_lang='c++', exp_lang='cpp')

0 commit comments

Comments
 (0)