Coverage Report

Created: 2025-01-24 06:31

/src/cppcheck/lib/path.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- C++ -*-
2
 * Cppcheck - A tool for static C/C++ code analysis
3
 * Copyright (C) 2007-2024 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
//---------------------------------------------------------------------------
20
#ifndef pathH
21
#define pathH
22
//---------------------------------------------------------------------------
23
24
#include "config.h"
25
#include "standards.h"
26
27
#include <set>
28
#include <string>
29
#include <vector>
30
31
/// @addtogroup Core
32
/// @{
33
34
35
/**
36
 * @brief Path handling routines.
37
 * Internally cppcheck wants to store paths with / separator which is also
38
 * native separator for Unix-derived systems. When giving path to user
39
 * or for other functions we convert path separators back to native type.
40
 */
41
class CPPCHECKLIB Path {
42
public:
43
    /**
44
     * Convert path to use native separators.
45
     * @param path Path string to convert.
46
     * @return converted path.
47
     */
48
    static std::string toNativeSeparators(std::string path);
49
50
    /**
51
     * Convert path to use internal path separators.
52
     * @param path Path string to convert.
53
     * @return converted path.
54
     */
55
    static std::string fromNativeSeparators(std::string path);
56
57
    /**
58
     * @brief Simplify path "foo/bar/.." => "foo"
59
     * @param originalPath path to be simplified, must have / -separators.
60
     * @return simplified path
61
     */
62
    static std::string simplifyPath(std::string originalPath);
63
64
    /**
65
     * @brief Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
66
     * @param filename filename to lookup, must have / -separators.
67
     * @return path part of the filename
68
     */
69
    static std::string getPathFromFilename(const std::string &filename);
70
71
    /**
72
     * @brief Compare filenames to see if they are the same.
73
     * On Linux the comparison is case-sensitive. On Windows it is case-insensitive.
74
     * @param fname1 one filename
75
     * @param fname2 other filename
76
     * @return true if the filenames match on the current platform
77
     */
78
    static bool sameFileName(const std::string &fname1, const std::string &fname2);
79
80
    /**
81
     * @brief Remove quotation marks (") from the path.
82
     * @param path path to be cleaned.
83
     * @return Cleaned path without quotation marks.
84
     */
85
    static std::string removeQuotationMarks(std::string path);
86
87
    /**
88
     * @brief Get an extension of the filename.
89
     * @param path Path containing filename.
90
     * @param lowercase Return the extension in lower case
91
     * @return Filename extension (containing the dot, e.g. ".h" or ".CPP").
92
     */
93
    static std::string getFilenameExtension(const std::string &path, bool lowercase = false);
94
95
    /**
96
     * @brief Get an extension of the filename in lower case.
97
     * @param path Path containing filename.
98
     * @return Filename extension (containing the dot, e.g. ".h").
99
     */
100
    static std::string getFilenameExtensionInLowerCase(const std::string &path);
101
102
    /**
103
     * @brief Returns the absolute path of current working directory
104
     * @return absolute path of current working directory
105
     */
106
    static std::string getCurrentPath();
107
108
    /**
109
     * @brief Returns the absolute path to the current executable
110
     * @return absolute path to the current executable
111
     */
112
    static std::string getCurrentExecutablePath(const char* fallback);
113
114
    /**
115
     * @brief Check if given path is absolute
116
     * @param path Path to check
117
     * @return true if given path is absolute
118
     */
119
    static bool isAbsolute(const std::string& path);
120
121
    /**
122
     * @brief Create a relative path from an absolute one, if absolute path is inside the basePaths.
123
     * @param absolutePath Path to be made relative.
124
     * @param basePaths Paths to which it may be made relative.
125
     * @return relative path, if possible. Otherwise absolutePath is returned unchanged
126
     */
127
    static std::string getRelativePath(const std::string& absolutePath, const std::vector<std::string>& basePaths);
128
129
    /**
130
     * @brief Get an absolute file path from a relative one.
131
     * @param filePath File path to be made absolute.
132
     * @return absolute path, if possible. Otherwise an empty path is returned
133
     */
134
    static std::string getAbsoluteFilePath(const std::string& filePath);
135
136
    /**
137
     * @brief Check if the file extension indicates that it's a C/C++ source file.
138
     * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
139
     * @param filename filename to check. path info is optional
140
     * @param lang the detected language
141
     * @return true if the file extension indicates it should be checked
142
     */
143
0
    static bool acceptFile(const std::string &filename, Standards::Language* lang = nullptr) {
144
0
        const std::set<std::string> extra;
145
0
        return acceptFile(filename, extra, lang);
146
0
    }
147
148
    /**
149
     * @brief Check if the file extension indicates that it's a C/C++ source file.
150
     * Check if the file has source file extension: *.c;*.cpp;*.cxx;*.c++;*.cc;*.txx
151
     * @param path filename to check. path info is optional
152
     * @param extra extra file extensions
153
     * @param lang the detected language
154
     * @return true if the file extension indicates it should be checked
155
     */
156
    static bool acceptFile(const std::string &path, const std::set<std::string> &extra, Standards::Language* lang = nullptr);
157
158
    /**
159
     * @brief Is filename a header based on file extension
160
     * @param path filename to check. path info is optional
161
     * @return true if filename extension is meant for headers
162
     */
163
    static bool isHeader(const std::string &path);
164
165
    /**
166
     * @brief Identify the language based on the file extension
167
     * @param path filename to check. path info is optional
168
     * @param cppHeaderProbe check optional Emacs marker to identify extension-less and *.h files as C++
169
     * @param header if provided indicates if the file is a header
170
     * @return the language type
171
     */
172
    static Standards::Language identify(const std::string &path, bool cppHeaderProbe, bool *header = nullptr);
173
174
    /**
175
     * @brief Get filename without a directory path part.
176
     * @param file filename to be stripped. path info is optional
177
     * @return filename without directory path part.
178
     */
179
    static std::string stripDirectoryPart(const std::string &file);
180
181
    /**
182
     * @brief Checks if given path is a file
183
     * @param path Path to be checked
184
     * @return true if given path is a file
185
     */
186
    static bool isFile(const std::string &path);
187
188
    /**
189
     * @brief Checks if a given path is a directory
190
     * @param path Path to be checked
191
     * @return true if given path is a directory
192
     */
193
    static bool isDirectory(const std::string &path);
194
195
    /**
196
     * @brief Checks if a given path exists (i.e. is a file or directory)
197
     * @param path Path to be checked
198
     * @return true if given path exists
199
     */
200
    static bool exists(const std::string &path);
201
202
    /**
203
     * join 2 paths with '/' separators
204
     */
205
    static std::string join(const std::string& path1, const std::string& path2);
206
};
207
208
/// @}
209
//---------------------------------------------------------------------------
210
#endif // pathH