diff --git a/Makefile b/Makefile index 188529d82a9..6ce7cfbc340 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/lib/utils.cpp b/lib/utils.cpp index 11661556968..3a2719bbf46 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -17,6 +17,7 @@ */ #include "utils.h" +#include "path.h" #include #include @@ -118,8 +119,19 @@ bool matchglob(const std::string& pattern, const std::string& name, bool caseIns } bool matchglobs(const std::vector &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)); }); } diff --git a/oss-fuzz/Makefile b/oss-fuzz/Makefile index 88449d09051..81db3c9d7a9 100644 --- a/oss-fuzz/Makefile +++ b/oss-fuzz/Makefile @@ -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 diff --git a/test/cli/more-projects_test.py b/test/cli/more-projects_test.py index 57f400fa197..8e746ef832b 100644 --- a/test/cli/more-projects_test.py +++ b/test/cli/more-projects_test.py @@ -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( + """ + + + + + +""") + + 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( + """ + + + + + +""".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: