more cleanups in handling of ignored files (#5767)
This commit is contained in:
parent
ef27c29f27
commit
f2622a673f
|
@ -198,8 +198,6 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
|
||||||
assert(!(!pathnamesRef.empty() && !fileSettingsRef.empty()));
|
assert(!(!pathnamesRef.empty() && !fileSettingsRef.empty()));
|
||||||
|
|
||||||
if (!fileSettingsRef.empty()) {
|
if (!fileSettingsRef.empty()) {
|
||||||
// TODO: handle ignored?
|
|
||||||
|
|
||||||
// TODO: de-duplicate
|
// TODO: de-duplicate
|
||||||
|
|
||||||
std::list<FileSettings> fileSettings;
|
std::list<FileSettings> fileSettings;
|
||||||
|
@ -1258,8 +1256,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
|
||||||
substituteTemplateFormatStatic(mSettings.templateFormat);
|
substituteTemplateFormatStatic(mSettings.templateFormat);
|
||||||
substituteTemplateLocationStatic(mSettings.templateLocation);
|
substituteTemplateLocationStatic(mSettings.templateLocation);
|
||||||
|
|
||||||
project.ignorePaths(mIgnoredPaths);
|
|
||||||
|
|
||||||
if (mSettings.force || maxconfigs)
|
if (mSettings.force || maxconfigs)
|
||||||
mSettings.checkAllConfigurations = true;
|
mSettings.checkAllConfigurations = true;
|
||||||
|
|
||||||
|
@ -1270,6 +1266,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
|
||||||
mSettings.maxConfigs = 1U;
|
mSettings.maxConfigs = 1U;
|
||||||
|
|
||||||
if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1 && mSettings.buildDir.empty()) {
|
if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1 && mSettings.buildDir.empty()) {
|
||||||
|
// TODO: bail out
|
||||||
mLogger.printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
|
mLogger.printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1280,6 +1277,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
|
||||||
|
|
||||||
// Print error only if we have "real" command and expect files
|
// Print error only if we have "real" command and expect files
|
||||||
if (mPathNames.empty() && project.guiProject.pathNames.empty() && project.fileSettings.empty()) {
|
if (mPathNames.empty() && project.guiProject.pathNames.empty() && project.fileSettings.empty()) {
|
||||||
|
// TODO: this message differs from the one reported in fillSettingsFromArgs()
|
||||||
mLogger.printError("no C or C++ source files found.");
|
mLogger.printError("no C or C++ source files found.");
|
||||||
return Result::Fail;
|
return Result::Fail;
|
||||||
}
|
}
|
||||||
|
@ -1287,8 +1285,15 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
|
||||||
if (!project.guiProject.pathNames.empty())
|
if (!project.guiProject.pathNames.empty())
|
||||||
mPathNames = project.guiProject.pathNames;
|
mPathNames = project.guiProject.pathNames;
|
||||||
|
|
||||||
if (!project.fileSettings.empty())
|
if (!project.fileSettings.empty()) {
|
||||||
|
project.ignorePaths(mIgnoredPaths);
|
||||||
|
if (project.fileSettings.empty()) {
|
||||||
|
mLogger.printError("no C or C++ source files found.");
|
||||||
|
mLogger.printMessage("all paths were ignored"); // TODO: log this differently?
|
||||||
|
return Result::Fail;
|
||||||
|
}
|
||||||
mFileSettings = project.fileSettings;
|
mFileSettings = project.fileSettings;
|
||||||
|
}
|
||||||
|
|
||||||
// Use paths _pathnames if no base paths for relative path output are given
|
// Use paths _pathnames if no base paths for relative path output are given
|
||||||
if (mSettings.basePaths.empty() && mSettings.relativePaths)
|
if (mSettings.basePaths.empty() && mSettings.relativePaths)
|
||||||
|
|
|
@ -427,7 +427,7 @@ unsigned int CppCheck::check(const std::string &path)
|
||||||
{
|
{
|
||||||
if (mSettings.clang) {
|
if (mSettings.clang) {
|
||||||
if (!mSettings.quiet)
|
if (!mSettings.quiet)
|
||||||
mErrorLogger.reportOut(std::string("Checking ") + path + "...", Color::FgGreen);
|
mErrorLogger.reportOut(std::string("Checking ") + path + " ...", Color::FgGreen);
|
||||||
|
|
||||||
const std::string lang = Path::isCPP(path) ? "-x c++" : "-x c";
|
const std::string lang = Path::isCPP(path) ? "-x c++" : "-x c";
|
||||||
const std::string analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, emptyString);
|
const std::string analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, emptyString);
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
|
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|
||||||
|
// TODO: align the exclusion logic with PathMatch
|
||||||
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
|
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
|
||||||
{
|
{
|
||||||
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
|
||||||
|
|
|
@ -38,6 +38,7 @@ bool PathMatch::match(const std::string &path) const
|
||||||
if (path.empty())
|
if (path.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// TODO: align the exclusion logic with ImportProject::ignorePaths()
|
||||||
for (std::vector<std::string>::const_iterator i = mExcludedPaths.cbegin(); i != mExcludedPaths.cend(); ++i) {
|
for (std::vector<std::string>::const_iterator i = mExcludedPaths.cbegin(); i != mExcludedPaths.cend(); ++i) {
|
||||||
const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, mWorkingDirectory) : *i);
|
const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, mWorkingDirectory) : *i);
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import pytest
|
import pytest
|
||||||
from testutils import cppcheck
|
from testutils import cppcheck, assert_cppcheck
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.call(['clang', '--version'])
|
subprocess.call(['clang', '--version'])
|
||||||
|
@ -122,3 +122,14 @@ def test_ast_control_flow():
|
||||||
def test_ast():
|
def test_ast():
|
||||||
check_ast('struct S { int x; }; S* foo() { return new S(); }')
|
check_ast('struct S { int x; }; S* foo() { return new S(); }')
|
||||||
|
|
||||||
|
def test_log(tmpdir):
|
||||||
|
test_file = os.path.join(tmpdir, 'test.cpp')
|
||||||
|
with open(test_file, 'wt'):
|
||||||
|
pass
|
||||||
|
|
||||||
|
args = ['--clang', test_file]
|
||||||
|
out_lines = [
|
||||||
|
'Checking {} ...'.format(test_file),
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)
|
|
@ -184,7 +184,11 @@ def test_exclude():
|
||||||
prjpath = getRelativeProjectPath()
|
prjpath = getRelativeProjectPath()
|
||||||
ret, stdout, _ = cppcheck(['-i' + prjpath, '--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
|
ret, stdout, _ = cppcheck(['-i' + prjpath, '--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert stdout == 'cppcheck: error: no C or C++ source files found.\n'
|
lines = stdout.splitlines()
|
||||||
|
assert lines == [
|
||||||
|
'cppcheck: error: no C or C++ source files found.',
|
||||||
|
'cppcheck: all paths were ignored'
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_build_dir_dump_output():
|
def test_build_dir_dump_output():
|
||||||
|
|
|
@ -157,7 +157,7 @@ def test_project_empty_fields(tmpdir):
|
||||||
</paths>
|
</paths>
|
||||||
<exclude/>
|
<exclude/>
|
||||||
<exclude>
|
<exclude>
|
||||||
<paths/>
|
<path/>
|
||||||
</exclude>
|
</exclude>
|
||||||
<function-contracts/>
|
<function-contracts/>
|
||||||
<variable-contracts/>
|
<variable-contracts/>
|
||||||
|
@ -531,4 +531,109 @@ def test_project_file_ignore(tmpdir):
|
||||||
'cppcheck: Maybe all paths were ignored?'
|
'cppcheck: Maybe all paths were ignored?'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_file_ignore_2(tmpdir):
|
||||||
|
test_file = os.path.join(tmpdir, 'test.cpp')
|
||||||
|
with open(test_file, 'wt') as f:
|
||||||
|
pass
|
||||||
|
|
||||||
|
project_file = os.path.join(tmpdir, 'test.cppcheck')
|
||||||
|
with open(project_file, 'wt') as f:
|
||||||
|
f.write(
|
||||||
|
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project>
|
||||||
|
<paths>
|
||||||
|
<dir name="{}"/>
|
||||||
|
</paths>
|
||||||
|
<exclude>
|
||||||
|
<path name="test.cpp"/>
|
||||||
|
</exclude>
|
||||||
|
</project>""".format(test_file))
|
||||||
|
|
||||||
|
args = ['--project={}'.format(project_file)]
|
||||||
|
out_lines = [
|
||||||
|
'cppcheck: error: could not find or open any of the paths given.',
|
||||||
|
'cppcheck: Maybe all paths were ignored?'
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_file_ignore_3(tmpdir):
|
||||||
|
test_file = os.path.join(tmpdir, 'test.cpp')
|
||||||
|
with open(test_file, 'wt') as f:
|
||||||
|
pass
|
||||||
|
|
||||||
|
project_file = os.path.join(tmpdir, 'test.cppcheck')
|
||||||
|
with open(project_file, 'wt') as f:
|
||||||
|
f.write(
|
||||||
|
"""<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project>
|
||||||
|
<paths>
|
||||||
|
<dir name="{}"/>
|
||||||
|
</paths>
|
||||||
|
<ignore>
|
||||||
|
<path name="test.cpp"/>
|
||||||
|
</ignore>
|
||||||
|
</project>""".format(test_file))
|
||||||
|
|
||||||
|
args = ['--project={}'.format(project_file)]
|
||||||
|
out_lines = [
|
||||||
|
'cppcheck: error: could not find or open any of the paths given.',
|
||||||
|
'cppcheck: Maybe all paths were ignored?'
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.xfail
|
||||||
|
def test_json_file_ignore(tmpdir):
|
||||||
|
test_file = os.path.join(tmpdir, 'test.cpp')
|
||||||
|
with open(test_file, 'wt') as f:
|
||||||
|
pass
|
||||||
|
|
||||||
|
compilation_db = [
|
||||||
|
{"directory": str(tmpdir),
|
||||||
|
"command": "c++ -o bug1.o -c bug1.cpp",
|
||||||
|
"file": "test.cpp",
|
||||||
|
"output": "test.o"}
|
||||||
|
]
|
||||||
|
|
||||||
|
project_file = os.path.join(tmpdir, 'test.json')
|
||||||
|
with open(project_file, 'wt') as f:
|
||||||
|
f.write(json.dumps(compilation_db))
|
||||||
|
|
||||||
|
args = ['-itest.cpp', '--project={}'.format(project_file)]
|
||||||
|
out_lines = [
|
||||||
|
'cppcheck: error: no C or C++ source files found.',
|
||||||
|
'cppcheck: all paths were ignored'
|
||||||
|
]
|
||||||
|
|
||||||
|
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_file_ignore_2(tmpdir):
|
||||||
|
test_file = os.path.join(tmpdir, 'test.cpp')
|
||||||
|
with open(test_file, 'wt') as f:
|
||||||
|
pass
|
||||||
|
|
||||||
|
compilation_db = [
|
||||||
|
{"directory": str(tmpdir),
|
||||||
|
"command": "c++ -o bug1.o -c bug1.cpp",
|
||||||
|
"file": "test.cpp",
|
||||||
|
"output": "test.o"}
|
||||||
|
]
|
||||||
|
|
||||||
|
project_file = os.path.join(tmpdir, 'test.json')
|
||||||
|
with open(project_file, 'wt') as f:
|
||||||
|
f.write(json.dumps(compilation_db))
|
||||||
|
|
||||||
|
args = ['-i{}'.format(test_file), '--project={}'.format(project_file)]
|
||||||
|
out_lines = [
|
||||||
|
'cppcheck: error: no C or C++ source files found.',
|
||||||
|
'cppcheck: all paths were ignored'
|
||||||
|
]
|
||||||
|
|
||||||
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
|
Loading…
Reference in New Issue