Skip to content

Commit 2a09245

Browse files
committed
Make basepath a class member
1 parent 9743d82 commit 2a09245

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

lib/importproject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
void ImportProject::ignorePaths(const std::vector<std::string> &ipaths, bool debug)
4747
{
48-
PathMatch matcher(ipaths, mPath);
48+
PathMatch matcher(ipaths);
4949
for (auto it = fileSettings.cbegin(); it != fileSettings.cend();) {
5050
if (matcher.match(it->filename())) {
5151
if (debug)

lib/pathmatch.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ static std::string translate(const std::string &s)
5757

5858
PathMatch::PathMatch(const std::vector<std::string> &paths, const std::string &basepath, Mode mode)
5959
{
60+
if (basepath.empty())
61+
mBasepath = Path::getCurrentPath();
62+
else if (Path::isAbsolute(basepath))
63+
mBasepath = basepath;
64+
else
65+
mBasepath = Path::getCurrentPath() + "/" + basepath;
66+
6067
if (mode == Mode::platform) {
6168
#ifdef _WIN32
6269
mode = Mode::icase;
@@ -74,12 +81,8 @@ PathMatch::PathMatch(const std::vector<std::string> &paths, const std::string &b
7481
if (!regex_string.empty())
7582
regex_string.push_back('|');
7683

77-
if (p.front() == '.') {
78-
if (Path::isAbsolute(basepath))
79-
p = basepath + "/" + p;
80-
else
81-
p = Path::getCurrentPath() + "/" + basepath + "/" + p;
82-
}
84+
if (p.front() == '.')
85+
p = mBasepath + "/" + p;
8386

8487
p = Path::fromNativeSeparators(p);
8588

@@ -107,17 +110,15 @@ PathMatch::PathMatch(const std::vector<std::string> &paths, const std::string &b
107110
mRegex = std::regex(regex_string, std::regex_constants::extended);
108111
}
109112

110-
bool PathMatch::match(const std::string &path, const std::string &basepath) const
113+
bool PathMatch::match(const std::string &path) const
111114
{
112115
std::string p;
113116
std::smatch m;
114117

115118
if (Path::isAbsolute(path))
116119
p = Path::fromNativeSeparators(Path::simplifyPath(path));
117-
else if (Path::isAbsolute(basepath))
118-
p = Path::fromNativeSeparators(Path::simplifyPath(basepath + "/" + path));
119120
else
120-
p = Path::fromNativeSeparators(Path::simplifyPath(Path::getCurrentPath() + "/" + basepath + "/" + path));
121+
p = Path::fromNativeSeparators(Path::simplifyPath(mBasepath + "/" + path));
121122

122123
return std::regex_search(p, m, mRegex, std::regex_constants::match_any | std::regex_constants::match_not_null);
123124
}

lib/pathmatch.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
* - Otherwise, match all files where the rule matches the file's simplified absolute path.
4141
* Globs are allowed in the rule.
4242
* - If a rule starts with '.':
43-
* - The rule is interpreted as a path relative to `basepath`, which should be the execution directory for rules
44-
* passed to the CLI, or the directory containing the project file when imported, and then converted to an
45-
* absolute path and treated as such according to the above procedure.
43+
* - The rule is interpreted as a path relative to `basepath` and then converted to an absolute path and
44+
* treated as such according to the above procedure. If the rule is relative to some other directory, it should
45+
* be modified to be relative to `basepath` first (this should be done with rules in project files, for example).
4646
* - Otherwise:
4747
* - No simplification is done to the rule.
4848
* - If the rule ends with a path separator:
@@ -83,8 +83,8 @@ class CPPCHECKLIB PathMatch {
8383
* If a path is a directory it needs to end with a file separator.
8484
*
8585
* @param paths List of masks.
86-
* @param basepath Path to which matched paths are relative, when applicable. Can be relative, in which case it is
87-
* appended to Path::getCurrentPath().
86+
* @param basepath Path to which rules and matched paths are relative, when applicable. Can be relative, in which
87+
* case it is appended to Path::getCurrentPath().
8888
* @param mode Case sensitivity mode.
8989
*/
9090
explicit PathMatch(const std::vector<std::string> &paths, const std::string &basepath = std::string(), Mode mode = Mode::platform);
@@ -97,9 +97,10 @@ class CPPCHECKLIB PathMatch {
9797
* @param path Path to match.
9898
* @return true if any of the masks match the path, false otherwise.
9999
*/
100-
bool match(const std::string &path, const std::string &basepath = std::string()) const;
100+
bool match(const std::string &path) const;
101101

102102
private:
103+
std::string mBasepath;
103104
std::regex mRegex;
104105
};
105106

0 commit comments

Comments
 (0)