Skip to content

Fix #13997 (file filter: match relative path in project file with absolute path in file filter) #7654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ $(libcppdir)/token.o: lib/token.cpp externals/simplecpp/simplecpp.h lib/addoninf
$(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/addoninfo.h lib/astutils.h lib/checkers.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/keywords.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/standards.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenlist.cpp

$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h
$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/path.h lib/standards.h lib/utils.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp

$(libcppdir)/vf_analyzers.o: lib/vf_analyzers.cpp lib/addoninfo.h lib/analyzer.h lib/astutils.h lib/calculate.h lib/checkers.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/programmemory.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/utils.h lib/valueflow.h lib/valueptr.h lib/vf_analyzers.h lib/vf_common.h lib/vf_settokenvalue.h lib/vfvalue.h
Expand Down
16 changes: 14 additions & 2 deletions lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "utils.h"
#include "path.h"

#include <algorithm>
#include <cctype>
Expand Down Expand Up @@ -118,8 +119,19 @@ bool matchglob(const std::string& pattern, const std::string& name, bool caseIns
}

bool matchglobs(const std::vector<std::string> &patterns, const std::string &name, bool caseInsensitive) {
return std::any_of(begin(patterns), end(patterns), [&name, caseInsensitive](const std::string &pattern) {
return matchglob(pattern, name, caseInsensitive);
std::string abspath, relpath;
if (Path::isAbsolute(name) || name.find("*") != std::string::npos) {
abspath = name;
const std::string& currentPath = Path::getCurrentPath();
if (name.size() > currentPath.size() + 1 && startsWith(name, currentPath + "/"))
relpath = name.substr(currentPath.size() + 1);
} else {
abspath = Path::simplifyPath(Path::getCurrentPath() + "/" + name);
relpath = name;
}

return std::any_of(begin(patterns), end(patterns), [&abspath, &relpath, caseInsensitive](const std::string &pattern) {
return matchglob(pattern, abspath, caseInsensitive) || (!relpath.empty() && matchglob(pattern, relpath, caseInsensitive));
});
}

Expand Down
2 changes: 1 addition & 1 deletion oss-fuzz/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ $(libcppdir)/token.o: ../lib/token.cpp ../externals/simplecpp/simplecpp.h ../lib
$(libcppdir)/tokenlist.o: ../lib/tokenlist.cpp ../externals/simplecpp/simplecpp.h ../lib/addoninfo.h ../lib/astutils.h ../lib/checkers.h ../lib/config.h ../lib/errorlogger.h ../lib/errortypes.h ../lib/keywords.h ../lib/library.h ../lib/mathlib.h ../lib/path.h ../lib/platform.h ../lib/settings.h ../lib/smallvector.h ../lib/standards.h ../lib/templatesimplifier.h ../lib/token.h ../lib/tokenlist.h ../lib/utils.h ../lib/vfvalue.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenlist.cpp

$(libcppdir)/utils.o: ../lib/utils.cpp ../lib/config.h ../lib/utils.h
$(libcppdir)/utils.o: ../lib/utils.cpp ../lib/config.h ../lib/path.h ../lib/standards.h ../lib/utils.h
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/utils.cpp

$(libcppdir)/vf_analyzers.o: ../lib/vf_analyzers.cpp ../lib/addoninfo.h ../lib/analyzer.h ../lib/astutils.h ../lib/calculate.h ../lib/checkers.h ../lib/config.h ../lib/errortypes.h ../lib/library.h ../lib/mathlib.h ../lib/platform.h ../lib/programmemory.h ../lib/settings.h ../lib/smallvector.h ../lib/sourcelocation.h ../lib/standards.h ../lib/symboldatabase.h ../lib/templatesimplifier.h ../lib/token.h ../lib/utils.h ../lib/valueflow.h ../lib/valueptr.h ../lib/vf_analyzers.h ../lib/vf_common.h ../lib/vf_settokenvalue.h ../lib/vfvalue.h
Expand Down
60 changes: 60 additions & 0 deletions test/cli/more-projects_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,66 @@ def test_project_file_filter_3(tmpdir):
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)


def test_project_relpath_file_filter_abspath(tmpdir):
"""
relative paths in project file, absolute path in file filter
"""
test_file_cpp = os.path.join(tmpdir, 'test.cpp')
with open(test_file_cpp, 'wt') as f:
pass
test_file_c = os.path.join(tmpdir, 'test.c')
with open(test_file_c, '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="test.cpp"/>
<dir name="test.c"/>
</paths>
</project>""")

out_lines = [
'Checking test.c ...'
]

args = ['--file-filter={}'.format(test_file_c), '--project=test.cppcheck']
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=tmpdir)


def test_project_abspath_file_filter_relpath(tmpdir):
"""
absolute paths in project file, relative path in file filter
"""
test_file_cpp = os.path.join(tmpdir, 'test.cpp')
with open(test_file_cpp, 'wt') as f:
pass
test_file_c = os.path.join(tmpdir, 'test.c')
with open(test_file_c, '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="{}"/>
<dir name="{}"/>
</paths>
</project>""".format(test_file_c, test_file_cpp))

out_lines = [
'Checking {} ...'.format(test_file_c)
]

args = ['--file-filter=test.c', '--project=test.cppcheck']
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=tmpdir)


def test_project_file_filter_no_match(tmpdir):
test_file = os.path.join(tmpdir, 'test.cpp')
with open(test_file, 'wt') as f:
Expand Down
Loading