Cppcheck
path.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 #if defined(__GNUC__) && (defined(_WIN32) || defined(__CYGWIN__))
20 #undef __STRICT_ANSI__
21 #endif
22 
23 #include "path.h"
24 #include "utils.h"
25 
26 #include <algorithm>
27 #include <cstdlib>
28 #include <sys/stat.h>
29 #include <unordered_set>
30 #include <utility>
31 
32 #include <simplecpp.h>
33 
34 #ifndef _WIN32
35 #include <sys/types.h>
36 #include <unistd.h>
37 #else
38 #include <direct.h>
39 #include <windows.h>
40 #endif
41 #if defined(__CYGWIN__)
42 #include <strings.h>
43 #endif
44 #if defined(__APPLE__)
45 #include <mach-o/dyld.h>
46 #endif
47 
48 
49 /** Is the filesystem case insensitive? */
50 static constexpr bool caseInsensitiveFilesystem()
51 {
52 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
53  // Windows is case insensitive
54  // MacOS is case insensitive by default (also supports case sensitivity)
55  return true;
56 #else
57  // TODO: Non-windows filesystems might be case insensitive
58  return false;
59 #endif
60 }
61 
62 std::string Path::toNativeSeparators(std::string path)
63 {
64 #if defined(_WIN32)
65  constexpr char separ = '/';
66  constexpr char native = '\\';
67 #else
68  constexpr char separ = '\\';
69  constexpr char native = '/';
70 #endif
71  std::replace(path.begin(), path.end(), separ, native);
72  return path;
73 }
74 
75 std::string Path::fromNativeSeparators(std::string path)
76 {
77  constexpr char nonnative = '\\';
78  constexpr char newsepar = '/';
79  std::replace(path.begin(), path.end(), nonnative, newsepar);
80  return path;
81 }
82 
83 std::string Path::simplifyPath(std::string originalPath)
84 {
85  return simplecpp::simplifyPath(std::move(originalPath));
86 }
87 
88 std::string Path::getPathFromFilename(const std::string &filename)
89 {
90  const std::size_t pos = filename.find_last_of("\\/");
91 
92  if (pos != std::string::npos)
93  return filename.substr(0, 1 + pos);
94 
95  return "";
96 }
97 
98 bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
99 {
100  return caseInsensitiveFilesystem() ? (caseInsensitiveStringCompare(fname1, fname2) == 0) : (fname1 == fname2);
101 }
102 
103 std::string Path::removeQuotationMarks(std::string path)
104 {
105  path.erase(std::remove(path.begin(), path.end(), '\"'), path.end());
106  return path;
107 }
108 
109 std::string Path::getFilenameExtension(const std::string &path, bool lowercase)
110 {
111  const std::string::size_type dotLocation = path.find_last_of('.');
112  if (dotLocation == std::string::npos)
113  return "";
114 
115  std::string extension = path.substr(dotLocation);
116  if (lowercase || caseInsensitiveFilesystem()) {
117  // on a case insensitive filesystem the case doesn't matter so
118  // let's return the extension in lowercase
119  strTolower(extension);
120  }
121  return extension;
122 }
123 
124 std::string Path::getFilenameExtensionInLowerCase(const std::string &path)
125 {
126  return getFilenameExtension(path, true);
127 }
128 
129 std::string Path::getCurrentPath()
130 {
131  char currentPath[4096];
132 
133 #ifndef _WIN32
134  if (getcwd(currentPath, 4096) != nullptr)
135 #else
136  if (_getcwd(currentPath, 4096) != nullptr)
137 #endif
138  return std::string(currentPath);
139 
140  return "";
141 }
142 
143 std::string Path::getCurrentExecutablePath(const char* fallback)
144 {
145  char buf[4096] = {};
146  bool success{};
147 #ifdef _WIN32
148  success = (GetModuleFileNameA(nullptr, buf, sizeof(buf)) < sizeof(buf));
149 #elif defined(__APPLE__)
150  uint32_t size = sizeof(buf);
151  success = (_NSGetExecutablePath(buf, &size) == 0);
152 #else
153  const char* procPath =
154 #ifdef __SVR4 // Solaris
155  "/proc/self/path/a.out";
156 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
157  "/proc/curproc/file";
158 #else // Linux
159  "/proc/self/exe";
160 #endif
161  success = (readlink(procPath, buf, sizeof(buf)) != -1);
162 #endif
163  return success ? std::string(buf) : std::string(fallback);
164 }
165 
166 bool Path::isAbsolute(const std::string& path)
167 {
168  const std::string& nativePath = toNativeSeparators(path);
169 
170 #ifdef _WIN32
171  if (path.length() < 2)
172  return false;
173 
174  // On Windows, 'C:\foo\bar' is an absolute path, while 'C:foo\bar' is not
175  return startsWith(nativePath, "\\\\") || (std::isalpha(nativePath[0]) != 0 && nativePath.compare(1, 2, ":\\") == 0);
176 #else
177  return !nativePath.empty() && nativePath[0] == '/';
178 #endif
179 }
180 
181 std::string Path::getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths)
182 {
183  for (const std::string &bp : basePaths) {
184  if (absolutePath == bp || bp.empty()) // Seems to be a file, or path is empty
185  continue;
186 
187  if (absolutePath.compare(0, bp.length(), bp) != 0)
188  continue;
189 
190  if (endsWith(bp,'/'))
191  return absolutePath.substr(bp.length());
192  if (absolutePath.size() > bp.size() && absolutePath[bp.length()] == '/')
193  return absolutePath.substr(bp.length() + 1);
194  }
195  return absolutePath;
196 }
197 
198 static const std::unordered_set<std::string> cpp_src_exts = {
199  ".cpp", ".cxx", ".cc", ".c++", ".tpp", ".txx", ".ipp", ".ixx"
200 };
201 
202 static const std::unordered_set<std::string> c_src_exts = {
203  ".c", ".cl"
204 };
205 
206 static const std::unordered_set<std::string> header_exts = {
207  ".h", ".hpp", ".h++", ".hxx", ".hh"
208 };
209 
210 bool Path::isC(const std::string &path)
211 {
212  // In unix, ".C" is considered C++ file
213  const std::string extension = getFilenameExtension(path);
214  return extension == ".c" ||
215  extension == ".cl";
216 }
217 
218 bool Path::isCPP(const std::string &path)
219 {
220  const std::string extension = getFilenameExtensionInLowerCase(path);
221  return extension == ".cpp" ||
222  extension == ".cxx" ||
223  extension == ".cc" ||
224  extension == ".c++" ||
225  extension == ".hpp" ||
226  extension == ".hxx" ||
227  extension == ".hh" ||
228  extension == ".tpp" ||
229  extension == ".txx" ||
230  extension == ".ipp" ||
231  extension == ".ixx" ||
232  getFilenameExtension(path) == ".C"; // In unix, ".C" is considered C++ file
233 }
234 
235 bool Path::acceptFile(const std::string &path, const std::set<std::string> &extra)
236 {
237  bool header = false;
238  return (identify(path, &header) != Standards::Language::None && !header) || extra.find(getFilenameExtension(path)) != extra.end();
239 }
240 
241 // cppcheck-suppress unusedFunction
242 bool Path::isHeader(const std::string &path)
243 {
244  const std::string extension = getFilenameExtensionInLowerCase(path);
245  return startsWith(extension, ".h");
246 }
247 
248 Standards::Language Path::identify(const std::string &path, bool *header)
249 {
250  // cppcheck-suppress uninitvar - TODO: FP
251  if (header)
252  *header = false;
253 
254  std::string ext = getFilenameExtension(path);
255  if (ext == ".C")
256  return Standards::Language::CPP;
257  if (c_src_exts.find(ext) != c_src_exts.end())
258  return Standards::Language::C;
259  // cppcheck-suppress knownConditionTrueFalse - TODO: FP
261  strTolower(ext);
262  if (ext == ".h") {
263  if (header)
264  *header = true;
265  return Standards::Language::C; // treat as C for now
266  }
267  if (cpp_src_exts.find(ext) != cpp_src_exts.end())
268  return Standards::Language::CPP;
269  if (header_exts.find(ext) != header_exts.end()) {
270  if (header)
271  *header = true;
272  return Standards::Language::CPP;
273  }
274  return Standards::Language::None;
275 }
276 
277 bool Path::isHeader2(const std::string &path)
278 {
279  bool header;
280  (void)Path::identify(path, &header);
281  return header;
282 }
283 
284 std::string Path::getAbsoluteFilePath(const std::string& filePath)
285 {
286  std::string absolute_path;
287 #ifdef _WIN32
288  char absolute[_MAX_PATH];
289  if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
290  absolute_path = absolute;
291 #elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) || defined(__CPPCHECK__)
292  char * absolute = realpath(filePath.c_str(), nullptr);
293  if (absolute)
294  absolute_path = absolute;
295  free(absolute);
296 #else
297 #error Platform absolute path function needed
298 #endif
299  return absolute_path;
300 }
301 
302 std::string Path::stripDirectoryPart(const std::string &file)
303 {
304 #if defined(_WIN32) && !defined(__MINGW32__)
305  constexpr char native = '\\';
306 #else
307  constexpr char native = '/';
308 #endif
309 
310  const std::string::size_type p = file.rfind(native);
311  if (p != std::string::npos) {
312  return file.substr(p + 1);
313  }
314  return file;
315 }
316 
317 #ifdef _WIN32
318 using mode_t = unsigned short;
319 #endif
320 
321 static mode_t file_type(const std::string &path)
322 {
323  struct stat file_stat;
324  if (stat(path.c_str(), &file_stat) == -1)
325  return 0;
326  return file_stat.st_mode & S_IFMT;
327 }
328 
329 bool Path::isFile(const std::string &path)
330 {
331  return file_type(path) == S_IFREG;
332 }
333 
334 bool Path::isDirectory(const std::string &path)
335 {
336  return file_type(path) == S_IFDIR;
337 }
338 
339 std::string Path::join(const std::string& path1, const std::string& path2) {
340  if (path1.empty() || path2.empty())
341  return path1 + path2;
342  if (path2.front() == '/')
343  return path2;
344  return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
345 }
static bool isHeader2(const std::string &path)
Is filename a header based on file extension.
Definition: path.cpp:277
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
Definition: path.cpp:83
static std::string getFilenameExtension(const std::string &path, bool lowercase=false)
Get an extension of the filename.
Definition: path.cpp:109
static std::string join(const std::string &path1, const std::string &path2)
join 2 paths with '/' separators
Definition: path.cpp:339
static bool isFile(const std::string &path)
Checks if given path is a file.
Definition: path.cpp:329
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 removeQuotationMarks(std::string path)
Remove quotation marks (") from the path.
Definition: path.cpp:103
static DEPRECATED bool isCPP(const std::string &path)
Identify language based on file extension.
Definition: path.cpp:218
static DEPRECATED bool isHeader(const std::string &path)
Is filename a header based on file extension.
Definition: path.cpp:242
static std::string getCurrentExecutablePath(const char *fallback)
Returns the absolute path to the current executable.
Definition: path.cpp:143
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
Definition: path.cpp:62
static Standards::Language identify(const std::string &path, bool *header=nullptr)
Identify the language based on the file extension.
Definition: path.cpp:248
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
Definition: path.cpp:88
static bool sameFileName(const std::string &fname1, const std::string &fname2)
Compare filenames to see if they are the same.
Definition: path.cpp:98
static std::string getAbsoluteFilePath(const std::string &filePath)
Get an absolute file path from a relative one.
Definition: path.cpp:284
static bool acceptFile(const std::string &filename)
Check if the file extension indicates that it's a C/C++ source file.
Definition: path.h:142
static DEPRECATED bool isC(const std::string &path)
Identify language based on file extension.
Definition: path.cpp:210
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 isDirectory(const std::string &path)
Checks if a given path is a directory.
Definition: path.cpp:334
static std::string stripDirectoryPart(const std::string &file)
Get filename without a directory path part.
Definition: path.cpp:302
static std::string getFilenameExtensionInLowerCase(const std::string &path)
Get an extension of the filename in lower case.
Definition: path.cpp:124
static bool isAbsolute(const std::string &path)
Check if given path is absolute.
Definition: path.cpp:166
static void replace(std::string &source, const std::unordered_map< std::string, std::string > &substitutionMap)
static const std::unordered_set< std::string > header_exts
Definition: path.cpp:206
static mode_t file_type(const std::string &path)
Definition: path.cpp:321
static constexpr bool caseInsensitiveFilesystem()
Is the filesystem case insensitive?
Definition: path.cpp:50
static const std::unordered_set< std::string > c_src_exts
Definition: path.cpp:202
static const std::unordered_set< std::string > cpp_src_exts
Definition: path.cpp:198
int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
Definition: utils.cpp:28
void strTolower(std::string &str)
Definition: utils.cpp:124
bool startsWith(const std::string &str, const char start[], std::size_t startlen)
Definition: utils.h:94
bool endsWith(const std::string &str, char c)
Definition: utils.h:110