Skip to content

Commit 8ae846f

Browse files
committed
Fix #13997 (file filter: match relative path in project file with absolute path in file filter)
1 parent a214e76 commit 8ae846f

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ $(libcppdir)/token.o: lib/token.cpp externals/simplecpp/simplecpp.h lib/addoninf
645645
$(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
646646
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenlist.cpp
647647

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

651651
$(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

lib/utils.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "utils.h"
20+
#include "path.h"
2021

2122
#include <algorithm>
2223
#include <cctype>
@@ -118,8 +119,19 @@ bool matchglob(const std::string& pattern, const std::string& name, bool caseIns
118119
}
119120

120121
bool matchglobs(const std::vector<std::string> &patterns, const std::string &name, bool caseInsensitive) {
121-
return std::any_of(begin(patterns), end(patterns), [&name, caseInsensitive](const std::string &pattern) {
122-
return matchglob(pattern, name, caseInsensitive);
122+
std::string abspath, relpath;
123+
if (Path::isAbsolute(name) || name.find("*") != std::string::npos) {
124+
abspath = name;
125+
const std::string& currentPath = Path::getCurrentPath();
126+
if (name.size() > currentPath.size() + 1 && startsWith(name, currentPath + "/"))
127+
relpath = name.substr(currentPath.size() + 1);
128+
} else {
129+
abspath = Path::simplifyPath(Path::getCurrentPath() + "/" + name);
130+
relpath = name;
131+
}
132+
133+
return std::any_of(begin(patterns), end(patterns), [&abspath, &relpath, caseInsensitive](const std::string &pattern) {
134+
return matchglob(pattern, abspath, caseInsensitive) || (!relpath.empty() && matchglob(pattern, relpath, caseInsensitive));
123135
});
124136
}
125137

oss-fuzz/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ $(libcppdir)/token.o: ../lib/token.cpp ../externals/simplecpp/simplecpp.h ../lib
333333
$(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
334334
$(CXX) ${LIB_FUZZING_ENGINE} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenlist.cpp
335335

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

339339
$(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

test/cli/more-projects_test.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,66 @@ def test_project_file_filter_3(tmpdir):
372372
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines)
373373

374374

375+
def test_project_relpath_file_filter_abspath(tmpdir):
376+
"""
377+
relative paths in project file, absolute path in file filter
378+
"""
379+
test_file_cpp = os.path.join(tmpdir, 'test.cpp')
380+
with open(test_file_cpp, 'wt') as f:
381+
pass
382+
test_file_c = os.path.join(tmpdir, 'test.c')
383+
with open(test_file_c, 'wt') as f:
384+
pass
385+
386+
project_file = os.path.join(tmpdir, 'test.cppcheck')
387+
with open(project_file, 'wt') as f:
388+
f.write(
389+
"""<?xml version="1.0" encoding="UTF-8"?>
390+
<project>
391+
<paths>
392+
<dir name="test.cpp"/>
393+
<dir name="test.c"/>
394+
</paths>
395+
</project>""")
396+
397+
out_lines = [
398+
'Checking test.c ...'
399+
]
400+
401+
args = ['--file-filter={}'.format(test_file_c), '--project=test.cppcheck']
402+
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=tmpdir)
403+
404+
405+
def test_project_abspath_file_filter_relpath(tmpdir):
406+
"""
407+
absolute paths in project file, relative path in file filter
408+
"""
409+
test_file_cpp = os.path.join(tmpdir, 'test.cpp')
410+
with open(test_file_cpp, 'wt') as f:
411+
pass
412+
test_file_c = os.path.join(tmpdir, 'test.c')
413+
with open(test_file_c, 'wt') as f:
414+
pass
415+
416+
project_file = os.path.join(tmpdir, 'test.cppcheck')
417+
with open(project_file, 'wt') as f:
418+
f.write(
419+
"""<?xml version="1.0" encoding="UTF-8"?>
420+
<project>
421+
<paths>
422+
<dir name="{}"/>
423+
<dir name="{}"/>
424+
</paths>
425+
</project>""".format(test_file_c, test_file_cpp))
426+
427+
out_lines = [
428+
'Checking {} ...'.format(test_file_c)
429+
]
430+
431+
args = ['--file-filter=test.c', '--project=test.cppcheck']
432+
assert_cppcheck(args, ec_exp=0, err_exp=[], out_exp=out_lines, cwd=tmpdir)
433+
434+
375435
def test_project_file_filter_no_match(tmpdir):
376436
test_file = os.path.join(tmpdir, 'test.cpp')
377437
with open(test_file, 'wt') as f:

0 commit comments

Comments
 (0)