1 | //===- llvm/Support/Path.h - Path Operating System Concept ------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file declares the llvm::sys::path namespace. It is designed after |
10 | // TR2/boost filesystem (v3), but modified to remove exception handling and the |
11 | // path class. |
12 | // |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_SUPPORT_PATH_H |
16 | #define LLVM_SUPPORT_PATH_H |
17 | |
18 | #include "llvm/ADT/Twine.h" |
19 | #include "llvm/ADT/iterator.h" |
20 | #include "llvm/Support/DataTypes.h" |
21 | #include <iterator> |
22 | #include <system_error> |
23 | |
24 | namespace llvm { |
25 | namespace sys { |
26 | namespace path { |
27 | |
28 | enum class Style { windows, posix, native }; |
29 | |
30 | /// @name Lexical Component Iterator |
31 | /// @{ |
32 | |
33 | /// Path iterator. |
34 | /// |
35 | /// This is an input iterator that iterates over the individual components in |
36 | /// \a path. The traversal order is as follows: |
37 | /// * The root-name element, if present. |
38 | /// * The root-directory element, if present. |
39 | /// * Each successive filename element, if present. |
40 | /// * Dot, if one or more trailing non-root slash characters are present. |
41 | /// Traversing backwards is possible with \a reverse_iterator |
42 | /// |
43 | /// Iteration examples. Each component is separated by ',': |
44 | /// @code |
45 | /// / => / |
46 | /// /foo => /,foo |
47 | /// foo/ => foo,. |
48 | /// /foo/bar => /,foo,bar |
49 | /// ../ => ..,. |
50 | /// C:\foo\bar => C:,/,foo,bar |
51 | /// @endcode |
52 | class const_iterator |
53 | : public iterator_facade_base<const_iterator, std::input_iterator_tag, |
54 | const StringRef> { |
55 | StringRef Path; ///< The entire path. |
56 | StringRef Component; ///< The current component. Not necessarily in Path. |
57 | size_t Position = 0; ///< The iterators current position within Path. |
58 | Style S = Style::native; ///< The path style to use. |
59 | |
60 | // An end iterator has Position = Path.size() + 1. |
61 | friend const_iterator begin(StringRef path, Style style); |
62 | friend const_iterator end(StringRef path); |
63 | |
64 | public: |
65 | reference operator*() const { return Component; } |
66 | const_iterator &operator++(); // preincrement |
67 | bool operator==(const const_iterator &RHS) const; |
68 | |
69 | /// Difference in bytes between this and RHS. |
70 | ptrdiff_t operator-(const const_iterator &RHS) const; |
71 | }; |
72 | |
73 | /// Reverse path iterator. |
74 | /// |
75 | /// This is an input iterator that iterates over the individual components in |
76 | /// \a path in reverse order. The traversal order is exactly reversed from that |
77 | /// of \a const_iterator |
78 | class reverse_iterator |
79 | : public iterator_facade_base<reverse_iterator, std::input_iterator_tag, |
80 | const StringRef> { |
81 | StringRef Path; ///< The entire path. |
82 | StringRef Component; ///< The current component. Not necessarily in Path. |
83 | size_t Position = 0; ///< The iterators current position within Path. |
84 | Style S = Style::native; ///< The path style to use. |
85 | |
86 | friend reverse_iterator rbegin(StringRef path, Style style); |
87 | friend reverse_iterator rend(StringRef path); |
88 | |
89 | public: |
90 | reference operator*() const { return Component; } |
91 | reverse_iterator &operator++(); // preincrement |
92 | bool operator==(const reverse_iterator &RHS) const; |
93 | |
94 | /// Difference in bytes between this and RHS. |
95 | ptrdiff_t operator-(const reverse_iterator &RHS) const; |
96 | }; |
97 | |
98 | /// Get begin iterator over \a path. |
99 | /// @param path Input path. |
100 | /// @returns Iterator initialized with the first component of \a path. |
101 | const_iterator begin(StringRef path, Style style = Style::native); |
102 | |
103 | /// Get end iterator over \a path. |
104 | /// @param path Input path. |
105 | /// @returns Iterator initialized to the end of \a path. |
106 | const_iterator end(StringRef path); |
107 | |
108 | /// Get reverse begin iterator over \a path. |
109 | /// @param path Input path. |
110 | /// @returns Iterator initialized with the first reverse component of \a path. |
111 | reverse_iterator rbegin(StringRef path, Style style = Style::native); |
112 | |
113 | /// Get reverse end iterator over \a path. |
114 | /// @param path Input path. |
115 | /// @returns Iterator initialized to the reverse end of \a path. |
116 | reverse_iterator rend(StringRef path); |
117 | |
118 | /// @} |
119 | /// @name Lexical Modifiers |
120 | /// @{ |
121 | |
122 | /// Remove the last component from \a path unless it is the root dir. |
123 | /// |
124 | /// Similar to the POSIX "dirname" utility. |
125 | /// |
126 | /// @code |
127 | /// directory/filename.cpp => directory/ |
128 | /// directory/ => directory |
129 | /// filename.cpp => <empty> |
130 | /// / => / |
131 | /// @endcode |
132 | /// |
133 | /// @param path A path that is modified to not have a file component. |
134 | void remove_filename(SmallVectorImpl<char> &path, Style style = Style::native); |
135 | |
136 | /// Replace the file extension of \a path with \a extension. |
137 | /// |
138 | /// @code |
139 | /// ./filename.cpp => ./filename.extension |
140 | /// ./filename => ./filename.extension |
141 | /// ./ => ./.extension |
142 | /// @endcode |
143 | /// |
144 | /// @param path A path that has its extension replaced with \a extension. |
145 | /// @param extension The extension to be added. It may be empty. It may also |
146 | /// optionally start with a '.', if it does not, one will be |
147 | /// prepended. |
148 | void replace_extension(SmallVectorImpl<char> &path, const Twine &extension, |
149 | Style style = Style::native); |
150 | |
151 | /// Replace matching path prefix with another path. |
152 | /// |
153 | /// @code |
154 | /// /foo, /old, /new => /foo |
155 | /// /old, /old, /new => /new |
156 | /// /old, /old/, /new => /old |
157 | /// /old/foo, /old, /new => /new/foo |
158 | /// /old/foo, /old/, /new => /new/foo |
159 | /// /old/foo, /old/, /new/ => /new/foo |
160 | /// /oldfoo, /old, /new => /oldfoo |
161 | /// /foo, <empty>, /new => /new/foo |
162 | /// /foo, <empty>, new => new/foo |
163 | /// /old/foo, /old, <empty> => /foo |
164 | /// @endcode |
165 | /// |
166 | /// @param Path If \a Path starts with \a OldPrefix modify to instead |
167 | /// start with \a NewPrefix. |
168 | /// @param OldPrefix The path prefix to strip from \a Path. |
169 | /// @param NewPrefix The path prefix to replace \a NewPrefix with. |
170 | /// @result true if \a Path begins with OldPrefix |
171 | bool replace_path_prefix(SmallVectorImpl<char> &Path, StringRef OldPrefix, |
172 | StringRef NewPrefix); |
173 | |
174 | /// Append to path. |
175 | /// |
176 | /// @code |
177 | /// /foo + bar/f => /foo/bar/f |
178 | /// /foo/ + bar/f => /foo/bar/f |
179 | /// foo + bar/f => foo/bar/f |
180 | /// @endcode |
181 | /// |
182 | /// @param path Set to \a path + \a component. |
183 | /// @param a The component to be appended to \a path. |
184 | void append(SmallVectorImpl<char> &path, const Twine &a, |
185 | const Twine &b = "" , |
186 | const Twine &c = "" , |
187 | const Twine &d = "" ); |
188 | |
189 | void append(SmallVectorImpl<char> &path, Style style, const Twine &a, |
190 | const Twine &b = "" , const Twine &c = "" , const Twine &d = "" ); |
191 | |
192 | /// Append to path. |
193 | /// |
194 | /// @code |
195 | /// /foo + [bar,f] => /foo/bar/f |
196 | /// /foo/ + [bar,f] => /foo/bar/f |
197 | /// foo + [bar,f] => foo/bar/f |
198 | /// @endcode |
199 | /// |
200 | /// @param path Set to \a path + [\a begin, \a end). |
201 | /// @param begin Start of components to append. |
202 | /// @param end One past the end of components to append. |
203 | void append(SmallVectorImpl<char> &path, const_iterator begin, |
204 | const_iterator end, Style style = Style::native); |
205 | |
206 | /// @} |
207 | /// @name Transforms (or some other better name) |
208 | /// @{ |
209 | |
210 | /// Convert path to the native form. This is used to give paths to users and |
211 | /// operating system calls in the platform's normal way. For example, on Windows |
212 | /// all '/' are converted to '\'. |
213 | /// |
214 | /// @param path A path that is transformed to native format. |
215 | /// @param result Holds the result of the transformation. |
216 | void native(const Twine &path, SmallVectorImpl<char> &result, |
217 | Style style = Style::native); |
218 | |
219 | /// Convert path to the native form in place. This is used to give paths to |
220 | /// users and operating system calls in the platform's normal way. For example, |
221 | /// on Windows all '/' are converted to '\'. |
222 | /// |
223 | /// @param path A path that is transformed to native format. |
224 | void native(SmallVectorImpl<char> &path, Style style = Style::native); |
225 | |
226 | /// Replaces backslashes with slashes if Windows. |
227 | /// |
228 | /// @param path processed path |
229 | /// @result The result of replacing backslashes with forward slashes if Windows. |
230 | /// On Unix, this function is a no-op because backslashes are valid path |
231 | /// chracters. |
232 | std::string convert_to_slash(StringRef path, Style style = Style::native); |
233 | |
234 | /// @} |
235 | /// @name Lexical Observers |
236 | /// @{ |
237 | |
238 | /// Get root name. |
239 | /// |
240 | /// @code |
241 | /// //net/hello => //net |
242 | /// c:/hello => c: (on Windows, on other platforms nothing) |
243 | /// /hello => <empty> |
244 | /// @endcode |
245 | /// |
246 | /// @param path Input path. |
247 | /// @result The root name of \a path if it has one, otherwise "". |
248 | StringRef root_name(StringRef path, Style style = Style::native); |
249 | |
250 | /// Get root directory. |
251 | /// |
252 | /// @code |
253 | /// /goo/hello => / |
254 | /// c:/hello => / |
255 | /// d/file.txt => <empty> |
256 | /// @endcode |
257 | /// |
258 | /// @param path Input path. |
259 | /// @result The root directory of \a path if it has one, otherwise |
260 | /// "". |
261 | StringRef root_directory(StringRef path, Style style = Style::native); |
262 | |
263 | /// Get root path. |
264 | /// |
265 | /// Equivalent to root_name + root_directory. |
266 | /// |
267 | /// @param path Input path. |
268 | /// @result The root path of \a path if it has one, otherwise "". |
269 | StringRef root_path(StringRef path, Style style = Style::native); |
270 | |
271 | /// Get relative path. |
272 | /// |
273 | /// @code |
274 | /// C:\hello\world => hello\world |
275 | /// foo/bar => foo/bar |
276 | /// /foo/bar => foo/bar |
277 | /// @endcode |
278 | /// |
279 | /// @param path Input path. |
280 | /// @result The path starting after root_path if one exists, otherwise "". |
281 | StringRef relative_path(StringRef path, Style style = Style::native); |
282 | |
283 | /// Get parent path. |
284 | /// |
285 | /// @code |
286 | /// / => <empty> |
287 | /// /foo => / |
288 | /// foo/../bar => foo/.. |
289 | /// @endcode |
290 | /// |
291 | /// @param path Input path. |
292 | /// @result The parent path of \a path if one exists, otherwise "". |
293 | StringRef parent_path(StringRef path, Style style = Style::native); |
294 | |
295 | /// Get filename. |
296 | /// |
297 | /// @code |
298 | /// /foo.txt => foo.txt |
299 | /// . => . |
300 | /// .. => .. |
301 | /// / => / |
302 | /// @endcode |
303 | /// |
304 | /// @param path Input path. |
305 | /// @result The filename part of \a path. This is defined as the last component |
306 | /// of \a path. Similar to the POSIX "basename" utility. |
307 | StringRef filename(StringRef path, Style style = Style::native); |
308 | |
309 | /// Get stem. |
310 | /// |
311 | /// If filename contains a dot but not solely one or two dots, result is the |
312 | /// substring of filename ending at (but not including) the last dot. Otherwise |
313 | /// it is filename. |
314 | /// |
315 | /// @code |
316 | /// /foo/bar.txt => bar |
317 | /// /foo/bar => bar |
318 | /// /foo/.txt => <empty> |
319 | /// /foo/. => . |
320 | /// /foo/.. => .. |
321 | /// @endcode |
322 | /// |
323 | /// @param path Input path. |
324 | /// @result The stem of \a path. |
325 | StringRef stem(StringRef path, Style style = Style::native); |
326 | |
327 | /// Get extension. |
328 | /// |
329 | /// If filename contains a dot but not solely one or two dots, result is the |
330 | /// substring of filename starting at (and including) the last dot, and ending |
331 | /// at the end of \a path. Otherwise "". |
332 | /// |
333 | /// @code |
334 | /// /foo/bar.txt => .txt |
335 | /// /foo/bar => <empty> |
336 | /// /foo/.txt => .txt |
337 | /// @endcode |
338 | /// |
339 | /// @param path Input path. |
340 | /// @result The extension of \a path. |
341 | StringRef extension(StringRef path, Style style = Style::native); |
342 | |
343 | /// Check whether the given char is a path separator on the host OS. |
344 | /// |
345 | /// @param value a character |
346 | /// @result true if \a value is a path separator character on the host OS |
347 | bool is_separator(char value, Style style = Style::native); |
348 | |
349 | /// Return the preferred separator for this platform. |
350 | /// |
351 | /// @result StringRef of the preferred separator, null-terminated. |
352 | StringRef get_separator(Style style = Style::native); |
353 | |
354 | /// Get the typical temporary directory for the system, e.g., |
355 | /// "/var/tmp" or "C:/TEMP" |
356 | /// |
357 | /// @param erasedOnReboot Whether to favor a path that is erased on reboot |
358 | /// rather than one that potentially persists longer. This parameter will be |
359 | /// ignored if the user or system has set the typical environment variable |
360 | /// (e.g., TEMP on Windows, TMPDIR on *nix) to specify a temporary directory. |
361 | /// |
362 | /// @param result Holds the resulting path name. |
363 | void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &result); |
364 | |
365 | /// Get the user's home directory. |
366 | /// |
367 | /// @param result Holds the resulting path name. |
368 | /// @result True if a home directory is set, false otherwise. |
369 | bool home_directory(SmallVectorImpl<char> &result); |
370 | |
371 | /// Has root name? |
372 | /// |
373 | /// root_name != "" |
374 | /// |
375 | /// @param path Input path. |
376 | /// @result True if the path has a root name, false otherwise. |
377 | bool has_root_name(const Twine &path, Style style = Style::native); |
378 | |
379 | /// Has root directory? |
380 | /// |
381 | /// root_directory != "" |
382 | /// |
383 | /// @param path Input path. |
384 | /// @result True if the path has a root directory, false otherwise. |
385 | bool has_root_directory(const Twine &path, Style style = Style::native); |
386 | |
387 | /// Has root path? |
388 | /// |
389 | /// root_path != "" |
390 | /// |
391 | /// @param path Input path. |
392 | /// @result True if the path has a root path, false otherwise. |
393 | bool has_root_path(const Twine &path, Style style = Style::native); |
394 | |
395 | /// Has relative path? |
396 | /// |
397 | /// relative_path != "" |
398 | /// |
399 | /// @param path Input path. |
400 | /// @result True if the path has a relative path, false otherwise. |
401 | bool has_relative_path(const Twine &path, Style style = Style::native); |
402 | |
403 | /// Has parent path? |
404 | /// |
405 | /// parent_path != "" |
406 | /// |
407 | /// @param path Input path. |
408 | /// @result True if the path has a parent path, false otherwise. |
409 | bool has_parent_path(const Twine &path, Style style = Style::native); |
410 | |
411 | /// Has filename? |
412 | /// |
413 | /// filename != "" |
414 | /// |
415 | /// @param path Input path. |
416 | /// @result True if the path has a filename, false otherwise. |
417 | bool has_filename(const Twine &path, Style style = Style::native); |
418 | |
419 | /// Has stem? |
420 | /// |
421 | /// stem != "" |
422 | /// |
423 | /// @param path Input path. |
424 | /// @result True if the path has a stem, false otherwise. |
425 | bool has_stem(const Twine &path, Style style = Style::native); |
426 | |
427 | /// Has extension? |
428 | /// |
429 | /// extension != "" |
430 | /// |
431 | /// @param path Input path. |
432 | /// @result True if the path has a extension, false otherwise. |
433 | bool has_extension(const Twine &path, Style style = Style::native); |
434 | |
435 | /// Is path absolute? |
436 | /// |
437 | /// @param path Input path. |
438 | /// @result True if the path is absolute, false if it is not. |
439 | bool is_absolute(const Twine &path, Style style = Style::native); |
440 | |
441 | /// Is path relative? |
442 | /// |
443 | /// @param path Input path. |
444 | /// @result True if the path is relative, false if it is not. |
445 | bool is_relative(const Twine &path, Style style = Style::native); |
446 | |
447 | /// Remove redundant leading "./" pieces and consecutive separators. |
448 | /// |
449 | /// @param path Input path. |
450 | /// @result The cleaned-up \a path. |
451 | StringRef remove_leading_dotslash(StringRef path, Style style = Style::native); |
452 | |
453 | /// In-place remove any './' and optionally '../' components from a path. |
454 | /// |
455 | /// @param path processed path |
456 | /// @param remove_dot_dot specify if '../' (except for leading "../") should be |
457 | /// removed |
458 | /// @result True if path was changed |
459 | bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot = false, |
460 | Style style = Style::native); |
461 | |
462 | } // end namespace path |
463 | } // end namespace sys |
464 | } // end namespace llvm |
465 | |
466 | #endif |
467 | |