Skip to content

Commit f2622a6

Browse files
authored
more cleanups in handling of ignored files (#5767)
1 parent ef27c29 commit f2622a6

7 files changed

Lines changed: 136 additions & 9 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,6 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
198198
assert(!(!pathnamesRef.empty() && !fileSettingsRef.empty()));
199199

200200
if (!fileSettingsRef.empty()) {
201-
// TODO: handle ignored?
202-
203201
// TODO: de-duplicate
204202

205203
std::list<FileSettings> fileSettings;
@@ -1258,8 +1256,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12581256
substituteTemplateFormatStatic(mSettings.templateFormat);
12591257
substituteTemplateLocationStatic(mSettings.templateLocation);
12601258

1261-
project.ignorePaths(mIgnoredPaths);
1262-
12631259
if (mSettings.force || maxconfigs)
12641260
mSettings.checkAllConfigurations = true;
12651261

@@ -1270,6 +1266,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12701266
mSettings.maxConfigs = 1U;
12711267

12721268
if (mSettings.checks.isEnabled(Checks::unusedFunction) && mSettings.jobs > 1 && mSettings.buildDir.empty()) {
1269+
// TODO: bail out
12731270
mLogger.printMessage("unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.");
12741271
}
12751272

@@ -1280,15 +1277,23 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
12801277

12811278
// Print error only if we have "real" command and expect files
12821279
if (mPathNames.empty() && project.guiProject.pathNames.empty() && project.fileSettings.empty()) {
1280+
// TODO: this message differs from the one reported in fillSettingsFromArgs()
12831281
mLogger.printError("no C or C++ source files found.");
12841282
return Result::Fail;
12851283
}
12861284

12871285
if (!project.guiProject.pathNames.empty())
12881286
mPathNames = project.guiProject.pathNames;
12891287

1290-
if (!project.fileSettings.empty())
1288+
if (!project.fileSettings.empty()) {
1289+
project.ignorePaths(mIgnoredPaths);
1290+
if (project.fileSettings.empty()) {
1291+
mLogger.printError("no C or C++ source files found.");
1292+
mLogger.printMessage("all paths were ignored"); // TODO: log this differently?
1293+
return Result::Fail;
1294+
}
12911295
mFileSettings = project.fileSettings;
1296+
}
12921297

12931298
// Use paths _pathnames if no base paths for relative path output are given
12941299
if (mSettings.basePaths.empty() && mSettings.relativePaths)

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ unsigned int CppCheck::check(const std::string &path)
427427
{
428428
if (mSettings.clang) {
429429
if (!mSettings.quiet)
430-
mErrorLogger.reportOut(std::string("Checking ") + path + "...", Color::FgGreen);
430+
mErrorLogger.reportOut(std::string("Checking ") + path + " ...", Color::FgGreen);
431431

432432
const std::string lang = Path::isCPP(path) ? "-x c++" : "-x c";
433433
const std::string analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, emptyString);

lib/importproject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#include "json.h"
4242

43+
// TODO: align the exclusion logic with PathMatch
4344
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
4445
{
4546
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {

lib/pathmatch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ bool PathMatch::match(const std::string &path) const
3838
if (path.empty())
3939
return false;
4040

41+
// TODO: align the exclusion logic with ImportProject::ignorePaths()
4142
for (std::vector<std::string>::const_iterator i = mExcludedPaths.cbegin(); i != mExcludedPaths.cend(); ++i) {
4243
const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, mWorkingDirectory) : *i);
4344

test/cli/test-clang-import.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
import subprocess
77
import pytest
8-
from testutils import cppcheck
8+
from testutils import cppcheck, assert_cppcheck
99

1010
try:
1111
subprocess.call(['clang', '--version'])
@@ -122,3 +122,14 @@ def test_ast_control_flow():
122122
def test_ast():
123123
check_ast('struct S { int x; }; S* foo() { return new S(); }')
124124

125+
def test_log(tmpdir):
126+
test_file = os.path.join(tmpdir, 'test.cpp')
127+
with open(test_file, 'wt'):
128+
pass
129+
130+
args = ['--clang', test_file]
131+
out_lines = [
132+
'Checking {} ...'.format(test_file),
133+
]
134+
135+
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)

test/cli/test-helloworld.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ def test_exclude():
184184
prjpath = getRelativeProjectPath()
185185
ret, stdout, _ = cppcheck(['-i' + prjpath, '--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
186186
assert ret == 1
187-
assert stdout == 'cppcheck: error: no C or C++ source files found.\n'
187+
lines = stdout.splitlines()
188+
assert lines == [
189+
'cppcheck: error: no C or C++ source files found.',
190+
'cppcheck: all paths were ignored'
191+
]
188192

189193

190194
def test_build_dir_dump_output():

test/cli/test-more-projects.py

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_project_empty_fields(tmpdir):
157157
</paths>
158158
<exclude/>
159159
<exclude>
160-
<paths/>
160+
<path/>
161161
</exclude>
162162
<function-contracts/>
163163
<variable-contracts/>
@@ -531,4 +531,109 @@ def test_project_file_ignore(tmpdir):
531531
'cppcheck: Maybe all paths were ignored?'
532532
]
533533

534+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
535+
536+
537+
def test_project_file_ignore_2(tmpdir):
538+
test_file = os.path.join(tmpdir, 'test.cpp')
539+
with open(test_file, 'wt') as f:
540+
pass
541+
542+
project_file = os.path.join(tmpdir, 'test.cppcheck')
543+
with open(project_file, 'wt') as f:
544+
f.write(
545+
"""<?xml version="1.0" encoding="UTF-8"?>
546+
<project>
547+
<paths>
548+
<dir name="{}"/>
549+
</paths>
550+
<exclude>
551+
<path name="test.cpp"/>
552+
</exclude>
553+
</project>""".format(test_file))
554+
555+
args = ['--project={}'.format(project_file)]
556+
out_lines = [
557+
'cppcheck: error: could not find or open any of the paths given.',
558+
'cppcheck: Maybe all paths were ignored?'
559+
]
560+
561+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
562+
563+
564+
def test_project_file_ignore_3(tmpdir):
565+
test_file = os.path.join(tmpdir, 'test.cpp')
566+
with open(test_file, 'wt') as f:
567+
pass
568+
569+
project_file = os.path.join(tmpdir, 'test.cppcheck')
570+
with open(project_file, 'wt') as f:
571+
f.write(
572+
"""<?xml version="1.0" encoding="UTF-8"?>
573+
<project>
574+
<paths>
575+
<dir name="{}"/>
576+
</paths>
577+
<ignore>
578+
<path name="test.cpp"/>
579+
</ignore>
580+
</project>""".format(test_file))
581+
582+
args = ['--project={}'.format(project_file)]
583+
out_lines = [
584+
'cppcheck: error: could not find or open any of the paths given.',
585+
'cppcheck: Maybe all paths were ignored?'
586+
]
587+
588+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
589+
590+
591+
@pytest.mark.xfail
592+
def test_json_file_ignore(tmpdir):
593+
test_file = os.path.join(tmpdir, 'test.cpp')
594+
with open(test_file, 'wt') as f:
595+
pass
596+
597+
compilation_db = [
598+
{"directory": str(tmpdir),
599+
"command": "c++ -o bug1.o -c bug1.cpp",
600+
"file": "test.cpp",
601+
"output": "test.o"}
602+
]
603+
604+
project_file = os.path.join(tmpdir, 'test.json')
605+
with open(project_file, 'wt') as f:
606+
f.write(json.dumps(compilation_db))
607+
608+
args = ['-itest.cpp', '--project={}'.format(project_file)]
609+
out_lines = [
610+
'cppcheck: error: no C or C++ source files found.',
611+
'cppcheck: all paths were ignored'
612+
]
613+
614+
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)
615+
616+
617+
def test_json_file_ignore_2(tmpdir):
618+
test_file = os.path.join(tmpdir, 'test.cpp')
619+
with open(test_file, 'wt') as f:
620+
pass
621+
622+
compilation_db = [
623+
{"directory": str(tmpdir),
624+
"command": "c++ -o bug1.o -c bug1.cpp",
625+
"file": "test.cpp",
626+
"output": "test.o"}
627+
]
628+
629+
project_file = os.path.join(tmpdir, 'test.json')
630+
with open(project_file, 'wt') as f:
631+
f.write(json.dumps(compilation_db))
632+
633+
args = ['-i{}'.format(test_file), '--project={}'.format(project_file)]
634+
out_lines = [
635+
'cppcheck: error: no C or C++ source files found.',
636+
'cppcheck: all paths were ignored'
637+
]
638+
534639
assert_cppcheck(args, ec_exp=1, err_exp=[], out_exp=out_lines)

0 commit comments

Comments
 (0)