Cppcheck
pathmatch.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "pathmatch.h"
20 
21 #include "path.h"
22 #include "utils.h"
23 
24 #include <cstddef>
25 #include <utility>
26 
27 PathMatch::PathMatch(std::vector<std::string> excludedPaths, bool caseSensitive)
28  : mExcludedPaths(std::move(excludedPaths)), mCaseSensitive(caseSensitive)
29 {
30  if (!mCaseSensitive)
31  for (std::string& excludedPath : mExcludedPaths)
32  strTolower(excludedPath);
34 }
35 
36 bool PathMatch::match(const std::string &path) const
37 {
38  if (path.empty())
39  return false;
40 
41  // TODO: align the exclusion logic with ImportProject::ignorePaths()
42  for (std::vector<std::string>::const_iterator i = mExcludedPaths.cbegin(); i != mExcludedPaths.cend(); ++i) {
43  const std::string excludedPath((!Path::isAbsolute(path) && Path::isAbsolute(*i)) ? Path::getRelativePath(*i, mWorkingDirectory) : *i);
44 
45  std::string findpath = Path::fromNativeSeparators(path);
46  if (!mCaseSensitive)
47  strTolower(findpath);
48 
49  // Filtering directory name
50  if (endsWith(excludedPath,'/')) {
51  if (!endsWith(findpath,'/'))
52  findpath = removeFilename(findpath);
53 
54  if (excludedPath.length() > findpath.length())
55  continue;
56  // Match relative paths starting with mask
57  // -isrc matches src/foo.cpp
58  if (findpath.compare(0, excludedPath.size(), excludedPath) == 0)
59  return true;
60  // Match only full directory name in middle or end of the path
61  // -isrc matches myproject/src/ but does not match
62  // myproject/srcfiles/ or myproject/mysrc/
63  if (findpath.find("/" + excludedPath) != std::string::npos)
64  return true;
65  }
66  // Filtering filename
67  else {
68  if (excludedPath.length() > findpath.length())
69  continue;
70  // Check if path ends with mask
71  // -ifoo.cpp matches (./)foo.c, src/foo.cpp and proj/src/foo.cpp
72  // -isrc/file.cpp matches src/foo.cpp and proj/src/foo.cpp
73  if (findpath.compare(findpath.size() - excludedPath.size(), findpath.size(), excludedPath) == 0)
74  return true;
75 
76  }
77  }
78  return false;
79 }
80 
81 std::string PathMatch::removeFilename(const std::string &path)
82 {
83  const std::size_t ind = path.find_last_of('/');
84  return path.substr(0, ind + 1);
85 }
std::vector< std::string > mExcludedPaths
Definition: pathmatch.h:61
PathMatch(std::vector< std::string > excludedPaths, bool caseSensitive=true)
The constructor.
Definition: pathmatch.cpp:27
static std::string removeFilename(const std::string &path)
Remove filename part from the path.
Definition: pathmatch.cpp:81
std::vector< std::string > mWorkingDirectory
Definition: pathmatch.h:63
bool match(const std::string &path) const
Match path against list of masks.
Definition: pathmatch.cpp:36
bool mCaseSensitive
Definition: pathmatch.h:62
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
Definition: path.cpp:75
static std::string getCurrentPath()
Returns the absolute path of current working directory.
Definition: path.cpp:129
static std::string getRelativePath(const std::string &absolutePath, const std::vector< std::string > &basePaths)
Create a relative path from an absolute one, if absolute path is inside the basePaths.
Definition: path.cpp:181
static bool isAbsolute(const std::string &path)
Check if given path is absolute.
Definition: path.cpp:166
void strTolower(std::string &str)
Definition: utils.cpp:124
bool endsWith(const std::string &str, char c)
Definition: utils.h:110