Skip to content

Commit 3253e48

Browse files
authored
cleaned up and test some enforcedLang handling (#7405)
1 parent 096a016 commit 3253e48

File tree

4 files changed

+85
-19
lines changed

4 files changed

+85
-19
lines changed

lib/cppcheck.cpp

+9-15
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

+2-2
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/clang-import_test.py

-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ def test_cmd_cpp(tmp_path):
174174

175175

176176
# files with unknown extensions are treated as C++
177-
@pytest.mark.xfail(strict=True)
178177
def test_cmd_unk(tmp_path):
179178
__test_cmd(tmp_path, 'test.cplusplus', [], '-x c++')
180179

test/cli/dumpfile_test.py

+74-1
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,75 @@ 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+
# headers default to C
73+
def test_language_h(tmp_path):
74+
__test_language(tmp_path, 'h', exp_lang='c')
75+
76+
77+
def test_language_h_force_c(tmp_path):
78+
__test_language(tmp_path, 'h', force_lang='c', exp_lang='c')
79+
80+
81+
def test_language_h_force_cpp(tmp_path):
82+
__test_language(tmp_path, 'h', force_lang='c++', exp_lang='cpp')
83+
84+
85+
# files with unknown extensions default to C++
86+
def test_language_unk(tmp_path):
87+
__test_language(tmp_path, 'src', exp_lang='cpp')
88+
89+
90+
def test_language_unk_force_c(tmp_path):
91+
__test_language(tmp_path, 'src', force_lang='c', exp_lang='c')
92+
93+
94+
def test_language_unk_force_cpp(tmp_path):
95+
__test_language(tmp_path, 'src', force_lang='c++', exp_lang='cpp')

0 commit comments

Comments
 (0)