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
15 changes: 10 additions & 5 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ bool CmdLineParser::fillSettingsFromArgs(int argc, const char* const argv[])
assert(!(!pathnamesRef.empty() && !fileSettingsRef.empty()));

if (!fileSettingsRef.empty()) {
// TODO: handle ignored?

// TODO: de-duplicate

std::list<FileSettings> fileSettings;
Expand Down Expand Up @@ -1247,8 +1245,6 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
substituteTemplateFormatStatic(mSettings.templateFormat);
substituteTemplateLocationStatic(mSettings.templateLocation);

project.ignorePaths(mIgnoredPaths);

if (mSettings.force || maxconfigs)
mSettings.checkAllConfigurations = true;

Expand All @@ -1259,6 +1255,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
mSettings.maxConfigs = 1U;

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.");
}

Expand All @@ -1269,15 +1266,23 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a

// Print error only if we have "real" command and expect files
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.");
return Result::Fail;
}

if (!project.guiProject.pathNames.empty())
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;
}

// Use paths _pathnames if no base paths for relative path output are given
if (mSettings.basePaths.empty() && mSettings.relativePaths)
Expand Down
2 changes: 1 addition & 1 deletion lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ unsigned int CppCheck::check(const std::string &path)
{
if (mSettings.clang) {
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 analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, emptyString);
Expand Down
1 change: 1 addition & 0 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "json.h"

// TODO: align the exclusion logic with PathMatch
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths)
{
for (std::list<FileSettings>::iterator it = fileSettings.begin(); it != fileSettings.end();) {
Expand Down
1 change: 1 addition & 0 deletions lib/pathmatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ bool PathMatch::match(const std::string &path) const
if (path.empty())
return false;

// TODO: align the exclusion logic with ImportProject::ignorePaths()
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);

Expand Down
13 changes: 12 additions & 1 deletion test/cli/test-clang-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import re
import subprocess
import pytest
from testutils import cppcheck
from testutils import cppcheck, assert_cppcheck

try:
subprocess.call(['clang', '--version'])
Expand Down Expand Up @@ -122,3 +122,14 @@ def test_ast_control_flow():
def test_ast():
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)
6 changes: 5 additions & 1 deletion test/cli/test-helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ def test_exclude():
prjpath = getRelativeProjectPath()
ret, stdout, _ = cppcheck(['-i' + prjpath, '--platform=win64', '--project=' + os.path.join(prjpath, 'helloworld.cppcheck')])
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():
Expand Down
107 changes: 106 additions & 1 deletion test/cli/test-more-projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_project_empty_fields(tmpdir):
</paths>
<exclude/>
<exclude>
<paths/>
<path/>
</exclude>
<function-contracts/>
<variable-contracts/>
Expand Down Expand Up @@ -531,4 +531,109 @@ def test_project_file_ignore(tmpdir):
'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)