Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,27 +366,20 @@ static void createDumpFile(const Settings& settings,
std::ofstream fout(getCtuInfoFileName(dumpFile));
}

// TODO: enforcedLang should be already applied in FileWithDetails object
std::string language;
switch (settings.enforcedLang) {

assert(file.lang() != Standards::Language::None);

switch (file.lang()) {
case Standards::Language::C:
language = " language=\"c\"";
break;
case Standards::Language::CPP:
language = " language=\"cpp\"";
break;
case Standards::Language::None:
{
// TODO: get language from FileWithDetails object
// TODO: error out on unknown language?
const Standards::Language lang = Path::identify(file.spath(), settings.cppHeaderProbe);
if (lang == Standards::Language::CPP)
language = " language=\"cpp\"";
else if (lang == Standards::Language::C)
language = " language=\"c\"";
break;
}
}

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

std::string CppCheck::getClangFlags(Standards::Language fileLang) const {
std::string CppCheck::getClangFlags(Standards::Language lang) const {
std::string flags;

const Standards::Language lang = mSettings.enforcedLang != Standards::None ? mSettings.enforcedLang : fileLang;
assert(lang != Standards::Language::None);

switch (lang) {
case Standards::Language::None:
case Standards::Language::C:
flags = "-x c ";
if (!mSettings.standards.stdValueC.empty())
Expand All @@ -636,6 +628,8 @@ std::string CppCheck::getClangFlags(Standards::Language fileLang) const {
if (!mSettings.standards.stdValueCPP.empty())
flags += "-std=" + mSettings.standards.stdValueCPP + " ";
break;
case Standards::Language::None:
break;
}

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

const std::string args2 = "-fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " +
getClangFlags(Path::identify(file.spath(), mSettings.cppHeaderProbe)) +
getClangFlags(file.lang()) +
file.spath();
const std::string redirect2 = clangStderr.empty() ? "2>&1" : ("2> " + clangStderr);
if (mSettings.verbose && !mSettings.quiet) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ class CPPCHECKLIB CppCheck {

/**
* @brief Get the clang command line flags using the Settings
* @param fileLang language guessed from filename
* @param lang language guessed from filename
* @return Clang command line flags
*/
std::string getClangFlags(Standards::Language fileLang) const;
std::string getClangFlags(Standards::Language lang) const;

private:
#ifdef HAVE_RULES
Expand Down
1 change: 0 additions & 1 deletion test/cli/clang-import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ def test_cmd_cpp(tmp_path):


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

Expand Down
75 changes: 74 additions & 1 deletion test/cli/dumpfile_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

# python -m pytest dumpfile_test.py

import os
import pathlib

from testutils import cppcheck
import xml.etree.ElementTree as ET


def test_libraries(tmpdir): #13701
Expand All @@ -20,3 +21,75 @@ def test_libraries(tmpdir): #13701
dump = f.read()
assert '<library lib="posix"/>' in dump
assert dump.find('<library lib="posix"/>') < dump.find('<dump cfg=')


def __test_language(tmp_path, file_ext, exp_lang, force_lang=None):
test_file = tmp_path / ('test.' + file_ext)
with open(test_file, 'wt'):
pass

args = [
'--dump',
str(test_file)
]
if force_lang:
args += ['--language=' + force_lang]

exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stdout if stdout else stderr

dump_s = pathlib.Path(str(test_file) + '.dump').read_text()

dump_xml = ET.fromstring(dump_s)
assert 'language' in dump_xml.attrib
assert dump_xml.attrib['language'] == exp_lang


def test_language_c(tmp_path):
__test_language(tmp_path, 'c', exp_lang='c')


def test_language_c_force_c(tmp_path):
__test_language(tmp_path, 'c', force_lang='c', exp_lang='c')


def test_language_c_force_cpp(tmp_path):
__test_language(tmp_path, 'c', force_lang='c++', exp_lang='cpp')


def test_language_cpp(tmp_path):
__test_language(tmp_path, 'cpp', exp_lang='cpp')


def test_language_cpp_force_cpp(tmp_path):
__test_language(tmp_path, 'cpp', force_lang='c++', exp_lang='cpp')


def test_language_cpp_force_c(tmp_path):
__test_language(tmp_path, 'cpp', force_lang='c', exp_lang='c')


# headers default to C
def test_language_h(tmp_path):
__test_language(tmp_path, 'h', exp_lang='c')


def test_language_h_force_c(tmp_path):
__test_language(tmp_path, 'h', force_lang='c', exp_lang='c')


def test_language_h_force_cpp(tmp_path):
__test_language(tmp_path, 'h', force_lang='c++', exp_lang='cpp')


# files with unknown extensions default to C++
def test_language_unk(tmp_path):
__test_language(tmp_path, 'src', exp_lang='cpp')


def test_language_unk_force_c(tmp_path):
__test_language(tmp_path, 'src', force_lang='c', exp_lang='c')


def test_language_unk_force_cpp(tmp_path):
__test_language(tmp_path, 'src', force_lang='c++', exp_lang='cpp')
Loading